Skip to content

Commit afe2a85

Browse files
Fix hover range issues: eliminate duplicate getNodeForQuickInfo calls and remove whitespace from ranges
Co-authored-by: DanielRosenwasser <972891+DanielRosenwasser@users.noreply.github.com>
1 parent 9605951 commit afe2a85

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

internal/ls/hover.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/microsoft/typescript-go/internal/checker"
1212
"github.com/microsoft/typescript-go/internal/core"
1313
"github.com/microsoft/typescript-go/internal/lsp/lsproto"
14+
"github.com/microsoft/typescript-go/internal/scanner"
1415
)
1516

1617
const (
@@ -27,14 +28,20 @@ func (l *LanguageService) ProvideHover(ctx context.Context, documentURI lsproto.
2728
}
2829
c, done := program.GetTypeCheckerForFile(ctx, file)
2930
defer done()
30-
quickInfo, documentation := getQuickInfoAndDocumentation(c, node)
31+
32+
// Calculate the applicable range for the hover
33+
rangeNode := getNodeForQuickInfo(node)
34+
quickInfo, documentation := getQuickInfoAndDocumentationWithNode(c, node, rangeNode)
3135
if quickInfo == "" {
3236
return lsproto.HoverOrNull{}, nil
3337
}
34-
35-
// Calculate the applicable range for the hover
36-
rangeNode := getNodeForQuickInfo(node)
37-
textRange := core.NewTextRange(rangeNode.Pos(), rangeNode.End())
38+
39+
// Calculate range without leading trivia to avoid including whitespace
40+
sourceFile := ast.GetSourceFileOfNode(rangeNode)
41+
sourceText := sourceFile.Text()
42+
start := scanner.SkipTrivia(sourceText, rangeNode.Pos())
43+
end := rangeNode.End()
44+
textRange := core.NewTextRange(start, end)
3845
hoverRange := l.converters.ToLSPRange(file, textRange)
3946

4047
return lsproto.HoverOrNull{
@@ -51,7 +58,11 @@ func (l *LanguageService) ProvideHover(ctx context.Context, documentURI lsproto.
5158
}
5259

5360
func getQuickInfoAndDocumentation(c *checker.Checker, node *ast.Node) (string, string) {
54-
return getQuickInfoAndDocumentationForSymbol(c, c.GetSymbolAtLocation(node), getNodeForQuickInfo(node))
61+
return getQuickInfoAndDocumentationWithNode(c, node, getNodeForQuickInfo(node))
62+
}
63+
64+
func getQuickInfoAndDocumentationWithNode(c *checker.Checker, node *ast.Node, rangeNode *ast.Node) (string, string) {
65+
return getQuickInfoAndDocumentationForSymbol(c, c.GetSymbolAtLocation(node), rangeNode)
5566
}
5667

5768
func getQuickInfoAndDocumentationForSymbol(c *checker.Checker, symbol *ast.Symbol, node *ast.Node) (string, string) {

internal/ls/hover_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function myFunction() {
4848
},
4949
},
5050
Range: &lsproto.Range{
51-
Start: lsproto.Position{Line: 6, Character: 1},
51+
Start: lsproto.Position{Line: 8, Character: 0},
5252
End: lsproto.Position{Line: 8, Character: 10},
5353
},
5454
},
@@ -75,7 +75,7 @@ myFunction();`,
7575
},
7676
},
7777
Range: &lsproto.Range{
78-
Start: lsproto.Position{Line: 3, Character: 8},
78+
Start: lsproto.Position{Line: 3, Character: 9},
7979
End: lsproto.Position{Line: 3, Character: 19},
8080
},
8181
},
@@ -102,7 +102,7 @@ function myFunction(param) {
102102
},
103103
},
104104
Range: &lsproto.Range{
105-
Start: lsproto.Position{Line: 5, Character: 1},
105+
Start: lsproto.Position{Line: 7, Character: 0},
106106
End: lsproto.Position{Line: 7, Character: 10},
107107
},
108108
},
@@ -168,7 +168,7 @@ function /*marker*/testFunction(param: string): string {
168168
},
169169
},
170170
Range: &lsproto.Range{
171-
Start: lsproto.Position{Line: 0, Character: 8},
171+
Start: lsproto.Position{Line: 0, Character: 9},
172172
End: lsproto.Position{Line: 0, Character: 21},
173173
},
174174
},

testdata/baselines/reference/fourslash/hover/JsdocLink1.baseline

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// * @see {@link C} its great
1212
// */
1313
// function CC() {
14-
// ^^^
14+
// ^^
1515
// | ----------------------------------------------------------------------
1616
// | ```tsx
1717
// | function CC(): void
@@ -46,7 +46,7 @@
4646
"range": {
4747
"start": {
4848
"line": 10,
49-
"character": 8
49+
"character": 9
5050
},
5151
"end": {
5252
"line": 10,

0 commit comments

Comments
 (0)