wjmccann
wjmccann

Reputation: 522

Visual Studio Code: Comments not being detected while trying to add support for a new language/filetype

So I am working on a research project that involves using a very specific piece of software that uses its own filetype; XPPAUT using .ode files. To prevent me and my team of not-neuroscientists from ripping our hair out trying to work with this, I decided to write a syntax highlighter for these .ode files.

To start I just wanted to be able to recognize and color linecomments, which are delineated with a #, similar to Python, however when I run the development environment, the comments are not highlighted with the color I set my dev workspace to use, or highlighted at all. I'm very new to this, so any help would be appreciated.

Here is my package.json file

{
    "name": "ode",
    "displayName": "XPP ODE",
    "description": "ODE files to be used with XPP/XPPAUT",
    "version": "0.0.1",
    "publisher": "wjmccann",
    "engines": {
        "vscode": "^1.22.0"
    },
    "categories": [
        "Languages"
    ],
    "contributes": {
        "languages": [{
            "id": "xpp",
            "aliases": ["XPP ODE", "XPP", "XPPAUT"],
            "extensions": [".ode"],
            "configuration": "./language-configuration.json"
        }],
        "grammars": [{
            "language": "xpp",
            "scopeName": "source.xpp",
            "path": "./syntaxes/xpp.tmLanguage.json"
        }]
    }
}

and the corresponding language-configuration.json

{
    "comments": {
        // symbol used for single line comment. Remove this entry if your language does not support line comments
        "lineComment": "#",
    },
    // symbols used as brackets
    "brackets": [
        ["{", "}"],
        ["[", "]"],
        ["(", ")"]
    ],
    // symbols that are auto closed when typing
    "autoClosingPairs": [
        ["{", "}"],
        ["[", "]"],
        ["(", ")"],
        ["\"", "\""],
        ["'", "'"]
    ],
    // symbols that that can be used to surround a selection
    "surroundingPairs": [
        ["{", "}"],
        ["[", "]"],
        ["(", ")"],
        ["\"", "\""],
        ["'", "'"]
    ]
}

Upvotes: 1

Views: 1913

Answers (1)

ryarco
ryarco

Reputation: 111

The language-configuration.json file defines text patterns used in a variety of standard features of VS Code such as comment toggling as described here.

Syntax highlighting/colouring is via the grammars contribution point in package.json as described here.

Based on your package.jsonyou will need to create a new file at ./syntaxes/xpp.tmLanguage.json with the following content for your comments to be coloured appropriately. The actual colour used will depend on your current theme.

{
    "$schema": "https://raw.githubusercontent.com/martinring/tmlanguage/master/tmlanguage.json",
    "name": "xpp",
    "scopeName": "source.xpp",
    "patterns": [
        {
            "include": "#comments"

        }

    ],
    "repository": {
        "comments": {
            "patterns": [{
                "name": "comment.line.number-sign.xpp",
                "match": "#.*"
            }]
        }
    }
}

Upvotes: 5

Related Questions