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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/syntax_tree/language_server/inlay_hints.rb', line 58

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 _, Assoc[key: Label[value: key], value: nil, location:]
      inlay_hints.missing_hash_value(key[0...-1], 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

#missing_hash_value(key, location) ⇒ Object

Adds the implicitly referenced value (local variable or method call) that is added into a hash when the value of a key-value pair is omitted. For example,

{ value: }

becomes

{ value: value }


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

def missing_hash_value(key, location)
  after[location.end_char] << " #{key}"
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 + 


53
54
55
56
# File 'lib/syntax_tree/language_server/inlay_hints.rb', line 53

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