Re: [textadept] Highlight matching braces

From: Robert <ro....at.web.de>
Date: Wed, 6 Oct 2010 22:16:01 +0200

On Tue, Sep 28, 2010 at 4:06 PM, mitchell <c....at.caladbolg.net> wrote:
> Robert,
>
>> currently ta highlights a brace if it is to the right of the cursor.
>> To have the highlighting as well when I'm before the brace, for
>> example to see which one was closed by the just inserted one
>> I came up with the attached solution. There are two ways to handle
>> being between two braces, for example Scite highlights the left, Gvim
>> the right one. I prefer Scite's way, for the aforementioned reason.
>> Would you consider this for inclusion?
>
> I like how SciTE does it too, and I would have implemented this a long time
> ago but I am concerned about performance. 'update_ui' is called A LOT. The
> more API calls that are made, the slower performance is. I haven't done a
> study, so maybe I'm too paranoid.
>
> mitchell
>

I'm not sure if this is a good way to measure this:
keys.ci = {
  function()
    local s = os.clock()
    for i=1,1000 do
      events.emit('update_ui')
    end
    local e = os.clock()
    buffer:add_text(string.format(" %.2f\n", e - s))
  end
}
Is there a better way?

I can't really see a difference (with a slightly changed version to
avoid call buffer.char_at twice):
events.connect('update_ui',
  function() -- highlights matching braces
    local buffer = buffer
    local pos = buffer.current_pos
    if braces[buffer.char_at[pos - 1]] then
      pos = pos - 1
      local found = true
    end
    if HIGHLIGHT_BRACES and found or braces[buffer.char_at[pos]] and
      buffer:get_style_name(buffer.style_at[pos]) == 'operator' then
      local match_pos = buffer:brace_match(pos)
      if match_pos ~= -1 then
        buffer:brace_highlight(pos, match_pos)
      else
        buffer:brace_bad_light(pos)
      end
    else
      buffer:brace_bad_light(-1)
    end
  end)

On my machine it's 0.3 to 0.5 seconds for 1000 emits of update_ui.
More leaning towards 0.5 if there is a brace.

Robert
Received on Wed 06 Oct 2010 - 16:16:01 EDT

This archive was generated by hypermail 2.2.0 : Thu 08 Mar 2012 - 11:52:06 EST