Skip to content

Commit

Permalink
Syntax: Add interactive unix shell support in fenced code blocks
Browse files Browse the repository at this point in the history
It seems common practice to treat `shell` code blocks as interactive
shell. Means each line starting with `$` is treated as shell command,
while other lines denote output only, which may not be highlighted.
  • Loading branch information
deathaxe committed Feb 20, 2022
1 parent 2e37810 commit c3169b4
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 4 deletions.
1 change: 1 addition & 0 deletions messages/3.1.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ feedback you can use [GitHub issues][issues].

* fix strikethrough visibility with `line_highlight`
* don't add symbols of fenced code blocks to symbol list
* fix interactive shell highlighting in fenced code blocks

## New Features

Expand Down
20 changes: 19 additions & 1 deletion syntaxes/Markdown.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,7 @@ contexts:
- include: fenced-ruby
- include: fenced-rust
- include: fenced-scala
- include: fenced-shell
- include: fenced-shell-script
- include: fenced-sql
- include: fenced-tsx
Expand Down Expand Up @@ -1531,11 +1532,28 @@ contexts:
0: meta.code-fence.definition.end.scala.markdown-gfm
1: punctuation.definition.raw.code-fence.end.markdown
fenced-shell:
- match: |-
(?x)
{{fenced_code_block_start}}
((?i:console|shell))
{{fenced_code_block_trailing_infostring_characters}}
captures:
0: meta.code-fence.definition.begin.shell.markdown-gfm
2: punctuation.definition.raw.code-fence.begin.markdown
5: constant.other.language-name.markdown
embed: scope:source.shell.interactive.markdown
embed_scope: markup.raw.code-fence.shell.markdown-gfm
escape: '{{fenced_code_block_escape}}'
escape_captures:
0: meta.code-fence.definition.end.shell.markdown-gfm
1: punctuation.definition.raw.code-fence.end.markdown
fenced-shell-script:
- match: |-
(?x)
{{fenced_code_block_start}}
((?i:console|shell(?:-script)?|sh|bash|zsh))
((?i:shell-script|sh|bash|zsh))
{{fenced_code_block_trailing_infostring_characters}}
captures:
0: meta.code-fence.definition.begin.shell-script.markdown-gfm
Expand Down
25 changes: 25 additions & 0 deletions syntaxes/Shell (for Markdown).sublime-syntax
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
%YAML 1.2
---
name: Interactive Unix Shell
scope: source.shell.interactive.markdown
hidden: true

contexts:
main:
- match: ^(?=\$\s)
push: shell-interactive
- include: shell-statements

shell-interactive:
- match: ^\$(?=\s)
scope: comment.other.shell
push: shell-statements
with_prototype:
# continuation lines begin with `> `
- match: ^>(?=\s)
scope: comment.other.shell
- match: ^
pop: true

shell-statements:
- include: scope:source.shell
32 changes: 29 additions & 3 deletions tests/syntax_test_markdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -1690,11 +1690,37 @@ unclosed_paren = (
|^^ meta.code-fence.definition.end.shell-script.markdown-gfm punctuation.definition.raw.code-fence.end.markdown

```shell
function foo () {
| <- markup.raw.code-fence.shell.markdown-gfm source.shell.interactive.markdown meta.function.shell storage.type
}
| <- markup.raw.code-fence.shell.markdown-gfm source.shell.interactive.markdown meta.function.shell punctuation.section

| <- markup.raw.code-fence.shell-script.markdown-gfm source.shell.bash
$ ls ~
| <- markup.raw.code-fence.shell.markdown-gfm source.shell.interactive comment.other.shell
| ^^ meta.function-call.shell variable.function.shell
| ^^ meta.function-call.arguments.shell

output.txt
| <- markup.raw.code-fence.shell.markdown-gfm source.shell.interactive - meta.function-call - variable
|^^^^^^^^^ markup.raw.code-fence.shell.markdown-gfm source.shell.interactive - meta.function-call - variable

$ ls \
> /foo/
| <- markup.raw.code-fence.shell.markdown-gfm source.shell.interactive.markdown comment.other.shell
|^^^^^^^ markup.raw.code-fence.shell.markdown-gfm source.shell.interactive.markdown

$ ls \
> /foo/
bar
| <- markup.raw.code-fence.shell.markdown-gfm source.shell.interactive.markdown - meta.function-call
|^^^ markup.raw.code-fence.shell.markdown-gfm source.shell.interactive.markdown - meta.function-call

function foo () {}
| <- markup.raw.code-fence.shell.markdown-gfm source.shell.interactive.markdown - meta.function
|^^^^^^^^^^^^^^^^^^ markup.raw.code-fence.shell.markdown-gfm source.shell.interactive.markdown - meta.function
```
| <- meta.code-fence.definition.end.shell-script.markdown-gfm punctuation.definition.raw.code-fence.end.markdown
|^^ meta.code-fence.definition.end.shell-script.markdown-gfm punctuation.definition.raw.code-fence.end.markdown
| <- meta.code-fence.definition.end.shell.markdown-gfm punctuation.definition.raw.code-fence.end.markdown
|^^ meta.code-fence.definition.end.shell.markdown-gfm punctuation.definition.raw.code-fence.end.markdown

```shell-script

Expand Down

0 comments on commit c3169b4

Please sign in to comment.