Skip to content
This repository has been archived by the owner on Jan 3, 2025. It is now read-only.

make dirty files unique as nimsuggest expects (fixes #180) #182

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion src/nimUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import path = require('path');
import os = require('os');
import cp = require('child_process');
import vscode = require('vscode');
const mkdirp = require('mkdirp');

export interface ProjectFileInfo {
wsFolder: vscode.WorkspaceFolder;
Expand Down Expand Up @@ -156,12 +157,36 @@ export function getProjectFileInfo(filename: string): ProjectFileInfo {
return _projects[0];
}

declare global {
interface String {
hashCode(): number;
}
}

/* fast string hash from https://werxltd.com/wp/2010/05/13/javascript-implementation-of-javas-string-hashcode-method/ */
String.prototype.hashCode = function () {
var hash = 0, i, chr;
for (i = 0; i < this.length; i++) {
chr = this.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return Math.abs(hash);
};

/**
* Returns temporary file path of edited document.
*/
export function getDirtyFile(document: vscode.TextDocument): string {
var dirtyFilePath = path.normalize(path.join(os.tmpdir(), 'vscodenimdirty.nim'));
let projectInfo = getProjectFileInfo(document.fileName);
let projectFilePath = document.fileName.substring(projectInfo.wsFolder.uri.path.length);
let uniqueStringHash = projectInfo.wsFolder.uri.path.hashCode();
let dirtyFilePath = path.normalize(path.join(os.tmpdir(), projectInfo.wsFolder.name + '-' + uniqueStringHash.toString(), projectFilePath));
if (fs.existsSync(dirtyFilePath) === false) {
mkdirp.sync(path.dirname(dirtyFilePath));
}
fs.writeFileSync(dirtyFilePath, document.getText());

return dirtyFilePath;
}

Expand Down