Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use fuzzy matching for workspace symbol search #161

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 0.4.0-alpha.9 — Unreleased
- Use fuzzy matching for workspace symbol search.

## 0.4.0-alpha.8 — October 31, 2023
- Fix potential catastrophic backtracking in a regular expression.
- Fix some false positives for link validation.
Expand Down
5 changes: 4 additions & 1 deletion src/languageFeatures/workspaceSymbols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Disposable } from '../util/dispose';
import { IWorkspace } from '../workspace';
import { MdWorkspaceInfoCache } from '../workspaceCache';
import { MdDocumentSymbolProvider } from './documentSymbols';
import { fuzzyContains } from '../util/string';

export class MdWorkspaceSymbolProvider extends Disposable {

Expand All @@ -33,7 +34,9 @@ export class MdWorkspaceSymbolProvider extends Disposable {
}

const normalizedQueryStr = query.toLowerCase();
return allSymbols.flat().filter(symbolInformation => symbolInformation.name.toLowerCase().includes(normalizedQueryStr));
return allSymbols.flat().filter(symbolInformation => {
return fuzzyContains(symbolInformation.name.toLowerCase(), normalizedQueryStr);
});
}

public async provideDocumentSymbolInformation(document: ITextDocument, token: CancellationToken): Promise<lsp.SymbolInformation[]> {
Expand Down
47 changes: 24 additions & 23 deletions src/test/workspaceSymbol.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ function getWorkspaceSymbols(store: DisposableStore, workspace: IWorkspace, quer
return workspaceSymbolProvider.provideWorkspaceSymbols(query, noopToken);
}

function assertSymbolsMatch(symbols: readonly lsp.WorkspaceSymbol[], expectedNames: readonly string[]): void {
assert.strictEqual(symbols.length, expectedNames.length);
for (let i = 0; i < symbols.length; i++) {
assert.strictEqual(symbols[i].name, expectedNames[i]);
}
}

suite('Workspace symbols', () => {
test('Should not return anything for empty workspace', withStore(async (store) => {
const workspace = store.add(new InMemoryWorkspace([]));
Expand All @@ -38,10 +45,7 @@ suite('Workspace symbols', () => {
new InMemoryDocument(workspacePath('test.md'), `# header1\nabc\n## header2`)
]));

const symbols = await getWorkspaceSymbols(store, workspace, '');
assert.strictEqual(symbols.length, 2);
assert.strictEqual(symbols[0].name, '# header1');
assert.strictEqual(symbols[1].name, '## header2');
assertSymbolsMatch(await getWorkspaceSymbols(store, workspace, ''), ['# header1', '## header2']);
}));

test('Should return all content basic workspace', withStore(async (store) => {
Expand All @@ -68,10 +72,7 @@ suite('Workspace symbols', () => {

// Update file
workspace.updateDocument(new InMemoryDocument(testFileName, `# new header\nabc\n## header2`, 2 /* version */));
const newSymbols = await getWorkspaceSymbols(store, workspace, '');
assert.strictEqual(newSymbols.length, 2);
assert.strictEqual(newSymbols[0].name, '# new header');
assert.strictEqual(newSymbols[1].name, '## header2');
assertSymbolsMatch(await getWorkspaceSymbols(store, workspace, ''), ['# new header', '## header2']);
}));

test('Should remove results when file is deleted', withStore(async (store) => {
Expand Down Expand Up @@ -112,27 +113,27 @@ suite('Workspace symbols', () => {
))
]));

const symbols = await getWorkspaceSymbols(store, workspace, '');
assert.strictEqual(symbols.length, 1);
assert.strictEqual(symbols[0].name, '# header1');
assertSymbolsMatch(await getWorkspaceSymbols(store, workspace, ''), ['# header1']);
}));

test('Should match case insensitively', withStore(async (store) => {
const workspace = store.add(new InMemoryWorkspace([
new InMemoryDocument(workspacePath('test.md'), `# aBc1\nabc\n## ABc2`)
]));

{
const symbols = await getWorkspaceSymbols(store, workspace, 'ABC');
assert.strictEqual(symbols.length, 2);
assert.strictEqual(symbols[0].name, '# aBc1');
assert.strictEqual(symbols[1].name, '## ABc2');
}
{
const symbols = await getWorkspaceSymbols(store, workspace, 'abc');
assert.strictEqual(symbols.length, 2);
assert.strictEqual(symbols[0].name, '# aBc1');
assert.strictEqual(symbols[1].name, '## ABc2');
}
assertSymbolsMatch(await getWorkspaceSymbols(store, workspace, 'ABC'), ['# aBc1', '## ABc2']);
assertSymbolsMatch(await getWorkspaceSymbols(store, workspace, 'abc'), ['# aBc1', '## ABc2']);
}));

test('Should match fuzzyily', withStore(async (store) => {
const workspace = store.add(new InMemoryWorkspace([
new InMemoryDocument(workspacePath('test.md'), `# cat dog fish`)
]));

assertSymbolsMatch(await getWorkspaceSymbols(store, workspace, 'cat'), ['# cat dog fish']);
assertSymbolsMatch(await getWorkspaceSymbols(store, workspace, 'cdf'), ['# cat dog fish']);
assertSymbolsMatch(await getWorkspaceSymbols(store, workspace, 'catfish'), ['# cat dog fish']);
assertSymbolsMatch(await getWorkspaceSymbols(store, workspace, 'fishcat'), []); // wrong order
assertSymbolsMatch(await getWorkspaceSymbols(store, workspace, 'ccat'), []);
}));
});
28 changes: 28 additions & 0 deletions src/util/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,31 @@ export function isEmptyOrWhitespace(str: string): boolean {
}

export const r = String.raw;

/**
* Checks if the characters of the provided query string are included in the
* target string. The characters do not have to be contiguous within the string.
*/
export function fuzzyContains(target: string, query: string): boolean {
if (target.length < query.length) {
return false; // impossible for query to be contained in target
}

const queryLen = query.length;
const targetLower = target.toLowerCase();

let index = 0;
let lastIndexOf = -1;
while (index < queryLen) {
const indexOf = targetLower.indexOf(query[index], lastIndexOf + 1);
if (indexOf < 0) {
return false;
}

lastIndexOf = indexOf;

index++;
}

return true;
}