Skip to content

Commit d8a14c9

Browse files
committed
Improve leniency towards fixing spelling mistakes
1 parent 2994d9d commit d8a14c9

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

src/DiagnosticCollection.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as vscode from 'vscode';
22
import * as vscodeOniguruma from 'vscode-oniguruma';
33
import { Node, QueryCapture } from 'web-tree-sitter';
44
import { getLastNode, getTrees, parseEvents, queryNode, toRange, trees } from "./TreeSitter";
5-
import { DocumentSelector, getPackageJSON, stringify, wagnerFischer } from "./extension";
5+
import { closeEnoughQuestionMark, DocumentSelector, getPackageJSON, stringify, wagnerFischer } from "./extension";
66
import { unicodeproperties } from "./UNICODE_PROPERTIES";
77

88

@@ -424,8 +424,8 @@ function diagnosticsBrokenIncludes(diagnostics: vscode.Diagnostic[], rootNode: N
424424
const distances = wagnerFischer(text, repoItems);
425425
const distance = distances[0].distance;
426426

427-
let message = `Cannot find repo '${text}'`;
428-
if (distance < 3) {
427+
let message = `Cannot find repo name '${text}'`;
428+
if (closeEnoughQuestionMark(distance, text)) {
429429
message += `. Did you mean '${distances[0].string}'?`;
430430
}
431431
else {

src/Providers/CodeActionsProvider.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as vscode from 'vscode';
22
import { Node, Tree } from 'web-tree-sitter';
33
import { getTrees, queryNode, toRange, trees } from "../TreeSitter";
44
import { unicodeproperties, UNICODE_PROPERTIES } from "../UNICODE_PROPERTIES";
5-
import { stringify, wagnerFischer } from "../extension";
5+
import { closeEnoughQuestionMark, stringify, wagnerFischer } from "../extension";
66

77
export const metadata: vscode.CodeActionProviderMetadata = {
88
providedCodeActionKinds: [
@@ -82,7 +82,7 @@ export const CodeActionsProvider: vscode.CodeActionProvider = {
8282
const distances = wagnerFischer(property, unicodeproperties);
8383
// vscode.window.showInformationMessage(JSON.stringify(distances));
8484
const distance = distances[0].distance;
85-
if (distance > 2) {
85+
if (!closeEnoughQuestionMark(distance, property)) {
8686
continue;
8787
}
8888

src/extension.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,22 @@ export function stringify(this: any, key: string, value: any) {
107107
return value;
108108
}
109109

110-
export function wagnerFischer(word: string, directory: string[]): { distance: number, index: number, string: string; }[] {
111-
const distances: { distance: number, index: number, string: string; }[] = [];
110+
111+
export function closeEnoughQuestionMark(distance: number, text: string): boolean {
112+
return distance < 1.5 * Math.sqrt(text.length); // more lenient for longer words
113+
}
114+
115+
type wagnerFischerResult = {
116+
distance: number,
117+
index: number,
118+
string: string | string[],
119+
};
120+
/** Wagner–Fischer algorithm is a dynamic programming algorithm that computes the edit distance between two strings of characters */
121+
export function wagnerFischer(word: string, directory: string[]): { distance: number, index: number, string: string; }[];
122+
/** Interestingly this also works with arrays of strings */
123+
export function wagnerFischer(words: string[], directorys: string[][]): { distance: number, index: number, string: string[]; }[];
124+
export function wagnerFischer(word: string | string[], directory: string[] | string[][]): wagnerFischerResult[] {
125+
const distances: wagnerFischerResult[] = [];
112126
let index = 0;
113127

114128
for (const targetWord of directory) {
@@ -137,7 +151,12 @@ export function wagnerFischer(word: string, directory: string[]): { distance: nu
137151
prev = next;
138152
}
139153

140-
const distance = { 'distance': prev[targetWord.length], index: index, string: targetWord };
154+
const distance = {
155+
distance: prev[targetWord.length],
156+
index: index,
157+
string: targetWord,
158+
prev: prev,
159+
};
141160
distances.push(distance);
142161

143162
index++;

0 commit comments

Comments
 (0)