Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highlighting trailing whitespace in Textmate for Python?

I would like to do something like this Textmate tip, so that trailing whitespace are always highlighted in some way when I code something in Python - it makes it easier to correct it immediately and other editors such as Emacs can do it.

Unfortunately the discussion after that post seems to suggest it's difficult to do. For me the invalid.trailing-whitespace scope selector is not even visible in the preferences after following this tip. Has anyone else had any success with this?

like image 917
pojo Avatar asked Mar 13 '09 08:03

pojo


People also ask

How do I fix trailing spaces in Python?

Python String strip() function will remove leading and trailing whitespaces. If you want to remove only leading or trailing spaces, use lstrip() or rstrip() function instead.

How do you solve trailing whitespace?

Hover the mouse on warning in VS Code or any IDE and use quick fix to remove white spaces. Press f1 then type trim trailing whitespace .

What is trailing whitespace in code?

Trailing whitespace. Description: Used when there is whitespace between the end of a line and the newline.

Why are trailing whitespace bad?

Some editors automatically remove trailing whitespace, some don't. This creates diff noise and can cause merge conflicts. You should coordinate with the people you're working with (colleagues, open-source crowd) what strategy everybody is using and make sure you all use the same strategy.


1 Answers

This code works (but not with comment) :

{   scopeName = 'source.whitespace';
    patterns = (
        {  name = 'source.invalid.trailing-whitespace';
            match = '(\s+)$';
            captures = { 1 = { name = 'invalid.trailing-whitespace'; }; };
         },
    );
}

PS: I have changed "source" to "source.whitespace"

For comment change in Python grammar :

{  name = 'comment.line.number-sign.python';
   match = '(#).*$\n?';
   captures = { 1 = { name = 'punctuation.definition.comment.python'; }; };
},

In:

{  name = 'comment.line.number-sign.python';
   match = '(#).*?(\s*)$\n?';
   captures = { 
     1 = { name = 'punctuation.definition.comment.python'; }; 
     2 = { name = 'invalid.trailing-whitespace';  }; 
   };
},

You'll need to add an 'include' in Python language definition where:

:
patterns = (
 {    name = 'comment.line.number-sign.python';
:

Turns to:

:
patterns = (
 {  include = 'source.whitespace'; },
 {    name = 'comment.line.number-sign.python';
:
like image 128
2 revs, 2 users 78% Avatar answered Oct 20 '22 00:10

2 revs, 2 users 78%