Skip to content

Commit

Permalink
Add heredoc delimiter documentation on hover (#2617)
Browse files Browse the repository at this point in the history
Co-authored-by: Nick Schwaderer <[email protected]>
  • Loading branch information
vinistock and Schwad authored Sep 25, 2024
1 parent 8501f5e commit b66d02a
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 0 deletions.
38 changes: 38 additions & 0 deletions lib/ruby_lsp/listeners/hover.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Hover
Prism::InstanceVariableWriteNode,
Prism::SymbolNode,
Prism::StringNode,
Prism::InterpolatedStringNode,
Prism::SuperNode,
Prism::ForwardingSuperNode,
],
Expand Down Expand Up @@ -68,9 +69,21 @@ def initialize(response_builder, global_state, uri, node_context, dispatcher, so
:on_instance_variable_target_node_enter,
:on_super_node_enter,
:on_forwarding_super_node_enter,
:on_string_node_enter,
:on_interpolated_string_node_enter,
)
end

sig { params(node: Prism::StringNode).void }
def on_string_node_enter(node)
generate_heredoc_hover(node)
end

sig { params(node: Prism::InterpolatedStringNode).void }
def on_interpolated_string_node_enter(node)
generate_heredoc_hover(node)
end

sig { params(node: Prism::ConstantReadNode).void }
def on_constant_read_node_enter(node)
return if @sorbet_level != RubyDocument::SorbetLevel::Ignore
Expand Down Expand Up @@ -155,6 +168,31 @@ def on_forwarding_super_node_enter(node)

private

sig { params(node: T.any(Prism::InterpolatedStringNode, Prism::StringNode)).void }
def generate_heredoc_hover(node)
return unless node.heredoc?

opening_content = node.opening_loc&.slice
return unless opening_content

match = /(<<(?<type>(-|~)?))(?<quote>['"`]?)(?<delimiter>\w+)\k<quote>/.match(opening_content)
return unless match

heredoc_delimiter = match.named_captures["delimiter"]

if heredoc_delimiter
message = if match["type"] == "~"
"This is a squiggly heredoc definition using the `#{heredoc_delimiter}` delimiter. " \
"Indentation will be ignored in the resulting string."
else
"This is a heredoc definition using the `#{heredoc_delimiter}` delimiter. " \
"Indentation will be considered part of the string."
end

@response_builder.push(message, category: :documentation)
end
end

sig { void }
def handle_super_node_hover
# Sorbet can handle super hover on typed true or higher
Expand Down
14 changes: 14 additions & 0 deletions test/expectations/hover/dash_heredoc.exp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"params": [
{
"line": 0,
"character": 2
}
],
"result": {
"contents": {
"kind": "markdown",
"value": "This is a heredoc definition using the `HEREDOC` delimiter. Indentation will be considered part of the string."
}
}
}
14 changes: 14 additions & 0 deletions test/expectations/hover/plain_heredoc.exp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"params": [
{
"line": 0,
"character": 2
}
],
"result": {
"contents": {
"kind": "markdown",
"value": "This is a heredoc definition using the `HEREDOC` delimiter. Indentation will be considered part of the string."
}
}
}
14 changes: 14 additions & 0 deletions test/expectations/hover/squiggly_heredoc.exp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"params": [
{
"line": 0,
"character": 2
}
],
"result": {
"contents": {
"kind": "markdown",
"value": "This is a squiggly heredoc definition using the `HEREDOC` delimiter. Indentation will be ignored in the resulting string."
}
}
}
3 changes: 3 additions & 0 deletions test/fixtures/dash_heredoc.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<<-HEREDOC
some text#{1 + 1}other text
HEREDOC
3 changes: 3 additions & 0 deletions test/fixtures/plain_heredoc.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<<HEREDOC
some text#{1 + 1}other text
HEREDOC
File renamed without changes.

0 comments on commit b66d02a

Please sign in to comment.