Skip to content

Commit 821d1cd

Browse files
committed
Improve keyvalue file auto detection
1 parent 173bdbd commit 821d1cd

File tree

2 files changed

+19
-45
lines changed

2 files changed

+19
-45
lines changed

package.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -313,11 +313,6 @@
313313
"configuration": {
314314
"title": "Source Engine",
315315
"properties": {
316-
"sourceEngine.kvAutoDetect.enabled": {
317-
"type": "boolean",
318-
"default": true,
319-
"description": "Enable automatic detection of KeyValues files."
320-
},
321316
"sourceEngine.performance.log": {
322317
"type": "boolean",
323318
"default": false,

src/KvFileDetection.ts

Lines changed: 19 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@ import path from "path";
22
import * as vscode from "vscode";
33
import * as main from "./main";
44

5-
function isAutoDetectEnabled(): boolean {
6-
return main.config.get<boolean>("kvAutoDetect.enabled", false);
7-
}
8-
95
type DetectableLanguageId = "keyvalue" | "soundscript" | "captions";
106

117
interface CommonFileName {
@@ -27,66 +23,49 @@ const commonKvFileNames: CommonFileName[] = [
2723
{ regex: /surfaceproperties_.+\.txt/i, languageId: "keyvalue" },
2824
{ regex: /weapon_.+\.txt/i, languageId: "keyvalue" },
2925
{ regex: /vgui_screens\.txt/i, languageId: "keyvalue" },
30-
{ regex: /credits\.txt/i, languageId: "keyvalue" }
26+
{ regex: /credits\.txt/i, languageId: "keyvalue" },
27+
{ regex: /soundmixers\.txt/i, languageId: "keyvalue" },
3128
];
3229

3330
function 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-
5334
export 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

8665
function 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

Comments
 (0)