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

Account for private constants in features #1004

Merged
merged 4 commits into from
Sep 20, 2023

Conversation

vinistock
Copy link
Member

Motivation

Closes #966

We should only provide definition, completion and hover for private constants if the reference happens inside the exact same namespace as they are defined. Otherwise, it's a reference to a constant that doesn't actually exist.

Similarly, private constants should never be shown in workspace symbol.

Implementation

I split the PR by commits to make it easier to review.

On each request, started skipping adding results if we find a non-private reference to a private constant.

Automated Tests

Added tests for all features.

@vinistock vinistock added the enhancement New feature or request label Sep 14, 2023
@vinistock vinistock added this to the 2023-Q3 milestone Sep 14, 2023
@vinistock vinistock self-assigned this Sep 14, 2023
@vinistock vinistock requested a review from a team as a code owner September 14, 2023 12:55
@vinistock vinistock requested review from andyw8 and Morriar September 14, 2023 12:55
@vinistock vinistock force-pushed the vs/handle_private_constant branch from e69179d to eae8b7b Compare September 14, 2023 13:58
Base automatically changed from vs/handle_private_constant to main September 15, 2023 12:24
@vinistock vinistock force-pushed the vs/account_for_private_constants_in_features branch from 97051dc to 0a1d9fd Compare September 15, 2023 12:26
@vinistock vinistock force-pushed the vs/account_for_private_constants_in_features branch from 0a1d9fd to fad6231 Compare September 19, 2023 20:06
Copy link
Contributor

@KaanOzkan KaanOzkan left a comment

Choose a reason for hiding this comment

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

Nice comments

end

def test_completion_private_constants_from_different_namespace
RubyLsp::DependencyDetector.const_set(:HAS_TYPECHECKER, false)
Copy link
Contributor

Choose a reason for hiding this comment

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

Not in this PR, but since we use this a lot I wonder we should extract it out, e.g.:

def with_typechecker_disabled(&block)
  RubyLsp::DependencyDetector.const_set(:HAS_TYPECHECKER, false)
  yield
ensure
  RubyLsp::DependencyDetector.const_set(:HAS_TYPECHECKER, true)
end

Copy link
Member Author

Choose a reason for hiding this comment

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

That's true. We can extract it in another PR.

RUBY

result = RubyLsp::Requests::WorkspaceSymbol.new("Foo::CONSTANT", @index).run
assert_equal(1, result.length)
Copy link
Contributor

Choose a reason for hiding this comment

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

I may be misunderstanding the test, but if we're looking up Foo::CONSTANT then why is Foo in the results?

Copy link
Member Author

Choose a reason for hiding this comment

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

Because we fuzzy match the results for workspace symbol and the Jaro-Winkler similarity between Foo and Foo::CONSTANT is high. The reason is likely because Jaro-Winkler gives a higher importance to the prefix than it does to the rest of the word

ruby-lsp(main):001> DidYouMean::JaroWinkler.distance("Foo", "Foo::CONSTANT")
=> 0.8205128205128205

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I see. That may be worth a code comment.

@vinistock vinistock merged commit b109915 into main Sep 20, 2023
@vinistock vinistock deleted the vs/account_for_private_constants_in_features branch September 20, 2023 15:07
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 private constants in the index
3 participants