class SyntaxTree::LanguageServer::InlayHints

This class provides inlay hints for the language server. For more information, see the spec here: github.com/microsoft/language-server-protocol/issues/956.

Attributes

hints[R]
stack[R]

Public Class Methods

new() click to toggle source
# File lib/syntax_tree/language_server.rb, line 43
def initialize
  @stack = []
  @hints = []
end

Public Instance Methods

visit(node) click to toggle source
Calls superclass method
# File lib/syntax_tree/language_server.rb, line 48
def visit(node)
  stack << node
  result = super
  stack.pop
  result
end
visit_assign(node) click to toggle source

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
Calls superclass method
# File lib/syntax_tree/language_server.rb, line 67
def visit_assign(node)
  parentheses(node.location) if stack[-2].is_a?(Params)
  super
end
visit_binary(node) click to toggle source

Adds parentheses around binary expressions to make it clear which subexpression will be evaluated first. For example,

a + b * c

becomes

a + ₍b * c₎
Calls superclass method
# File lib/syntax_tree/language_server.rb, line 81
def visit_binary(node)
  case stack[-2]
  when Assign, OpAssign
    parentheses(node.location)
  when Binary
    parentheses(node.location) if stack[-2].operator != node.operator
  end

  super
end
visit_if_op(node) click to toggle source

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₎
Calls superclass method
# File lib/syntax_tree/language_server.rb, line 102
def visit_if_op(node)
  case stack[-2]
  when Assign, Binary, IfOp, OpAssign
    parentheses(node.location)
  end

  super
end
visit_rescue(node) click to toggle source

Adds the implicitly rescued StandardError into a bare rescue clause. For example,

begin
rescue
end

becomes

begin
rescue StandardError
end
Calls superclass method
# File lib/syntax_tree/language_server.rb, line 124
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) click to toggle source

Adds parentheses around unary statements using the - operator that are contained within Binary nodes. For example,

-a + b

becomes

-a₎ + b
Calls superclass method
# File lib/syntax_tree/language_server.rb, line 145
def visit_unary(node)
  if stack[-2].is_a?(Binary) && (node.operator == "-")
    parentheses(node.location)
  end

  super
end