Skip to content

Commit 964c002

Browse files
committed
Fix vmt texture path completion
1 parent ba25e36 commit 964c002

File tree

3 files changed

+44
-32
lines changed

3 files changed

+44
-32
lines changed

src/kv-core/source-fs.test.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/kv-core/source-fs.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { normalize } from "path";
2+
13
// ==========================================================================
24
// Purpose:
35
// Utility functions to navigate in a source-engine-like filesystem.
@@ -8,5 +10,5 @@ export function getParentDocumentDirectory(path: string, directoryName: string):
810
const materialPathIndex = path.indexOf(directoryName) + directoryName.length;
911
if(materialPathIndex < 0) return null;
1012

11-
return path.substring(0, materialPathIndex);
13+
return normalize(path.substring(0, materialPathIndex));
1214
}

src/language/lang-vmt.ts

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import { getParentDocumentDirectory } from "../kv-core/source-fs";
1212
import { config } from "../main";
1313
import { isFloatValue, isScalarValue } from "../kv-core/kv-string-util";
1414
import { getColorMatches, ColorMatchDescription, ColorMatchParenthesisType } from "../kv-core/kv-color";
15+
import { exists, existsSync, fstat } from "fs";
16+
import path from "path";
1517

1618
export const filterVmtSaved: DocumentFilter = {
1719
language: "vmt",
@@ -135,6 +137,12 @@ export class VmtSemanticTokenProvider extends KvTokensProviderBase {
135137
tokensBuilder.push(range, "keyword");
136138
return;
137139
}
140+
const materialDir = getParentDocumentDirectory(kvDoc.document.uri.fsPath, "materials");
141+
if(materialDir != null) {
142+
if(!existsSync(path.join(materialDir, kv.value + ".vtf"))) {
143+
this.diagnostics.push(new Diagnostic(range, "Texture not found on disk", DiagnosticSeverity.Warning));
144+
}
145+
}
138146

139147
tokensBuilder.push(range, "string");
140148
}
@@ -217,7 +225,7 @@ export class ShaderParamCompletionItemProvider implements CompletionItemProvider
217225
}
218226

219227

220-
if(kv.valueRange.contains(position) && kv.value === "") {
228+
if(kv.valueRange.contains(position)) {
221229
const param = shaderParams.find(p => p.name == kv.key);
222230

223231
const completions = new CompletionList();
@@ -256,23 +264,40 @@ export class ShaderParamCompletionItemProvider implements CompletionItemProvider
256264
completions.items.push(completion);
257265
});
258266

259-
const materialRoot = getParentDocumentDirectory(document.uri.path, "material");
267+
const materialRoot = getParentDocumentDirectory(document.uri.fsPath, "materials");
268+
if(materialRoot == null) return;
260269

261-
if(materialRoot != null) {
262-
263-
const textureFiles = listFilesSync(materialRoot, "vtf");
264-
textureFiles.forEach( t => {
265-
const filePath = t.substring(materialRoot.length + 1);
266-
const filePathWithoutExtension = filePath.substring(0, filePath.length - 4);
267-
268-
const completion = new CompletionItem(filePath);
269-
completion.insertText = filePathWithoutExtension;
270-
completion.detail = "Texture Path";
271-
completion.kind = CompletionItemKind.File;
272-
completion.preselect = true;
273-
completions.items.push(completion);
274-
});
270+
// Exit early if file already exists
271+
if(existsSync(path.join(materialRoot, kv.value + ".vtf"))) {
272+
return;
273+
}
274+
275+
let cursorStartDir;
276+
if(kv.value.endsWith("\\") || kv.value.endsWith("/")) {
277+
cursorStartDir = kv.value;
278+
} else {
279+
cursorStartDir = path.dirname(kv.value);
275280
}
281+
const startDir = path.join(materialRoot, cursorStartDir);
282+
283+
if(!existsSync(startDir)) return;
284+
285+
const textureFiles = listFilesSync(startDir, "vtf");
286+
textureFiles.forEach( t => {
287+
let filePath = t.substring(startDir.length);
288+
if(filePath.startsWith("\\") || filePath.startsWith("/")) {
289+
filePath = filePath.slice(1);
290+
}
291+
const filePathWithoutExtension = filePath.substring(0, filePath.length - 4).replace("\\", "/");
292+
293+
const completion = new CompletionItem(filePathWithoutExtension);
294+
completion.insertText = filePathWithoutExtension;
295+
completion.detail = "Texture Path";
296+
completion.kind = CompletionItemKind.File;
297+
completion.preselect = true;
298+
completions.items.push(completion);
299+
});
300+
276301
}
277302
}
278303

0 commit comments

Comments
 (0)