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. It is loosely designed around the LSP spec, but existed before the spec was finalized so is a little different for now.
For more information, see the spec here: github.com/microsoft/language-server-protocol/issues/956.
Instance Attribute Summary collapse
-
#after ⇒ Object
readonly
Returns the value of attribute after.
-
#before ⇒ Object
readonly
Returns the value of attribute before.
-
#stack ⇒ Object
readonly
Returns the value of attribute stack.
Class Method Summary collapse
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.
15 16 17 18 19 |
# File 'lib/syntax_tree/language_server/inlay_hints.rb', line 15 def initialize @stack = [] @before = Hash.new { |hash, key| hash[key] = +"" } @after = Hash.new { |hash, key| hash[key] = +"" } end |
Instance Attribute Details
#after ⇒ Object (readonly)
Returns the value of attribute after.
13 14 15 |
# File 'lib/syntax_tree/language_server/inlay_hints.rb', line 13 def after @after end |
#before ⇒ Object (readonly)
Returns the value of attribute before.
13 14 15 |
# File 'lib/syntax_tree/language_server/inlay_hints.rb', line 13 def before @before end |
#stack ⇒ Object (readonly)
Returns the value of attribute stack.
13 14 15 |
# File 'lib/syntax_tree/language_server/inlay_hints.rb', line 13 def stack @stack end |
Class Method Details
.find(program) ⇒ Object
123 124 125 126 127 |
# File 'lib/syntax_tree/language_server/inlay_hints.rb', line 123 def self.find(program) visitor = new visitor.visit(program) visitor end |
Instance Method Details
#visit(node) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/syntax_tree/language_server/inlay_hints.rb', line 21 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
39 40 41 42 |
# File 'lib/syntax_tree/language_server/inlay_hints.rb', line 39 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₎
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/syntax_tree/language_server/inlay_hints.rb', line 53 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₎
75 76 77 78 79 80 81 82 83 |
# File 'lib/syntax_tree/language_server/inlay_hints.rb', line 75 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
98 99 100 101 102 103 104 |
# File 'lib/syntax_tree/language_server/inlay_hints.rb', line 98 def visit_rescue(node) if node.exception.nil? after[node.location.start_char + "rescue".length] << " 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
115 116 117 118 119 120 121 |
# File 'lib/syntax_tree/language_server/inlay_hints.rb', line 115 def visit_unary(node) if stack[-2].is_a?(Binary) && (node.operator == "-") parentheses(node.location) end super end |