Class: Solargraph::CodeMap

Inherits:
Object
  • Object
show all
Includes:
NodeMethods
Defined in:
lib/solargraph/code_map.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from NodeMethods

#const_from, #infer_literal_node_type, #pack_name, #resolve_node_signature, #unpack_name

Constructor Details

#initialize(code: '', filename: nil, workspace: nil, api_map: nil, cursor: nil) ⇒ CodeMap

Returns a new instance of CodeMap.



35
36
37
38
39
40
41
42
43
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/solargraph/code_map.rb', line 35

def initialize code: '', filename: nil, workspace: nil, api_map: nil, cursor: nil
  @workspace = workspace
  # HACK: Adjust incoming filename's path separator for yardoc file comparisons
  filename = filename.gsub(File::ALT_SEPARATOR, File::SEPARATOR) unless filename.nil? or File::ALT_SEPARATOR.nil?
  @filename = filename
  @api_map = api_map
  @code = code.gsub(/\r/, '')
  tries = 0
  tmp = @code
  cursor = CodeMap.get_offset(@code, cursor[0], cursor[1]) if cursor.kind_of?(Array)
  fixed_cursor = false
  begin
    # HACK: The current file is parsed with a trailing underscore to fix
    # incomplete trees resulting from short scripts (e.g., a lone variable
    # assignment).
    node, @comments = Parser::CurrentRuby.parse_with_comments(tmp + "\n_")
    @node = self.api_map.append_node(node, @comments, filename)
    @parsed = tmp
    @code.freeze
    @parsed.freeze
  rescue Parser::SyntaxError => e
    if tries < 10
      tries += 1
      if tries == 10 and e.message.include?('token $end')
        tmp += "\nend"
      else
        if !fixed_cursor and !cursor.nil? and e.message.include?('token $end') and cursor >= 2
          fixed_cursor = true
          spot = cursor - 2
          if tmp[cursor - 1] == '.'
            repl = ';'
          else
            repl = '#'
          end
        else
          spot = e.diagnostic.location.begin_pos
          repl = '_'
          if tmp[spot] == '@' or tmp[spot] == ':'
            # Stub unfinished instance variables and symbols
            spot -= 1
          elsif tmp[spot - 1] == '.'
            # Stub unfinished method calls
            repl = '#' if spot == tmp.length or tmp[spot] == '\n'
            spot -= 2
          else
            # Stub the whole line
            spot = beginning_of_line_from(tmp, spot)
            repl = '#'
            if tmp[spot+1..-1].rstrip == 'end'
              repl= 'end;end'
            end
          end
        end
        tmp = tmp[0..spot] + repl + tmp[spot+repl.length+1..-1].to_s
      end
      retry
    end
    raise e
  end
end

Instance Attribute Details

#code ⇒ String (readonly)

The source code being analyzed.

Returns:

  • (String)


14
15
16
# File 'lib/solargraph/code_map.rb', line 14

def code
  @code
end

#filename ⇒ String (readonly)

The filename for the source code.

Returns:

  • (String)


25
26
27
# File 'lib/solargraph/code_map.rb', line 25

def filename
  @filename
end

#node ⇒ AST::Node

The root node of the parsed code.

Returns:

  • (AST::Node)


9
10
11
# File 'lib/solargraph/code_map.rb', line 9

def node
  @node
end

#parsed ⇒ String (readonly)

The source code after modification to fix syntax errors during parsing. This string will match #code if no modifications were made.

Returns:

  • (String)


20
21
22
# File 'lib/solargraph/code_map.rb', line 20

def parsed
  @parsed
end

#workspace ⇒ String (readonly)

The root directory of the project. The ApiMap will search here for additional files to parse and analyze.

Returns:

  • (String)


31
32
33
# File 'lib/solargraph/code_map.rb', line 31

def workspace
  @workspace
end

Class Method Details

.get_offset(text, line, col) ⇒ Object



117
118
119
120
121
122
123
124
125
# File 'lib/solargraph/code_map.rb', line 117

