forked from streetsidesoftware/vscode-spell-checker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codeActions.mts
203 lines (164 loc) · 7.12 KB
/
codeActions.mts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import type { SpellingDictionary, SuggestionResult, SuggestOptions } from 'cspell-lib';
import { CompoundWordsMethod, constructSettingsForText, getDefaultSettings, getDictionary, IssueType, Text } from 'cspell-lib';
import type { CancellationToken, CodeActionParams, Range as LangServerRange, RequestHandler, TextDocuments } from 'vscode-languageserver/node.js';
import { ResponseError } from 'vscode-languageserver/node.js';
import type { TextDocument } from 'vscode-languageserver-textdocument';
import { Diagnostic, WorkspaceEdit } from 'vscode-languageserver-types';
import { CodeAction, CodeActionKind, TextEdit } from 'vscode-languageserver-types';
import * as Validator from './validator.mjs';
import { getSettigsForDocument, userWords } from './main';
function extractText(textDocument: TextDocument, range: LangServerRange) {
return textDocument.getText(range);
}
function extractDiagnosticData(diag: Diagnostic): Validator.SpellCheckerDiagnosticData {
const { data } = diag;
if (!data || typeof data !== 'object' || Array.isArray(data)) return {};
return data as Validator.SpellCheckerDiagnosticData;
}
export function createOnActionResolveHandler(): RequestHandler<CodeAction, CodeAction, void> {
return (_action: CodeAction, _token: CancellationToken) => { return new ResponseError(0, "Error resolving command"); }
}
export function createOnCodeActionHandler(
documents: TextDocuments<TextDocument>,
): (params: CodeActionParams) => Promise<CodeAction[]> {
const codeActionHandler = new CodeActionHandler(documents);
return (params) => codeActionHandler.handler(params);
}
class CodeActionHandler {
private sugGen: SuggestionGenerator;
constructor(
readonly documents: TextDocuments<TextDocument>,
) {
this.sugGen = new SuggestionGenerator();
}
public async handler(params: CodeActionParams): Promise<CodeAction[]> {
const {
context,
textDocument: { uri },
} = params;
const { diagnostics } = context;
const spellCheckerDiags = diagnostics;
if (!spellCheckerDiags.length) return [];
const textDocument = this.documents.get(uri);
if (!textDocument) return [];
const rangeIntersectDiags = [...spellCheckerDiags]
.map((diag) => diag.range)
.reduce((a: LangServerRange | undefined, _) => a, params.range);
// Only provide suggestions if the selection is contained in the diagnostics.
if (!rangeIntersectDiags || !isWordLikeSelection(textDocument, params.range)) {
return [];
}
const ctx = {
params,
textDocument,
};
const actions = await this.handlerCSpell({ ...ctx, diags: spellCheckerDiags });
const codeAction: CodeAction = {
title: "Add to dictrionary",
kind: CodeActionKind.QuickFix,
diagnostics: diagnostics,
command: {
title: "Add to dictrionary",
command: "AddToDictionary",
arguments: [
{
uri: params.textDocument.uri,
range: diagnostics[0].range,
message: diagnostics[0].message
}
]
},
};
actions.push(codeAction);
return actions;
}
private async handlerCSpell(handlerContext: CodeActionHandlerContext) {
const { textDocument, diags: spellCheckerDiags } = handlerContext;
const actions: CodeAction[] = [];
const uri = textDocument.uri;
if (!spellCheckerDiags.length) return [];
// We do not want to clutter the actions when someone is trying to refactor code
if (spellCheckerDiags.length > 1) return [];
// const { settings: docSetting, dictionary } = await this.getSettings(textDocument);
function replaceText(range: LangServerRange, text?: string) {
return TextEdit.replace(range, text || '');
}
const getSuggestions = (word: string) => {
return this.sugGen.genWordSuggestions(textDocument, word);
};
async function genCodeActionsForSuggestions(_dictionary: SpellingDictionary) {
let diagWord: string | undefined;
for (const diag of spellCheckerDiags) {
const { issueType = IssueType.spelling, suggestions } = extractDiagnosticData(diag);
const srcWord = extractText(textDocument, diag.range);
diagWord = diagWord || srcWord;
const sugs: Validator.Suggestion[] = suggestions ?? (await getSuggestions(srcWord));
sugs.map(({ word, isPreferred }) => ({ word: Text.isLowerCase(word) ? Text.matchCase(srcWord, word) : word, isPreferred }))
.forEach((sug) => {
const sugWord = sug.word;
const title = suggestionToTitle(sug, issueType);
if (!title) return;
// const cmd = createCommand(title, 'cSpell.editText', uri, textDocument.version, [replaceText(diag.range, sugWord)]);
var workspaceEdit: WorkspaceEdit = {
changes: { [uri]: [replaceText(diag.range, sugWord)] }
};
const action = crateWorkspaceAction(title, workspaceEdit, [diag], sug.isPreferred);
actions.push(action);
});
}
return actions;
}
const dictionary = await getDictionary({});
return genCodeActionsForSuggestions(dictionary);
}
}
interface CodeActionHandlerContext {
params: CodeActionParams;
diags: Diagnostic[];
textDocument: TextDocument;
}
function suggestionToTitle(sug: Validator.Suggestion, _: IssueType): string | undefined {
return sug.word + (sug.isPreferred ? ' (preferred)' : '');
}
function crateWorkspaceAction(title: string, workspaceEdit: WorkspaceEdit, diags: Diagnostic[] | undefined, isPreferred?: boolean): CodeAction {
const action = CodeAction.create(title, workspaceEdit, CodeActionKind.QuickFix);
action.diagnostics = diags;
if (isPreferred) {
action.isPreferred = true;
}
return action;
}
function isWordLikeSelection(doc: TextDocument, range: LangServerRange): boolean {
if (range.start.line !== range.end.line) return false;
const text = doc.getText(range);
const hasSpace = /\s/.test(text.trim());
return !hasSpace;
}
const wordLengthForLimitingSuggestions = 15;
const maxNumberOfSuggestionsForLongWords = 1;
const regexJoinedWords = /[+]/g;
class SuggestionGenerator {
async genSuggestions(doc: TextDocument, word: string): Promise<SuggestionResult[]> {
const settings = await getSettigsForDocument(doc);
const dictionary = await getDictionary(settings);
const numSuggestions = 5;
if (word.length > 20) {
return [];
}
const numSugs =
word.length > wordLengthForLimitingSuggestions ? Math.min(maxNumberOfSuggestionsForLongWords, numSuggestions) : numSuggestions;
const options: SuggestOptions = {
numChanges: 3,
numSuggestions: numSugs,
// Turn off compound suggestions for now until it works a bit better.
compoundMethod: CompoundWordsMethod.NONE,
ignoreCase: false,
// Do not included ties, it could create a long list of suggestions.
includeTies: false,
};
return dictionary.suggest(word, options).map((s) => ({ ...s, word: s.word.replace(regexJoinedWords, '') }));
}
async genWordSuggestions(doc: TextDocument, word: string): Promise<Validator.Suggestion[]> {
return (await this.genSuggestions(doc, word)).map(({ word, isPreferred }) => ({ word, isPreferred }));
}
}