Class: 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 Visitor
#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
112 113 114 115 116 |
# File 'lib/syntax_tree/language_server/inlay_hints.rb', line 112 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 |
# File 'lib/syntax_tree/language_server/inlay_hints.rb', line 39 def visit_assign(node) parentheses(node.location) if stack[-2].is_a?(Params) 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₎
52 53 54 55 56 57 58 59 60 |
# File 'lib/syntax_tree/language_server/inlay_hints.rb', line 52 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 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₎
72 73 74 75 76 |
# File 'lib/syntax_tree/language_server/inlay_hints.rb', line 72 def visit_if_op(node) if stack[-2] in Assign | Binary | IfOp | OpAssign parentheses(node.location) end 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
91 92 93 94 95 |
# File 'lib/syntax_tree/language_server/inlay_hints.rb', line 91 def visit_rescue(node) if node.exception.nil? after[node.location.start_char + "rescue".length] << " StandardError" end 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
106 107 108 109 110 |
# File 'lib/syntax_tree/language_server/inlay_hints.rb', line 106 def visit_unary(node) if stack[-2].is_a?(Binary) && (node.operator == "-") parentheses(node.location) end end |