Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
Merge pull request #93 from atom/wl-comment-fixes
Browse files Browse the repository at this point in the history
Address multiple issues with comments and multiline tags
  • Loading branch information
50Wliu committed Sep 16, 2017
2 parents 2392040 + 4c27ed7 commit c0860d9
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 8 deletions.
36 changes: 28 additions & 8 deletions grammars/yaml.cson
Expand Up @@ -38,7 +38,7 @@
{
# hello: >
# hello: |
'begin': '^(\\s*)(?!-\\s*)(.*\\S)\\s*(:)(?:\\s+((!)[^!\\s]+))?\\s+(?=\\||>)'
'begin': '^(\\s*)(?!-\\s*)([^!@#%&*>,][^#]*\\S)\\s*(:)(?:\\s+((!)[^!\\s]+))?\\s+(?=\\||>)'
'beginCaptures':
'2':
'name': 'entity.name.tag.yaml'
Expand All @@ -48,9 +48,19 @@
'name': 'keyword.other.tag.local.yaml'
'5':
'name': 'punctuation.definition.tag.local.yaml'
'end': '^(((?!$)(?!\\1\\s+))|(?=\\1\\s*(-|[^!@#%&*>,].*:\\s+)))'
'end': '^(?!$)(?!\\1\\s+)'
'contentName': 'string.unquoted.block.yaml'
'patterns': [
{
# Comments can only appear on the same line as the tag name
'begin': '\\G'
'end': '$'
'patterns': [
{
'include': '#comment'
}
]
}
{
'include': '#constants'
}
Expand All @@ -64,23 +74,33 @@
# - |
# - hello: >
# - hello: |
'begin': '^(\\s*)(?:(-)|(?:(?:(-)\\s*)?(.*\\S)\\s*(:)))(?:\\s+((!)[^!\\s]+))?\\s+(?=\\||>)'
'begin': '^(\\s*)(?:(-)|(?:(?:(-)(\\s*))?([^!@#%&*>,][^#]*\\S)\\s*(:)))(?:\\s+((!)[^!\\s]+))?\\s+(?=\\||>)'
'beginCaptures':
'2':
'name': 'punctuation.definition.entry.yaml'
'3':
'name': 'punctuation.definition.entry.yaml'
'4':
'name': 'entity.name.tag.yaml'
'5':
'name': 'punctuation.separator.key-value.yaml'
'name': 'entity.name.tag.yaml'
'6':
'name': 'keyword.other.tag.local.yaml'
'name': 'punctuation.separator.key-value.yaml'
'7':
'name': 'keyword.other.tag.local.yaml'
'8':
'name': 'punctuation.definition.tag.local.yaml'
'end': '^(((?!$)(?!\\1\\s+))|(?=\\1\\s*(-|[^!@#%&*>,].*:\\s+)))'
'end': '^((?!$)(?!\\1\\s+)|(?=\\s\\4(-|[^\\s!@#%&*>,].*:\\s+)))'
'contentName': 'string.unquoted.block.yaml'
'patterns': [
{
# Comments can only appear on the same line as the tag name
'begin': '\\G'
'end': '$'
'patterns': [
{
'include': '#comment'
}
]
}
{
'include': '#constants'
}
Expand Down
33 changes: 33 additions & 0 deletions spec/yaml-spec.coffee
Expand Up @@ -265,6 +265,18 @@ describe "YAML grammar", ->
expect(lines[3][3]).toEqual value: " ", scopes: ["source.yaml"]
expect(lines[3][4]).toEqual value: "following text", scopes: ["source.yaml", "string.unquoted.yaml"]

it "ignores key-like structures in block content", ->
lines = grammar.tokenizeLines """
- textblock: >
am i a key: nope
text
"""
expect(lines[0][0]).toEqual value: "-", scopes: ["source.yaml", "punctuation.definition.entry.yaml"]
expect(lines[0][2]).toEqual value: "textblock", scopes: ["source.yaml", "entity.name.tag.yaml"]
expect(lines[0][3]).toEqual value: ":", scopes: ["source.yaml", "punctuation.separator.key-value.yaml"]
expect(lines[1][0]).toEqual value: " am i a key: nope", scopes: ["source.yaml", "string.unquoted.block.yaml"]
expect(lines[2][0]).toEqual value: " text", scopes: ["source.yaml", "string.unquoted.block.yaml"]

it "parses content even when not using | or >", ->
lines = grammar.tokenizeLines """
- textblock:
Expand Down Expand Up @@ -536,6 +548,27 @@ describe "YAML grammar", ->
expect(lines[4][1]).toEqual value: "String", scopes: ["source.yaml", "string.unquoted.yaml"]
expect(lines[5][0]).toEqual value: "#", scopes: ["source.yaml", "comment.line.number-sign.yaml", "punctuation.definition.comment.yaml"]

it "parses comments on the same line as a multiline tag", ->
{tokens} = grammar.tokenizeLine "# condition: >"
expect(tokens[0]).toEqual value: "#", scopes: ["source.yaml", "comment.line.number-sign.yaml", "punctuation.definition.comment.yaml"]

{tokens} = grammar.tokenizeLine "condition: > # comment"
expect(tokens[4]).toEqual value: "#", scopes: ["source.yaml", "string.unquoted.block.yaml", "comment.line.number-sign.yaml", "punctuation.definition.comment.yaml"]

it "ignores comments in proper multiline tags", ->
lines = grammar.tokenizeLines """
multiline: >
This should still be a string # not a comment!
Ditto
# Guess what this is
String
# comment
"""

expect(lines[1][0]).toEqual value: " This should still be a string # not a comment!", scopes: ["source.yaml", "string.unquoted.block.yaml"]
expect(lines[3][0]).toEqual value: " # Guess what this is", scopes: ["source.yaml", "string.unquoted.block.yaml"]
expect(lines[5][0]).toEqual value: "#", scopes: ["source.yaml", "comment.line.number-sign.yaml", "punctuation.definition.comment.yaml"]

it "does not confuse keys and comments", ->
{tokens} = grammar.tokenizeLine("- Entry 2 # This colon breaks syntax highlighting: see?")
expect(tokens[0]).toEqual value: "-", scopes: ["source.yaml", "punctuation.definition.entry.yaml"]
Expand Down

0 comments on commit c0860d9

Please sign in to comment.