Skip to content

Signature help implementation #861

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

Open
wants to merge 41 commits into
base: main
Choose a base branch
from

Conversation

navya9singh
Copy link
Member

This pr covers mostly everything that is needed for signature help except for signature help in .js files. Some tests that I haven't yet ported from Strada are tests for:

  1. Tagged templates
  2. Verify no signature help
  3. Filtered triggers
  4. Tests with JSDoc comments and other comments. It needs some changes in the test setup

@Copilot Copilot AI review requested due to automatic review settings May 12, 2025 21:09
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR implements support for signature help in the language server and makes extensive naming refactors throughout the checker and utility modules. Key changes include adding a new handler for signature help in the server, updating configuration for the SignatureHelpProvider, and renaming many internal API functions (e.g. getSignaturesOfType → GetSignaturesOfType) to improve consistency and clarity.

Reviewed Changes

Copilot reviewed 16 out of 16 changed files in this pull request and generated no comments.

Show a summary per file
File Description
internal/lsp/server.go Adds new case handling for SignatureHelpParams and configures the signature help provider.
internal/ls/utilities.go Updates comments and refactors type argument info functions without altering behavior.
internal/checker/utilities.go Renames internal helper functions (e.g. CanHaveSymbol, IsCallOrNewExpression) for consistency.
internal/checker/types.go Adds accessor methods on Signature and TupleType for better API exposure.
internal/checker/services.go Consistently replaces lower-case API functions with their exported versions.
internal/checker/relater.go Refactors signature comparison and tuple handling to use new exported functions.
internal/checker/printer.go Adjusts printer logic to use the new exported APIs and updates variadic parameter expansion.
internal/checker/jsx.go Updates JSX-related checks to use renamed functions.
internal/checker/inference.go Updates inference utilities with new function names.
internal/checker/flow.go Refactors flow analysis to call updated API names.
internal/checker/checker.go Widespread updates to use capitalized exported API functions for type retrievals and checks.
internal/astnav/tokens.go Adds a new helper function, HasQuestionToken, for token analysis.
internal/ast/utilities.go Adds helpers for handling template literal tokens and string text nodes.
_submodules/TypeScript Updates the submodule commit to a new revision.

@@ -2073,3 +2084,211 @@ func (c *Checker) checkNotCanceled() {
panic("Checker was previously cancelled")
}
}

Copy link
Member Author

Choose a reason for hiding this comment

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

These are some functions I added temporarily to make Signature help work. They will be removed once the node builder pr is merged. Also, these do not give the best result for generics, and returns object instead. This is why some of the tests have a comments //returns object because this part returns object.

Comment on lines 104 to 105
// Converting signatureHelpParameter to *lsproto.ParameterInformation
var parameters []*lsproto.ParameterInformation
Copy link
Member Author

Choose a reason for hiding this comment

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

Some of these functions are converting from local types to lsproto type because they needed some extra parameters like isRest, isOptional or isVariadic to be able to correctly calculate the parameter count and the parameter index that would be highlighted.

Copy link
Member

Choose a reason for hiding this comment

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

This slice can be allocated with the correct length/capacity

@@ -602,3 +602,13 @@ func getNodeVisitor(
},
})
}

