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

Index constants #893

Merged
merged 2 commits into from
Aug 17, 2023
Merged

Index constants #893

merged 2 commits into from
Aug 17, 2023

Conversation

st0012
Copy link
Member

@st0012 st0012 commented Aug 16, 2023

Motivation

Having constants in the index would be helpful for both go-to-definition and hover features.

Closes #883

Implementation

Since the goal is to index constants' definitions, I introduced new methods to handle Constant(Path)WriteNode and Constant(Path)OperatorOrWriteNode. Those nodes will be added to the index as RubyIndexer::Index::Constant with fully qualified names.

Automated Tests

I added lib/ruby_indexer/test/constant_test.rb for it.

Manual Tests

@st0012 st0012 added the enhancement New feature or request label Aug 16, 2023
@st0012 st0012 added this to the 2023-Q3 milestone Aug 16, 2023
@st0012 st0012 self-assigned this Aug 16, 2023
@st0012 st0012 requested a review from a team as a code owner August 16, 2023 12:18
@st0012 st0012 requested review from andyw8 and Morriar August 16, 2023 12:18
@github-actions
Copy link
Contributor

github-actions bot commented Aug 16, 2023

Benchmark results in seconds (slowest at top)

          textDocument/completion average: 0.32872 std_dev: 0.0131
          textDocument/diagnostic average: 0.061307 std_dev: 0.013851
          textDocument/definition average: 0.007225 std_dev: 0.003727
      textDocument/selectionRange average: 0.00588 std_dev: 0.001005
        textDocument/documentLink average: 0.003708 std_dev: 0.001001
      textDocument/documentSymbol average: 0.003671 std_dev: 0.000702
 textDocument/semanticTokens/full average: 0.003651 std_dev: 0.000778
            textDocument/codeLens average: 0.003638 std_dev: 0.000673
   textDocument/documentHighlight average: 0.003595 std_dev: 0.000777
        textDocument/foldingRange average: 0.003458 std_dev: 0.000665
textDocument/semanticTokens/range average: 0.002287 std_dev: 0.000451
           textDocument/inlayHint average: 0.00193 std_dev: 0.000492
               textDocument/hover average: 0.001852 std_dev: 0.000464
               codeAction/resolve average: 0.001848 std_dev: 0.000426
          textDocument/formatting average: 0.001205 std_dev: 0.000513
    textDocument/onTypeFormatting average: 0.00117 std_dev: 0.000339
          textDocument/codeAction average: 0.001169 std_dev: 0.000302


================================================================================
Comparison with main branch:

 textDocument/semanticTokens/full unchanged
textDocument/semanticTokens/range unchanged
      textDocument/documentSymbol unchanged
        textDocument/foldingRange unchanged
          textDocument/formatting unchanged
          textDocument/diagnostic unchanged
        textDocument/documentLink unchanged
           textDocument/inlayHint unchanged
      textDocument/selectionRange unchanged
   textDocument/documentHighlight unchanged
               textDocument/hover unchanged
          textDocument/codeAction unchanged
    textDocument/onTypeFormatting unchanged
               codeAction/resolve unchanged
          textDocument/completion unchanged
            textDocument/codeLens unchanged
          textDocument/definition unchanged


================================================================================
Missing benchmarks:

RubyLsp::Requests::ShowSyntaxTree

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.

Looking great!!

end
def add_constant(node)
comments = collect_comments(node)
@index << Index::Entry::Constant.new(fully_qualify_name(node.name), @file_path, node.location, comments)
Copy link
Member

Choose a reason for hiding this comment

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

Do we not need the same logic for the name here? And why not?

fully_qualified_name = name.start_with?("::") ? name.delete_prefix("::") : fully_qualify_name(name)

Copy link
Member Author

@st0012 st0012 Aug 16, 2023

Choose a reason for hiding this comment

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

Because if there's ::, either in the middle of as a prefix, it'd be a ConstantPath* node instead.

).void
end
def add_constant_with_path(node)
name = node.target.location.slice
Copy link
Member

Choose a reason for hiding this comment

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

This method is almost identical to the other one. Is there a way we can write this to share the same implementation between the two?

Copy link
Member Author

Choose a reason for hiding this comment

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

ConstantWriteNode and ConstantPathWriteNode needs to be indexed differently in 2 places:

  • How name is retrieved
  • Whether :: needs to be handled

Consider these, I prefer having them separated for now instead of checking node type inside the shared method.

Copy link
Member

Choose a reason for hiding this comment

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

We could move the :: handling inside fully_qualify_name, which probably fits the responsibility better.

And we can also get the name for the constant with node.location.slice. It's just a suggestion though, it might allow some cleanup.

Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if the :: prefix should always be deleted in Index#<<. It seems it's more a concern of the index itself rather than the visitor.

If I was to use an index manually, shouldn't I benefit from this behavior instead of having to reimplement it myself every time?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, that might be the case. Maybe we can move the handling of the prefix to the index.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, I will open another PR for this as it affects class/module indexing too.

lib/ruby_indexer/test/constant_test.rb Show resolved Hide resolved
@st0012 st0012 requested a review from vinistock August 16, 2023 14:45
).void
end
def add_constant_with_path(node)
name = node.target.location.slice
Copy link
Member

Choose a reason for hiding this comment

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

We could move the :: handling inside fully_qualify_name, which probably fits the responsibility better.

And we can also get the name for the constant with node.location.slice. It's just a suggestion though, it might allow some cleanup.

@st0012 st0012 merged commit 40fac28 into main Aug 17, 2023
@st0012 st0012 deleted the index-constants branch August 17, 2023 13:53
vinistock pushed a commit that referenced this pull request Feb 28, 2024
…d-patch-b823885b92

Bump the minor-and-patch group with 4 updates
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Handle constants in the indexer
3 participants