How to create a simple custom language colorization to VS Code

前端 未结 3 2057
情书的邮戳
情书的邮戳 2020-12-31 11:26

I\'m trying to create a simple colorization for log files, now that it\'s possible include custom languages in Code (I\'m on 0.9.2). I have created a simple .tmLanguage file

3条回答
  •  太阳男子
    2020-12-31 12:16

    You need to use regular expressions instead of static strings to describe the pattern:

    match
    q  <- This needs to be a regular expression
    name
    comment
    

    I provide a more useful example for a log file highlighter. It colors numbers, hints, warnings and errors in different colors. The rules to identify these keywords and numbers are based on regular expression.

    
    
    
        
            scopeName
            text.log
    
            fileTypes
            
                log
            
    
            name
            Log file
    
            patterns
            
                
                    match
                    \b(?i:(hint|info|information))\b
                    name
    
                    info-token
                                
                
                    match
                    \b(?i:(warning|warn))\b
                    name
                    warn-token
                
                
                    match
                    \b(?i:(Error|Failure|Fail))\b
                    name
                    error-token
                
                
                    match
                    \b((0(x|X)[0-9a-fA-F]*)|(([0-9]+\.?[0-9]*)|(\.[0-9]+))((e|E)(\+|-)?[0-9]+)?)(L|l|UL|ul|u|U|F|f|ll|LL|ull|ULL)?\b
                    name
                    constant.numeric
                                                
            
            uuid
            FF0550E0-3A29-11E3-AA6E-0800200C9A77
        
    
    

    The highlighter gives a result like this (using the default theme):

    I didn't find an official documentation about the available tokens (like error-token, constant.numeric etc). But there is a file located in %VSCODE_INSTALLATION%\resources\app\out\vs\languages\markdown\common\tokens.css. It seems to list all available tokens etc. Use it as a reference when you create the .tmLanguage file.

    But pay attention: Some themes are using only the basic tokens. And some other themes are using the same color for many different tokens. So you should test the highlighter frequently against the most common themes to see whether the result looks good or not.

    You should definitely visit this page about Language Grammars to learn more.

提交回复
热议问题