Class: SourceStructure

Inherits:
Object
  • Object
show all
Defined in:
ext/ae-editor/ae-editor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_source) ⇒ SourceStructure

Returns a new instance of SourceStructure.



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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'ext/ae-editor/ae-editor.rb', line 38

def initialize(_source)
  _row = 1
  _liv = 0
  _livs = Array.new
  @root = TreeNode.new(nil, 'KRoot'){|_node|
    _node.rif= 'root'
    _node.label=''
  }
  _livs[_liv]=@root
  _source.each_line{|line|
    line = "\s"+line.split("#")[0]+"\s"
    m = /[\s\n\t\;]+(module|class|def|if|unless|begin|case|for|while|do)[\s\n\t\;]+/.match("\s#{line}")
    if m
      index = m.post_match.strip.length - 1
      if m.post_match.strip[index,index]=='{'
        _row = _row +1
        next
      end
      _liv>=0? _liv = _liv + 1:_liv=1
      _pliv = _liv
      _parent = nil
      while (_parent == nil && _pliv>=0)
        _pliv = _pliv -1
        _parent = _livs[_pliv]
      end
      if _parent
        _helptext = m.post_match.strip
        _label = _helptext.split('<')[0]
        if _label == nil || _label.strip.length==0
          _label = _helptext
        end
        if (m[0].strip[0..4] == "class" && m.pre_match.strip.length==0)
          _kind = 'KClass'
        elsif (m[0].strip[0..4] == "class" && m.pre_match.strip.length>0)
          _row = _row +1
          _liv = _liv - 1
          next
        elsif (m[0].strip[0..5] == "module" && m.pre_match.strip.length==0)
          _kind = 'KModule'
        elsif (m[0].strip[0..5] == "module" && m.pre_match.strip.length>0)
          _row = _row +1
          _liv = _liv - 1
          next
        elsif ((m[0].strip[0..4] == "begin")||(m[0].strip[0..3] == "case") ||(m[0].strip[0..4] == "while") || (m[0].strip[0..2] == "for") || (m[0].strip[0..1] == "do") || ((m[0].strip[0..1] == "if" || m[0].strip[0..5] == "unless") && m.pre_match.strip.length==0))
          _row = _row +1
          next
        elsif ((m[0].strip[0..1] == "if" || m[0].strip[0..5] == "unless") && m.pre_match.strip.length>0)
          _row = _row +1
          _liv = _liv - 1
          next
        elsif (m[0].strip[0..2] == "def" && m.pre_match.strip.length==0)
          _kind = 'KDef'
          if _label.include?(_parent.label + '.')
            _kind = 'KDefClass'
          end
#          elsif (m[0].strip[0..10] == "attr_reader" && m.pre_match.strip.length==0)
#            _kind = 'KAttr_reader'
#            _liv = _liv - 1
#            _row = _row +1
        end
        
        if  _livs[_liv-1] && _livs[_liv-1].kind != 'KDef'
          TreeNode.new(_parent, _kind){|_node|
            _node.label = _label
            _node.helptext = _helptext
            _node.rif = _row.to_s
            _livs[_pliv + 1]=_node
          }
#          elsif _kind == 'KDef' && _parent == @root
#            TreeNode.new(_livs[_liv+1], _kind){|_node|
#              _node.label = _label
#              _node.helptext = _helptext
#              _node.rif = _row.to_s
#              _livs[_pliv+2]=_node
#            }
#            _liv = _liv + 2
        else
          TreeNode.new(_livs[_liv-3], _kind){|_node|
            _node.label = _label
            _node.helptext = _helptext
            _node.rif = _row.to_s
            _livs[_pliv-1]=_node
          }
          _liv = _liv - 2
        end
      else
        _row = _row +1
        _liv = _liv - 1
        next
      end
    end
    m_end = /[\s\n\t\;]+end[\s\n\t\;]+/.match(line)
    if m_end
      if _livs[_liv]
        _livs[_liv].rif_end = _row
        #p "#{_livs[_liv].helptext} rif_end = #{_livs[_liv].rif_end}"
      end
      _liv = _liv - 1
    end
    _row = _row +1
  }
end

Instance Attribute Details

#injected_rowObject (readonly)

Returns the value of attribute injected_row.



37
38
39
# File 'ext/ae-editor/ae-editor.rb', line 37

def injected_row
  @injected_row
end

#rootObject (readonly)

Returns the value of attribute root.



36
37
38
# File 'ext/ae-editor/ae-editor.rb', line 36

def root
  @root
end

Instance Method Details

#class_methods(_class) ⇒ Object



203
204
# File 'ext/ae-editor/ae-editor.rb', line 203

def class_methods(_class)
end

#class_node_by_line(_line) ⇒ Object



190
191
192
193
194
195
196
197
# File 'ext/ae-editor/ae-editor.rb', line 190

def (_line)
  line_node = (@root, _line)
  class_node = line_node
  while class_node != nil && class_node.kind != "KClass"
    class_node = class_node.parent
  end
  return class_node
end

#classiesObject



199
200
# File 'ext/ae-editor/ae-editor.rb', line 199

def classies
end

#modulesObject



201
202
# File 'ext/ae-editor/ae-editor.rb', line 201

def modules
end

#node_by_line(_from_node, _line) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'ext/ae-editor/ae-editor.rb', line 172

def (_from_node, _line)
  _found_node = nil
  _begin = _from_node.rif.to_i
  _end = _from_node.rif_end.to_i
  if _line.to_i <= _end && _line.to_i >= _begin
    _found_node = _from_node
  else 
    _sons = _from_node.sons
    for inode in 0.._sons.length - 1
      _son = _sons[inode]
      _found_node = (_son, _line)
      break if _found_node
    end
  end
  return _found_node
end

#scheletor_from_node(_node, _source = '', _injected_source = '', _injected_class = '') ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'ext/ae-editor/ae-editor.rb', line 141

def scheletor_from_node(_node, _source='', _injected_source='', _injected_class='')
  _hinner_source = ''
  #_sons = _node.sons.sort
  _sons = _node.sons
  for inode in 0.._sons.length - 1
    _son = _sons[inode]
    if _son.kind == 'KClass'
       _hinner_source = "#{_hinner_source}class #{_son.helptext}\n"
    elsif _son.kind == 'KModule'
       _hinner_source = "#{_hinner_source}module #{_son.helptext}\n"
    elsif _son.kind == 'KDef' && _son.helptext != 'initialize'
       
       _hinner_source = "#{_hinner_source}  def #{_son.helptext}\n"
       _hinner_source = "#{_hinner_source}  end\n"
    elsif _son.kind == 'KDefClass'
       _hinner_source = "#{_hinner_source}  def #{_son.helptext}\n"
       _hinner_source = "#{_hinner_source}  end\n"
    end
    _hinner_source= scheletor_from_node(_son, _hinner_source, _injected_source, _injected_class)
  end
  _source = "#{_source}#{_hinner_source}" if _hinner_source.strip.length>0
  if _node.kind == 'KClass' && _node.label == _injected_class
    _source = "#{_source}  def initialize\n  #{_injected_source}  end\n"
    @injected_row = _source.split("\n").length-2
  end
  _source = "#{_source}end\n" if _node.kind == 'KClass' || _node.kind == 'KModule'
  _source
end