def self.get_offset text, line, col
  offset = 0
  if line > 0
    text.lines[0..line - 1].each { |l|
      offset += l.length
    }
  end
  offset + col      
end

Instance Method Details

#api_map ⇒ Solargraph::ApiMap

Get the ApiMap that was generated for this CodeMap.

Returns:



99
100
101
102
103
# File 'lib/solargraph/code_map.rb', line 99

def api_map
  @api_map ||= ApiMap.new(workspace)
  @api_map.refresh
  @api_map
end

#comment_at?(index) ⇒ Boolean

Determine if the specified index is inside a comment.

Returns:

  • (Boolean)


158
159
160
161
162
163
# File 'lib/solargraph/code_map.rb', line 158

def comment_at?(index)
  @comments.each do |c|
    return true if index > c.location.expression.begin_pos and index <= c.location.expression.end_pos
  end
  false
end

#get_class_variables_at(index) ⇒ Object



250
251
252
253
# File 'lib/solargraph/code_map.rb', line 250

def get_class_variables_at(index)
  ns = namespace_at(index) || ''
  api_map.get_class_variables(ns)
end

#get_instance_variables_at(index) ⇒ Object



255
256
257
258
259
# File 'lib/solargraph/code_map.rb', line 255

def get_instance_variables_at(index)
  node = parent_node_from(index, :def, :defs, :class, :module)
  ns = namespace_at(index) || ''
  api_map.get_instance_variables(ns, (node.type == :def ? :instance : :class))
end

#get_local_variables_and_methods_at(index) ⇒ Array<Solargraph::Suggestion>

Get an array of local variables and methods that can be accessed from the specified location in the code.

Parameters:

  • index (Integer)

Returns:



653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
# File 'lib/solargraph/code_map.rb', line 653

def get_local_variables_and_methods_at(index)
  result = []
  local = parent_node_from(index, :class, :module, :def, :defs) || @node
  result += get_local_variables_from(local)
  scope = namespace_at(index) || @node
  if local.type == :def
    result += api_map.get_instance_methods(scope, visibility: [:public, :private, :protected])
  else
    result += api_map.get_methods(scope, scope, visibility: [:public, :private, :protected])
  end
  if local.type == :def or local.type == :defs
    result += get_method_arguments_from local
  end
  result += get_yieldparams_at(index)
  result += api_map.get_methods('Kernel')
  result
end

#get_offset(line, col) ⇒ Integer

Get the offset of the specified line and column. The offset (also called the "index") is typically used to identify the cursor's location in the code when generating suggestions. The line and column numbers should start at zero.

Parameters:

  • line (Integer)
  • col (Integer)

Returns:

  • (Integer)


113
114
115
# File 'lib/solargraph/code_map.rb', line 113

def get_offset line, col
  CodeMap.get_offset @code, line, col
end

#get_signature_at(index) ⇒ String

Get the signature at the specified index. A signature is a method call that can start with a constant, method, or variable and does not include any method arguments. Examples:

  • String.new -> String.new
  • @x.bar -> @x.bar
  • y.split(', ').length -> y.split.length

Parameters:

  • index (Integer)

Returns:

  • (String)


551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
# File 'lib/solargraph/code_map.rb', line 551

def get_signature_at index
  brackets = 0
  squares = 0
  parens = 0
  signature = ''
  index -=1
  while index >= 0
    unless string_at?(index)
      break if brackets > 0 or parens > 0 or squares > 0
      char = @code[index, 1]
      if char == ')'
        parens -=1
      elsif char == ']'
        squares -=1
      elsif char == '}'
        brackets -= 1
      elsif char == '('
        parens += 1
      elsif char == '{'
        brackets += 1
      elsif char == '['
        squares += 1
        signature = ".[]#{signature}" if squares == 0 and @code[index-2] != '%'
      end
      if brackets == 0 and parens == 0 and squares == 0
        break if ['"', "'", ',', ' ', "\t", "\n", ';', '%'].include?(char)
        signature = char + signature if char.match(/[a-z0-9:\._@]/i) and @code[index - 1] != '%'
        if char == '@'
          signature = "@#{signature}" if @code[index-1, 1] == '@'
          break
        end
      end
    end
    index -= 1
  end
  signature = signature[1..-1] if signature.start_with?('.')
  #signature = signature[2..-1] if signature.start_with?('[]')
  signature
