Skip to content

Commit

Permalink
Added error diagnostic if a file is included by itself via #include. …
Browse files Browse the repository at this point in the history
…Now it also does not consider functions to be duplicated.
  • Loading branch information
eyza-cod2 committed Sep 21, 2024
1 parent 63f1b7b commit 498be7e
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 46 deletions.
4 changes: 4 additions & 0 deletions src/GscDiagnosticsCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@ export class GscDiagnosticsCollection {
// #include
if (group.parent?.type === GroupType.PreprocessorStatement && group.parent.items.at(0)?.isReservedKeywordOfName("#include") === true) {

if (referenceData.gscFile?.uri.toString() === gscFile.uri.toString()) {
return new vscode.Diagnostic(group.getRange(), "File is including itself", vscode.DiagnosticSeverity.Error);
}

if (referenceData.gscFile === undefined) {

// This file path is ignored by configuration
Expand Down
4 changes: 4 additions & 0 deletions src/GscFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ export class GscFunctions {
if (referencedFile === undefined) {
continue; // File not found
}

if (referencedFile.uri.toString() === gscFile.uri.toString()) {
continue;
}

referencedFile.data.functions.forEach(f => {
if (!funcNameId || f.nameId === funcNameId) {
Expand Down
115 changes: 69 additions & 46 deletions src/test/workspace/GscComplex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,55 +19,78 @@ suite('GscComplex', () => {

// Check case insensitivity of function calls (file paths, function names)
test('GscComplex.FunctionReferences -> error + hover + definition', async () => {
const gsc = await tests.loadGscFile(['GscComplex', 'FunctionReferences.gsc']);

// There should be no error - everything is case insensitive
assert.ok(gsc.diagnostics.length === 0);

// Correct path
// FunctionReferencesFolder\FunctionReferencesFile::funcName();
const hover1 = await GscHoverProvider.getHover(gsc, new vscode.Position(5, 58));
tests.checkHover(hover1, GscFunction.generateMarkdownDescription({name: "funcName", parameters: []}, false, tests.filePathToUri("GscComplex/FunctionReferencesFolder/FunctionReferencesFile.gsc").toString()).value);
//checkHover(hover1, "\n```\nfuncName()\n```\nFile: ```GscComplex/FunctionReferencesFolder/FunctionReferencesFile.gsc```");
const locations1 = await GscDefinitionProvider.getFunctionDefinitionLocations(gsc, new vscode.Position(5, 58));
tests.checkDefinition(locations1, "GscComplex/FunctionReferencesFolder/FunctionReferencesFile.gsc");

// Lowercase path
// functionreferencesfolder\FunctionReferencesFile::funcName();
const hover2 = await GscHoverProvider.getHover(gsc, new vscode.Position(8, 58));
tests.checkHover(hover2, GscFunction.generateMarkdownDescription({name: "funcName", parameters: []}, false, tests.filePathToUri("GscComplex/FunctionReferencesFolder/FunctionReferencesFile.gsc").toString()).value);
const locations2 = await GscDefinitionProvider.getFunctionDefinitionLocations(gsc, new vscode.Position(8, 58));
tests.checkDefinition(locations2, "GscComplex/FunctionReferencesFolder/FunctionReferencesFile.gsc");

// Lowercase path + file
// functionreferencesfolder\functionreferencesfile::funcName();
const hover3 = await GscHoverProvider.getHover(gsc, new vscode.Position(11, 58));
tests.checkHover(hover3, GscFunction.generateMarkdownDescription({name: "funcName", parameters: []}, false, tests.filePathToUri("GscComplex/FunctionReferencesFolder/FunctionReferencesFile.gsc").toString()).value);
const locations3 = await GscDefinitionProvider.getFunctionDefinitionLocations(gsc, new vscode.Position(11, 58));
tests.checkDefinition(locations3, "GscComplex/FunctionReferencesFolder/FunctionReferencesFile.gsc");

// Lowercase path + file + func name
// functionreferencesfolder\functionreferencesfile::funcname();
const hover4 = await GscHoverProvider.getHover(gsc, new vscode.Position(14, 58));
tests.checkHover(hover4, GscFunction.generateMarkdownDescription({name: "funcName", parameters: []}, false, tests.filePathToUri("GscComplex/FunctionReferencesFolder/FunctionReferencesFile.gsc").toString()).value);
const locations4 = await GscDefinitionProvider.getFunctionDefinitionLocations(gsc, new vscode.Position(14, 58));
tests.checkDefinition(locations4, "GscComplex/FunctionReferencesFolder/FunctionReferencesFile.gsc");

// Hover over "includedFuncName();" - its case insensitive, external file should be found
const hover5 = await GscHoverProvider.getHover(gsc, new vscode.Position(17, 8));
tests.checkHover(hover5, GscFunction.generateMarkdownDescription({name: "includedFuncName", parameters: []}, false, tests.filePathToUri("GscComplex/FunctionReferencesFolder/FunctionReferencesFile.gsc").toString(), "Included via '#include'").value);
const locations5 = await GscDefinitionProvider.getFunctionDefinitionLocations(gsc, new vscode.Position(17, 8));
tests.checkDefinition(locations5, "GscComplex/FunctionReferencesFolder/FunctionReferencesFile.gsc");

// Hover over "localFunc();" - its case insensitive, local func should be found
const hover6 = await GscHoverProvider.getHover(gsc, new vscode.Position(20, 8));
tests.checkHover(hover6, GscFunction.generateMarkdownDescription({name: "LOCALFUNC", parameters: []}, true, undefined, undefined).value);
const locations6 = await GscDefinitionProvider.getFunctionDefinitionLocations(gsc, new vscode.Position(20, 8));
tests.checkDefinition(locations6, "GscComplex/FunctionReferences.gsc");
try {
const gsc = await tests.loadGscFile(['GscComplex', 'FunctionReferences.gsc']);

// There should be no error - everything is case insensitive
assert.ok(gsc.diagnostics.length === 0);

// Correct path
// FunctionReferencesFolder\FunctionReferencesFile::funcName();
const hover1 = await GscHoverProvider.getHover(gsc, new vscode.Position(5, 58));
tests.checkHover(hover1, GscFunction.generateMarkdownDescription({name: "funcName", parameters: []}, false, tests.filePathToUri("GscComplex/FunctionReferencesFolder/FunctionReferencesFile.gsc").toString()).value);
//checkHover(hover1, "\n```\nfuncName()\n```\nFile: ```GscComplex/FunctionReferencesFolder/FunctionReferencesFile.gsc```");
const locations1 = await GscDefinitionProvider.getFunctionDefinitionLocations(gsc, new vscode.Position(5, 58));
tests.checkDefinition(locations1, "GscComplex/FunctionReferencesFolder/FunctionReferencesFile.gsc");

// Lowercase path
// functionreferencesfolder\FunctionReferencesFile::funcName();
const hover2 = await GscHoverProvider.getHover(gsc, new vscode.Position(8, 58));
tests.checkHover(hover2, GscFunction.generateMarkdownDescription({name: "funcName", parameters: []}, false, tests.filePathToUri("GscComplex/FunctionReferencesFolder/FunctionReferencesFile.gsc").toString()).value);
const locations2 = await GscDefinitionProvider.getFunctionDefinitionLocations(gsc, new vscode.Position(8, 58));
tests.checkDefinition(locations2, "GscComplex/FunctionReferencesFolder/FunctionReferencesFile.gsc");


// Lowercase path + file
// functionreferencesfolder\functionreferencesfile::funcName();
const hover3 = await GscHoverProvider.getHover(gsc, new vscode.Position(11, 58));
tests.checkHover(hover3, GscFunction.generateMarkdownDescription({name: "funcName", parameters: []}, false, tests.filePathToUri("GscComplex/FunctionReferencesFolder/FunctionReferencesFile.gsc").toString()).value);
const locations3 = await GscDefinitionProvider.getFunctionDefinitionLocations(gsc, new vscode.Position(11, 58));
tests.checkDefinition(locations3, "GscComplex/FunctionReferencesFolder/FunctionReferencesFile.gsc");

// Lowercase path + file + func name
// functionreferencesfolder\functionreferencesfile::funcname();
const hover4 = await GscHoverProvider.getHover(gsc, new vscode.Position(14, 58));
tests.checkHover(hover4, GscFunction.generateMarkdownDescription({name: "funcName", parameters: []}, false, tests.filePathToUri("GscComplex/FunctionReferencesFolder/FunctionReferencesFile.gsc").toString()).value);
const locations4 = await GscDefinitionProvider.getFunctionDefinitionLocations(gsc, new vscode.Position(14, 58));
tests.checkDefinition(locations4, "GscComplex/FunctionReferencesFolder/FunctionReferencesFile.gsc");

// Hover over "includedFuncName();" - its case insensitive, external file should be found
const hover5 = await GscHoverProvider.getHover(gsc, new vscode.Position(17, 8));
tests.checkHover(hover5, GscFunction.generateMarkdownDescription({name: "includedFuncName", parameters: []}, false, tests.filePathToUri("GscComplex/FunctionReferencesFolder/FunctionReferencesFile.gsc").toString(), "Included via '#include'").value);
const locations5 = await GscDefinitionProvider.getFunctionDefinitionLocations(gsc, new vscode.Position(17, 8));
tests.checkDefinition(locations5, "GscComplex/FunctionReferencesFolder/FunctionReferencesFile.gsc");

// Hover over "localFunc();" - its case insensitive, local func should be found
const hover6 = await GscHoverProvider.getHover(gsc, new vscode.Position(20, 8));
tests.checkHover(hover6, GscFunction.generateMarkdownDescription({name: "LOCALFUNC", parameters: []}, true, undefined, undefined).value);
const locations6 = await GscDefinitionProvider.getFunctionDefinitionLocations(gsc, new vscode.Position(20, 8));
tests.checkDefinition(locations6, "GscComplex/FunctionReferences.gsc");

} catch (error) {
tests.printDebugInfoForError(error);
}
});

// Check case insensitivity of function calls (file paths, function names)
test('GscComplex.DoubleInclude', async () => {
try {
const gsc = await tests.loadGscFile(['GscComplex', 'scripts', 'DoubleInclude.gsc']);

tests.checkDiagnostic(gsc.diagnostics, 0, "File is including itself", vscode.DiagnosticSeverity.Error);
assert.strictEqual(gsc.diagnostics.length, 1);

// Correct path
// FunctionReferencesFolder\FunctionReferencesFile::funcName();
const hover1 = await GscHoverProvider.getHover(gsc, new vscode.Position(3, 6));
tests.checkHover(hover1, GscFunction.generateMarkdownDescription({name: "func1", parameters: []}, true, tests.filePathToUri("GscComplex/scripts/DoubleInclude.gsc").toString()).value);

const locations1 = await GscDefinitionProvider.getFunctionDefinitionLocations(gsc, new vscode.Position(3, 6));
tests.checkDefinition(locations1, "GscComplex/scripts/DoubleInclude.gsc");


} catch (error) {
tests.printDebugInfoForError(error);
}
});

});
9 changes: 9 additions & 0 deletions src/test/workspace/GscComplex/scripts/DoubleInclude.gsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include scripts\DoubleInclude;

main() {
func1();
}

func1() {

}

0 comments on commit 498be7e

Please sign in to comment.