- Scintilla basics in WxRuby
- More Scintilla in WxRuby
- Autocomplete in WxRuby’s Scintilla
- AUI in WxRuby
- Project browser in WxRuby
- Styling in WxRuby’s Scintilla
- Custom folding in wxRuby’s Scintilla
- WxRuby Editor Source
- wxRuby Editor – Bug fixes and new features
- wxRuby Editor – More bug fixes and Scintilla text slinging
- A Note on Regular Expressions
- Automatic multiple line comments
- Managing escaping when indenting
- Scintilla event problem in new wxRuby – kinda solved
I’ve recently had the opportunity to check out TextMate and it has a nice feature that lets you select some lines and automatically add # to the beginning of them in order to comment them out.
I realized that this is of course a missing feature in the Pico Editor so here we go:
def onComment(sci) txt = sci.get_text_range(sci.get_selection_start, sci.get_selection_end) sci.clear comment_rgx = Regexp.new('^' + Regexp.escape(@single_comment)) new_txt =  txt.gsub(/\r/, '').split(/\n/).each do |line| if comment_rgx.match(line) new_txt << line.slice(@single_comment.length, line.length - 1) else new_txt << @single_comment + line end end sci.insert_text(sci.get_current_pos, new_txt.join("\n")) end
First we get the selected text, then we remove it by clearing it in the editor. We create the regular expression to use, really simple one; /^#/ in our case (more on @single_comment soon).
We remove any \r characters in case we are on windows followed by splitting by new lines.
We loop through the array we get from the split. Each line is now matched against the regular expression, if we have a match we will keep everything but the comment. If we don’t have a match we add the comment to the beginning of the line.
Finally we insert the new text we have joined with new lines again, at the current position.
The above results in a toggle per line (on Ctrl-t), I don’t know if that is really what I want in the long run, but it will do for now though.
Each lexer will of course sport its own @single_comment, in lexer.rb it’s ‘//‘, pico.rb overrides this with ‘#‘.
Update Oct 1 2008: The source has been updated.