Class: RText::LinkDetector

Inherits:
Object
  • Object
show all
Includes:
Tokenizer
Defined in:
lib/rtext/link_detector.rb

Defined Under Namespace

Classes: LinkDesc

Instance Method Summary collapse

Methods included from Tokenizer

#tokenize

Constructor Details

#initialize(lang) ⇒ LinkDetector

Returns a new instance of LinkDetector.



9
10
11
# File 'lib/rtext/link_detector.rb', line 9

def initialize(lang)
  @lang = lang
end

Instance Method Details

#detect(lines, column) ⇒ Object

column numbers start at 1



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rtext/link_detector.rb', line 16

def detect(lines, column)
  # make sure there is a space at the end of the line
  # otherwise an important attribute value (e.g. the local name) might be missing in the
  # context model since the context builder removes it as "just being completed"
  lines.last.concat(" ")
  current_line = lines.last
  context = ContextBuilder.build_context(@lang, lines, lines.last.size)
  tokens = tokenize(lines.last, @lang.reference_regexp)
  token = tokens.find{|t| t.scol && t.scol <= column && t.ecol && t.ecol >= column}
  if context && context.element && token &&
      [:reference, :integer, :string, :identifier].include?(token.kind)
    if column > 1
      line_prefix = lines.last[0..column-2]
    else
      line_prefix = ""
    end
    context2 = ContextBuilder.build_context(@lang, lines, line_prefix.size) 
    # for command prefixes nested below a lable, the context element and feature are
    # the parent element and feature, this is not what's expected here
    if line_prefix =~ /^\s*\w*$/
      context2.element = nil
      context2.feature = nil
    end
    bwref_attr = @lang.backward_ref_attribute.call(context.element.class.ecore)
    if bwref_attr
      is_backward = (context2.feature && context2.feature.name == bwref_attr)
    else
      is_backward = (token.kind == :identifier && line_prefix =~ /^\s*\w*$/)
    end
    is_backward = is_backward ? true : false
    if context2.element && context2.feature
      index = context2.element.getGenericAsArray(context2.feature.name).size
    end
    LinkDesc.new(context.element, context2.feature, index, is_backward, token.value, token.scol, token.ecol)
  else
    nil
  end
end