如何在Textmate中为Python代码突出显示末尾空格?

11

我想做类似于这个Textmate提示的事情,这样当我编写Python代码时,尾随空格总是以某种方式突出显示-这使得立即纠正更容易,并且其他编辑器如Emacs也可以实现。

不幸的是,在该文章之后的讨论中,似乎难以实现。对我而言,即使按照此提示操作,invalid.trailing-whitespace范围选择器在首选项中也不可见。是否有其他人成功实现过?

2个回答

5
这段代码可以正常运行(但不能包含注释):
{   scopeName = 'source.whitespace';
    patterns = (
        {  name = 'source.invalid.trailing-whitespace';
            match = '(\s+)$';
            captures = { 1 = { name = 'invalid.trailing-whitespace'; }; };
         },
    );
}

PS:我已将“源”更改为“source.whitespace”

对于Python语法中的注释更改:

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

在:

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

在Python语言定义中,您需要添加一个“include”,其中:

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

Turns to:

:
patterns = (
 {  include = 'source.whitespace'; },
 {    name = 'comment.line.number-sign.python';
:

5
我不知道如何高亮尾随空格,但你可以通过以下步骤移除它:
Bundles -> Text -> Converting/Stripping -> Remove trailing spaces in document
另外,由于 Textmate 具有 Emacs 绑定功能,您也可以像在 Emacs 中一样进行操作。

非常好!我将该捆绑操作分配给了^S,并将保存操作从“无”更改为“当前文件”。因此,现在如果我按下^S,在将文件保存到磁盘之前,空格将被删除。 - pojo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接