Class: Yarsh::Multiline::IdentifierSurround

Inherits:
Object
  • Object
show all
Defined in:
lib/yarsh/multiline.rb

Instance Method Summary collapse

Constructor Details

#initializeIdentifierSurround

Returns a new instance of IdentifierSurround.



26
27
28
# File 'lib/yarsh/multiline.rb', line 26

def initialize
  @local_vars = {}
end

Instance Method Details

#save_local_vars(line) ⇒ Object

detect if the line assign a new local variable if yes, save it's name



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/yarsh/multiline.rb', line 32

def save_local_vars(line)
  tokens = Prism.lex(line).value.map { |token, _| token }
  return unless tokens.any? { |token| token.type == :EQUAL }

  tokens
    .take_while { |token| token.type != :EQUAL }
    .filter { |token| token.type == :IDENTIFIER }
    .each do |token|
    @local_vars[token.value] =
      token
  end
end

#surrond_with_bind(line, bind_name) ⇒ Object

surroud the local variable with 'bind.eval()' a local variable is an identifier who was registred in a precedent line



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/yarsh/multiline.rb', line 48

def surrond_with_bind(line, bind_name)
  Prism.lex(line).value
       .map { |token, _| token }
       .filter { |token| token.type == :IDENTIFIER }
       .each do |token|
         next unless @local_vars[token.value]

         location = token.location
         line[location.start_column..(location.end_column - 1)] =
           "__yarsh__(\"#{token.value}\", #{bind_name})"
       end
  line
end