diff --git a/Changelog.md b/Changelog.md index 35dc5d70..c1f048af 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,5 +1,9 @@ # Changelog +## 5.0.2 - 2023-08-10 🐈 + +- Fix language mode resetting when VS Code restarts ([Issue #392](https://github.com/mark-wiemer-org/ahkpp/issues/392)) + ## 5.0.1 - 2023-08-08 πŸ˜Άβ€πŸŒ«οΈ - `ahk++.file.interpreterPathV2` now defaults to `C:/Program Files/AutoHotkey/v2/AutoHotkey64.exe` ([Issue #387](https://github.com/mark-wiemer-org/ahkpp/issues/387)) diff --git a/demos/manualTests/languageVersion/1.ahk b/demos/manualTests/languageVersion/1.ahk new file mode 100644 index 00000000..0da26d1f --- /dev/null +++ b/demos/manualTests/languageVersion/1.ahk @@ -0,0 +1,7 @@ +; Open both 1.ahk and 2.ahk in the same editor group +; Set file association for .ahk to AHK v2 +; Reload window +; File associations will start AHK v2 (green icon), then update to AHK v1 (blue icon) + +#Requires AutoHotkey v1 + diff --git a/demos/manualTests/languageVersion/2.ahk b/demos/manualTests/languageVersion/2.ahk new file mode 100644 index 00000000..0da26d1f --- /dev/null +++ b/demos/manualTests/languageVersion/2.ahk @@ -0,0 +1,7 @@ +; Open both 1.ahk and 2.ahk in the same editor group +; Set file association for .ahk to AHK v2 +; Reload window +; File associations will start AHK v2 (green icon), then update to AHK v1 (blue icon) + +#Requires AutoHotkey v1 + diff --git a/src/service/languageVersionService.ts b/src/service/languageVersionService.ts index f72d734c..b04914c0 100644 --- a/src/service/languageVersionService.ts +++ b/src/service/languageVersionService.ts @@ -23,21 +23,17 @@ const tryGetRequiresVersion = (doc: vscode.TextDocument): 1 | 2 | undefined => { * Makes a function that switches to the correct version of AHK. */ const makeOnSwitchFile = - (seen: Set) => (editor: vscode.TextEditor | undefined) => { + (seen: Set) => (doc: vscode.TextDocument | undefined) => { // If not an AHK file, or already seen, do nothing. - if ( - !editor || - !isAHK(editor.document.languageId) || - seen.has(editor.document.uri) - ) { + if (!doc || !isAHK(doc.languageId) || seen.has(doc.uri)) { return; } - seen.add(editor.document.uri); - const versionNumber = tryGetRequiresVersion(editor.document); + seen.add(doc.uri); + const versionNumber = tryGetRequiresVersion(doc); const newLang = [LanguageId.ahk1, LanguageId.ahk2][versionNumber - 1]; - const currentLang = editor.document.languageId; + const currentLang = doc.languageId; if (newLang && newLang !== currentLang) { - switchLang(editor.document, newLang); + switchLang(doc, newLang); } }; @@ -68,9 +64,19 @@ export const initializeLanguageVersionService = ( ) => { const onSwitchFile = makeOnSwitchFile(new Set()); vscode.window.onDidChangeActiveTextEditor( - onSwitchFile, + (newActiveEditor) => onSwitchFile(newActiveEditor.document), null, context.subscriptions, ); - onSwitchFile(vscode.window.activeTextEditor); + // Update language version of all open text editors + vscode.window.tabGroups.all.forEach((tabGroup) => + tabGroup.tabs + .filter((t) => t.input instanceof vscode.TabInputText) + .forEach(async (t) => { + const doc = await vscode.workspace.openTextDocument( + (t.input as vscode.TabInputText).uri, + ); + onSwitchFile(doc); + }), + ); };