Skip to content

Fix hover range calculation: eliminate duplicate calls and remove whitespace from ranges #1489

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 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
7 changes: 7 additions & 0 deletions internal/ls/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
if quickInfo == "" {
return lsproto.HoverOrNull{}, nil
}

// Calculate the applicable range for the hover
rangeNode := getNodeForQuickInfo(node)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gets called twice - once from within getQuickInfoAndDocumentation.

In Strada, this is named as nodeForQuickInfo. We should just calculate it above the call to getQuickInfoAndDocumentation and get rid of the call within that function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in afe2a85 by refactoring to calculate getNodeForQuickInfo() once in the main function and passing it through to getQuickInfoAndDocumentationWithNode(), eliminating the duplicate call.

textRange := core.NewTextRange(rangeNode.Pos(), rangeNode.End())
hoverRange := l.converters.ToLSPRange(file, textRange)

Check failure on line 38 in internal/ls/hover.go

View workflow job for this annotation

GitHub Actions / copilot

no new variables on left side of :=

return lsproto.HoverOrNull{
Hover: &lsproto.Hover{
Contents: lsproto.MarkupContentOrStringOrMarkedStringWithLanguageOrMarkedStrings{
Expand All @@ -39,6 +45,7 @@
Value: formatQuickInfo(quickInfo) + documentation,
},
},
Range: &hoverRange,
},
}, nil
}
Expand Down
38 changes: 38 additions & 0 deletions internal/ls/hover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ function myFunction() {
Value: "```tsx\nfunction myFunction(): string\n```\nA function with JSDoc links that previously caused panic\n`console.log` and `Array.from` and `Object.keys`",
},
},
Range: &lsproto.Range{
Start: lsproto.Position{Line: 6, Character: 1},
End: lsproto.Position{Line: 8, Character: 10},
},
},
},
},
Expand All @@ -70,6 +74,10 @@ myFunction();`,
Value: "```tsx\nfunction myFunction(param: string): string\n```\n\n\n*@param* `param` - the greatest of days\n",
},
},
Range: &lsproto.Range{
Start: lsproto.Position{Line: 3, Character: 8},
End: lsproto.Position{Line: 3, Character: 19},
},
},
},
},
Expand All @@ -93,6 +101,10 @@ function myFunction(param) {
Value: "```tsx\nfunction myFunction(param: string): string\n```\n\n\n*@param* `param` - the greatest of days\n",
},
},
Range: &lsproto.Range{
Start: lsproto.Position{Line: 5, Character: 1},
End: lsproto.Position{Line: 7, Character: 10},
},
},
},
},
Expand All @@ -116,6 +128,10 @@ myFunction();`,
Value: "```tsx\n(parameter) param: string\n```\n- the greatest of days\n",
},
},
Range: &lsproto.Range{
Start: lsproto.Position{Line: 3, Character: 20},
End: lsproto.Position{Line: 3, Character: 25},
},
},
},
},
Expand All @@ -136,6 +152,28 @@ myFunction();`,
"marker": nil,
},
},
{
title: "HoverRangeTest",
input: `
// @filename: index.ts
function /*marker*/testFunction(param: string): string {
return param;
}`,
expected: map[string]*lsproto.Hover{
"marker": {
Contents: lsproto.MarkupContentOrStringOrMarkedStringWithLanguageOrMarkedStrings{
MarkupContent: &lsproto.MarkupContent{
Kind: lsproto.MarkupKindMarkdown,
Value: "```tsx\nfunction testFunction(param: string): string\n```\n",
},
},
Range: &lsproto.Range{
Start: lsproto.Position{Line: 0, Character: 8},
End: lsproto.Position{Line: 0, Character: 21},
},
},
},
},
}

for _, testCase := range testCases {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
// ...args
// ): StylingFunction => {
// return curry(getStylingByKeys, 2)(mergedStyling, ...args);
// ^
// ^^^^
// | ----------------------------------------------------------------------
// | ```tsx
// | (parameter) args: []
Expand All @@ -108,6 +108,16 @@
"contents": {
"kind": "markdown",
"value": "```tsx\n(parameter) args: []\n```\n"
},
"range": {
"start": {
"line": 82,
"character": 60
},
"end": {
"line": 82,
"character": 64
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
// }
// declare const a: ThingWithDeprecations<void>
// a.subscribe(() => {
// ^
// ^^^^^^^^^
// | ----------------------------------------------------------------------
// | ```tsx
// | (method) ThingWithDeprecations.subscribe(observer?: PartialObserver<void>): Subscription
Expand All @@ -47,6 +47,16 @@
"contents": {
"kind": "markdown",
"value": "```tsx\n(method) ThingWithDeprecations.subscribe(observer?: PartialObserver<void>): Subscription\n```\n"
},
"range": {
"start": {
"line": 22,
"character": 2
},
"end": {
"line": 22,
"character": 11
}
}
}
}
Expand Down
Loading
Loading