Home Download Buy Blog Forum Support

whitespace handlers could play better together

whitespace handlers could play better together

Postby jaraco on Thu Mar 01, 2012 6:31 pm

I use SublimeText2 with the following two settings:

"ensure_newline_at_eof_on_save": true,
"trim_trailing_white_space_on_save": true,

However, if I attempt to save a file that ends in \n\t, it gets updated and saved ending instead in \n\n. An extra newline has been added that I then need to remove.

I suspect this is because first ensure_newline_at_eof_on_save is invoked (so the file ends in \n\t\n), then trailing whitespace is removed.

Could it be that the order of these two operations could be swapped, effectively solving the problem, without any adverse side-effects? If so, it would save me repeatedly encountering this annoyance.

If not, are there any other suggestions?
jaraco
 
Posts: 8
Joined: Thu Jan 26, 2012 6:53 pm

Re: whitespace handlers could play better together

Postby facelessuser on Thu Mar 01, 2012 7:13 pm

If you install the RegReplace plugin, you can add these to the settings file (you can modify these how you like):

Code: Select all
      // Remove trailing spaces
      "remove_trailing_spaces": {
         "find": "[ \\t]+$",
         "replace": "",
         "greedy": true,
         "case": true
      },
      // Remove all new lines at the start of the file and remove newlines > 1 at end of file
      "trim_excessive_newlines": {
         // (new lines at end of file | new lines at start of file)
         "find": "(((\\r?\\n)+)(?=(\\r?\\n){1}(?![\\s\\S\\r\\n]))|(?<![\\s\\S\\r\\n])((\\r?\\n)+))",
         "replace": ""
      },
      // Make sure a new line is present at file end
      "ensure_newline_at_file_end":{
         "find": "(([^\n\r])(?![\\s\\S\\r\\n]))",
         "replace": "\\1\\n"
      },


Then drop this simple plugin into your User folder (I called it "on_save.py")
Code: Select all
import sublime_plugin
import reg_replace


class OnSaveListenerCommand(sublime_plugin.EventListener):
    def on_pre_save(self, view):
        edit = view.begin_edit()
        reg_replace.RegReplaceCommand(view).run(edit, replacements=["remove_trailing_spaces", "trim_excessive_newlines", "ensure_newline_at_file_end"])
        view.end_edit(edit)


And that should do what you want. You would need to disable the sublime's settings though:

Code: Select all
"ensure_newline_at_eof_on_save": false,
"trim_trailing_white_space_on_save": false,


I might add this directly to RegReplace in the future, so you can just define an array in the reg_replace.sublime-settings file like this (but this does not exist currently):
Code: Select all
"replace_on_save": ["replacement1", "replacement2", "etc"]
facelessuser
 
Posts: 1576
Joined: Tue Apr 05, 2011 7:38 pm

Re: whitespace handlers could play better together

Postby facelessuser on Thu Mar 01, 2012 10:30 pm

RegReplace now supports on_pre_save events. You can read more about it here viewtopic.php?f=5&t=4594&p=25056#p25056
facelessuser
 
Posts: 1576
Joined: Tue Apr 05, 2011 7:38 pm

Re: whitespace handlers could play better together

Postby superfake123 on Fri Jun 29, 2012 9:56 pm

Thanks for this great plugin!

The regex you had for the trim_excessive_newlines was still leaving a line for me (You may have wanted that). This will remove ALL lines at the end of the file:

Code: Select all
"trim_excessive_newlines":
{
    "find":  "[\\s]*$(?![\\w\\W])",
    "replace": ""
}



best,
M
superfake123
 
Posts: 24
Joined: Wed Jun 27, 2012 2:24 pm

Re: whitespace handlers could play better together

Postby facelessuser on Sat Jun 30, 2012 12:07 am

superfake123 wrote:Thanks for this great plugin!

The regex you had for the trim_excessive_newlines was still leaving a line for me (You may have wanted that). This will remove ALL lines at the end of the file:

Code: Select all
"trim_excessive_newlines":
{
    "find":  "[\\s]*$(?![\\w\\W])",
    "replace": ""
}



best,
M


That was by design. That is why it said trim "excessive" newlines; I want one at the end of every file. It is common requirement to have at least one new line at the end of a file. Our compilers give us warnings if there is not. That is why I also have a rule to add a new line if there were no newlines: "ensure_newline_at_file_end".
facelessuser
 
Posts: 1576
Joined: Tue Apr 05, 2011 7:38 pm


Return to Technical Support

Who is online

Users browsing this forum: blurk, Google [Bot] and 22 guests