// !!!
func HasQuestionToken(node *ast.Node) bool {
Copy link
Member

Choose a reason for hiding this comment

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

Should this go into the ast package?

@@ -17392,7 +17392,7 @@ func (c *Checker) getTypeOfPropertyOfType(t *Type, name string) *Type {
return nil
}

func (c *Checker) getSignaturesOfType(t *Type, kind SignatureKind) []*Signature {
func (c *Checker) GetSignaturesOfType(t *Type, kind SignatureKind) []*Signature {
Copy link
Member

Choose a reason for hiding this comment

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

Don't rename this method; just declare GetSignaturesOfType in exports.go and then forward to this unexported method.

I think the same should go for all of the other unexported functions; we're trying to consolidate these into a clearer API and also avoid huge diffs due to renames (which this PR does at the moment).

Copy link
Member

@DanielRosenwasser DanielRosenwasser left a comment

Choose a reason for hiding this comment

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

Mostly stylistic feedback. I haven't fully reviewed but I haven't seen anything glaringly different from the original so far!

Comment on lines 487 to 489
ancestorNode := ast.FindAncestor(node, func(n *ast.Node) bool {
return ast.IsCallLikeOrFunctionLikeExpression(n)
})
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
ancestorNode := ast.FindAncestor(node, func(n *ast.Node) bool {
return ast.IsCallLikeOrFunctionLikeExpression(n)
})
ancestorNode := ast.FindAncestor(node, ast.IsCallLikeOrFunctionLikeExpression)

return ast.IsCallLikeOrFunctionLikeExpression(n)
})
if ancestorNode != nil {
cachedResolvedSignatures := make(map[*SignatureLinks]*Signature)
Copy link
Member

Choose a reason for hiding this comment

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

I might just do two slices here instead of a map, but this might be cleaner.

Either way, I would just add a comment like

Suggested change
cachedResolvedSignatures := make(map[*SignatureLinks]*Signature)
// We're going to temporarily reset resolved signatures for all surrounding calls.
cachedResolvedSignatures := make(map[*SignatureLinks]*Signature)

Comment on lines 505 to 506
result := fn()
for signatureLinks, resolvedSignature := range cachedResolvedSignatures {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
result := fn()
for signatureLinks, resolvedSignature := range cachedResolvedSignatures {
result := fn()
// Once our work is over, we'll restore any signatures we previously resolved.
for signatureLinks, resolvedSignature := range cachedResolvedSignatures {

// if tokenIn != nil {
// searchPosition = scanner.GetTokenPosOfNode(tokenIn, sourceFile, false /*includeJSDoc*/)
// }
if strings.LastIndex(sourceFile.Text(), "<") == -1 {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if strings.LastIndex(sourceFile.Text(), "<") == -1 {
if strings.LastIndexByte(sourceFile.Text(), '<') == -1 {

Comment on lines 261 to 265
// This function determines if the node could be type argument position
// Since during editing, when type argument list is not complete,
// the tree could be of any shape depending on the tokens parsed before current node,
// scanning of the previous identifier followed by "<" before current node would give us better result
// Note that we also balance out the already provided type arguments, arrays, object literals while doing so
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// This function determines if the node could be type argument position
// Since during editing, when type argument list is not complete,
// the tree could be of any shape depending on the tokens parsed before current node,
// scanning of the previous identifier followed by "<" before current node would give us better result
// Note that we also balance out the already provided type arguments, arrays, object literals while doing so
// This function determines if the node could be a type argument position
// When editing, it is common to have an incomplete type argument list (e.g. missing ">"),
// so the tree can have any shape depending on the tokens before the current node.
// Instead, scanning for an identifier followed by a "<" before current node
// will typically give us better results than inspecting the tree.
// Note that we also balance out the already provided type arguments, arrays, object literals while doing so.

if token == nil || !ast.IsIdentifier(token) {
return nil
}
if remainingLessThanTokens <= 0 {
Copy link
Member

Choose a reason for hiding this comment

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

No need to check less-than, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, it can instead be changed to check if remainingLessThanTokens == 0 (in Strada it had (!remainingLessThanTokens)) because this part is trying to check if we have found the start of the type argument.

}
tokenKind := token.Kind
remainingMatchingTokens := 0
for true {
Copy link
Member

Choose a reason for hiding this comment

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

I think the test of the codebases uses

Suggested change
for true {
for {

Copy link
Member

@gabritto gabritto left a comment

Choose a reason for hiding this comment

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

Some minor comments, but in general I think this is looking good.

}
// This is a rare case, but one that saves on a _lot_ of work if true - if the source file has _no_ `<` character,
// then there obviously can't be any type arguments - no expensive brace-matching backwards scanning required
// searchPosition := len(sourceFile.Text())
Copy link
Member

Choose a reason for hiding this comment

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

Is this missing a // !!!, or are we getting rid of this code?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, getting rid of it.

}

// Only need to be careful if the user typed a character and signature help wasn't showing.
onlyUseSyntacticOwners := context.TriggerKind == 2
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
onlyUseSyntacticOwners := context.TriggerKind == 2
onlyUseSyntacticOwners := context.TriggerKind == lsproto.SignatureHelpTriggerKindTriggerCharacter

printer := printer.NewPrinter(printer.PrinterOptions{NewLine: core.NewLineKindLF}, printer.PrintHandlers{}, nil)

var getTypeParameters []signatureHelpParameter = []signatureHelpParameter{}
if candidateSignature.TypeParameters() != nil && len(candidateSignature.TypeParameters()) != 0 {
Copy link
Member

Choose a reason for hiding this comment

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

This here is what I mentioned elsewhere that could become just len(candidateSignature.TypeParameters()) != 0.

Copy link
Member

@andrewbranch andrewbranch left a comment

Choose a reason for hiding this comment

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

This is looking pretty good overall. Beyond the comments I left, I suggest you look at every declaration of a slice and ask these questions:

  • is this a displayParts slice that should just be a string or a strings.Builder?
  • if not, can I allocate it with the correct capacity up front?

Comment on lines 25973 to 25975
result := [][]*ast.Symbol{}
for _, t := range restType.AsUnionType().Types() {
result = append(result, c.expandSignatureParametersWithTupleMembers(signature, t.AsTypeReference(), restIndex, restSymbol))
Copy link
Member

Choose a reason for hiding this comment

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

You can allocate this array with the right size:

Suggested change
result := [][]*ast.Symbol{}
for _, t := range restType.AsUnionType().Types() {
result = append(result, c.expandSignatureParametersWithTupleMembers(signature, t.AsTypeReference(), restIndex, restSymbol))
types := restType.AsUnionType().Types()
result := make([][]*ast.Symbol, len(types))
for i, t := range types {
result[i] = c.expandSignatureParametersWithTupleMembers(signature, t.AsTypeReference(), restIndex, restSymbol)

@@ -585,7 +585,7 @@ func (p *Printer) printSignature(sig *Signature, returnSeparator string) {
p.printType(p.c.getTypeOfSymbol(sig.thisParameter))
tail = true
}
expandedParameters := p.c.GetExpandedParameters(sig)
expandedParameters := p.c.getExpandedParameters(sig, true)[0]
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
expandedParameters := p.c.getExpandedParameters(sig, true)[0]
expandedParameters := p.c.getExpandedParameters(sig, true /*skipUnionExpanding*/)[0]

func (c *Checker) getResolvedSignatureWorker(node *ast.Node, candidatesOutArray *[]*Signature, checkMode CheckMode, argumentCount int) *Signature {
parsedNode := printer.NewEmitContext().ParseNode(node)
c.apparentArgumentCount = &argumentCount
var res *Signature = nil
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
var res *Signature = nil
var res *Signature


func (c *Checker) getResolvedSignatureWorker(node *ast.Node, candidatesOutArray *[]*Signature, checkMode CheckMode, argumentCount int) *Signature {
parsedNode := printer.NewEmitContext().ParseNode(node)
c.apparentArgumentCount = &argumentCount
Copy link
Member

Choose a reason for hiding this comment

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

You forgot to unset this at the end of the function.

// in the UI but can be omitted.
Documentation *string
// The Parameters of this signature.
Parameters *[]signatureHelpParameter
Copy link
Member

Choose a reason for hiding this comment

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

Don't use a pointer to a slice if you can avoid it. You range over this without checking for nil, so it seems like you can avoid it.

Comment on lines 120 to 125
signatureInformation := []*lsproto.SignatureInformation{}
signatureInformation = append(signatureInformation, &lsproto.SignatureInformation{
Label: item.Label,
Documentation: nil,
Parameters: &parameters,
})
Copy link
Member

Choose a reason for hiding this comment

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

This can be a single initialization

}

// Creating display label
typeParameterDisplayParts := []string{}
Copy link
Member

Choose a reason for hiding this comment

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

I would use a strings.Builder here

Comment on lines 191 to 197
callTargetDisplayParts := []string{}
if callTargetSymbol != nil {
callTargetDisplayParts = append(callTargetDisplayParts, c.SymbolToString(callTargetSymbol))
}
var items [][]signatureInformation
for _, candidateSignature := range *candidates {
items = append(items, getSignatureHelpItem(candidateSignature, argumentInfo.isTypeParameterList, callTargetDisplayParts, enclosingDeclaration, sourceFile, c))
Copy link
Member

Choose a reason for hiding this comment

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

This is the only call to getSignatureHelpItem, and callTargetDisplayParts is always length 0 or 1. It could just be a string. We know we're not going to have display parts anymore, so it doesn't seem like there's a need to keep the slices around. A lot of things can probably just become strings.

if callTargetSymbol != nil {
callTargetDisplayParts = append(callTargetDisplayParts, c.SymbolToString(callTargetSymbol))
}
var items [][]signatureInformation
Copy link
Member

Choose a reason for hiding this comment

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

This can be allocated with the correct length/capacity

Comment on lines 200 to 218
selectedItemIndex := 0
itemSeen := 0
for i := range items {
item := items[i]
if (*candidates)[i] == resolvedSignature {
selectedItemIndex = itemSeen
if len(item) > 1 {
count := 0
for _, j := range item {
if j.IsVariadic || len(*j.Parameters) >= argumentInfo.argumentCount {
selectedItemIndex = itemSeen + count
break
}
count++
}
}
}
itemSeen = itemSeen + len(item)
}
Copy link
Member

Choose a reason for hiding this comment

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

This looks terrifying but I see it's ported exactly 😅

@navya9singh navya9singh requested a review from andrewbranch May 29, 2025 17:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants