Shopify / ruby-lsp

An opinionated language server for Ruby
https://shopify.github.io/ruby-lsp/
MIT License
1.62k stars 168 forks source link

Support Completion for StringNode, SymbolNode #2910

Open tiev opened 4 days ago

tiev commented 4 days ago

I have checked that this feature is not already implemented

Use case

I'm writing an Addon and also need to support StringNode and SymbolNode.

In my tools, String is used for the keys of components in a container. So, when seeing the syntax of accessing the container, I want to have auto-completion for available keys.

E.g.,

class Dummy
  include App::Deps['component_key']
end

Description

To make the completion for component keys, the Completion should support also StringNode (and I also want to support SymbolNode)

Implementation

module RubyLsp
  module Requests
    class Completion < Request
      def initialize(document, global_state, params, sorbet_level, dispatcher)
        ...
        node_context = RubyDocument.locate(
          document.parse_result.value,
          char_position,
          node_types: [
            Prism::CallNode,
            Prism::ConstantReadNode,
            Prism::ConstantPathNode,
            Prism::GlobalVariableAndWriteNode,
            Prism::GlobalVariableOperatorWriteNode,
            Prism::GlobalVariableOrWriteNode,
            Prism::GlobalVariableReadNode,
            Prism::GlobalVariableTargetNode,
            Prism::GlobalVariableWriteNode,
            Prism::InstanceVariableReadNode,
            Prism::InstanceVariableAndWriteNode,
            Prism::InstanceVariableOperatorWriteNode,
            Prism::InstanceVariableOrWriteNode,
            Prism::InstanceVariableTargetNode,
            Prism::InstanceVariableWriteNode,
            Prism::StringNode,
            Prism::SymbolNode,
          ],
          code_units_cache: document.code_units_cache,
        )
...
tiev commented 4 days ago

If it's OK, I'm happy to make the PR

vinistock commented 4 days ago

Thanks for the feature suggestion! We can definitely do that, but the change is a bit more complicated than just adding the nodes to the list.

The moment you add them, it will break the completion for require and require_relative because those are currently triggering on the call node. You'll need to adjust those to work based on the string node as part of the addition.

tiev commented 2 days ago

I will consider either this or the approach for https://github.com/Shopify/ruby-lsp/issues/2913 Either approach can fulfill my use case. Let me play around with them.