Skip to content

Commit

Permalink
Shell session & bash: Highlight all commands after the start of any H…
Browse files Browse the repository at this point in the history
…eredoc string (#2509)
  • Loading branch information
RunDevelopment committed Aug 27, 2020
1 parent bafab63 commit 6c92180
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 15 deletions.
20 changes: 17 additions & 3 deletions components/prism-bash.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@
// + make sure PS1..4 are here as they are not always set,
// - some useless things.
var envVars = '\\b(?:BASH|BASHOPTS|BASH_ALIASES|BASH_ARGC|BASH_ARGV|BASH_CMDS|BASH_COMPLETION_COMPAT_DIR|BASH_LINENO|BASH_REMATCH|BASH_SOURCE|BASH_VERSINFO|BASH_VERSION|COLORTERM|COLUMNS|COMP_WORDBREAKS|DBUS_SESSION_BUS_ADDRESS|DEFAULTS_PATH|DESKTOP_SESSION|DIRSTACK|DISPLAY|EUID|GDMSESSION|GDM_LANG|GNOME_KEYRING_CONTROL|GNOME_KEYRING_PID|GPG_AGENT_INFO|GROUPS|HISTCONTROL|HISTFILE|HISTFILESIZE|HISTSIZE|HOME|HOSTNAME|HOSTTYPE|IFS|INSTANCE|JOB|LANG|LANGUAGE|LC_ADDRESS|LC_ALL|LC_IDENTIFICATION|LC_MEASUREMENT|LC_MONETARY|LC_NAME|LC_NUMERIC|LC_PAPER|LC_TELEPHONE|LC_TIME|LESSCLOSE|LESSOPEN|LINES|LOGNAME|LS_COLORS|MACHTYPE|MAILCHECK|MANDATORY_PATH|NO_AT_BRIDGE|OLDPWD|OPTERR|OPTIND|ORBIT_SOCKETDIR|OSTYPE|PAPERSIZE|PATH|PIPESTATUS|PPID|PS1|PS2|PS3|PS4|PWD|RANDOM|REPLY|SECONDS|SELINUX_INIT|SESSION|SESSIONTYPE|SESSION_MANAGER|SHELL|SHELLOPTS|SHLVL|SSH_AUTH_SOCK|TERM|UID|UPSTART_EVENTS|UPSTART_INSTANCE|UPSTART_JOB|UPSTART_SESSION|USER|WINDOWID|XAUTHORITY|XDG_CONFIG_DIRS|XDG_CURRENT_DESKTOP|XDG_DATA_DIRS|XDG_GREETER_DATA_DIR|XDG_MENU_PREFIX|XDG_RUNTIME_DIR|XDG_SEAT|XDG_SEAT_PATH|XDG_SESSION_DESKTOP|XDG_SESSION_ID|XDG_SESSION_PATH|XDG_SESSION_TYPE|XDG_VTNR|XMODIFIERS)\\b';

var commandAfterHeredoc = {
pattern: /(^(["']?)\w+\2)[ \t]+\S.*/,
lookbehind: true,
alias: 'punctuation', // this looks reasonably well in all themes
inside: null // see below
};

var insideString = {
'bash': commandAfterHeredoc,
'environment': {
pattern: RegExp("\\$" + envVars),
alias: 'constant'
Expand Down Expand Up @@ -107,17 +116,20 @@
'string': [
// Support for Here-documents https://en.wikipedia.org/wiki/Here_document
{
pattern: /((?:^|[^<])<<-?\s*)(\w+?)\s*(?:\r?\n|\r)[\s\S]*?(?:\r?\n|\r)\2/,
pattern: /((?:^|[^<])<<-?\s*)(\w+?)\s[\s\S]*?(?:\r?\n|\r)\2/,
lookbehind: true,
greedy: true,
inside: insideString
},
// Here-document with quotes around the tag
// → No expansion (so no “inside”).
{
pattern: /((?:^|[^<])<<-?\s*)(["'])(\w+)\2\s*(?:\r?\n|\r)[\s\S]*?(?:\r?\n|\r)\3/,
pattern: /((?:^|[^<])<<-?\s*)(["'])(\w+)\2\s[\s\S]*?(?:\r?\n|\r)\3/,
lookbehind: true,
greedy: true
greedy: true,
inside: {
'bash': commandAfterHeredoc
}
},
// “Normal” string
{
Expand Down Expand Up @@ -172,6 +184,8 @@
}
};

commandAfterHeredoc.inside = Prism.languages.bash;

/* Patterns in command substitution. */
var toBeCopied = [
'comment',
Expand Down
2 changes: 1 addition & 1 deletion components/prism-bash.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 2 additions & 6 deletions components/prism-shell-session.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@
/(["'])(?:\\[\s\S]|\$\([^)]+\)|`[^`]+`|(?!\1)[^\\])*\1/.source,

// here doc
// 1 capturing group
/<<-?\s*(\w+?)[ \t]*(?!.)[\s\S]*?[\r\n]\2/.source,

// here doc quoted
// 2 capturing group
/<<-?\s*(["'])(\w+)\3[ \t]*(?!.)[\s\S]*?[\r\n]\4/.source
// 2 capturing groups
/<<-?\s*(["']?)(\w+)\2\s[\s\S]*?[\r\n]\3/.source
].join('|');

Prism.languages['shell-session'] = {
Expand Down
2 changes: 1 addition & 1 deletion components/prism-shell-session.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 13 additions & 2 deletions tests/languages/bash/string_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,21 @@ STRING_END
["punctuation", "\\"],
"'a ",
["comment", "# ' not a string"],

["operator", [
"<<"
]],
["string", [
"STRING_END\r\nfoo\r\nbar\r\nSTRING_END"
]],

["operator", [
"<<-"
]],
["string", [
"STRING_END\r\nfoo\r\nbar\r\nSTRING_END"
]],

["operator", [
"<<"
]],
Expand All @@ -111,20 +114,28 @@ STRING_END
["variable", "$@"],
"\r\nbar\r\nEOF"
]],

["operator", [
"<<"
]],
["string", "'EOF'\r\n'single quoted string'\r\n\"double quoted string\"\r\nEOF"],
["string", [
"'EOF'\r\n'single quoted string'\r\n\"double quoted string\"\r\nEOF"
]],

["operator", [
"<<"
]],
["string", "\"EOF\"\r\nfoo\r\n$bar\r\nEOF"],
["string", [
"\"EOF\"\r\nfoo\r\n$bar\r\nEOF"
]],

["operator", [
"<<"
]],
["string", [
"STRING_END\r\n# comment\r\nSTRING_END"
]],

["string", [
"\" # comment \""
]]
Expand Down
31 changes: 29 additions & 2 deletions tests/languages/shell-session/command_string_feature.test
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
$ echo 'Foo
> Bar'

$ echo "Foo
> Bar"

Expand All @@ -15,6 +16,11 @@ STRING_END

$ echo \'a # '

$ cat << "EOF" > /etc/ipsec.secrets
: RSA vpn-server-a.key
# : RSA vpn-server-b.key
EOF

----------------------------------------------------

[
Expand Down Expand Up @@ -58,7 +64,9 @@ $ echo \'a # '
["operator", [
"<<-"
]],
["string", "\"STRING_END\"\r\nfoo\r\nbar\r\nSTRING_END"]
["string", [
"\"STRING_END\"\r\nfoo\r\nbar\r\nSTRING_END"
]]
]]
]],

Expand All @@ -71,7 +79,26 @@ $ echo \'a # '
["comment", "# "]
]]
]],
["output", "'"]
["output", "'\r\n\r\n"],
["command", [
["shell-symbol", "$"],
["bash", [
["function", "cat"],
["operator", [
"<<"
]],
["string", [
"\"EOF\"",
["bash", [
["operator", [
">"
]],
" /etc/ipsec.secrets"
]],
"\r\n: RSA vpn-server-a.key\r\n# : RSA vpn-server-b.key\r\nEOF"
]]
]]
]]
]

----------------------------------------------------
Expand Down

0 comments on commit 6c92180

Please sign in to comment.