bmalehorn / vscode-fish

Fish syntax highlighting and formatting
MIT License
62 stars 6 forks source link

Add syntax highlighting for `and`, `or`, and after a semicolon #20

Closed piechologist closed 2 years ago

piechologist commented 3 years ago

EDIT: Added not.

Could you add highlighting for the keywordsand, or, and not? Also, they should change the syntax of the following words the way if or while do.

In version 1.0.20, you added function syntax highlighting for (, |, &&. I think, adding ; to this list should not do any harm.

PS: Thanks a lot for this extension! I use it almost every day… 👍

bmalehorn commented 3 years ago

Hi @piechologist, glad to see you're enjoying the extension!

I've gone ahead and added this syntax highlighting in https://github.com/bmalehorn/vscode-fish/commit/e8750f10d91879b12382e1c49ff6ba3db771368d:

before after
image image

Technically, I don't have a special rule for highlighting and, or, and not. Instead, I just highlight whatever comes after the ;. This should cover your use case but work for all functions in general. After all, and, or, and not are not special syntax in fish, they're just built-in functions. Hope you enjoy!

piechologist commented 3 years ago

Thank you for the quick update! Words after semicolons look perfect now.

Regarding and, or, and not, fish itself classifies these as operators on the command-line (as in the universal variable fish_color_operator). For instance, that's a screenshot from my terminal (true and echo are colored as commands):

Screen Shot 2021-05-20 at 10 38 40

Currently, true and echo look like plain text in VSCode:

Screen Shot 2021-05-20 at 10 27 15

I propose to handle and, or, and not as keywords in your extension. true and echo would be automatically colored as commands. It would look like this (which is closer to the command-line, depending on individual color settings of course):

Screen Shot 2021-05-20 at 10 28 05

I fiddled with the file ~/.vscode/extensions/bmalehorn.vscode-fish-*/syntaxes/fish.tmLanguage.json and just changed while to while|and|or|not inside the regexes (twice on line 57 and once on line 65). So, no need to change the logic of the syntax coloring and no fear of breaking things. What do you think?

bmalehorn commented 2 years ago

Hi @piechologist, I'm going through and clearing out old issues.

You're right that these should be highlighted similarly to fish itself. I implemented a fix in 2fe4ccc, highlighting them as keywords.

It's not totally correct, since and, or, and not are actually built-in functions and not of keywords. You could define the functions yourself like this:

function and
    test $status -eq 0 && $argv
end

function or
    test $status -eq 0 || $argv
end

function not
    $argv
    if test $status -eq 0
        return 1
    else
        return 0
    end
end

...while keywords like function, if, end are parsed differently and couldn't be defined by the user.

However I'm happy to make these highlighted closer to whatever shows up in your terminal.