Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Markdown] add syntax highlighting in code fences for a few languages #1274

Merged
merged 14 commits into from
Apr 15, 2018
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
300 changes: 292 additions & 8 deletions Markdown/Markdown.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ variables:
)
fenced_code_block_start: |-
(?x:
[ ]{0,3} # up to 3 spaces
[ ]*
([ ]{0,3}) # up to 3 spaces
(
`{3,} # 3 or more backticks
(?![^`]*`) # not followed by any more backticks on the same line
Expand All @@ -121,6 +122,15 @@ variables:
(?![^~]*~) # not followed by any more tildas on the same line
)
)
code_fence_escape: |-
(?x:
^ # the beginning of the line
#\1 # whitespace previously captured
#[ ]{0,3} # up to 3 spaces - doesn't have to be the same as the opening fence, but within 3 spaces
[ ]*
(\2) # the backtick/tilde combination that opened the code fence
\s*$ # any amount of whitespace until EOL
)
html_tag_open_commonmark: |-
(?xi:
<
Expand Down Expand Up @@ -163,6 +173,13 @@ variables:
(?i:address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h1|h2|h3|h4|h5|h6|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|meta|nav|noframes|ol|optgroup|option|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)
(?:\s|$|/?>)
)
# code_fence_escape: |-
# (?x:
# ^ # the beginning of the line
# [ ]{0,3} # up to 3 spaces
# (\1) # the backtick/tilde combination that opened the code fence
# \s*$ # any amount of whitespace until EOL
# )
contexts:
main:
- match: |-
Expand Down Expand Up @@ -847,7 +864,7 @@ contexts:
- include: block-quote
- match: $
pop: true
- match: '^(?=\s+{{fenced_code_block_start}})'
- match: '^(?={{fenced_code_block_start}})'
push:
- include: fenced-code-block
- match: ''
Expand Down Expand Up @@ -881,7 +898,7 @@ contexts:
pop: true
list-content:
- meta_content_scope: meta.paragraph.list.markdown
- match: ^\s*(?={{fenced_code_block_start}})
- match: ^(?={{fenced_code_block_start}})
push:
- match: $
pop: true
Expand All @@ -898,18 +915,285 @@ contexts:
- match: $
pop: true
fenced-code-block:
- match: |-
(?x)
{{fenced_code_block_start}}
((?i:xml))
\s*$ # any whitespace until EOL
captures:
2: markup.raw.code-fence.xml.markdown-gfm punctuation.definition.raw.code-fence.begin.markdown
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think these captures should be getting the markup.raw scope. They aren't part of the "raw" segment since they are not rendered verbatim. (Also, I have dimmed background for markup.raw and it would be nice if that started on the first line of the raw block.)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And the escape_captures for 0 should be removed.

3: markup.raw.code-fence.xml.markdown-gfm constant.other.language-name.markdown
embed: scope:text.xml
embed_scope: markup.raw.code-fence.xml.markdown-gfm
escape: '{{code_fence_escape}}'
escape_captures:
0: markup.raw.code-fence.xml.markdown-gfm
1: punctuation.definition.raw.code-fence.end.markdown
- match: |-
(?x)
{{fenced_code_block_start}}
((?i:sql))
\s*$ # any whitespace until EOL
captures:
2: markup.raw.code-fence.sql.markdown-gfm punctuation.definition.raw.code-fence.begin.markdown
3: markup.raw.code-fence.sql.markdown-gfm constant.other.language-name.markdown
embed: scope:source.sql
embed_scope: markup.raw.code-fence.sql.markdown-gfm
escape: '{{code_fence_escape}}'
escape_captures:
0: markup.raw.code-fence.sql.markdown-gfm
1: punctuation.definition.raw.code-fence.end.markdown
- match: |-
(?x)
{{fenced_code_block_start}}
((?i:python|py))
\s*$ # any whitespace until EOL
captures:
2: markup.raw.code-fence.python.markdown-gfm punctuation.definition.raw.code-fence.begin.markdown
3: markup.raw.code-fence.python.markdown-gfm constant.other.language-name.markdown
embed: scope:source.python
embed_scope: markup.raw.code-fence.python.markdown-gfm
escape: '{{code_fence_escape}}'
escape_captures:
0: markup.raw.code-fence.python.markdown-gfm
1: punctuation.definition.raw.code-fence.end.markdown
- match: |-
(?x)
{{fenced_code_block_start}}
((?i:graphviz))
\s*$ # any whitespace until EOL
captures:
2: markup.raw.code-fence.graphviz.markdown-gfm punctuation.definition.raw.code-fence.begin.markdown
3: markup.raw.code-fence.graphviz.markdown-gfm constant.other.language-name.markdown
embed: scope:source.dot
embed_scope: markup.raw.code-fence.graphviz.markdown-gfm
escape: '{{code_fence_escape}}'
escape_captures:
0: markup.raw.code-fence.graphviz.markdown-gfm
1: punctuation.definition.raw.code-fence.end.markdown
- match: |-
(?x)
{{fenced_code_block_start}}
((?i:javascript|js))
\s*$ # any whitespace until EOL
captures:
2: markup.raw.code-fence.javascript.markdown-gfm punctuation.definition.raw.code-fence.begin.markdown
3: markup.raw.code-fence.javascript.markdown-gfm constant.other.language-name.markdown
embed: scope:source.javascript
embed_scope: markup.raw.code-fence.javascript.markdown-gfm
escape: '{{code_fence_escape}}'
escape_captures:
0: markup.raw.code-fence.javascript.markdown-gfm
1: punctuation.definition.raw.code-fence.end.markdown
- match: |-
(?x)
{{fenced_code_block_start}}
((?i:java))
\s*$ # any whitespace until EOL
captures:
2: markup.raw.code-fence.java.markdown-gfm punctuation.definition.raw.code-fence.begin.markdown
3: markup.raw.code-fence.java.markdown-gfm constant.other.language-name.markdown
embed: scope:source.java
embed_scope: markup.raw.code-fence.java.markdown-gfm
escape: '{{code_fence_escape}}'
escape_captures:
0: markup.raw.code-fence.java.markdown-gfm
1: punctuation.definition.raw.code-fence.end.markdown
- match: |-
(?x)
{{fenced_code_block_start}}
((?i:csharp|c\#|cs))
\s*$ # any whitespace until EOL
captures:
2: markup.raw.code-fence.csharp.markdown-gfm punctuation.definition.raw.code-fence.begin.markdown
3: markup.raw.code-fence.csharp.markdown-gfm constant.other.language-name.markdown
embed: scope:source.cs
embed_scope: markup.raw.code-fence.csharp.markdown-gfm
escape: '{{code_fence_escape}}'
escape_captures:
0: markup.raw.code-fence.csharp.markdown-gfm
1: punctuation.definition.raw.code-fence.end.markdown
- match: |-
(?x)
{{fenced_code_block_start}}
((?i:rust))
\s*$ # any whitespace until EOL
captures:
2: markup.raw.code-fence.rust.markdown-gfm punctuation.definition.raw.code-fence.begin.markdown
3: markup.raw.code-fence.rust.markdown-gfm constant.other.language-name.markdown
embed: scope:source.rust
embed_scope: markup.raw.code-fence.rust.markdown-gfm
escape: '{{code_fence_escape}}'
escape_captures:
0: markup.raw.code-fence.rust.markdown-gfm
1: punctuation.definition.raw.code-fence.end.markdown
- match: |-
(?x)
{{fenced_code_block_start}}
((?i:shell-script|sh|bash|zsh))
\s*$ # any whitespace until EOL
captures:
2: markup.raw.code-fence.shell-script.markdown-gfm punctuation.definition.raw.code-fence.begin.markdown
3: markup.raw.code-fence.shell-script.markdown-gfm constant.other.language-name.markdown
embed: scope:source.shell.bash
embed_scope: markup.raw.code-fence.shell-script.markdown-gfm
escape: '{{code_fence_escape}}'
escape_captures:
0: markup.raw.code-fence.shell-script.markdown-gfm
1: punctuation.definition.raw.code-fence.end.markdown
- match: |-
(?x)
{{fenced_code_block_start}}
((?i:php|inc))
\s*$ # any whitespace until EOL
captures:
2: markup.raw.code-fence.php.markdown-gfm punctuation.definition.raw.code-fence.begin.markdown
3: markup.raw.code-fence.php.markdown-gfm constant.other.language-name.markdown
embed: scope:source.php
embed_scope: markup.raw.code-fence.php.markdown-gfm
escape: '{{code_fence_escape}}'
escape_captures:
0: markup.raw.code-fence.php.markdown-gfm
1: punctuation.definition.raw.code-fence.end.markdown
- match: |-
(?x)
{{fenced_code_block_start}}
((?i:html\+php))
\s*$ # any whitespace until EOL
captures:
2: markup.raw.code-fence.html-php.markdown-gfm punctuation.definition.raw.code-fence.begin.markdown
3: markup.raw.code-fence.html-php.markdown-gfm constant.other.language-name.markdown
embed: scope:embedding.php
embed_scope: markup.raw.code-fence.html-php.markdown-gfm
escape: '{{code_fence_escape}}'
escape_captures:
0: markup.raw.code-fence.html-php.markdown-gfm
1: punctuation.definition.raw.code-fence.end.markdown
- match: |-
(?x)
{{fenced_code_block_start}}
((?i:rscript|r|splus))
\s*$ # any whitespace until EOL
captures:
2: markup.raw.code-fence.r.markdown-gfm punctuation.definition.raw.code-fence.begin.markdown
3: markup.raw.code-fence.r.markdown-gfm constant.other.language-name.markdown
embed: scope:source.r
embed_scope: markup.raw.code-fence.r.markdown-gfm
escape: '{{code_fence_escape}}'
escape_captures:
0: markup.raw.code-fence.r.markdown-gfm
1: punctuation.definition.raw.code-fence.end.markdown
- match: |-
(?x)
{{fenced_code_block_start}}
((?i:golang))
\s*$ # any whitespace until EOL
captures:
2: markup.raw.code-fence.go.markdown-gfm punctuation.definition.raw.code-fence.begin.markdown
3: markup.raw.code-fence.go.markdown-gfm constant.other.language-name.markdown
embed: scope:source.go
embed_scope: markup.raw.code-fence.go.markdown-gfm
escape: '{{code_fence_escape}}'
escape_captures:
0: markup.raw.code-fence.go.markdown-gfm
1: punctuation.definition.raw.code-fence.end.markdown
- match: |-
(?x)
{{fenced_code_block_start}}
((?i:ruby|rb|rbx))
\s*$ # any whitespace until EOL
captures:
2: markup.raw.code-fence.ruby.markdown-gfm punctuation.definition.raw.code-fence.begin.markdown
3: markup.raw.code-fence.ruby.markdown-gfm constant.other.language-name.markdown
embed: scope:source.ruby
embed_scope: markup.raw.code-fence.ruby.markdown-gfm
escape: '{{code_fence_escape}}'
escape_captures:
0: markup.raw.code-fence.ruby.markdown-gfm
1: punctuation.definition.raw.code-fence.end.markdown
- match: |-
(?x)
{{fenced_code_block_start}}
((?i:objc|obj-c|objectivec|objective-c))
\s*$ # any whitespace until EOL
captures:
2: markup.raw.code-fence.objc.markdown-gfm punctuation.definition.raw.code-fence.begin.markdown
3: markup.raw.code-fence.objc.markdown-gfm constant.other.language-name.markdown
embed: scope:source.objc
embed_scope: markup.raw.code-fence.objc.markdown-gfm
escape: '{{code_fence_escape}}'
escape_captures:
0: markup.raw.code-fence.objc.markdown-gfm
1: punctuation.definition.raw.code-fence.end.markdown
- match: |-
(?x)
{{fenced_code_block_start}}
((?i:objc\+\+|obj-c\+\+|objectivec\+\+|objective-c\+\+))
\s*$ # any whitespace until EOL
captures:
2: markup.raw.code-fence.objc++.markdown-gfm punctuation.definition.raw.code-fence.begin.markdown
3: markup.raw.code-fence.objc++.markdown-gfm constant.other.language-name.markdown
embed: scope:source.objc++
embed_scope: markup.raw.code-fence.objc++.markdown-gfm
escape: '{{code_fence_escape}}'
escape_captures:
0: markup.raw.code-fence.objc++.markdown-gfm
1: punctuation.definition.raw.code-fence.end.markdown
- match: |-
(?x)
{{fenced_code_block_start}}
((?i:c))
\s*$ # any whitespace until EOL
captures:
2: markup.raw.code-fence.c.markdown-gfm punctuation.definition.raw.code-fence.begin.markdown
3: markup.raw.code-fence.c.markdown-gfm constant.other.language-name.markdown
embed: scope:source.c
embed_scope: markup.raw.code-fence.c.markdown-gfm
escape: '{{code_fence_escape}}'
escape_captures:
0: markup.raw.code-fence.c.markdown-gfm
1: punctuation.definition.raw.code-fence.end.markdown
- match: |-
(?x)
{{fenced_code_block_start}}
((?i:c\+\+|cpp))
\s*$ # any whitespace until EOL
captures:
2: markup.raw.code-fence.c++.markdown-gfm punctuation.definition.raw.code-fence.begin.markdown
3: markup.raw.code-fence.c++.markdown-gfm constant.other.language-name.markdown
embed: scope:source.c++
embed_scope: markup.raw.code-fence.c++.markdown-gfm
escape: '{{code_fence_escape}}'
escape_captures:
0: markup.raw.code-fence.c++.markdown-gfm
1: punctuation.definition.raw.code-fence.end.markdown
- match: |-
(?x)
{{fenced_code_block_start}}
((?i:regexp|regex))
\s*$ # any whitespace until EOL
captures:
1: markup.raw.code-fence.regexp.markdown-gfm punctuation.definition.raw.code-fence.begin.markdown
2: markup.raw.code-fence.regexp.markdown-gfm constant.other.language-name.markdown
embed: scope:source.regexp
embed_scope: markup.raw.code-fence.regexp.markdown-gfm
escape: '^[ ]{,3}(\1)\s*$'
escape_captures:
0: markup.raw.code-fence.regexp.markdown-gfm
1: punctuation.definition.raw.code-fence.end.markdown
- match: |-
(?x)
{{fenced_code_block_start}}
([\w-]*) # any number of word characters or dashes
.*$ # all characters until eol
.*$ # all characters until EOL
captures:
1: punctuation.definition.raw.code-fence.begin.markdown
2: constant.other.language-name.markdown
2: punctuation.definition.raw.code-fence.begin.markdown
3: constant.other.language-name.markdown
push:
- meta_scope: markup.raw.code-fence.markdown-gfm
Copy link
Collaborator

@FichteFoll FichteFoll Mar 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the same reason, this should be meta_content_scope. And if it would otherwise match on the newline, the match probably needs to include \n also, but I'm not sure about that.

- match: '[ ]{,3}\1\s*$'
scope: punctuation.definition.raw.code-fence.end.markdown
- match: '{{code_fence_escape}}'
captures:
1: punctuation.definition.raw.code-fence.end.markdown
pop: true
code-span:
- match: (`+)(?!`)
Expand Down
Loading