Skip to content

Commit

Permalink
Remove node override selection ranges (#1085)
Browse files Browse the repository at this point in the history
* Migrate selection range

Co-authored-by: Kevin Newton <[email protected]>

* Fix tests

Co-authored-by: Kevin Newton <[email protected]>

---------

Co-authored-by: Kevin Newton <[email protected]>
  • Loading branch information
vinistock and kddnewton authored Oct 13, 2023
1 parent 4fb4953 commit 71e5cb3
Show file tree
Hide file tree
Showing 37 changed files with 1,147 additions and 182 deletions.
117 changes: 13 additions & 104 deletions lib/ruby_lsp/requests/selection_ranges.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,123 +20,32 @@ module Requests
# puts "Hello, world!" # --> Cursor is on this line
# end
# ```
class SelectionRanges < BaseRequest
class SelectionRanges
extend T::Sig

NODES_THAT_CAN_BE_PARENTS = T.let(
[
YARP::ArgumentsNode,
YARP::ArrayNode,
YARP::AssocNode,
YARP::BeginNode,
YARP::BlockNode,
YARP::CallNode,
YARP::CaseNode,
YARP::ClassNode,
YARP::DefNode,
YARP::ElseNode,
YARP::EnsureNode,
YARP::ForNode,
YARP::HashNode,
YARP::HashPatternNode,
YARP::IfNode,
YARP::InNode,
YARP::InterpolatedStringNode,
YARP::KeywordHashNode,
YARP::LambdaNode,
YARP::LocalVariableWriteNode,
YARP::ModuleNode,
YARP::ParametersNode,
YARP::RescueNode,
YARP::StringConcatNode,
YARP::StringNode,
YARP::UnlessNode,
YARP::UntilNode,
YARP::WhenNode,
YARP::WhileNode,
].freeze,
T::Array[T.class_of(YARP::Node)],
)
include Support::Common

sig { params(document: Document).void }
def initialize(document)
super(document)

@document = document
@ranges = T.let([], T::Array[Support::SelectionRange])
@stack = T.let([], T::Array[Support::SelectionRange])
end

sig { override.returns(T.all(T::Array[Support::SelectionRange], Object)) }
sig { returns(T.all(T::Array[Support::SelectionRange], Object)) }
def run
visit(@document.tree)
@ranges.reverse!
end
# [node, parent]
queue = [[@document.tree, nil]]

private
until queue.empty?
node, parent = queue.shift
next unless node

sig { override.params(node: T.nilable(YARP::Node)).void }
def visit(node)
return if node.nil?

range = if node.is_a?(YARP::InterpolatedStringNode)
create_heredoc_selection_range(node, @stack.last)
else
create_selection_range(node.location, @stack.last)
range = Support::SelectionRange.new(range: range_from_location(node.location), parent: parent)
T.unsafe(queue).unshift(*node.child_nodes.map { |child| [child, range] })
@ranges.unshift(range)
end
@ranges << range

return if node.child_nodes.empty?

@stack << range if NODES_THAT_CAN_BE_PARENTS.include?(node.class)
visit_all(node.child_nodes)
@stack.pop if NODES_THAT_CAN_BE_PARENTS.include?(node.class)
end

sig do
params(
node: YARP::InterpolatedStringNode,
parent: T.nilable(Support::SelectionRange),
).returns(Support::SelectionRange)
end
def create_heredoc_selection_range(node, parent)
opening_loc = node.opening_loc || node.location
closing_loc = node.closing_loc || node.location

RubyLsp::Requests::Support::SelectionRange.new(
range: Interface::Range.new(
start: Interface::Position.new(
line: opening_loc.start_line - 1,
character: opening_loc.start_column,
),
end: Interface::Position.new(
line: closing_loc.end_line - 1,
character: closing_loc.end_column,
),
),
parent: parent,
)
end

sig do
params(
location: YARP::Location,
parent: T.nilable(Support::SelectionRange),
).returns(Support::SelectionRange)
end
def create_selection_range(location, parent)
RubyLsp::Requests::Support::SelectionRange.new(
range: Interface::Range.new(
start: Interface::Position.new(
line: location.start_line - 1,
character: location.start_column,
),
end: Interface::Position.new(
line: location.end_line - 1,
character: location.end_column,
),
),
parent: parent,
)
@ranges
end
end
end
Expand Down
24 changes: 24 additions & 0 deletions test/expectations/selection_ranges/array_literal_oneline.exp.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,30 @@
"line": 0,
"character": 10
}
},
"parent": {
"range": {
"start": {
"line": 0,
"character": 0
},
"end": {
"line": 0,
"character": 10
}
},
"parent": {
"range": {
"start": {
"line": 0,
"character": 0
},
"end": {
"line": 0,
"character": 10
}
}
}
}
}
}
Expand Down
48 changes: 42 additions & 6 deletions test/expectations/selection_ranges/begin_rescue_ensure.exp.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
"parent": {
"range": {
"start": {
"line": 4,
"character": 0
"line": 5,
"character": 2
},
"end": {
"line": 5,
Expand All @@ -31,7 +31,7 @@
"parent": {
"range": {
"start": {
"line": 2,
"line": 4,
"character": 0
},
"end": {
Expand All @@ -42,12 +42,48 @@
"parent": {
"range": {
"start": {
"line": 0,
"line": 2,
"character": 0
},
"end": {
"line": 8,
"character": 3
"line": 5,
"character": 18
}
},
"parent": {
"range": {
"start": {
"line": 0,
"character": 0
},
"end": {
"line": 8,
"character": 3
}
},
"parent": {
"range": {
"start": {
"line": 0,
"character": 0
},
"end": {
"line": 8,
"character": 3
}
},
"parent": {
"range": {
"start": {
"line": 0,
"character": 0
},
"end": {
"line": 8,
"character": 3
}
}
}
}
}
}
Expand Down
46 changes: 41 additions & 5 deletions test/expectations/selection_ranges/case_when.exp.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
"parent": {
"range": {
"start": {
"line": 1,
"character": 0
"line": 2,
"character": 2
},
"end": {
"line": 2,
Expand All @@ -31,12 +31,48 @@
"parent": {
"range": {
"start": {
"line": 0,
"line": 1,
"character": 0
},
"end": {
"line": 5,
"character": 3
"line": 2,
"character": 13
}
},
"parent": {
"range": {
"start": {
"line": 0,
"character": 0
},
"end": {
"line": 5,
"character": 3
}
},
"parent": {
"range": {
"start": {
"line": 0,
"character": 0
},
"end": {
"line": 5,
"character": 3
}
},
"parent": {
"range": {
"start": {
"line": 0,
"character": 0
},
"end": {
"line": 5,
"character": 3
}
}
}
}
}
}
Expand Down
64 changes: 56 additions & 8 deletions test/expectations/selection_ranges/class_declaration.exp.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,71 @@
"parent": {
"range": {
"start": {
"line": 1,
"character": 2
"line": 2,
"character": 4
},
"end": {
"line": 3,
"character": 5
"line": 2,
"character": 17
}
},
"parent": {
"range": {
"start": {
"line": 0,
"character": 0
"line": 1,
"character": 2
},
"end": {
"line": 4,
"character": 3
"line": 3,
"character": 5
}
},
"parent": {
"range": {
"start": {
"line": 1,
"character": 2
},
"end": {
"line": 3,
"character": 5
}
},
"parent": {
"range": {
"start": {
"line": 0,
"character": 0
},
"end": {
"line": 4,
"character": 3
}
},
"parent": {
"range": {
"start": {
"line": 0,
"character": 0
},
"end": {
"line": 4,
"character": 3
}
},
"parent": {
"range": {
"start": {
"line": 0,
"character": 0
},
"end": {
"line": 4,
"character": 3
}
}
}
}
}
}
}
Expand Down
Loading

0 comments on commit 71e5cb3

Please sign in to comment.