Different syntax highlighting for sub-types of comments (?)

≯℡__Kan透↙ 提交于 2019-12-10 18:52:09

问题


I'm working in TextMate2, but this question may apply to other text editors as well.

My script is in R. I intend to use rmarkdown::render() on the script to create a "report".

The clever part of these reports is that they distinguish between the standard comment symbol in R (#), and the following:

  1. #' indicates markdown, like in roxygen,
  2. #+ indicates that a knitr code chunk will follow

I suck at editing TextMate2 bundles. I managed to get hotkeys set up to comment out lines with #' and #+, and to do it with proper indentation. Now, I wish I could edit my theme (which I designed in TextMate1) to make one of those "special" comments a different color.

I've edited the R bundle's language grammar (this is how the file starts):

{   patterns = (
        {   name = 'comment.line.pragma-mark.r';
            match = '^(#pragma[ \t]+mark)[ \t](.*)';
            captures = {
                1 = { name = 'comment.line.pragma.r'; };
                2 = { name = 'entity.name.pragma.name.r'; };
            };
        },
        {   begin = '(^[ \t]+)?(?=#)';
            end = '(?!\G)';
            beginCaptures = { 1 = { name = 'punctuation.whitespace.comment.leading.r'; }; };
            patterns = (
                {   name = 'comment.line.number-sign.r';
                    begin = '#';
                    end = '\n';
                    beginCaptures = { 0 = { name = 'punctuation.definition.comment.r'; }; };
                },
            );
        },

And inserted the following into the middle, hoping it would let me specify a new scope for syntax highlighting:

        # START MY STUFF
        {   begin = '(^[ \t]+)?(?=#'')';
            end = '(?!\G)';
            beginCaptures = { 1 = { name = 'punctuation.whitespace.comment.leading.r'; }; };
            patterns = (
                {   name = 'comment.line.number-sign-tick.r';
                    begin = "#'";
                    end = '\n';
                    beginCaptures = { 0 = { name = 'punctuation.definition.comment.r'; }; };
                },
            );
        },
        # END MY STUFF

If it would help, I could provide the rest of the language grammar, but I'm not sure it's relevant here.

I tried to be more specific when redefining the comment in the theme (previous was just comment, which I changed to comment.line.number-sign.r). Here are (what I think are) the relevant lines of the theme:

{   name = 'Comment';
            scope = 'comment.line.number-sign.r';
            settings = {
                fontStyle = 'italic';
                foreground = '#279797';
            };
        },
        {   name = 'Comment';
            scope = 'comment.line.number-sign-tick.r';
            settings = {
                fontStyle = 'italic';
                foreground = '#C5060B';
            };
},

So far, I cannot achieve any difference in the syntax highlighting of a line that starts with # versus a line that starts with #'. I can get both to change, but no independently. Any help in figuring out how to achieve different syntax highlighting for those two would be great.


回答1:


TextMate is preferring the first scope, comment.line.number-sign.r to your custom grammars. All I did is paste your code above my comment.line.number-sign.r definition, instead of after as you had indicated, and expanded upon your existing grammar/theme.

Here's what I've got:

In Bundle Editor-> R -> Language Grammars -> R

{   patterns = (
        //default block
        {   name = 'comment.line.pragma-mark.r';
            match = '^(#pragma[ \t]+mark)[ \t](.*)';
            captures = {
                1 = { name = 'comment.line.pragma.r'; };
                2 = { name = 'entity.name.pragma.name.r'; };
            };
        },
        //your block
        {   begin = '(^[ \t]+)?(?=#'')';
            end = '(?!\G)';
            beginCaptures = { 1 = { name = 'punctuation.whitespace.comment.leading.r'; }; };
            patterns = (
                {   name = 'comment.line.number-sign-tick.r';
                    begin = "#'";
                    end = '\n';
                    beginCaptures = { 0 = { name = 'punctuation.definition.comment.r'; }; };
                },
            );
        },
        //my block
        {   begin = '(^[ \t]+)?(?=#\+)';
            end = '(?!\G)';
            beginCaptures = { 1 = { name = 'punctuation.whitespace.comment.leading.r'; }; };
            patterns = (
                {   name = 'comment.line.number-sign-plus.r';
                    begin = '#\+';
                    end = '\n';
                    beginCaptures = { 0 = { name = 'punctuation.definition.comment.r'; }; };
                },
            );
        },
        //default caption block
        {   begin = '(^[ \t]+)?(?=#)';
            end = '(?!\G)';
            beginCaptures = { 1 = { name = 'punctuation.whitespace.comment.leading.r'; }; };
            patterns = (
                {   name = 'comment.line.number-sign.r';
                    begin = '#';
                    end = '\n';
                    beginCaptures = { 0 = { name = 'punctuation.definition.comment.r'; }; };
                },
            );
        },
        //... 

And then, in my theme:

        //...
        {   name = 'Comment';
            scope = 'comment.line.number-sign.r';
            settings = {
                fontStyle = 'italic';
                foreground = '#279797';
            };
        },
        {   name = 'Comment';
            scope = 'comment.line.number-sign-tick.r';
            settings = {
                fontStyle = 'italic';
                foreground = '#C5060B';
            };
        },
        {   name = 'Comment';
            scope = 'comment.line.number-sign-plus.r';
            settings = {
                fontStyle = 'italic';
                foreground = '#ff00ff';//fix this color(!)
            };
        },
    );
}

I don't use R, so I just Googled for a quick example with all 3 kinds of comments. Here's the file I used to test.

A screenshot of what I'm seeing:



来源:https://stackoverflow.com/questions/32020469/different-syntax-highlighting-for-sub-types-of-comments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!