robballou / gitconfig-sublimetext

Sublime Text 2 language file for .gitconfig and .gitignore files
18 stars 4 forks source link

Matching ";" in aliases #4

Open qwindelzorf opened 9 years ago

qwindelzorf commented 9 years ago

Currently, if you have an alias that contains a quoted command which in turn contains a ;, the syntax highlighting thinks that everything after the ; is not part of the command.

For a simple example, consider the alias

foobar = "!f() { echo 'foo'; echo 'bar'; }; f"

Everything inside the double quotes should be highlighted, ut only stuff up to the end of the first echo statement gets highlighted. For a more realistic use case, I have the alias:

sshow = "!f() { git stash show stash^{/$*} -p; }; f"

With this in my gitconfig, syntax highlighting picks up everything up to -p. The stuff after that, ; }; f" is not highlighted, even though it should be.

Looking at the main file (Git Config.tmLanguage), the quick solution would be removing the ; from the negative match group at the end of the keyword match on line 28.

Fixing the regex to not end on ; or # inside quotes is probably the right answer, but that would involve some fancy regex lookaround magic, and that sounds much too complicated to think about before I've finished my first cup of coffee

robballou commented 9 years ago

I think maybe we should adopt this: https://github.com/textmate/git.tmbundle/blob/master/Syntaxes/Git%20Config.tmLanguage. That is used in TM2 and Atom https://github.com/atom/language-git/blob/master/grammars/git%20config.cson. Can you try that and see if it works for you?

qwindelzorf commented 9 years ago

That seems to work reasonably well. At the very least, the regexes appear to properly deal with comment characters inside of quoted strings. I'm not sure that just picking up the whole file would be the best plan, as it does some other weird stuff (e.g.: it picks up every instance of "yes", "no", "true" or "false" for extra highlighting, even when they are part of other arguments).

robballou commented 9 years ago

I added a commit that should help out with this. Check it out and let me know how it goes. Thanks!

qwindelzorf commented 9 years ago

Looks like that fixes the semicolon issue, but introduces a failure on escaped quotes within strings, which cause the regex match to terminate prematurely. Simply changing the match on line 32 to ^\s*([a-zA-Z0-9_][a-zA-Z0-9_\-]*)\s*=\s*!?"((?:[^"\\]|\\.)*)" seems to fix that though.