@@ -2,10 +2,6 @@ import path from "path";
22import * as vscode from "vscode" ;
33import * as main from "./main" ;
44
5- function isAutoDetectEnabled ( ) : boolean {
6- return main . config . get < boolean > ( "kvAutoDetect.enabled" , false ) ;
7- }
8-
95type DetectableLanguageId = "keyvalue" | "soundscript" | "captions" ;
106
117interface CommonFileName {
@@ -27,66 +23,49 @@ const commonKvFileNames: CommonFileName[] = [
2723 { regex : / s u r f a c e p r o p e r t i e s _ .+ \. t x t / i, languageId : "keyvalue" } ,
2824 { regex : / w e a p o n _ .+ \. t x t / i, languageId : "keyvalue" } ,
2925 { regex : / v g u i _ s c r e e n s \. t x t / i, languageId : "keyvalue" } ,
30- { regex : / c r e d i t s \. t x t / i, languageId : "keyvalue" }
26+ { regex : / c r e d i t s \. t x t / i, languageId : "keyvalue" } ,
27+ { regex : / s o u n d m i x e r s \. t x t / i, languageId : "keyvalue" } ,
3128] ;
3229
3330function getCommonFileNameMatch ( fileName : string ) : CommonFileName | undefined {
3431 return commonKvFileNames . find ( ( c ) => c . regex . test ( fileName ) ) ;
3532}
3633
37- const globalStateAutoDetectRecommended = "kvAutoDetect.recommended" ;
38- function recommendAutoDetection ( context : vscode . ExtensionContext ) : void {
39-
40- const alreadyRecommended = context . globalState . get ( globalStateAutoDetectRecommended , false ) ;
41- if ( alreadyRecommended ) return ;
42-
43- vscode . window . showInformationMessage ( "Source Engine keyvalue files can be auto detected. Do you want to enable this feature?" , "Yes" , "No" )
44- . then ( ( value : string | undefined ) => {
45- if ( value === "Yes" ) {
46- main . config . update ( "kvAutoDetect.enabled" , true , true ) ;
47- }
48-
49- context . globalState . update ( globalStateAutoDetectRecommended , true ) ;
50- } ) ;
51- }
52-
5334export function init ( context : vscode . ExtensionContext ) : void {
54- const onChange = vscode . window . onDidChangeActiveTextEditor ( ( editor : vscode . TextEditor | undefined ) : void => {
55- main . debugOutput . appendLine ( `Active editor changed to ${ editor ?. document . uri . fsPath } ` ) ;
56- if ( editor === undefined ) return ;
57- detectKeyvalueFile ( editor , context ) ;
58- } ) ;
35+ const onChange = vscode . workspace . onDidOpenTextDocument ( onChangeEditor ) ;
5936 context . subscriptions . push ( onChange ) ;
37+
38+ // Detect all currently open documents (When the extension is loaded, there might already be opened documents)
39+ vscode . workspace . textDocuments . forEach ( detectKeyvalueFile ) ;
6040}
6141
62- function detectKeyvalueFile ( editor : vscode . TextEditor , context : vscode . ExtensionContext ) : void {
42+ function onChangeEditor ( document : vscode . TextDocument ) : void {
43+ main . debugOutput . appendLine ( `Active editor changed to ${ document . uri . fsPath } ` ) ;
6344
64- if ( ! isAutoDetectEnabled ( ) ) {
65- main . debugOutput . appendLine ( "Auto detect is disabled. Not detecting kv file." ) ;
45+ detectKeyvalueFile ( document ) ;
46+ }
6647
67- recommendAutoDetection ( context ) ;
68- return ;
69- }
48+ function detectKeyvalueFile ( document : vscode . TextDocument ) : void {
7049
71- main . debugOutput . appendLine ( "File has language id: " + editor . document . languageId ) ;
72- if ( editor . document . languageId === "plaintext" ) {
73- main . debugOutput . appendLine ( `Potential kv file opened (${ editor . document . uri . fsPath } )` ) ;
50+ main . debugOutput . appendLine ( "File has language id: " + document . languageId ) ;
51+ if ( document . languageId === "plaintext" ) {
52+ main . debugOutput . appendLine ( `Potential kv file opened (${ document . uri . fsPath } )` ) ;
7453
75- const langId = getPotentialKvFileLanguageId ( editor . document ) ;
54+ const langId = getPotentialKvFileLanguageId ( document ) ;
7655 if ( langId === undefined ) {
77- main . debugOutput . appendLine ( `Not changing document language of (${ editor . document . uri . fsPath } )` ) ;
56+ main . debugOutput . appendLine ( `Not changing document language of (${ document . uri . fsPath } )` ) ;
7857 return ;
7958 }
8059
81- main . debugOutput . appendLine ( `Changing document language of (${ editor . document . uri . fsPath } ) to keyvalue` ) ;
82- vscode . languages . setTextDocumentLanguage ( editor . document , langId ) ;
60+ main . debugOutput . appendLine ( `Changing document language of (${ document . uri . fsPath } ) to keyvalue` ) ;
61+ vscode . languages . setTextDocumentLanguage ( document , langId ) ;
8362 }
8463}
8564
8665function getPotentialKvFileLanguageId ( document : vscode . TextDocument ) : DetectableLanguageId | undefined {
8766 const documentFileName = path . basename ( document . uri . fsPath ) ;
8867
89- main . debugOutput . appendLine ( `Auto detect is enabled. Checking if filename (${ documentFileName } ) is in list of common kv file names.` ) ;
68+ main . debugOutput . appendLine ( `Checking if filename (${ documentFileName } ) is in list of common kv file names.` ) ;
9069 const match = getCommonFileNameMatch ( documentFileName ) ;
9170 if ( match == undefined ) {
9271 return undefined ;
0 commit comments