Skip to content
Open
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
12 changes: 4 additions & 8 deletions src/languageFeatures/documentSymbols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ export class MdDocumentSymbolProvider {
while (additionalSymbols.length && isBefore(additionalSymbols[0].range.end, entry.sectionLocation.range.start)) {
parent.children.push(additionalSymbols.shift()!);
}

while (parent && entry.level <= parent.level) {
parent = parent.parent;
}

if (!parent) {
// Should not happen
return;
Expand All @@ -107,21 +107,17 @@ export class MdDocumentSymbolProvider {
const symbol = this.#tocToDocumentSymbol(entry);
symbol.children = [];
parent.children.push(symbol);

parent = { level: entry.level, children: symbol.children, parent, range: entry.sectionLocation.range };
}
}

#tocToDocumentSymbol(entry: TocEntry): lsp.DocumentSymbol {
return {
name: this.#getTocSymbolName(entry),
name: entry.text,
kind: lsp.SymbolKind.String,
range: entry.sectionLocation.range,
selectionRange: entry.sectionLocation.range
};
}

#getTocSymbolName(entry: TocEntry): string {
return '#'.repeat(entry.level) + ' ' + entry.text;
}
}
40 changes: 20 additions & 20 deletions src/test/documentSymbols.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ suite('Document symbols', () => {
test('Should return single symbol for single header', withStore(async (store) => {
const symbols = await getSymbolsForFile(store, '# h');
assertDocumentSymbolsEqual(symbols, [
{ name: '# h' },
{ name: 'h' },
]);
}));

test('Should not care about symbol level for single header', withStore(async (store) => {
const symbols = await getSymbolsForFile(store, '### h');
assertDocumentSymbolsEqual(symbols, [
{ name: '### h' },
{ name: 'h' },
]);
}));

Expand All @@ -104,8 +104,8 @@ suite('Document symbols', () => {
`## h2`,
));
assertDocumentSymbolsEqual(symbols, [
{ name: '## h' },
{ name: '## h2' },
{ name: 'h' },
{ name: 'h2' },
]);
}));

Expand All @@ -117,10 +117,10 @@ suite('Document symbols', () => {
));
assertDocumentSymbolsEqual(symbols, [
{
name: '# h',
name: 'h',
children: [
{ name: '## h2' },
{ name: '## h3' },
{ name: 'h2' },
{ name: 'h3' },
]
}
]);
Expand All @@ -133,9 +133,9 @@ suite('Document symbols', () => {
));
assertDocumentSymbolsEqual(symbols, [
{
name: '# h',
name: 'h',
children: [
{ name: '#### h2' }
{ name: 'h2' }
]
}
]);
Expand All @@ -149,10 +149,10 @@ suite('Document symbols', () => {
));
assertDocumentSymbolsEqual(symbols, [
{
name: '# h',
name: 'h',
children: [
{ name: '### h2' },
{ name: '## h3' },
{ name: 'h2' },
{ name: 'h3' },
]
},
]);
Expand All @@ -167,8 +167,8 @@ suite('Document symbols', () => {
`- bar`,
));
assertDocumentSymbolsEqual(symbols, [
{ name: '# A' },
{ name: '# B' },
{ name: 'A' },
{ name: 'B' },
]);
}));

Expand Down Expand Up @@ -207,16 +207,16 @@ suite('Document symbols', () => {
assertDocumentSymbolsEqual(symbols, [
{ name: '[def 1]', range: lsp.Range.create(0, 0, 0, 27), selectionRange: lsp.Range.create(0, 1, 0, 6), },
{
name: '# h1',
name: 'h1',
children: [
{ name: '[def 2]', range: lsp.Range.create(2, 0, 2, 27), selectionRange: lsp.Range.create(2, 1, 2, 6), },
{
name: '#### h2',
name: 'h2',
children: [
{ name: '[def 3]', range: lsp.Range.create(4, 0, 4, 27), selectionRange: lsp.Range.create(4, 1, 4, 6), },
]
},
{ name: '## h3', children: [] },
{ name: 'h3', children: [] },
]
},
{ name: '[def 4]', range: lsp.Range.create(6, 0, 6, 27), selectionRange: lsp.Range.create(6, 1, 6, 6), },
Expand All @@ -229,8 +229,8 @@ suite('Document symbols', () => {
'## [a `b` **c**](http://example.com)',
));
assertDocumentSymbolsEqual(symbols, [
{ name: '## a b c' },
{ name: '## a b c' },
{ name: 'a b c' },
{ name: 'a b c' },
]);
}));

Expand All @@ -239,7 +239,7 @@ suite('Document symbols', () => {
'## <b>a</b> `b` **c**',
));
assertDocumentSymbolsEqual(symbols, [
{ name: '## a b c' },
{ name: 'a b c' },
]);
}));
});
20 changes: 10 additions & 10 deletions src/test/workspaceSymbol.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ suite('Workspace symbols', () => {
new InMemoryDocument(workspacePath('test.md'), `# header1\nabc\n## header2`)
]));

assertSymbolsMatch(await getWorkspaceSymbols(store, workspace, ''), ['# header1', '## header2']);
assertSymbolsMatch(await getWorkspaceSymbols(store, workspace, ''), ['header1', 'header2']);
}));

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

// Update file
workspace.updateDocument(new InMemoryDocument(testFileName, `# new header\nabc\n## header2`, 2 /* version */));
assertSymbolsMatch(await getWorkspaceSymbols(store, workspace, ''), ['# new header', '## 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,28 +112,28 @@ suite('Workspace symbols', () => {
))
]));

assertSymbolsMatch(await getWorkspaceSymbols(store, workspace, ''), ['# 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`)
]));

assertSymbolsMatch(await getWorkspaceSymbols(store, workspace, 'ABC'), ['# aBc1', '## ABc2']);
assertSymbolsMatch(await getWorkspaceSymbols(store, workspace, 'abc'), ['# aBc1', '## 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, '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'), []);
assertSymbolsMatch(await getWorkspaceSymbols(store, workspace, 'ccat'), []);
}));

test('Should strip markup in headers', withStore(async (store) => {
Expand All @@ -144,6 +144,6 @@ suite('Workspace symbols', () => {
))
]));

assertSymbolsMatch(await getWorkspaceSymbols(store, workspace, 'a'), ['# a b c', '# a x y']);
assertSymbolsMatch(await getWorkspaceSymbols(store, workspace, 'a'), ['a b c', 'a x y']);
}));
});