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

Provide constant type differentiation in semantic highlighting request #1324

Closed
wants to merge 5 commits into from

Conversation

mateusdeap
Copy link
Contributor

Motivation

This is a new version of #1148 which closed when I tried to update my fork of the repo. Also making this a draft to gather feedback on my solution and give me time to do proper testing, since I use neovim and I'll probably need to figure some things out in that area.

Fixes #1128 (Potentially)

This PR aims to modify the SemanticHighlighting class so that it can properly determine what type of constant is being referenced in a given piece of code. The details and discussion can be found in the issue, but we want syntax highlighting to know that Foo in Foo.new below is referencing the class Foo and isn't just some other constant Foo, for example:

class Foo
  THIS_IS_A_CONSTANT = 1

  def self.create_instance
    Foo.new
  end
end

Implementation

Following @vinistock's recommendations, I made sure an instance of RubyIndexer::Index is given to the request and created a variable to keep track of nesting. Additionally subscribed to events needed to keep track of this nesting.

At the time of submission, the PR is incomplete, however. I wasn't able to figure out how to properly set up the index in the tests, so the lines where I call index_single are definitely something that needs fixing.

Finally, one thing discussed in the issue is that, even if this is implemented correctly, it might be too resource intensive, so if this gets finished, benchmarks will be required

Automated Tests

Haven't added any new tests so far but only tried to modify the current test to reflect the needed changes.

Manual Tests

The issue was opened referencing neovim, so I'm not sure if this affects VS Code. However the request doesn't have this specific capability so I think it's surely possible to replicate this by opening the example file provided in the issue.

@@ -34,6 +34,7 @@ class SemanticHighlighting < Listener
regexp: 20,
operator: 21,
decorator: 22,
constant: 23
Copy link
Member

Choose a reason for hiding this comment

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

This token type doesn't exist in the specification. We can't add it here because editors won't know what to do with that type.

We have two options here. We can use static variable as the token type for constants or we can propose the addition of constant as a token type to the specification.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Interesting. I was completely unaware of this.

Particularly, I'll just use the static variable, but do you think there is value in adding the constant token type to the spec? I'm assuming this might be an issue that the spec is built thinking of wording that doesn't exactly match Ruby concepts. So maybe static variable would be the equivalent of a Ruby constant. Despite the glaring contradiction in naming.

Comment on lines +198 to +202
add_token(node.location, :class, [:declaration])
when RubyIndexer::Entry::Module
add_token(node.location, :namespace, [:declaration])
when RubyIndexer::Entry::Constant
add_token(node.location, :constant, [:declaration])
Copy link
Member

Choose a reason for hiding this comment

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

Constant reads are references to constants, not declarations. So you need to remove the modifiers on all of these add_token calls.

Copy link
Member

Choose a reason for hiding this comment

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

Let's not commit these changes.

Comment on lines +18 to +28
Dir.glob(TEST_RUBY_LSP_FIXTURES).each do |path|
index.index_single(
RubyIndexer::IndexablePath.new(
"#{Dir.pwd}/lib",
File.expand_path(
"../../#{path}",
__dir__,
),
),
)
end
Copy link
Member

Choose a reason for hiding this comment

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

Instead of indexing every fixture here, we should use the source we receive in this method instead. Something like

index.index_single(
  RubyIndexer::IndexablePath.new(
    "fake",
    "fake/path.rb",
  ),
  source
)

Otherwise, we'd be indexing every fixture file in the codebase every time we're running the test for a single fixture.

Copy link
Contributor

This pull request is being marked as stale because there was no activity in the last 2 months

@github-actions github-actions bot added the Stale label Apr 13, 2024
@github-actions github-actions bot closed this Apr 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Semantic highlighting marking a class usage as a constant
2 participants