Skip to content

Go-to-def should include both declaration(s) and target call signature #1543

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

Merged
merged 3 commits into from
Aug 12, 2025

Conversation

ahejlsberg
Copy link
Member

Fixes #1528.

@Copilot Copilot AI review requested due to automatic review settings August 8, 2025 14:32
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 pull request improves the "Go to Definition" functionality by ensuring that both general declarations and specific call signatures are included in the results. When a call signature can be resolved, it replaces function-like declarations while preserving non-function declarations.

  • Refactored the definition resolution logic to extract it into a reusable function
  • Modified the signature handling to include both general declarations and specific call signatures
  • Simplified the location creation logic by removing body-based filtering

calledDeclaration := tryGetSignatureDeclaration(c, node)
if calledDeclaration != nil {
// If we can resolve a call signature, remove all function-like declarations and add that signature.
nonFunctionDeclarations := core.Filter(slices.Clip(declarations), func(node *ast.Node) bool { return !ast.IsFunctionLike(node) })
Copy link
Preview

Copilot AI Aug 8, 2025

Choose a reason for hiding this comment

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

Using slices.Clip(declarations) creates an unnecessary copy of the slice. Since core.Filter likely creates a new slice anyway, you can pass declarations directly to avoid the extra allocation.

Suggested change
nonFunctionDeclarations := core.Filter(slices.Clip(declarations), func(node *ast.Node) bool { return !ast.IsFunctionLike(node) })
nonFunctionDeclarations := core.Filter(declarations, func(node *ast.Node) bool { return !ast.IsFunctionLike(node) })

Copilot uses AI. Check for mistakes.

Copy link
Member Author

Choose a reason for hiding this comment

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

core.Filter only creates a new slice if something was removed. Thus the need for slices.Clip.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe we should just make filter always clip its input for safety?

@ahejlsberg ahejlsberg requested a review from jakebailey August 9, 2025 06:38
calledDeclaration := tryGetSignatureDeclaration(c, node)
if calledDeclaration != nil {
// If we can resolve a call signature, remove all function-like declarations and add that signature.
nonFunctionDeclarations := core.Filter(slices.Clip(declarations), func(node *ast.Node) bool { return !ast.IsFunctionLike(node) })
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we should just make filter always clip its input for safety?

@ahejlsberg
Copy link
Member Author

ahejlsberg commented Aug 12, 2025

Maybe we should just make filter always clip its input for safety?

There are several options for slices returned from Map, Filter, etc.

  • The returned slice is always a fresh copy. Safe to modify or append elements.
  • The returned slice is clipped, but not guaranteed to be a fresh copy. Safe to append elements. Not safe to modify elements.
  • The returned slice may or may not be fresh. Not safe to append or modify elements.

The first option leads to excessive allocation and copying. The second option is subtle, but desirable when concatenating slices. The third option is simple to understand, but requires callers to do the right thing.

We currently use option three. Not sure the other ones are better.

@ahejlsberg ahejlsberg added this pull request to the merge queue Aug 12, 2025
Merged via the queue into main with commit cedc0cb Aug 12, 2025
22 checks passed
@ahejlsberg ahejlsberg deleted the fix-1528 branch August 12, 2025 07:39
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.

[lsp]: instance of Go to Definition going to the type definition instead.
2 participants