Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add heredoc delimiter documentation on hover #2617

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Loading