2015-08-15 49 views
3

我在TextMate2中工作,但此問題也可能適用於其他文本編輯器。針對子類型註釋的不同語法高亮(?)

我的腳本在R.我打算在腳本上使用rmarkdown::render()創建一個"report"

這些報告的巧妙的部分是,它們區分R中的標準註釋符號(#)之間,並且執行以下操作:

  1. #'表示降價,像roxygen
  2. #+指示knitr code chunk will follow

我在編輯TextMate2包吸吮。我設法設置熱鍵來註釋掉#'#+,並用適當的縮進來完成。現在,我希望我可以編輯我的主題(我在TextMate1中設計),使這些「特殊」評論之一變成另一種顏色。

我已經編輯將R包的語言的語法(這是文件的啓動方式):

{ 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'; }; }; 
       }, 
      ); 
     }, 

並插入以下到中間,希望這將讓我指定語法高亮一個新的範圍:

 # 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 

如果有幫助,我可以提供其餘的語言語法,但我不確定它在這裏是否有關。

我試圖在重新定義主題中的評論時更具體(之前只是comment,我將其更改爲comment.line.number-sign.r)。這裏是(我認爲是)爲主題的相關線路:

{ 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'; 
      }; 
}, 

到目前爲止,我無法實現與#開始對與#'開頭的行一行的語法高亮任何區別。我可以同時改變,但不能獨立。在搞清楚如何爲這兩個語法實現不同的語法突出顯示的任何幫助將是很好的。

回答

1

TextMate寧願將第一個作用域comment.line.number-sign.r添加到您的自定義語法中。我所做的只是將你的代碼粘貼在我的comment.line.number-sign.r定義之上,而不是像你之前指出的那樣,並且擴展了你現有的語法/主題。

下面是我得到了什麼:

在包編輯器 - >的R - >語言語法 - > [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'; }; }; 
       }, 
      ); 
     }, 
     //... 

,然後在我的主題:

 //... 
     { 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(!) 
      }; 
     }, 
    ); 
} 

我不要使用R,所以我只是用一個簡單的例子來搜索所有3種評論。 Here's the file I used to test

我所看到的截圖: screenshot

+0

哇 - 我火上澆油試試這個。謝謝。問題:爲什麼你要改變這種顏色?有問題,或只是不是你的口味? – rbatt

+0

這工作就像一個魅力。現在我只需要找到可以工作的顏色...... – rbatt

+0

如果你喜歡它,歡迎使用fuscia,哈哈,我只是覺得我會把它叫出來,因爲它只是一個佔位符。很高興它對你有效! –