end

#get_signature_index_at(index) ⇒ Object



591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
# File 'lib/solargraph/code_map.rb', line 591

def get_signature_index_at index
  brackets = 0
  squares = 0
  parens = 0
  signature = ''
  index -=1
  while index >= 0
    break if brackets > 0 or parens > 0 or squares > 0
    char = @code[index, 1]
    if char == ')'
      parens -=1
    elsif char == ']'
      squares -=1
    elsif char == '}'
      brackets -= 1
    elsif char == '('
      parens += 1
    elsif char == '{'
      brackets += 1
    elsif char == '['
      squares += 1
      signature = ".[]#{signature}" if squares == 0
    end
    if brackets == 0 and parens == 0 and squares == 0
      break if ['"', "'", ',', ' ', "\t", "\n", ';'].include?(char)
      signature = char + signature if char.match(/[a-z0-9:\._@]/i)
      if char == '@'
        signature = "@#{signature}" if @code[index-1, 1] == '@'
        break
      end
    end
    index -= 1
  end
  signature = signature[1..-1] if signature.start_with?('.')
  signature = signature[2..-1] if signature.start_with?('[]')
  index + 1
end

#get_snippets_at(index) ⇒ Object



629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
# File 'lib/solargraph/code_map.rb', line 629

def get_snippets_at(index)
  result = []
  Snippets.definitions.each_pair { |name, detail|
    matched = false
    prefix = detail['prefix']
    while prefix.length > 0
      if @code[index-prefix.length, prefix.length] == prefix
        matched = true
        break
      end
      prefix = prefix[0..-2]
    end
    if matched
      result.push Suggestion.new(detail['prefix'], kind: Suggestion::SNIPPET, detail: name, insert: detail['body'].join("\r\n"))
    end
  }
  result
end

#get_type_comment(node) ⇒ Object



531
532
533
534
535
536
537
538
539
# File 'lib/solargraph/code_map.rb', line 531

def get_type_comment node
  obj = nil
  cmnt = api_map.get_comment_for(node)
  unless cmnt.nil?
    tag = cmnt.tag(:type)
    obj = tag.types[0] unless tag.nil? or tag.types.empty?
  end
  obj
end

#infer_signature_at(index) ⇒ String

Infer the type of the signature located at the specified index.

Examples:

# Given the following code:
nums = [1, 2, 3]
nums.join
# ...and given an index that points at the end of "nums.join",
# infer_signature_at will identify nums as an Array and the return
# type of Array#join as a String, so the signature's type will be
# String.

Returns:

  • (String)


389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# File 'lib/solargraph/code_map.rb', line 389

