Home Download Buy Blog Forum Support

Plugin to highlighting trailing whitespace?

Plugin to highlighting trailing whitespace?

Postby mattstopa on Thu Sep 15, 2011 1:46 pm

I need to be able to highlight all trailing spaces in red as well as all tabs. I was wondering if anyone here has an idea of how hard it would be to do that in sublime2. I don't want to simply remove them, I just need to be able to highlight them. Ideally I would just like to pass it a regex to search for these things. This was fairly trivial in VIM but I have no idea how hard this would be in sublime2. Any help is appreciated. My company is very particular about not adding any tabs or trailing whitespace, and I really need something that stands out like I have in VIM. Hopefully this is doable, thanks for any help.
mattstopa
 
Posts: 4
Joined: Thu Sep 15, 2011 1:42 pm

Re: Plugin to highlighting trailing whitespace?

Postby guillermooo on Thu Sep 15, 2011 2:34 pm

Consider adding a rule to the syntax definition for the language you're using so that it gives trailing whitespace an invalid/illegal scope. This depends on the color scheme you're using too, though (as any other method I can think of now, btw).

More on editing syntax defs here:

http://sublimetext.info/docs/en/extensi ... xdefs.html
guillermooo
 
Posts: 718
Joined: Thu Jul 23, 2009 9:06 am

Re: Plugin to highlighting trailing whitespace?

Postby mattstopa on Thu Sep 15, 2011 5:06 pm

Do you know if there is a way to apply this globally? I need it for every format I work with. I could theoretically add it to every syntax definition but I was hoping there might be a global way of doing it.

Thanks
mattstopa
 
Posts: 4
Joined: Thu Sep 15, 2011 1:42 pm

Re: Plugin to highlighting trailing whitespace?

Postby guillermooo on Thu Sep 15, 2011 7:56 pm

Not that I know of... In that case, I believe a plugin is the only option. It should be fairly easy to highlight offending lines by making use of the "scope" parameter in .add_regions(). Although I haven't tried, I anticipate it won't be very usable with large files--it might slow down Sublime Text considerably. If you just need to highlight trailing whitespace in the current line or in the visible region, then it should work just fine.

Other than that, I haven't got any more ideas how you could highlight regions in the way you need to...
guillermooo
 
Posts: 718
Joined: Thu Jul 23, 2009 9:06 am

Re: Plugin to highlighting trailing whitespace?

Postby wuub on Fri Sep 16, 2011 9:04 am

SublimeREPL via Package Control & SublimePTY dev/pre-alpha. Donations are appreciated, but not required :) Twitter: @wuub
wuub
 
Posts: 218
Joined: Wed Aug 10, 2011 8:16 am
Location: Poland

Re: Plugin to highlighting trailing whitespace?

Postby aparajita on Fri Sep 16, 2011 2:16 pm

mattstopa wrote:My company is very particular about not adding any tabs or trailing whitespace


No plugin is necessary, the solution is built in. Go to Preferences->File Settings - User, and set these three settings to true:

"translate_tabs_to_spaces": true,
"trim_automatic_white_space": true,
"trim_trailing_white_space_on_save": true,

Then it isn't necessary to highlight anything during editing, because it will always get fixed when saving.
aparajita
 
Posts: 173
Joined: Sun Jul 24, 2011 10:42 pm

Re: Plugin to highlighting trailing whitespace?

Postby chikamichi on Mon Oct 17, 2011 12:47 am

I created a plugin to both highlight trailing spaces, and enable deletion of those text regions. I'd rather use a plugin and a command bound to a shortcut than the auto-delete feature, as in some cases, trailing spaces must/should not be deleted (whether they're meaningful, or you're reviewing a script but won't touch it, and/or your auto-save feature would mess it up, and so on).

It's here: https://github.com/SublimeText/TrailingSpaces
It's inspired by the bitbucket version above, but revamped and extended, leveraging ST2's new Plugin API, so I encourage you to use it instead of the old, broken version ;)

Trailing spaces detection is basic and I'm using Linux, so please report any bug.
chikamichi
 
Posts: 14
Joined: Sun Oct 16, 2011 2:26 am

Re: Plugin to highlighting trailing whitespace?

Postby facelessuser on Mon Oct 17, 2011 10:34 pm

I am going to throw my two cents in here. I like the idea, but I think this shouldn't be running on every key press, at least not without some kind of debouncing (I do like that you gave a max size though). But generally I think this would work better as an on demand thing, that way you wouldn't even need a max size.

I think in general you don't need a constant checking of trailing spaces because the spaces are usually a mistake; something in general you get accustomed to avoiding, but can still occur by mistake. More often I would like to check after I am done coding.

What I think would work better is if you could set up a shortcut or a sublime command to toggle highlighting on and off. So if you issue the command, it will find all trailing spaces, if you issue the command again and the view is dirty (trailing is currently highlighted) it would clear all highlighting. If you wanted to delete trailing spaces you could bind that to a different shortcut, or a sublime command, so after viewing all the trailing spaces, you could decide "yeah, I want to delete all of that", or no and just delete what you want and hide it.

This is all just my personal opinion. I may fork it for personal use and do what I described if this is not the direction you would like to go, but I do think this plugin could be very useful for controlled trailing space deletion in cases where you don't want to obliterate all trailing spaces.
facelessuser
 
Posts: 1446
Joined: Tue Apr 05, 2011 7:38 pm

Re: Plugin to highlighting trailing whitespace?

Postby chikamichi on Tue Oct 18, 2011 12:33 am

I like the idea. Actually, my focus was on enabling trailing spaces monitoring over auto-deletion (which it what ST2 provides by default as an option), so it makes sense to provide another way than live highlighting :)

On my daily workflow, live highlighting is what I need, but I'll hack the toggling option. This should be available tomorrow. I think I'll stick to live highlighting by default though, as this is what most people around me seem to require.

As for debouncing, well, throttling, I'm working on it.

A line-by-line deletion mode could be nice too, but this may be too much overhead for no real use-cases.
chikamichi
 
Posts: 14
Joined: Sun Oct 16, 2011 2:26 am

Re: Plugin to highlighting trailing whitespace?

Postby facelessuser on Tue Oct 18, 2011 1:43 am

Cool.

Yeah, I realize my suggestion is a matter of taste.

After taking a look at some of the plugins I used, I realized more how much they can impact usability individually and more so when multiple live plugins are executing together on the same event. That is why I personally try to minimize live plugins on my system. The performance hit adds up.

Anyways, look forward to seeing what you cook up. I find controlled whitespace deletion good mainly because when I make a change at work, I like not to impact a 100+ lines of code. Corporate metrics and all like it when we have little impact...even if the impact makes the code better. It can be silly, but I figure at least I can keep my edits clean. :)
facelessuser
 
Posts: 1446
Joined: Tue Apr 05, 2011 7:38 pm

Next

Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 3 guests