|
| 1 | +import path from "path"; |
| 2 | +import * as vscode from "vscode"; |
| 3 | +import * as main from "./main"; |
| 4 | +import * as shared from "./language/Shared"; |
| 5 | + |
| 6 | +function isAutoDetectEnabled(): boolean { |
| 7 | + return main.config.get<boolean>("kvAutoDetect.enabled", false); |
| 8 | +} |
| 9 | + |
| 10 | +function isAutoDetectEnabledOnlyInWorkspaces(): boolean { |
| 11 | + return main.config.get<boolean>("kvAutoDetect.onlyInSourceEngineWorkspaces", true); |
| 12 | +} |
| 13 | + |
| 14 | +function getSourceEngineWorkspaces(): string[] { |
| 15 | + return main.config.get<string[]>("sourceEngineWorkspaces", []); |
| 16 | +} |
| 17 | + |
| 18 | + |
| 19 | +type DetectableLanguageId = "keyvalue3" | "soundscript" | "captions"; |
| 20 | +interface CommonFileName { |
| 21 | + regex: RegExp; |
| 22 | + languageId: DetectableLanguageId; |
| 23 | +} |
| 24 | + |
| 25 | +const commonKvFileNames: CommonFileName[] = [ |
| 26 | + { regex: /gameinfo\.txt/i, languageId: "keyvalue3" }, |
| 27 | + { regex: /subtitles_.+\.txt/i, languageId: "captions" }, |
| 28 | + { regex: /captions_.+\.txt/i, languageId: "captions" }, |
| 29 | + { regex: /basemodui_.+\.txt/i, languageId: "keyvalue3" }, |
| 30 | + { regex: /instructor_lessons\.txt/i, languageId: "keyvalue3" }, |
| 31 | + { regex: /instructor_textures\.txt/i, languageId: "keyvalue3" }, |
| 32 | + { regex: /game_sounds_.+\.txt/i, languageId: "soundscript" }, |
| 33 | + { regex: /npc_sounds_.+\.txt/i, languageId: "soundscript" }, |
| 34 | + { regex: /level_sounds_.+\.txt/i, languageId: "soundscript" }, |
| 35 | + { regex: /soundscapes_.+\.txt/i, languageId: "soundscript" }, |
| 36 | + { regex: /surfaceproperties_.+\.txt/i, languageId: "keyvalue3" }, |
| 37 | + { regex: /weapon_.+\.txt/i, languageId: "keyvalue3" }, |
| 38 | + { regex: /vgui_screens\.txt/i, languageId: "keyvalue3" }, |
| 39 | + { regex: /credits\.txt/i, languageId: "keyvalue3" } |
| 40 | +]; |
| 41 | + |
| 42 | +function getCommonFileNameMatch(fileName: string): CommonFileName | undefined { |
| 43 | + return commonKvFileNames.find((c) => c.regex.test(fileName)); |
| 44 | +} |
| 45 | + |
| 46 | +const globalStateAutoDetectRecommended = "kvAutoDetect.recommended"; |
| 47 | +function recommendAutoDetection(context: vscode.ExtensionContext): void { |
| 48 | + |
| 49 | + const alreadyRecommended = context.globalState.get(globalStateAutoDetectRecommended, false); |
| 50 | + if(alreadyRecommended) return; |
| 51 | + |
| 52 | + vscode.window.showInformationMessage("Source Engine keyvalue files can be auto detected. Do you want to enable this feature?", "Yes", "No") |
| 53 | + .then((value: string | undefined) => { |
| 54 | + if(value === "Yes") { |
| 55 | + main.config.update("kvAutoDetect.enabled", true, true); |
| 56 | + } |
| 57 | + |
| 58 | + context.globalState.update(globalStateAutoDetectRecommended, true); |
| 59 | + }); |
| 60 | +} |
| 61 | + |
| 62 | +export function init(context: vscode.ExtensionContext): void { |
| 63 | + const onChange = vscode.window.onDidChangeActiveTextEditor((editor: vscode.TextEditor | undefined): void => { |
| 64 | + main.debugOutput.appendLine(`Active editor changed to ${editor?.document.uri.fsPath}`); |
| 65 | + if(editor === undefined) return; |
| 66 | + detectKeyvalueFile(editor, context); |
| 67 | + }); |
| 68 | + context.subscriptions.push(onChange); |
| 69 | +} |
| 70 | + |
| 71 | +function detectKeyvalueFile(editor: vscode.TextEditor, context: vscode.ExtensionContext): void { |
| 72 | + |
| 73 | + if(!isAutoDetectEnabled()) { |
| 74 | + main.debugOutput.appendLine("Auto detect is disabled. Not detecting kv file."); |
| 75 | + |
| 76 | + recommendAutoDetection(context); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + main.debugOutput.appendLine("File has language id: " + editor.document.languageId); |
| 81 | + if(editor.document.languageId === "plaintext") { |
| 82 | + main.debugOutput.appendLine(`Potential kv file opened (${editor.document.uri.fsPath})`); |
| 83 | + |
| 84 | + const langId = getPotentialKvFileLanguageId(editor.document); |
| 85 | + if(langId === undefined) { |
| 86 | + main.debugOutput.appendLine(`Not changing document language of (${editor.document.uri.fsPath})`); |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + main.debugOutput.appendLine(`Changing document language of (${editor.document.uri.fsPath}) to keyvalue3`); |
| 91 | + vscode.languages.setTextDocumentLanguage(editor.document, langId); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +function getPotentialKvFileLanguageId(document: vscode.TextDocument): DetectableLanguageId | undefined { |
| 96 | + if(isAutoDetectEnabledOnlyInWorkspaces()) { |
| 97 | + |
| 98 | + if(!isDocumentInSourceEngineWorkspace(document)) { |
| 99 | + main.debugOutput.appendLine(`Auto detect is enabled only in source engine workspaces, but the document (${document.uri.fsPath}) is not in a source engine workspace.`); |
| 100 | + return undefined; |
| 101 | + } |
| 102 | + |
| 103 | + } |
| 104 | + |
| 105 | + const documentFileName = path.basename(document.uri.fsPath); |
| 106 | + |
| 107 | + main.debugOutput.appendLine(`Auto detect is enabled. Checking if filename (${documentFileName}) is in list of common kv file names.`); |
| 108 | + const match = getCommonFileNameMatch(documentFileName); |
| 109 | + if(match == undefined) { |
| 110 | + return undefined; |
| 111 | + } |
| 112 | + |
| 113 | + main.debugOutput.appendLine(`! File (${document.uri.fsPath}) has a common file name and is associated with LanguageId: ${match.languageId}`); |
| 114 | + return match.languageId; |
| 115 | +} |
| 116 | + |
| 117 | +function isDocumentInSourceEngineWorkspace(document: vscode.TextDocument): boolean { |
| 118 | + let fileUri: string = document.uri.fsPath; |
| 119 | + let sourceEngineWorkspaces: string[] = getSourceEngineWorkspaces(); |
| 120 | + |
| 121 | + // Make paths lower case on windows |
| 122 | + if(process.platform === "win32") { |
| 123 | + fileUri = fileUri.toLowerCase(); |
| 124 | + sourceEngineWorkspaces = sourceEngineWorkspaces.map((workspace) => workspace.toLowerCase()); |
| 125 | + } |
| 126 | + |
| 127 | + main.debugOutput.appendLine(`Checking if file (${fileUri}) is in any source engine workspace.`); |
| 128 | + |
| 129 | + for(const workspace of sourceEngineWorkspaces) { |
| 130 | + main.debugOutput.appendLine(`- Checking if file (${fileUri}) is in workspace (${workspace})`); |
| 131 | + if(fileUri.startsWith(workspace)) { |
| 132 | + main.debugOutput.appendLine(`! File (${fileUri}) is in workspace (${workspace})`); |
| 133 | + return true; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + main.debugOutput.appendLine(`! File (${fileUri}) is not in any source engine workspace.`); |
| 138 | + |
| 139 | + return false; |
| 140 | +} |
| 141 | + |
0 commit comments