Skip to content
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

Definition support for autoloaded constants #1995

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

Conversation

Super-Xray
Copy link

Motivation

There are an issue calling for complete this function (#1893).

Implementation

autoload argument is a Prism::CallNode which has name :autoload. I create handle_autoload_definition method and add it to on_call_node_enter method in 'lib/ruby_lsp/listeners/definition.rb'. Then in handle_autoload_definition method, we can use the name of the constant in the Prism::SymbolNode under the Prism::CallNode and pass the name to find_in_index, which can create the response of jumping.

Automated Tests

Completed automated tests in test/requests/definition_expectations_test.rb, including test_jumping_to_autoload_definition_when_declaration_exists, test_jumping_to_autoload_definition_with_two_definition and test_does_nothing_when_autoload_declaration_does_not_exist

Manual Tests

  1. Start the extension on this branch
  2. open an .rb file which has autoload arguments.
  3. move the cursor above the autoload arguments, press ctrl & click it
  4. sometimes it doesn't work in original file of ruby-lsp since it's a sorbet project

@Super-Xray Super-Xray requested a review from a team as a code owner April 30, 2024 07:08
@Super-Xray
Copy link
Author

I have signed the CLA!

@Super-Xray
Copy link
Author

I have signed the CLA!

Copy link
Member

@vinistock vinistock 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 great! There's only one piece missing. To improve the precision of where you can cmd click, you need to add one extra check to this condition

target.name != :autoload

Comment on lines 153 to 155
if name != :autoload
return
end
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 already being checked where the method is invoked, so this can be removed.

Copy link
Author

Choose a reason for hiding this comment

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

got it

Comment on lines 157 to 163
arguments = node.arguments # should be 'ArgumentsNode'
return unless arguments.is_a?(Prism::ArgumentsNode)

symbol = arguments.arguments[0] # should be 'SymbolNode'
return unless symbol.is_a?(Prism::SymbolNode)

find_in_index(symbol.value)
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
arguments = node.arguments # should be 'ArgumentsNode'
return unless arguments.is_a?(Prism::ArgumentsNode)
symbol = arguments.arguments[0] # should be 'SymbolNode'
return unless symbol.is_a?(Prism::SymbolNode)
find_in_index(symbol.value)
arguments = node.arguments&.arguments
return unless arguments
value = arguments.first&.value
return unless value
find_in_index(value)

end
end

def test_jumping_to_autoload_definition_with_two_definition
Copy link
Member

Choose a reason for hiding this comment

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

Multiple declarations for the same constant is already handled and tested by the index, so I'd remove this test.

@Super-Xray
Copy link
Author

connected another email with the CLA

@Super-Xray
Copy link
Author

I have signed the CLA!

Copy link
Contributor

@andyw8 andyw8 left a comment

Choose a reason for hiding this comment

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

Needs a rebase, otherwise looks ready to go.

@@ -145,6 +147,18 @@ def handle_require_definition(node)
end
end

sig { params(node: Prism::CallNode).void }
def handle_autoload_definition(node)

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's remove the blank line here.

server.process_message(
id: 1,
method: "textDocument/definition",
params: { textDocument: { uri: uri }, position: { character: 3, line: 3 } },
Copy link
Contributor

@andyw8 andyw8 May 10, 2024

Choose a reason for hiding this comment

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

Once #2036 is solved, the tests can be updated so that the position corresponds to :Bar rather than autoload.

@andyw8
Copy link
Contributor

andyw8 commented May 10, 2024

There is one typecheck failure, it may indicate a missing nil check.

@andyw8 andyw8 added enhancement New feature or request server This pull request should be included in the server gem's release notes labels May 10, 2024
@Super-Xray
Copy link
Author

Super-Xray commented May 13, 2024

There is one typecheck failure, it may indicate a missing nil check.
image
@andyw8 Did you mean this error?
I have fixed it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request server This pull request should be included in the server gem's release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants