@@ -12,6 +12,8 @@ import { getParentDocumentDirectory } from "../kv-core/source-fs";
1212import { config } from "../main" ;
1313import { isFloatValue , isScalarValue } from "../kv-core/kv-string-util" ;
1414import { getColorMatches , ColorMatchDescription , ColorMatchParenthesisType } from "../kv-core/kv-color" ;
15+ import { exists , existsSync , fstat } from "fs" ;
16+ import path from "path" ;
1517
1618export 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