Skip to content

Commit

Permalink
Migrate Path Completion to YARP [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
andyw8 committed Aug 18, 2023
1 parent 768b8c6 commit 31aaeb3
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 33 deletions.
37 changes: 12 additions & 25 deletions lib/ruby_lsp/executor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 7 additions & 6 deletions lib/ruby_lsp/requests/path_completion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_lsp/requests/support/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)
Expand Down
2 changes: 1 addition & 1 deletion test/requests/path_completion_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 31aaeb3

Please sign in to comment.