Class: RubyLsp::Requests::PathCompletion
- Inherits:
-
BaseRequest
- Object
- SyntaxTree::Visitor
- BaseRequest
- RubyLsp::Requests::PathCompletion
- Extended by:
- T::Sig
- Defined in:
- lib/ruby_lsp/requests/path_completion.rb
Overview
![Path completion demo](../../misc/path_completion.gif)
The [completion](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`, … “`
Instance Method Summary collapse
-
#initialize(document, position) ⇒ PathCompletion
constructor
A new instance of PathCompletion.
- #run ⇒ Object
Methods inherited from BaseRequest
#full_constant_name, #locate, #range_from_syntax_tree_node, #visible?
Constructor Details
#initialize(document, position) ⇒ PathCompletion
Returns a new instance of PathCompletion.
20 21 22 23 24 25 |
# File 'lib/ruby_lsp/requests/path_completion.rb', line 20 def initialize(document, position) super(document) @tree = T.let(Support::PrefixTree.new(collect_load_path_files), Support::PrefixTree) @position = position end |
Instance Method Details
#run ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/ruby_lsp/requests/path_completion.rb', line 28 def run # We can't verify if we're inside a require when there are syntax errors return [] if @document.syntax_error? char_position = @document.create_scanner.find_char_position(@position) target = T.let(find(char_position), T.nilable(SyntaxTree::TStringContent)) # no target means the we are not inside a `require` call return [] unless target text = target.value @tree.search(text).sort.map! do |path| build_completion(path, path.delete_prefix(text)) end end |