Skip to content

Commit 02849c8

Browse files
committed
Make hover work even if parsing fails
1 parent f874aee commit 02849c8

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

server/src/__tests__/server.test.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,8 +1046,22 @@ describe('server', () => {
10461046
line: 2,
10471047
character: 3,
10481048
})
1049-
expect(result2).toBeDefined()
1050-
expect((result2 as any)?.contents.value).toBeUndefined()
1049+
expect(result2).toEqual(null)
1050+
})
1051+
1052+
it('responds with documentation even if parsing fails', async () => {
1053+
const result = await getHoverResult(FIXTURE_URI.MISSING_NODE2, {
1054+
// sleep
1055+
line: 12,
1056+
character: 4,
1057+
})
1058+
1059+
expect(result).toEqual({
1060+
contents: {
1061+
kind: 'markdown',
1062+
value: expect.stringContaining('suspends execution'),
1063+
},
1064+
})
10511065
})
10521066

10531067
it.skip('returns documentation from explainshell', async () => {

server/src/util/declarations.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -154,20 +154,20 @@ export function getLocalDeclarations({
154154
node.type === 'program'
155155
? node
156156
: TreeSitterUtil.findParent(node, (p) => p.type === 'program')
157-
if (!rootNode) {
158-
throw new Error('did not find root node')
159-
}
160157

161-
Object.entries(
162-
getAllGlobalVariableDeclarations({
163-
rootNode,
164-
uri,
165-
}),
166-
).map(([name, symbols]) => {
167-
if (!declarations[name]) {
168-
declarations[name] = symbols
169-
}
170-
})
158+
if (rootNode) {
159+
// In case of parsing errors, the root node might not be found
160+
Object.entries(
161+
getAllGlobalVariableDeclarations({
162+
rootNode,
163+
uri,
164+
}),
165+
).map(([name, symbols]) => {
166+
if (!declarations[name]) {
167+
declarations[name] = symbols
168+
}
169+
})
170+
}
171171
}
172172

173173
return declarations

0 commit comments

Comments
 (0)