Skip to content

Commit

Permalink
Fix #392
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-wiemer committed Aug 11, 2023
1 parent 697f44c commit 18c248b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
7 changes: 7 additions & 0 deletions demos/manualTests/languageVersion/1.ahk
Original file line number Diff line number Diff line change
@@ -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

7 changes: 7 additions & 0 deletions demos/manualTests/languageVersion/2.ahk
Original file line number Diff line number Diff line change
@@ -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

30 changes: 18 additions & 12 deletions src/service/languageVersionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<vscode.Uri>) => (editor: vscode.TextEditor | undefined) => {
(seen: Set<vscode.Uri>) => (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);
}
};

Expand Down Expand Up @@ -68,9 +64,19 @@ export const initializeLanguageVersionService = (
) => {
const onSwitchFile = makeOnSwitchFile(new Set<vscode.Uri>());
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);
}),
);
};

0 comments on commit 18c248b

Please sign in to comment.