def infer_signature_at index
  signature = get_signature_at(index)
  # Check for literals first
  return 'Integer' if signature.match(/^[0-9]+?\.?$/)
  literal = nil
  if (signature.empty? and @code[index - 1] == '.') or signature == '[].'
    literal = node_at(index - 2)
  elsif signature.start_with?('.')
    literal = node_at(index - 1)
  else
    beg_sig = get_signature_index_at(index)
    if @code[beg_sig] == '.'
      literal = node_at(beg_sig - 1)
    else
      literal = node_at(1 + beg_sig)
    end
  end
  type = infer_literal_node_type(literal)
  if type.nil?
    node = parent_node_from(index, :class, :module, :def, :defs) || @node
    result = infer_signature_from_node signature, node
    if result.nil? or result.empty?
      arg = nil
      if node.type == :def or node.type == :defs or node.type == :block
        # Check for method arguments
        parts = signature.split('.', 2)
        # @type [Solargraph::Suggestion]
        arg = get_method_arguments_from(node).keep_if{|s| s.to_s == parts[0] }.first
        unless arg.nil?
          if parts[1].nil?
            result = arg.return_type
          else
            result = api_map.infer_signature_type(parts[1], parts[0], scope: :instance)
          end
        end
      end
      if arg.nil?
        # Check for yieldparams
        parts = signature.split('.', 2)
        yp = get_yieldparams_at(index).keep_if{|s| s.to_s == parts[0]}.first
        unless yp.nil?
          if parts[1].nil? or parts[1].empty?
            result = yp.return_type
          else
            newsig = parts[1..-1].join('.')
            result = api_map.infer_signature_type(newsig, yp.return_type, scope: :instance)
          end
        end
      end
    end
  else
    if signature.empty? or signature == '[].'
      result = type
    else
      cursed = get_signature_index_at(index)
      if signature.start_with?('[].')
        rest = signature[3..-1]
      else
        if signature.start_with?('.')
          rest = signature[literal.loc.expression.end_pos+(cursed-literal.loc.expression.end_pos)..-1]
        else
          rest = signature
        end
      end
      return type if rest.nil?
      lit_code = @code[literal.loc.expression.begin_pos..literal.loc.expression.end_pos]
      rest = rest[lit_code.length..-1] if rest.start_with?(lit_code)
      rest = rest[1..-1] if rest.start_with?('.')
      rest = rest[0..-2] if rest.end_with?('.')
      if rest.empty?
        result = type
      else
        result = api_map.infer_signature_type(rest, type, scope: :instance)
      end
    end
  end
  result
end

#infer_signature_from_node(signature, node) ⇒ Object



477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
# File 'lib/solargraph/code_map.rb', line 477

def infer_signature_from_node signature, node
  inferred = nil
  parts = signature.split('.')
  ns_here = namespace_from(node)
  unless signature.include?('.')
    fqns = api_map.find_fully_qualified_namespace(signature, ns_here)
    return "Class<#{fqns}>" unless fqns.nil?
  end
  start = parts[0]
  return nil if start.nil?
  remainder = parts[1..-1]
  if start.start_with?('@@')
    type = api_map.infer_class_variable(start, ns_here)
    return nil if type.nil?
    return type if remainder.empty?
    return api_map.infer_signature_type(remainder.join('.'), type, scope: :instance)
  elsif start.start_with?('@')
    scope = (node.type == :def ? :instance : :scope)
    type = api_map.infer_instance_variable(start, ns_here, scope)
    return nil if type.nil?
    return type if remainder.empty?
    return api_map.infer_signature_type(remainder.join('.'), type, scope: :instance)
  end
  var = find_local_variable_node(start, node)
  if var.nil?
    scope = (node.type == :def ? :instance : :class)
    type = api_map.infer_signature_type(signature, ns_here, scope: scope)
    return type unless type.nil?
  else
    # Signature starts with a local variable
    type = get_type_comment(var)
    type = infer_literal_node_type(var.children[1]) if type.nil?
    if type.nil?
      vsig = resolve_node_signature(var.children[1])
      type = infer_signature_from_node vsig, node
    end
  end
  unless type.nil?
    if remainder[0] == 'new'
      remainder.shift
      if remainder.empty?
        inferred = type
      else
        inferred = api_map.infer_signature_type(remainder.join('.'), type, scope: :instance)
      end
    elsif remainder.empty?
      inferred = type
    else
      inferred = api_map.infer_signature_type(remainder.join('.'), type, scope: :instance)
    end
  end
  inferred
end

#local_variable_in_node?(name, node) ⇒ Boolean

Returns:

  • (Boolean)


468
469
470
471
472
473
474
475
# File 'lib/solargraph/code_map.rb', line 468

def local_variable_in_node?(name, node)
  return true unless find_local_variable_node(name, node).nil?
  if node.type == :def or node.type == :defs
    args = get_method_arguments_from(node).keep_if{|a| a.label == name}
    return true unless args.empty?
  end
  false
