diff --git a/lib/ruby_lsp/listeners/document_highlight.rb b/lib/ruby_lsp/listeners/document_highlight.rb index c9a45a8ea5..b18c00ee34 100644 --- a/lib/ruby_lsp/listeners/document_highlight.rb +++ b/lib/ruby_lsp/listeners/document_highlight.rb @@ -119,7 +119,8 @@ def initialize(response_builder, target, parent, dispatcher, position) Prism::RequiredParameterNode, Prism::RestParameterNode [target, node_value(target)] when Prism::ModuleNode, Prism::ClassNode, Prism::SingletonClassNode, Prism::DefNode, Prism::CaseNode, - Prism::WhileNode, Prism::UntilNode, Prism::ForNode, Prism::IfNode, Prism::UnlessNode + Prism::WhileNode, Prism::UntilNode, Prism::ForNode, Prism::IfNode, Prism::UnlessNode, Prism::BlockNode, + Prism::LambdaNode [target, nil] end @@ -184,6 +185,8 @@ def initialize(response_builder, target, parent, dispatcher, position) :on_for_node_enter, :on_if_node_enter, :on_unless_node_enter, + :on_block_node_enter, + :on_lambda_node_enter, ) end end @@ -578,6 +581,20 @@ def on_unless_node_enter(node) add_matching_end_highlights(node.keyword_loc, node.end_keyword_loc) end + sig { params(node: Prism::BlockNode).void } + def on_block_node_enter(node) + return unless @target.is_a?(Prism::BlockNode) + + add_matching_end_highlights(node.opening_loc, node.closing_loc) + end + + sig { params(node: Prism::LambdaNode).void } + def on_lambda_node_enter(node) + return unless @target.is_a?(Prism::LambdaNode) + + add_matching_end_highlights(node.opening_loc, node.closing_loc) + end + private sig { params(node: Prism::Node, classes: T::Array[T.class_of(Prism::Node)]).returns(T.nilable(T::Boolean)) } diff --git a/test/expectations/document_highlight/do_blocks.exp.json b/test/expectations/document_highlight/do_blocks.exp.json new file mode 100644 index 0000000000..8945361611 --- /dev/null +++ b/test/expectations/document_highlight/do_blocks.exp.json @@ -0,0 +1,36 @@ +{ + "params": [ + { + "line": 0, + "character": 10 + } + ], + "result": [ + { + "range": { + "start": { + "line": 0, + "character": 10 + }, + "end": { + "line": 0, + "character": 12 + } + }, + "kind": 1 + }, + { + "range": { + "start": { + "line": 2, + "character": 0 + }, + "end": { + "line": 2, + "character": 3 + } + }, + "kind": 1 + } + ] +} diff --git a/test/expectations/document_highlight/lambda_literal_do_end.exp.json b/test/expectations/document_highlight/lambda_literal_do_end.exp.json new file mode 100644 index 0000000000..5135ddbbee --- /dev/null +++ b/test/expectations/document_highlight/lambda_literal_do_end.exp.json @@ -0,0 +1,36 @@ +{ + "params": [ + { + "line": 0, + "character": 19 + } + ], + "result": [ + { + "range": { + "start": { + "line": 0, + "character": 18 + }, + "end": { + "line": 0, + "character": 20 + } + }, + "kind": 1 + }, + { + "range": { + "start": { + "line": 2, + "character": 0 + }, + "end": { + "line": 2, + "character": 3 + } + }, + "kind": 1 + } + ] +}