wxRuby Editor - Bug fixes and new features

This is embarrassing, I noticed that after getting a new browser to the left - for instance when searching the whole project - and going back to the projects browser to open a new file there, the editor would crash because it could not find references to various locations. The initialize function in treectrl.rb looks like this now:

def initialize(parent, tpl = false, type = :file_browser)
  super(parent, 
    :size => Wx::Size.new(150, -1), 
    :style => TR_DEFAULT_STYLE|TR_HAS_BUTTONS|TR_HIDE_ROOT|TR_LINES_AT_ROOT)
  @aui    = parent
  @type   = type
  @config = tpl ? tpl.config.clone : YAML.load(File.open("config.yaml"))
  self.create_image_list(tpl)
  if tpl
    @class_jumper = tpl.class_jumper.clone
    @lexer_conf   = tpl.lexer_conf.clone
    @pr_info      = tpl.pr_info.clone
    @pr_folders   = tpl.pr_folders.clone
    @tpl          = tpl
  else
    self.build
  end
  evt_tree_item_activated self, :on_item_activated
end

Yes of course I wanted to copy the stuff when opening a new browser, not create links to the counterparts in the projects browser. Simply forgot the clone at the end everywhere, oops…

Another bug fix became apparent when there were escaped quotes in quoted symbols, for example: “Diving is \”cool\” isn’t it?”. Naturally the two escaped quotes in the middle should count neither in the highlighting logic nor the indenting logic, therefore line 57 in pico.rb looks like this now:

elsif m = /(.*)("[^\\"]*")(.*)/.match(txt)

And 100-101 looks like this:

when 34 # "
  dq += 1 if sci.get_char_at(sci.position_before(pos)) != 92 # \

There are also two new features, the first one is a file browser. When doing ctrl-b the currently open file will display to the left with it’s own class tree etc. In editor.rb we got the new onBrowse function:

def onBrowse
  @treebook.add_page(self.create_tree(@project_tree), @project_tree.getName(@sci.file_path), true)
  @mgr.update
  @cur_tree.buildFileTree(@sci.file_path, @sci.proj_id).expand_all
end

And in treectrl.rb we got the buildFileTree function of course:

def buildFileTree(file_path, proj_id)
  file_id             = self.getIdFromPath(file_path, proj_id)
  @new_files          = {}
  @new_jumpers        = {}
  sel_image           = self.getImageWithPath(file_path)
  root_id             = add_root(self.getName(file_path), sel_image, sel_image)
  @new_files[root_id] = file_path
  self.buildArb(root_id, file_id, root_id)
  @proj_id            = proj_id
  @pr_info[proj_id]   = @new_files
  @class_jumper       = @new_jumpers
  self.sort_children(root_id)
  self
end

As you can see we start to get a lot of similar code here when generating all these different browsers, the buildArb method is a step in the right direction to try to mitigate this development. It’s still quite ugly though.

def buildArb(parent_id, orig_id, file_id)
  return if @tpl.get_children_count(orig_id) == 0
  @tpl.get_children(orig_id).each do |child_id|
    jumper                  = @class_jumper[child_id]
    image                   = @icon_files[jumper[:type]][:pos]
    jumper_id               = append_item(parent_id, jumper[:name], image, image)
    jumper[:file_id]        = file_id
    @new_jumpers[jumper_id] = jumper
    self.buildArb(jumper_id, child_id, file_id)
  end
end

We are simply using the information we started with when the new tree was created and filter out what we don’t need. The new tree is then built with what we’ve got left.

Another smaller but handy feature is the ability to select a block through the brace highlighting. Maybe not super important in a “normal” language but in Lisp it’s very handy for quickly selecting expressions that are of sub line size. (Remember we already have a way of selecting a multi line s-expressions through the code folding logic regardless of cursor position within the expression through the ctrl-e command).

The main piece of logic for the brace selection is in lexer.rb:

def selMatch(sci)
  cur_pos   = sci.get_current_pos - 1
  other_pos = sci.brace_match(cur_pos)
  if other_pos > cur_pos
    sci.set_selection(cur_pos, other_pos + 1)
  else
    sci.set_selection(other_pos, cur_pos + 1)
  end
end

Not much to say really. We deduct and add some here and there to make the selection encompass the whole expression, including the parenthesizes. The editor source has been updated.

Related Posts

Tags: , ,