Skip to content

Commit 962cc59

Browse files
authored
Fix issues with compileFolder and importFolder commands (#860)
1 parent 60404b1 commit 962cc59

File tree

2 files changed

+54
-11
lines changed

2 files changed

+54
-11
lines changed

src/commands/compile.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,15 @@ import {
1313
workspaceState,
1414
} from "../extension";
1515
import { DocumentContentProvider } from "../providers/DocumentContentProvider";
16-
import { currentFile, CurrentFile, currentWorkspaceFolder, outputChannel, uriOfWorkspaceFolder } from "../utils";
16+
import {
17+
currentFile,
18+
CurrentFile,
19+
currentFileFromContent,
20+
currentWorkspaceFolder,
21+
outputChannel,
22+
throttleRequests,
23+
uriOfWorkspaceFolder,
24+
} from "../utils";
1725
import { PackageNode } from "../explorer/models/packageNode";
1826
import { NodeBase } from "../explorer/models/nodeBase";
1927

@@ -372,16 +380,18 @@ export async function namespaceCompile(askFlags = false): Promise<any> {
372380

373381
function importFiles(files, noCompile = false) {
374382
return Promise.all<CurrentFile>(
375-
files.map((file) =>
376-
vscode.workspace
377-
.openTextDocument(file)
378-
.then(currentFile)
379-
.then((curFile) =>
380-
importFile(curFile).then((data) => {
381-
outputChannel.appendLine("Imported file: " + curFile.fileName);
382-
return curFile;
383-
})
384-
)
383+
files.map(
384+
throttleRequests((file) =>
385+
fs.promises
386+
.readFile(file, { encoding: "utf8" })
387+
.then((content) => currentFileFromContent(file, content))
388+
.then((curFile) =>
389+
importFile(curFile).then((data) => {
390+
outputChannel.appendLine("Imported file: " + curFile.fileName);
391+
return curFile;
392+
})
393+
)
394+
)
385395
)
386396
).then(noCompile ? Promise.resolve : compile);
387397
}

src/utils/index.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,39 @@ export function isImportableLocalFile(file: vscode.TextDocument): boolean {
113113
return false;
114114
}
115115

116+
export function currentFileFromContent(fileName: string, content: string): CurrentFile {
117+
const uri = vscode.Uri.file(fileName);
118+
const workspaceFolder = workspaceFolderOfUri(uri);
119+
let name = "";
120+
let ext = "";
121+
if (fileName.split(".").pop().toLowerCase() === "cls") {
122+
// Allow Unicode letters
123+
const match = content.match(/^[ \t]*Class[ \t]+(%?[\p{L}\d]+(?:\.[\p{L}\d]+)+)/imu);
124+
if (match) {
125+
[, name, ext = "cls"] = match;
126+
}
127+
} else {
128+
const match = content.match(/^ROUTINE ([^\s]+)(?:\s*\[\s*Type\s*=\s*\b([a-z]{3})\b)?/i);
129+
if (match) {
130+
[, name, ext = "mac"] = match;
131+
} else {
132+
[name, ext = "mac"] = path.basename(fileName).split(".");
133+
}
134+
}
135+
name = `${name}.${ext}`;
136+
const firstLF = content.indexOf("\n");
137+
138+
return {
139+
content,
140+
fileName,
141+
uri,
142+
workspaceFolder,
143+
name,
144+
uniqueId: `${workspaceFolder}:${name}`,
145+
eol: firstLF > 0 && content[firstLF - 1] === "\r" ? vscode.EndOfLine.CRLF : vscode.EndOfLine.LF,
146+
};
147+
}
148+
116149
export function currentFile(document?: vscode.TextDocument): CurrentFile {
117150
document =
118151
document ||

0 commit comments

Comments
 (0)