Class: SyntaxTree::LanguageServer::InlayHints
- Inherits:
-
Visitor
- Object
- BasicVisitor
- Visitor
- SyntaxTree::LanguageServer::InlayHints
- Defined in:
- lib/syntax_tree/language_server/inlay_hints.rb
Overview
This class provides inlay hints for the language server. For more information, see the spec here: github.com/microsoft/language-server-protocol/issues/956.
Defined Under Namespace
Classes: Hint
Instance Attribute Summary collapse
-
#hints ⇒ Object
readonly
Returns the value of attribute hints.
-
#stack ⇒ Object
readonly
Returns the value of attribute stack.
Instance Method Summary collapse
-
#initialize ⇒ InlayHints
constructor
A new instance of InlayHints.
- #visit(node) ⇒ Object
-
#visit_assign(node) ⇒ Object
Adds parentheses around assignments contained within the default values of parameters.
-
#visit_binary(node) ⇒ Object
Adds parentheses around binary expressions to make it clear which subexpression will be evaluated first.
-
#visit_if_op(node) ⇒ Object
Adds parentheses around ternary operators contained within certain expressions where it could be confusing which subexpression will get evaluated first.
-
#visit_rescue(node) ⇒ Object
Adds the implicitly rescued StandardError into a bare rescue clause.
-
#visit_unary(node) ⇒ Object
Adds parentheses around unary statements using the - operator that are contained within Binary nodes.
Methods inherited from BasicVisitor
#visit_all, #visit_child_nodes, visit_method, visit_methods
Constructor Details
#initialize ⇒ InlayHints
Returns a new instance of InlayHints.
33 34 35 36 |
# File 'lib/syntax_tree/language_server/inlay_hints.rb', line 33 def initialize @stack = [] @hints = [] end |
Instance Attribute Details
#hints ⇒ Object (readonly)
Returns the value of attribute hints.
31 32 33 |
# File 'lib/syntax_tree/language_server/inlay_hints.rb', line 31 def hints @hints end |
#stack ⇒ Object (readonly)
Returns the value of attribute stack.
31 32 33 |
# File 'lib/syntax_tree/language_server/inlay_hints.rb', line 31 def stack @stack end |
Instance Method Details
#visit(node) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/syntax_tree/language_server/inlay_hints.rb', line 38 def visit(node) stack << node result = super stack.pop result end |
#visit_assign(node) ⇒ Object
Adds parentheses around assignments contained within the default values of parameters. For example,
def foo(a = b = c)
end
becomes
def foo(a = ₍b = c₎)
end
56 57 58 59 |
# File 'lib/syntax_tree/language_server/inlay_hints.rb', line 56 def visit_assign(node) parentheses(node.location) if stack[-2].is_a?(Params) super end |
#visit_binary(node) ⇒ Object
Adds parentheses around binary expressions to make it clear which subexpression will be evaluated first. For example,
a + b * c
becomes
a + ₍b * c₎
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/syntax_tree/language_server/inlay_hints.rb', line 70 def visit_binary(node) case stack[-2] in Assign | OpAssign parentheses(node.location) in Binary[operator: operator] if operator != node.operator parentheses(node.location) else end super end |
#visit_if_op(node) ⇒ Object
Adds parentheses around ternary operators contained within certain expressions where it could be confusing which subexpression will get evaluated first. For example,
a ? b : c ? d : e
becomes
a ? b : ₍c ? d : e₎
92 93 94 95 96 97 98 99 100 |
# File 'lib/syntax_tree/language_server/inlay_hints.rb', line 92 def visit_if_op(node) case stack[-2] in Assign | Binary | IfOp | OpAssign parentheses(node.location) else end super end |
#visit_rescue(node) ⇒ Object
Adds the implicitly rescued StandardError into a bare rescue clause. For example,
begin
rescue
end
becomes
begin
rescue StandardError
end
115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/syntax_tree/language_server/inlay_hints.rb', line 115 def visit_rescue(node) if node.exception.nil? hints << Hint.new( line: node.location.start_line - 1, character: node.location.start_column + "rescue".length, label: " StandardError" ) end super end |
#visit_unary(node) ⇒ Object
Adds parentheses around unary statements using the - operator that are contained within Binary nodes. For example,
-a + b
becomes
₍-a₎ + b
136 137 138 139 140 141 142 |
# File 'lib/syntax_tree/language_server/inlay_hints.rb', line 136 def visit_unary(node) if stack[-2].is_a?(Binary) && (node.operator == "-") parentheses(node.location) end super end |