From d0d1874f0a3cb3a4c950142e52d196d193d9b1ad Mon Sep 17 00:00:00 2001 From: "Lance Y." <139173168+LanceYing2004@users.noreply.github.com> Date: Fri, 17 Apr 2026 17:34:04 -0400 Subject: [PATCH 1/2] fix(vscode): initialize dialect picker with current document dialect (#2667) --- packages/vscode-plugin/src/extension.ts | 71 ++++++++++++++++++- .../src/tests/fixtures/.vscode/settings.json | 6 +- .../src/tests/fixtures/integration.md | 5 -- 3 files changed, 72 insertions(+), 10 deletions(-) delete mode 100644 packages/vscode-plugin/src/tests/fixtures/integration.md diff --git a/packages/vscode-plugin/src/extension.ts b/packages/vscode-plugin/src/extension.ts index fd318fbc71..7adb256b71 100644 --- a/packages/vscode-plugin/src/extension.ts +++ b/packages/vscode-plugin/src/extension.ts @@ -204,9 +204,8 @@ async function changeDialect(): Promise { label: name, })); - const selected = await window.showQuickPick(dialects, { - placeHolder: 'Select Harper dialect', - }); + const currentDialect = getCurrentDialect(); + const selected = await showDialectQuickPick(dialects, currentDialect); if (selected && typeof selected !== 'string') { await workspace @@ -215,6 +214,72 @@ async function changeDialect(): Promise { } } +/** + Retrieves the currently active Harper dialect for the active editor. + This function reads the `harper.dialect` configuration scoped to the + currently active document (if one exists). If no active editor is present + or no dialect has been explicitly set, it returns an empty string. + @returns {string} The current dialect (e.g., "American", "British"), + or an empty string if no dialect is configured. + */ + +function getCurrentDialect(): string { + const activeDocumentUri = window.activeTextEditor?.document.uri; + return workspace.getConfiguration('harper', activeDocumentUri).get('dialect', ''); +} + + +/** + + Displays a VS Code QuickPick UI for selecting a Harper dialect, + preselecting the currently active dialect. + + Unlike `window.showQuickPick`, this implementation explicitly sets + the active (highlighted) item to match the current dialect, ensuring + the UI reflects the actual document state instead of defaulting to "US". + The function resolves with the selected item when the user confirms, or + `undefined` if the picker is dismissed without selection. + + @param {QuickPickItem[]} dialects - List of available dialect options. + @param {string} currentDialect - The currently active dialect label. + @returns {Promise} The selected dialect item, + + or `undefined` if the user cancels. + */ +async function showDialectQuickPick( + dialects: QuickPickItem[], + currentDialect: string, +): Promise { + const quickPick = window.createQuickPick(); + quickPick.items = dialects; + quickPick.placeholder = 'Select Harper dialect'; + + const activeDialect = dialects.find((dialect) => dialect.label === currentDialect); + if (activeDialect) { + quickPick.activeItems = [activeDialect]; + } + + return await new Promise((resolve) => { + let accepted = false; + + quickPick.onDidAccept(() => { + accepted = true; + resolve(quickPick.selectedItems[0]); + quickPick.hide(); + }); + + quickPick.onDidHide(() => { + quickPick.dispose(); + if (!accepted) { + resolve(undefined); + } + }); + + quickPick.show(); + }); +} + + export function deactivate(): Thenable | undefined { if (!client) { return undefined; diff --git a/packages/vscode-plugin/src/tests/fixtures/.vscode/settings.json b/packages/vscode-plugin/src/tests/fixtures/.vscode/settings.json index e0801147ad..e9643dd281 100644 --- a/packages/vscode-plugin/src/tests/fixtures/.vscode/settings.json +++ b/packages/vscode-plugin/src/tests/fixtures/.vscode/settings.json @@ -4,6 +4,8 @@ }, "harper.linters.SpellCheck": true, "harper.linters.RepeatedWords": true, - "harper.dialect": "American", - "harper.excludePatterns": [] + "harper.dialect": "British", + "harper.excludePatterns": [ + "*.md" + ] } diff --git a/packages/vscode-plugin/src/tests/fixtures/integration.md b/packages/vscode-plugin/src/tests/fixtures/integration.md deleted file mode 100644 index 187c8293fe..0000000000 --- a/packages/vscode-plugin/src/tests/fixtures/integration.md +++ /dev/null @@ -1,5 +0,0 @@ -# Integration - -This sentence has grammar errorz, like this this one. - -On the other hand, you'll realise that this sentence doesn't have an error if you're using British English. From b9b36dd8e0cd148bad78be958b5a083978ce5c58 Mon Sep 17 00:00:00 2001 From: "Lance Y." <139173168+LanceYing2004@users.noreply.github.com> Date: Sun, 19 Apr 2026 22:57:54 -0400 Subject: [PATCH 2/2] restore previously changed file --- packages/vscode-plugin/src/extension.ts | 64 +++++++++---------- .../src/tests/fixtures/.vscode/settings.json | 6 +- .../src/tests/fixtures/integration.md | 5 ++ 3 files changed, 38 insertions(+), 37 deletions(-) create mode 100644 packages/vscode-plugin/src/tests/fixtures/integration.md diff --git a/packages/vscode-plugin/src/extension.ts b/packages/vscode-plugin/src/extension.ts index 7adb256b71..8501f016c3 100644 --- a/packages/vscode-plugin/src/extension.ts +++ b/packages/vscode-plugin/src/extension.ts @@ -224,11 +224,10 @@ async function changeDialect(): Promise { */ function getCurrentDialect(): string { - const activeDocumentUri = window.activeTextEditor?.document.uri; - return workspace.getConfiguration('harper', activeDocumentUri).get('dialect', ''); + const activeDocumentUri = window.activeTextEditor?.document.uri; + return workspace.getConfiguration('harper', activeDocumentUri).get('dialect', ''); } - /** Displays a VS Code QuickPick UI for selecting a Harper dialect, @@ -247,38 +246,37 @@ function getCurrentDialect(): string { or `undefined` if the user cancels. */ async function showDialectQuickPick( - dialects: QuickPickItem[], - currentDialect: string, + dialects: QuickPickItem[], + currentDialect: string, ): Promise { - const quickPick = window.createQuickPick(); - quickPick.items = dialects; - quickPick.placeholder = 'Select Harper dialect'; - - const activeDialect = dialects.find((dialect) => dialect.label === currentDialect); - if (activeDialect) { - quickPick.activeItems = [activeDialect]; - } - - return await new Promise((resolve) => { - let accepted = false; - - quickPick.onDidAccept(() => { - accepted = true; - resolve(quickPick.selectedItems[0]); - quickPick.hide(); - }); - - quickPick.onDidHide(() => { - quickPick.dispose(); - if (!accepted) { - resolve(undefined); - } - }); - - quickPick.show(); - }); -} + const quickPick = window.createQuickPick(); + quickPick.items = dialects; + quickPick.placeholder = 'Select Harper dialect'; + + const activeDialect = dialects.find((dialect) => dialect.label === currentDialect); + if (activeDialect) { + quickPick.activeItems = [activeDialect]; + } + + return await new Promise((resolve) => { + let accepted = false; + + quickPick.onDidAccept(() => { + accepted = true; + resolve(quickPick.selectedItems[0]); + quickPick.hide(); + }); + + quickPick.onDidHide(() => { + quickPick.dispose(); + if (!accepted) { + resolve(undefined); + } + }); + quickPick.show(); + }); +} export function deactivate(): Thenable | undefined { if (!client) { diff --git a/packages/vscode-plugin/src/tests/fixtures/.vscode/settings.json b/packages/vscode-plugin/src/tests/fixtures/.vscode/settings.json index e9643dd281..e0801147ad 100644 --- a/packages/vscode-plugin/src/tests/fixtures/.vscode/settings.json +++ b/packages/vscode-plugin/src/tests/fixtures/.vscode/settings.json @@ -4,8 +4,6 @@ }, "harper.linters.SpellCheck": true, "harper.linters.RepeatedWords": true, - "harper.dialect": "British", - "harper.excludePatterns": [ - "*.md" - ] + "harper.dialect": "American", + "harper.excludePatterns": [] } diff --git a/packages/vscode-plugin/src/tests/fixtures/integration.md b/packages/vscode-plugin/src/tests/fixtures/integration.md new file mode 100644 index 0000000000..187c8293fe --- /dev/null +++ b/packages/vscode-plugin/src/tests/fixtures/integration.md @@ -0,0 +1,5 @@ +# Integration + +This sentence has grammar errorz, like this this one. + +On the other hand, you'll realise that this sentence doesn't have an error if you're using British English.