Skip to content

Commit 01d3a0e

Browse files
Add range field to hover responses for applicable ranges
Co-authored-by: DanielRosenwasser <[email protected]>
1 parent 45feed3 commit 01d3a0e

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

internal/ls/hover.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ func (l *LanguageService) ProvideHover(ctx context.Context, documentURI lsproto.
3131
if quickInfo == "" {
3232
return lsproto.HoverOrNull{}, nil
3333
}
34+
35+
// Calculate the applicable range for the hover
36+
rangeNode := getNodeForQuickInfo(node)
37+
textRange := core.NewTextRange(rangeNode.Pos(), rangeNode.End())
38+
hoverRange := l.converters.ToLSPRange(file, textRange)
39+
3440
return lsproto.HoverOrNull{
3541
Hover: &lsproto.Hover{
3642
Contents: lsproto.MarkupContentOrStringOrMarkedStringWithLanguageOrMarkedStrings{
@@ -39,6 +45,7 @@ func (l *LanguageService) ProvideHover(ctx context.Context, documentURI lsproto.
3945
Value: formatQuickInfo(quickInfo) + documentation,
4046
},
4147
},
48+
Range: &hoverRange,
4249
},
4350
}, nil
4451
}

internal/ls/hover_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ function myFunction() {
4747
Value: "```tsx\nfunction myFunction(): string\n```\nA function with JSDoc links that previously caused panic\n`console.log` and `Array.from` and `Object.keys`",
4848
},
4949
},
50+
Range: &lsproto.Range{
51+
Start: lsproto.Position{Line: 6, Character: 1},
52+
End: lsproto.Position{Line: 8, Character: 10},
53+
},
5054
},
5155
},
5256
},
@@ -70,6 +74,10 @@ myFunction();`,
7074
Value: "```tsx\nfunction myFunction(param: string): string\n```\n\n\n*@param* `param` - the greatest of days\n",
7175
},
7276
},
77+
Range: &lsproto.Range{
78+
Start: lsproto.Position{Line: 3, Character: 8},
79+
End: lsproto.Position{Line: 3, Character: 19},
80+
},
7381
},
7482
},
7583
},
@@ -93,6 +101,10 @@ function myFunction(param) {
93101
Value: "```tsx\nfunction myFunction(param: string): string\n```\n\n\n*@param* `param` - the greatest of days\n",
94102
},
95103
},
104+
Range: &lsproto.Range{
105+
Start: lsproto.Position{Line: 5, Character: 1},
106+
End: lsproto.Position{Line: 7, Character: 10},
107+
},
96108
},
97109
},
98110
},
@@ -116,6 +128,10 @@ myFunction();`,
116128
Value: "```tsx\n(parameter) param: string\n```\n- the greatest of days\n",
117129
},
118130
},
131+
Range: &lsproto.Range{
132+
Start: lsproto.Position{Line: 3, Character: 20},
133+
End: lsproto.Position{Line: 3, Character: 25},
134+
},
119135
},
120136
},
121137
},
@@ -136,6 +152,28 @@ myFunction();`,
136152
"marker": nil,
137153
},
138154
},
155+
{
156+
title: "HoverRangeTest",
157+
input: `
158+
// @filename: index.ts
159+
function /*marker*/testFunction(param: string): string {
160+
return param;
161+
}`,
162+
expected: map[string]*lsproto.Hover{
163+
"marker": {
164+
Contents: lsproto.MarkupContentOrStringOrMarkedStringWithLanguageOrMarkedStrings{
165+
MarkupContent: &lsproto.MarkupContent{
166+
Kind: lsproto.MarkupKindMarkdown,
167+
Value: "```tsx\nfunction testFunction(param: string): string\n```\n",
168+
},
169+
},
170+
Range: &lsproto.Range{
171+
Start: lsproto.Position{Line: 0, Character: 8},
172+
End: lsproto.Position{Line: 0, Character: 21},
173+
},
174+
},
175+
},
176+
},
139177
}
140178

141179
for _, testCase := range testCases {

0 commit comments

Comments
 (0)