diff --git a/lib/ruby_lsp/executor.rb b/lib/ruby_lsp/executor.rb index b6ab3d7094..aa7d436fb5 100644 --- a/lib/ruby_lsp/executor.rb +++ b/lib/ruby_lsp/executor.rb @@ -396,42 +396,29 @@ def semantic_tokens_range(uri, range) end def completion(uri, position) document = @store.get(uri) - return unless document.parsed? char_position = document.create_scanner.find_char_position(position) - matched, parent = document.locate( - T.must(document.tree), - char_position, - node_types: [SyntaxTree::Command, SyntaxTree::CommandCall, SyntaxTree::CallNode], - ) - + matched, parent = + document.locate(document.tree, char_position, node_types: [YARP::CallNode]) return unless matched && parent - target = case matched - when SyntaxTree::Command, SyntaxTree::CallNode, SyntaxTree::CommandCall - message = matched.message - return if message.is_a?(Symbol) - return unless message.value == "require" + message = matched.message + return unless message == "require" - args = matched.arguments - args = args.arguments if args.is_a?(SyntaxTree::ArgParen) - return if args.nil? || args.is_a?(SyntaxTree::ArgsForward) + args = matched.arguments + return unless args - argument = args.parts.first - return unless argument.is_a?(SyntaxTree::StringLiteral) + args = args.arguments + return if args.is_a?(YARP::ForwardingArgumentsNode) - path_node = argument.parts.first - return unless path_node.is_a?(SyntaxTree::TStringContent) - return unless (path_node.location.start_char..path_node.location.end_char).cover?(char_position) + argument = args.first + return unless argument.is_a?(YARP::StringNode) - path_node - end - - return unless target + return unless (argument.location.start_offset..argument.location.end_offset).cover?(char_position) emitter = EventEmitter.new listener = Requests::PathCompletion.new(emitter, @message_queue) - emitter.emit_for_target(target) + emitter.emit_for_target(argument) listener.response end diff --git a/lib/ruby_lsp/requests/path_completion.rb b/lib/ruby_lsp/requests/path_completion.rb index 27f366c4b5..ffb0b8f99f 100644 --- a/lib/ruby_lsp/requests/path_completion.rb +++ b/lib/ruby_lsp/requests/path_completion.rb @@ -28,12 +28,12 @@ def initialize(emitter, message_queue) @response = T.let([], ResponseType) @tree = T.let(Support::PrefixTree.new(collect_load_path_files), Support::PrefixTree) - emitter.register(self, :on_tstring_content) + emitter.register(self, :on_string_node) end - sig { params(node: SyntaxTree::TStringContent).void } - def on_tstring_content(node) - @tree.search(node.value).sort.each do |path| + sig { params(node: YARP::StringNode).void } + def on_string_node(node) + @tree.search(node.content).sort.each do |path| @response << build_completion(path, node) end end @@ -49,12 +49,13 @@ def collect_load_path_files end end - sig { params(label: String, node: SyntaxTree::TStringContent).returns(Interface::CompletionItem) } + sig { params(label: String, node: YARP::StringNode).returns(Interface::CompletionItem) } def build_completion(label, node) + # debugger Interface::CompletionItem.new( label: label, text_edit: Interface::TextEdit.new( - range: range_from_syntax_tree_node(node), + range: range_from_location(node.content_loc), new_text: label, ), kind: Constant::CompletionItemKind::REFERENCE, diff --git a/lib/ruby_lsp/requests/support/common.rb b/lib/ruby_lsp/requests/support/common.rb index a1ee196293..6152c261d6 100644 --- a/lib/ruby_lsp/requests/support/common.rb +++ b/lib/ruby_lsp/requests/support/common.rb @@ -16,7 +16,7 @@ def range_from_syntax_tree_node(node) Interface::Range.new( start: Interface::Position.new( line: loc.start_line - 1, - character: loc.start_column, + character: loc.start_column + 1, ), end: Interface::Position.new(line: loc.end_line - 1, character: loc.end_column), ) diff --git a/test/requests/path_completion_test.rb b/test/requests/path_completion_test.rb index 2ee5807d81..d2a0ce4d69 100644 --- a/test/requests/path_completion_test.rb +++ b/test/requests/path_completion_test.rb @@ -27,7 +27,7 @@ def test_completion_command } end_position = { line: 0, - character: document.source.rindex('"'), + character: T.must(document.source.rindex('"')) - 1, } result = with_file_structure do