Class: Redcar::Comment::ToggleLineCommentCommand

Inherits:
EditTabCommand show all
Defined in:
plugins/comment/lib/comment.rb

Constant Summary

Constants included from Observable

Observable::ASPECTS

Instance Attribute Summary (collapse)

Attributes inherited from Redcar::Command

#error

Instance Method Summary (collapse)

Methods inherited from EditTabCommand

#doc, #edit_view

Methods inherited from Redcar::Command

active_changed, #environment, inherited, #inspect, norecord, record?, #run

Methods included from Sensitive

#active?, #sensitivities, #sensitivity_names, #sensitize

Methods included from Observable

#add_listener, #notify_listeners, #remove_listener

Instance Attribute Details

- (Object) comment (readonly)

Returns the value of attribute comment



123
124
125
# File 'plugins/comment/lib/comment.rb', line 123

def comment
  @comment
end

Instance Method Details

- (Object) add_comment(line, offset)



147
148
149
150
151
152
153
# File 'plugins/comment/lib/comment.rb', line 147

def add_comment(line, offset)
  if line.length < offset
    line + " "*(offset - line.length) + comment + " "
  else
    line.clone.insert(offset, comment + " ")
  end
end

- (Object) comment_insertion_point_for(line)



155
156
157
158
# File 'plugins/comment/lib/comment.rb', line 155

def comment_insertion_point_for(line)
  md = line.match(/^(\s*)([^\s])/)
  md[1].length
end

- (Object) execute



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'plugins/comment/lib/comment.rb', line 160

def execute
  type = Comment.comment_map["#{tab.edit_view.grammar.gsub("\"","")}"]
  if type
    @comment = type["line_comment"]
    return unless comment
  else
    Comment.grammar_missing(tab.edit_view.grammar)
    @comment = Comment.storage['default_line_comment']
  end
  selected              = doc.selection?
  cursor_offset         = doc.cursor_offset
  selection_offset      = doc.selection_offset
  cursor_line           = doc.cursor_line
  selection_line        = doc.selection_line
  cursor_line_offset    = doc.cursor_line_offset
  selection_line_offset = doc.selection_line_offset
  if cursor_offset < selection_offset
    start_point_offset      = cursor_offset
    end_point_offset        = selection_offset
    start_point_line        = cursor_line
    start_point_line_offset = cursor_line_offset
  else
    start_point_offset      = selection_offset
    end_point_offset        = cursor_offset
    start_point_line        = selection_line
    start_point_line_offset = selection_line_offset
  end
  start_line            = doc.line_at_offset(start_point_offset)
  end_line              = doc.line_at_offset(end_point_offset)
  
  if doc.offset_at_line(end_line) == end_point_offset and start_line != end_line
    end_line -= 1
  end
  
  doc.controllers(Redcar::AutoIndenter::DocumentController).first.disable do
    doc.compound do
      all_lines_are_already_commented = true
      start_line_comment_offset       = nil
      insertion_column                = 1000
      
      (start_line..end_line).each do |line|
        text = doc.get_line(line)
        
        if text =~ /^\s*$/
        else
          insertion_column = [insertion_column, comment_insertion_point_for(text)].min
        end
        
        if line == start_point_line and selected
          text = text[start_point_line_offset..-1]
        end
        
        unless starts_with_comment?(text)
          all_lines_are_already_commented = false
        end
      end
      
      if all_lines_are_already_commented
        (start_line..end_line).each do |line|
          doc.replace_line(line) do |text|
            new_text = 
              if line == start_point_line and selected
                strip_comment(text, start_point_line_offset)
              else
                strip_comment(text)
              end
            diff = text.length - new_text.length
            if cursor_offset < selection_offset
              selection_offset -= diff
            else
              if cursor_line > line or (cursor_line == line and cursor_line_offset > @point_comment_removed)
                cursor_offset -= diff
              end
            end
            new_text
          end
        end
      else
        (start_line..end_line).each do |line|
          doc.replace_line(line) do |text|
            new_text = if line == start_point_line and selected
                         add_comment(text, [start_point_line_offset, insertion_column].max)
                       else
                         add_comment(text, insertion_column)
                       end
            diff = new_text.length - text.length
            if cursor_offset < selection_offset
              selection_offset += diff
            else
              if cursor_line > line or (cursor_line == line and cursor_line_offset > insertion_column)
                cursor_offset += diff
              end
            end
            new_text
          end
        end
      end
    end
  end

  if selected
    doc.set_selection_range(cursor_offset, selection_offset)
  else
    doc.set_selection_range(cursor_offset, cursor_offset)
  end
end

- (Object) line_start_regex



125
126
127
# File 'plugins/comment/lib/comment.rb', line 125

def line_start_regex
  /^(\s*)#{comment}\s?/
end

- (Boolean) starts_with_comment?(line)

Returns:

  • (Boolean)


129
130
131
# File 'plugins/comment/lib/comment.rb', line 129

def starts_with_comment?(line)
  line =~ line_start_regex
end

- (Object) strip_comment(line, offset = nil)



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'plugins/comment/lib/comment.rb', line 133

def strip_comment(line, offset=nil)
  new_line = line.clone
  if offset and offset != 0 and 
      new_line[0..offset] !~ /^\s*$/
    new_line[offset..(offset + comment.length)] = ""
    @point_comment_removed = offset
    new_line
  else
    md = line.match(line_start_regex)
    @point_comment_removed = md[1].length
    md[1] + md.post_match
  end
end