end

#namespace_at(index) ⇒ String

Get the namespace at the specified location. For example, given the code class Foo; def bar; end; end, index 14 (the center) is in the "Foo" namespace.

Returns:

  • (String)


186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/solargraph/code_map.rb', line 186

def namespace_at(index)
  tree = tree_at(index)
  return nil if tree.length == 0
  slice = tree
  parts = []
  slice.reverse.each { |n|
    if n.type == :class or n.type == :module
      c = const_from(n.children[0])
      parts.push c
    end
  }
  parts.join("::")
end

#namespace_from(node) ⇒ String

Get the namespace for the specified node. For example, given the code class Foo; def bar; end; end, the node for def bar is in the "Foo" namespace.

Returns:

  • (String)


205
206
207
208
209
210
211
# File 'lib/solargraph/code_map.rb', line 205

def namespace_from(node)
  if node.respond_to?(:loc)
    namespace_at(node.loc.expression.begin_pos)
  else
    ''
  end
end

#node_at(index) ⇒ AST::Node

Get the nearest node that contains the specified index.

Parameters:

  • index (Integer)

Returns:

  • (AST::Node)


143
144
145
# File 'lib/solargraph/code_map.rb', line 143

def node_at(index)
  tree_at(index).first
end

#parent_node_from(index, *types) ⇒ AST::Node

Find the nearest parent node from the specified index. If one or more types are provided, find the nearest node whose type is in the list.

Parameters:

  • index (Integer)
  • types (Array<Symbol>)

Returns:

  • (AST::Node)


171
172
173
174
175
176
177
178
179
# File 'lib/solargraph/code_map.rb', line 171

def parent_node_from(index, *types)
  arr = tree_at(index)
  arr.each { |a|
    if a.kind_of?(AST::Node) and (types.empty? or types.include?(a.type))
      return a
    end
  }
  @node
end

#phrase_at(index) ⇒ String

Select the phrase that directly precedes the specified index. A phrase can consist of most types of characters other than whitespace, semi-colons, equal signs, parentheses, or brackets.

Parameters:

  • index (Integer)

Returns:

  • (String)


219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/solargraph/code_map.rb', line 219

def phrase_at index
  word = ''
  cursor = index - 1
  while cursor > -1
    char = @code[cursor, 1]
    break if char.nil? or char == ''
    break unless char.match(/[\s;=\(\)\[\]\{\}]/).nil?
    word = char + word
    cursor -= 1
  end
  word
end

#resolve_object_at(index) ⇒ Object



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/solargraph/code_map.rb', line 339

