Skip to content

Commit b917a10

Browse files
committed
Improve outline hierarchy
By providing the container name and changing the symbols ranges to contain the entire nodes ranges not just the name of the symbol. The last part has the unfortunate consequence that when you go to a definition it will no longer go to the exact word but instead the line where the symbol is defined.
1 parent b8d9f07 commit b917a10

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

server/src/analyser.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,21 @@ export default class Analyzer {
190190
const name = contents.slice(named.startIndex, named.endIndex)
191191
const namedDeclarations = this.uriToDeclarations[uri][name] || []
192192

193+
const parent = TreeSitterUtil.findParent(n, p => p.type === 'function_definition')
194+
const parentName = parent
195+
? contents.slice(
196+
parent.firstNamedChild.startIndex,
197+
parent.firstNamedChild.endIndex,
198+
)
199+
: null
200+
193201
namedDeclarations.push(
194202
LSP.SymbolInformation.create(
195203
name,
196204
this.treeSitterTypeToLSPKind[n.type],
197-
TreeSitterUtil.range(named),
205+
TreeSitterUtil.range(n),
198206
uri,
207+
parentName,
199208
),
200209
)
201210
this.uriToDeclarations[uri][name] = namedDeclarations

server/src/util/tree-sitter.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,17 @@ export function isReference(n: ASTNode): boolean {
4040
return false
4141
}
4242
}
43+
44+
export function findParent(
45+
start: ASTNode,
46+
predicate: (n: ASTNode) => boolean,
47+
): ASTNode | null {
48+
let node = start.parent
49+
while (node !== null) {
50+
if (predicate(node)) {
51+
return node
52+
}
53+
node = node.parent
54+
}
55+
return null
56+
}

0 commit comments

Comments
 (0)