Re: [code] Highlight trailing whitespace

From: Mitchell <m.att.foicica.com>
Date: Wed, 4 Mar 2015 09:16:07 -0500 (EST)

Hi Carlos,

On Wed, 4 Mar 2015, Carlos Pita wrote:

> This is a cleanup and simplification of some gist around the web. I
> want to share it and ask for code review from the savvy users here
> (specially in case I'm missing an important event or doing something
> in a very inefficient way).
>
> -- Highlight trailing whitespace
> local tw_indicator = _SCINTILLA.next_indic_number()
> buffer.indic_style[tw_indicator] = buffer.INDIC_ROUNDBOX
> buffer.indic_fore[tw_indicator] = 0x0000FF
> events.connect(events.UPDATE_UI, function()
> buffer.indicator_current = tw_indicator
> buffer:indicator_clear_range(0, buffer.length)
> for s, e in buffer:get_text():gmatch('()[ \t]+()\r?\n') do
> buffer:indicator_fill_range(s - 1, e - (s - 1))
> end
> end)

For large buffers, the constant string allocation and deallocation by Lua
due to 'get_text()' may hamper performance. One idea is to operate on
lines directly. Something like this untested snippet may work:

   for i = 0, buffer.line_count - 1 do
     local pos = buffer:position_from_line(i)
     local line = buffer:get_line(i)
     local s, e = line:find('()[ \t]+()\r?\n$')
     if s and e then
       buffer:indicator_fill_range(pos + s - 1, pos + e - (s - 1))
     end
   end

Cheers,
Mitchell

-- 
You are subscribed to code.att.foicica.com.
To change subscription settings, send an e-mail to code+help.att.foicica.com.
To unsubscribe, send an e-mail to code+unsubscribe.att.foicica.com.
Received on Wed 04 Mar 2015 - 09:16:07 EST

This archive was generated by hypermail 2.2.0 : Thu 05 Mar 2015 - 06:38:43 EST