Class: SyntaxTree::LanguageServer::InlayHints

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree/language_server/inlay_hints.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInlayHints

Returns a new instance of InlayHints.



8
9
10
11
# File 'lib/syntax_tree/language_server/inlay_hints.rb', line 8

def initialize
  @before = Hash.new { |hash, key| hash[key] = +"" }
  @after = Hash.new { |hash, key| hash[key] = +"" }
end

Instance Attribute Details

#afterObject (readonly)

Returns the value of attribute after.



6
7
8
# File 'lib/syntax_tree/language_server/inlay_hints.rb', line 6

def after
  @after
end

#beforeObject (readonly)

Returns the value of attribute before.



6
7
8
# File 'lib/syntax_tree/language_server/inlay_hints.rb', line 6

def before
  @before
end

Class Method Details

.find(program) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/syntax_tree/language_server/inlay_hints.rb', line 44

def self.find(program)
  inlay_hints = new
  queue = [[nil, program]]

  until queue.empty?
    parent_node, child_node = queue.shift

    child_node.child_nodes.each do |grand_child_node|
      queue << [child_node, grand_child_node] if grand_child_node
    end

    case [parent_node, child_node]
    in _, Rescue[exception: nil, location:]
      inlay_hints.bare_rescue(location)
    in Assign | Binary | IfOp | OpAssign, IfOp[location:]
      inlay_hints.precedence_parentheses(location)
    in Assign | OpAssign, Binary[location:]
      inlay_hints.precedence_parentheses(location)
    in Binary[operator: parent_oper], Binary[operator: child_oper, location:] if parent_oper != child_oper
      inlay_hints.precedence_parentheses(location)
    in Binary, Unary[operator: "-", location:]
      inlay_hints.precedence_parentheses(location)
    in Params, Assign[location:]
      inlay_hints.precedence_parentheses(location)
    else
      # do nothing
    end
  end

  inlay_hints
end

Instance Method Details

#bare_rescue(location) ⇒ Object

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

begin
rescue
end

becomes

begin
rescue StandardError
end


26
27
28
# File 'lib/syntax_tree/language_server/inlay_hints.rb', line 26

def bare_rescue(location)
  after[location.start_char + "rescue".length] << " StandardError"
end

#precedence_parentheses(location) ⇒ Object

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

a + b * c

becomes

a + 


39
40
41
42
# File 'lib/syntax_tree/language_server/inlay_hints.rb', line 39

def precedence_parentheses(location)
  before[location.start_char] << "₍"
  after[location.end_char] << "₎"
end