Skip to content

Commit

Permalink
fix: add 4 commands: jump previous/next spell error (#1198)
Browse files Browse the repository at this point in the history
* implemented
* Update packages/client/src/commands.ts
* Update package.json
* change to info message
* added 2 more commands to just jump without suggestion & rename commands
  • Loading branch information
elazarcoh authored Aug 26, 2021
1 parent 0a25bf2 commit 165fb66
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
20 changes: 20 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,26 @@
"category": "Spell",
"title": "Spelling Suggestions..."
},
{
"command": "cSpell.goToNextSpellingIssue",
"category": "Spell",
"title": "Go to Next Spelling Issue"
},
{
"command": "cSpell.goToPreviousSpellingIssue",
"category": "Spell",
"title": "Go to Previous Spelling Issue"
},
{
"command": "cSpell.goToNextSpellingIssueAndSuggest",
"category": "Spell",
"title": "Go to Next Spelling Issue and Suggest"
},
{
"command": "cSpell.goToPreviousSpellingIssueAndSuggest",
"category": "Spell",
"title": "Go to Previous Spelling Issue and Suggest"
},
{
"command": "cSpellRegExpTester.testRegExp",
"title": "Test a Regular Expression on the current document."
Expand Down
39 changes: 39 additions & 0 deletions packages/client/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ import {
QuickPickOptions,
Range,
TextDocument,
TextEditorRevealType,
Uri,
window,
workspace,
WorkspaceEdit,
Selection,
Diagnostic,
} from 'vscode';
import { TextEdit } from 'vscode-languageclient/node';
import * as di from './di';
Expand Down Expand Up @@ -119,6 +122,11 @@ const commandHandlers: CommandHandler = {

'cSpell.suggestSpellingCorrections': actionSuggestSpellingCorrections,

'cSpell.goToNextSpellingIssue': () => actionJumpToSpellingError('next', false),
'cSpell.goToPreviousSpellingIssue': () => actionJumpToSpellingError('previous', false),
'cSpell.goToNextSpellingIssueAndSuggest': () => actionJumpToSpellingError('next', true),
'cSpell.goToPreviousSpellingIssueAndSuggest': () => actionJumpToSpellingError('previous', true),

'cSpell.enableLanguage': enableLanguageIdCmd,
'cSpell.disableLanguage': disableLanguageIdCmd,
'cSpell.enableForGlobal': async () => setEnableSpellChecking(await tfCfg(ConfigurationTarget.Global), true),
Expand Down Expand Up @@ -531,3 +539,34 @@ async function createCSpellConfig(): Promise<void> {
export const __testing__ = {
commandHandlers,
};

function nextDiags(diags: Diagnostic[], selection: Selection): Diagnostic | undefined {
// concat next diags with the first diag to get a cycle
return diags.filter((d) => d.range?.start.isAfter(selection.end)).concat(diags[0])[0];
}

function previousDiags(diags: Diagnostic[], selection: Selection): Diagnostic | undefined {
// concat the last diag with all previous diags to get a cycle
return [diags[diags.length - 1]].concat(diags.filter((d) => d.range?.end.isBefore(selection.start))).pop();
}

async function actionJumpToSpellingError(which: 'next' | 'previous', suggest: boolean) {
const editor = window.activeTextEditor;
if (!editor) return;
const document = editor.document;
const selection = editor.selection;
const diags = document ? getCSpellDiags(document.uri) : undefined;

const matchingDiags = diags ? (which === 'next' ? nextDiags(diags, selection) : previousDiags(diags, selection)) : undefined;
const range = matchingDiags?.range;
if (!document || !selection || !range || !matchingDiags) {
return pVoid(window.showInformationMessage('No issues found in this document.'), 'actionSuggestSpellingCorrections', onError);
}

editor.revealRange(range, TextEditorRevealType.InCenterIfOutsideViewport);
editor.selection = new Selection(range.start, range.end);

if (suggest) {
await commands.executeCommand('cSpell.suggestSpellingCorrections');
}
}

0 comments on commit 165fb66

Please sign in to comment.