Sha256: 39ffa0cfbbf8329aae12ae5d33132d42f8330082137ff5d35fbd5b72ddce3865

Contents?: true

Size: 1.97 KB

Versions: 18

Compression:

Stored size: 1.97 KB

Contents

# typed: strict
# frozen_string_literal: true

module RubyLsp
  module Requests
    # ![Path completion demo](../../path_completion.gif)
    #
    # The [completion](https://microsoft.github.io/language-server-protocol/specification#textDocument_completion)
    # request looks up Ruby files in the $LOAD_PATH to suggest path completion inside `require` statements.
    #
    # # Example
    #
    # ```ruby
    # require "ruby_lsp/requests" # --> completion: suggests `base_request`, `code_actions`, ...
    # ```
    class PathCompletion < Listener
      extend T::Sig
      extend T::Generic

      ResponseType = type_member { { fixed: T::Array[Interface::CompletionItem] } }

      sig { override.returns(ResponseType) }
      attr_reader :response

      sig { params(emitter: EventEmitter, message_queue: Thread::Queue).void }
      def initialize(emitter, message_queue)
        super
        @response = T.let([], ResponseType)
        @tree = T.let(Support::PrefixTree.new(collect_load_path_files), Support::PrefixTree)

        emitter.register(self, :on_tstring_content)
      end

      sig { params(node: SyntaxTree::TStringContent).void }
      def on_tstring_content(node)
        @tree.search(node.value).sort.each do |path|
          @response << build_completion(path, node)
        end
      end

      private

      sig { returns(T::Array[String]) }
      def collect_load_path_files
        $LOAD_PATH.flat_map do |p|
          Dir.glob("**/*.rb", base: p)
        end.map! do |result|
          result.delete_suffix!(".rb")
        end
      end

      sig { params(label: String, node: SyntaxTree::TStringContent).returns(Interface::CompletionItem) }
      def build_completion(label, node)
        Interface::CompletionItem.new(
          label: label,
          text_edit: Interface::TextEdit.new(
            range: range_from_syntax_tree_node(node),
            new_text: label,
          ),
          kind: Constant::CompletionItemKind::REFERENCE,
        )
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
ruby-lsp-0.9.3 lib/ruby_lsp/requests/path_completion.rb
ruby-lsp-0.9.2 lib/ruby_lsp/requests/path_completion.rb
ruby-lsp-0.9.1 lib/ruby_lsp/requests/path_completion.rb
ruby-lsp-0.9.0 lib/ruby_lsp/requests/path_completion.rb
ruby-lsp-0.8.1 lib/ruby_lsp/requests/path_completion.rb
ruby-lsp-0.8.0 lib/ruby_lsp/requests/path_completion.rb
ruby-lsp-0.7.6 lib/ruby_lsp/requests/path_completion.rb
ruby-lsp-0.7.5 lib/ruby_lsp/requests/path_completion.rb
ruby-lsp-0.7.4 lib/ruby_lsp/requests/path_completion.rb
ruby-lsp-0.7.3 lib/ruby_lsp/requests/path_completion.rb
ruby-lsp-0.7.2 lib/ruby_lsp/requests/path_completion.rb
ruby-lsp-0.7.1 lib/ruby_lsp/requests/path_completion.rb
ruby-lsp-0.7.0 lib/ruby_lsp/requests/path_completion.rb
ruby-lsp-0.6.2 lib/ruby_lsp/requests/path_completion.rb
ruby-lsp-0.6.1 lib/ruby_lsp/requests/path_completion.rb
ruby-lsp-0.6.0 lib/ruby_lsp/requests/path_completion.rb
ruby-lsp-0.5.1 lib/ruby_lsp/requests/path_completion.rb
ruby-lsp-0.5.0 lib/ruby_lsp/requests/path_completion.rb