def resolve_object_at index
  return [] if string_at?(index)
  signature = get_signature_at(index)
  cursor = index
  while @code[cursor] =~ /[a-z0-9_\?]/i
    signature += @code[cursor]
    cursor += 1
    break if cursor >= @code.length
  end
  return [] if signature.to_s == ''
  path = nil
  ns_here = namespace_at(index)
  node = parent_node_from(index, :class, :module, :def, :defs) || @node
  parts = signature.split('.')
  if parts.length > 1
    beginner = parts[0..-2].join('.')
    type = infer_signature_from_node(beginner, node)
    ender = parts.last
    path = "#{type}##{ender}"
  else
    if local_variable_in_node?(signature, node)
      path = infer_signature_from_node(signature, node)
    elsif signature.start_with?('@')
      path = api_map.infer_instance_variable(signature, ns_here, (node.type == :def ? :instance : :class))
    else
      path = signature
    end
    if path.nil?
      path = api_map.find_fully_qualified_namespace(signature, ns_here)
    end
  end
  return [] if path.nil?
  if path.start_with?('Class<')
    path.gsub!(/^Class<([a-z0-9_:]*)>#([a-z0-9_]*)$/i, '\\1.\\2')
  end
  api_map.yard_map.objects(path, ns_here)
end

#signatures_at(index) ⇒ Object



332
333
334
335
336
337
# File 'lib/solargraph/code_map.rb', line 332

def signatures_at index
  sig = signature_index_before(index)
  return [] if sig.nil?
  word = word_at(sig)
  suggest_at(sig).reject{|s| s.label != word}
end

#string_at?(index) ⇒ Boolean

Determine if the specified index is inside a string.

Returns:

  • (Boolean)


150
151
152
153
# File 'lib/solargraph/code_map.rb', line 150

def string_at?(index)
  n = node_at(index)
  n.kind_of?(AST::Node) and n.type == :str
end

#suggest_at(index, filtered: false, with_snippets: false) ⇒ Array<Suggestions>

Get suggestions for code completion at the specified location in the source.

Returns:

  • (Array<Suggestions>) —

    The completion suggestions



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/solargraph/code_map.rb', line 265

def suggest_at index, filtered: false, with_snippets: false
  return [] if string_at?(index) or string_at?(index - 1) or comment_at?(index)
  result = []
  signature = get_signature_at(index)
  if index == 0 or @code[index - 1].match(/[\.\s]/)
    type = infer_signature_at(index)
  else
    if signature.include?('.')
      last_period = @code[0..index].rindex('.')
      if last_period.nil?
        type = infer_signature_at(index)
      else
        type = infer_signature_at(last_period)
      end  
    else
      if signature.start_with?('@@')
        return get_class_variables_at(index)
      elsif signature.start_with?('@')
        return get_instance_variables_at(index)
      elsif signature.start_with?('$')
        return api_map.get_global_variables
      else
        type = infer_signature_at(index)
      end
    end
  end
  if type.nil?
    unless signature.include?('.')
      phrase = phrase_at(index)
      signature = get_signature_at(index)
      namespace = namespace_at(index)
      if phrase.include?('::')
        parts = phrase.split('::', -1)
        ns = parts[0..-2].join('::')
        if parts.last.include?('.')
          ns = parts[0..-2].join('::') + '::' + parts.last[0..parts.last.index('.')-1]
          result = api_map.get_methods(ns)
        else
          result = api_map.namespaces_in(ns, namespace)
        end
      else
        type = infer_literal_node_type(node_at(index - 2))
        if type.nil?
          current_namespace = namespace_at(index)
          parts = current_namespace.to_s.split('::')
          result += get_snippets_at(index) if with_snippets
          result += get_local_variables_and_methods_at(index)
          result += ApiMap.get_keywords
          while parts.length > 0
            ns = parts.join('::')
            result += api_map.namespaces_in(ns, namespace)
            parts.pop
          end
          result += api_map.namespaces_in('')
          result += api_map.get_instance_methods('Kernel')
        else
          result.concat api_map.get_instance_methods(type)
        end
      end
    end
  else
    result.concat api_map.get_instance_methods(type)
  end
  result = reduce_starting_with(result, word_at(index)) if filtered
  result.uniq{|s| s.path}.sort{|a,b| a.label <=> b.label}
end

#tree_at(index) ⇒ Array<AST::Node>

Get an array of nodes containing the specified index, starting with the topmost node and ending with the nearest.

Parameters:

  • index (Integer)

Returns:

  • (Array<AST::Node>)


132
133
134
135
136
137
# File 'lib/solargraph/code_map.rb', line 132

def tree_at(index)
  arr = []
  arr.push @node
  inner_node_at(index, @node, arr)
  arr
end

#word_at(index) ⇒ String

Select the word that directly precedes the specified index. A word can only consist of letters, numbers, and underscores.

Parameters:

  • index (Integer)

Returns:

  • (String)


237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/solargraph/code_map.rb', line 237

def word_at index
  word = ''
  cursor = index - 1
  while cursor > -1
    char = @code[cursor, 1]
    break if char.nil? or char == ''
    break unless char.match(/[a-z0-9_]/i)
    word = char + word
    cursor -= 1
  end
  word
end