Skip to content

Commit aa9bd6c

Browse files
committed
feat: implement filepath checking using tree-sitter
1 parent 55dad63 commit aa9bd6c

File tree

1 file changed

+40
-35
lines changed

1 file changed

+40
-35
lines changed

packages/server/src/validations/validateFilePath.ts

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { extname } from 'path';
2-
import { Diagnostic } from 'vscode-languageserver';
1+
import { basename, extname } from 'path';
2+
import { Diagnostic, DiagnosticSeverity } from 'vscode-languageserver';
33
import { TextDocument } from 'vscode-languageserver-textdocument';
44
import { configService } from '../services/configuration.service';
55
import { fileSystemService } from '../services/fs.service';
@@ -19,61 +19,66 @@ export function validateFilePaths(textDocument: TextDocument): Diagnostic[] {
1919

2020
const queryString = `\
2121
(assignment
22-
key: (property) @key
22+
key: (property)
2323
value: (modulePath
24-
extension: (fileExtension) @extension
24+
extension: (fileExtension)
2525
) @modulePathValue
26-
) @assignment`;
26+
)`;
2727

2828
const filepathQuery = new Query(language as Language, queryString);
2929

3030
const tree = parser.parse(text);
3131

3232
const captures = filepathQuery.captures(tree.rootNode);
3333

34-
const problems = 0;
34+
let problems = 0;
3535
const diagnostics: Diagnostic[] = [];
3636

3737
for (const capture of captures) {
3838
if (problems >= configService.globalSettings.maxNumberOfProblems) {
3939
break;
4040
}
4141

42-
console.log(capture.name);
43-
console.log(capture.node.text);
44-
const normalizedPath = capture.name;
45-
46-
// if (!checkIfPathExists(normalizedPath)) {
47-
// problems++;
48-
// const diagnostic: Diagnostic = {
49-
// severity: DiagnosticSeverity.Error,
50-
// range: {
51-
// start: textDocument.positionAt(captures.index + captures[1].length),
52-
// end: textDocument.positionAt(captures.index + captures[0].length),
53-
// },
54-
// message: `Cannot find the file ${captures[2]}`,
55-
// source: 'CC Language Features',
56-
// };
57-
// if (configService.hasDiagnosticRelatedInformationCapability) {
58-
// diagnostic.relatedInformation = [
59-
// {
60-
// location: {
61-
// uri: textDocument.uri,
62-
// range: Object.assign({}, diagnostic.range),
63-
// },
64-
// message:
65-
// 'This may be due to a bad file extension/path, or incorrect capitalization.',
66-
// },
67-
// ];
68-
// }
69-
// diagnostics.push(diagnostic);
70-
// }
42+
const filePath = capture.node.text;
43+
44+
if (!checkIfPathExists(filePath)) {
45+
problems++;
46+
const diagnostic: Diagnostic = {
47+
severity: DiagnosticSeverity.Error,
48+
range: {
49+
start: textDocument.positionAt(capture.node.startIndex),
50+
end: textDocument.positionAt(capture.node.endIndex),
51+
},
52+
message: `Cannot find the file ${basename(filePath)} (${filePath})`,
53+
source: 'CC Language Features',
54+
};
55+
56+
if (configService.hasDiagnosticRelatedInformationCapability) {
57+
diagnostic.relatedInformation = [
58+
{
59+
location: {
60+
uri: textDocument.uri,
61+
range: Object.assign({}, diagnostic.range),
62+
},
63+
message:
64+
'This may be due to a bad file extension/path, or incorrect capitalization.',
65+
},
66+
];
67+
}
68+
diagnostics.push(diagnostic);
69+
}
7170
}
7271

7372
// Send the computed diagnostics to VSCode.
7473
return diagnostics;
7574
}
7675

76+
/**
77+
* Checks if a given file path exists in the list of module files.
78+
* If not found, and the filepath is an image, it checks if there is an image file representing a frame of a sprite (e.g. Img000.png).
79+
* @param filePath a string representing the module file path to check. @example "Base.rte/Explosion.png"
80+
* @returns True if the list of included module files contains the given file path or its sprite frame variant, false otherwise.
81+
*/
7782
function checkIfPathExists(filePath: string): boolean {
7883
if (fileSystemService.moduleFileList.includes(filePath)) {
7984
return true;

0 commit comments

Comments
 (0)