rokucommunity / brighterscript

A superset of Roku's BrightScript language
MIT License
160 stars 46 forks source link

How to enable syntax highlighting with LSP? #937

Closed praxder closed 11 months ago

praxder commented 11 months ago

I'm using the Brighterscript LSP in NeoVim, and it seems to be working, except for syntax highlighting. I'm starting it with /usr/local/bin/bsc --lsp --stdio, and that successfully provider things like go-to-definition, hover, etc. but doesn't provide syntax highlighting. Is there an easy way to get the Brighterscript language server to support / enable syntax highlighting?

TwitchBronBron commented 11 months ago

Oh cool, glad to hear the LSP is functional in NeoVim!

I don't believe the language server specification supports full syntax highlighting. The closest thing is the semantic tokens request, but its purpose is not to color the whole file, but instead to add additional color information that only a language server could provide (like coloring class/namespace references differently than variables)

The BrightScript Language extension for vscode includes a separate textmate grammar file for syntax highlighting support. You can find that file here. This PR registered the same textmate grammar file to drive GitHub's syntax highlighting as well.

I'd suggest you find a way to integrate our textmate grammar file into NeoVim (or roll your own in whatever format they require). However, if that's not an option, I'm open open to seeing a PR that sends all of the above token types, but it would need to be off by default with maybe a bsconfig.json option to enable it so we can protect against performance issues.

The Language Server Protocol has support for the following tokens, so the LSP could at least send those, but again, it would be fairly expensive since the textmate regex-based logic seems to be much more performant in editors.

enum SemanticTokenTypes {
    namespace = 'namespace',
    type = 'type',
    class = 'class',
    enum = 'enum',
    interface = 'interface',
    struct = 'struct',
    typeParameter = 'typeParameter',
    parameter = 'parameter',
    variable = 'variable',
    property = 'property',
    enumMember = 'enumMember',
    event = 'event',
    function = 'function',
    method = 'method',
    macro = 'macro',
    keyword = 'keyword',
    modifier = 'modifier',
    comment = 'comment',
    string = 'string',
    number = 'number',
    regexp = 'regexp',
    operator = 'operator',
    decorator = 'decorator'
}
praxder commented 11 months ago

Thanks for the quick and thorough response, @TwitchBronBron! Much appreciated. I'm fairly new to the idea of language servers, so I was unaware that the language server didn't provide this functionality. After some quick research, it appears as if NeoVim uses TreeSitter, so I'll look into if there's a way to convert the textmate Brighterscript grammar file into one that tree-sitter expects.

In the interim, for anyone with a similar issue, the roku.vim plugin provider syntax highlighting, but it appears the author has simply rolled his own highlighting, so long-term it would be good if the above referenced textmate grammar file was converted to a TreeSitter language parser.