How to syntax highlight separate types of keywords conditionally?


#1

Hi,

I’ve been working on adding a feature to the language-shellscript package (see pull #50 for extra details) namely that I want common Unix commands (like commands provided by the GNU Coreutils package on Linux, along with sudo, grep, etc.) to be highlighted by the package too. The problem I’ve encountered is that I do not know how to edit the grammars/shell-unix-bash.cson file so that if the coreutils commands (e.g., cp, install, etc.) appears after another command on the same line, it is left unhighlighted. The only exception, I would like to impose, is that if the coreutils command appears after the sudo command, in which case I want the sudo command to be highlighted the way specified earlier in the cson file (under the support.function.extras.shell class), and the coreutils command to be highlighted the way it is when no commands appear before it (under the support.function.coreutils.shell class). Here is my current grammars/shell-unix-bash.cson file: https://gist.github.com/fusion809/59cc66ea396c9148a9f8dc5431ea73fb.

An example of the install command appearing after another command would be apm install, I don’t want the coreutils syntax-highlighting to be applied to the install keyword, because it is not the coreutils command being invoked it is the apm command being invoked. I have tried modifying:

      {
        'match': '(?<![-/./])\\b(?:base32|base64|basename|cat|chcon|chgrp|chmod|chown|chroot|cksum|comm|cp|csplit|cut|date|dd|df|dir|dircolors|dirname|du|echo|env|expand|expr|factor|false|fmt|fold|head|hostid|id|install|join|link|ln|logname|ls|md5sum|mkdir|mkfifo|mknod|mktemp|mv|nice|nl|nohup|nproc|numfmt|od|paste|pathchk|pinky|pr|printenv|printf|ptx|pwd|readlink|realpath|rm|rmdir|runcon|seq|sha1sum|sha224sum|sha256sum|sha384sum|sha512sum|shred|shuf|sleep|sort|split|stat|stdbuf|stty|sum|sync|tac|tail|tee|test|timeout|touch|tr|true|truncate|tsort|tty|uname|unexpand|uniq|unlink|users|vdir|wc|who|whoami|yes)\\b(?![-/=/])'
        'name': 'support.function.coreutils.shell'
      }

to:

      {
        'match': '((?<![-/./^[a-zA-Z]+$ /])|(sudo ))\\b(?:base32|base64|basename|cat|chcon|chgrp|chmod|chown|chroot|cksum|comm|cp|csplit|cut|date|dd|df|dir|dircolors|dirname|du|echo|env|expand|expr|factor|false|fmt|fold|head|hostid|id|install|join|link|ln|logname|ls|md5sum|mkdir|mkfifo|mknod|mktemp|mv|nice|nl|nohup|nproc|numfmt|od|paste|pathchk|pinky|pr|printenv|printf|ptx|pwd|readlink|realpath|rm|rmdir|runcon|seq|sha1sum|sha224sum|sha256sum|sha384sum|sha512sum|shred|shuf|sleep|sort|split|stat|stdbuf|stty|sum|sync|tac|tail|tee|test|timeout|touch|tr|true|truncate|tsort|tty|uname|unexpand|uniq|unlink|users|vdir|wc|who|whoami|yes)\\b(?![-/=/])'
        'name': 'support.function.coreutils.shell'
      }

but, unfortunately, this syntax-highlighted sudo install using the support.function.coreutils.shell class and did not syntax-highlight sudo using the support.function.extras.shell class and install using the support.function.coreutils.shell class.

Thanks for your time,
Brenton