Skip to content

Commit

Permalink
rewrite comments
Browse files Browse the repository at this point in the history
Fixes: #260
  • Loading branch information
matter123 committed Jun 26, 2019
1 parent bb966d7 commit b140eae
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 126 deletions.
5 changes: 3 additions & 2 deletions language_tags/cpp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ comment.block.preprocessor.else-branch.cpp
comment.block.preprocessor.else-branch.in-block.cpp
comment.block.preprocessor.if-branch.cpp
comment.block.preprocessor.if-branch.in-block.cpp
comment.line.banner.cpp
comment.line.block.cpp
comment.line.double-slash.cpp
constant.character.escape.cpp
constant.character.escape.line-continuation.cpp
Expand Down Expand Up @@ -397,6 +397,7 @@ meta.arguments.operator.noexcept.cpp
meta.arguments.operator.sizeof.cpp
meta.arguments.operator.typeid.cpp
meta.asm.cpp
meta.banner.character.cpp
meta.block.class.cpp
meta.block.cpp
meta.block.enum.cpp
Expand Down Expand Up @@ -475,7 +476,7 @@ meta.tail.union.cpp
meta.template.call.cpp
meta.template.definition.cpp
meta.toc-list.banner.block.cpp
meta.toc-list.banner.line.cpp
meta.toc-list.banner.double-slash.cpp
meta.using-namespace.cpp
punctuation.accessor.attribute.cpp
punctuation.definition.begin.bracket.square.array.type.cpp
Expand Down
161 changes: 98 additions & 63 deletions source/languages/cpp/generate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2327,69 +2327,104 @@ def generateBlockFinder( name:"", tag_as:"", start_pattern:nil, needs_semicolon:
),
includes: [:function_body_context]
)
cpp_grammar[:comments_context] = {
patterns: [
{
captures: {
"1" => {
name: "meta.toc-list.banner.block"
}
},
match: "^/\\* =(\\s*.*?)\\s*= \\*/$\\n?",
name: "comment.block"
},
{
begin: "/\\*",
beginCaptures: {
"0" => {
name: "punctuation.definition.comment.begin"
}
},
end: "\\*/",
endCaptures: {
"0" => {
name: "punctuation.definition.comment.end"
}
},
name: "comment.block"
},
{
captures: {
"1" => {
name: "meta.toc-list.banner.line"
}
},
match: "^// =(\\s*.*?)\\s*=\\s*$\\n?",
name: "comment.line.banner"
},
{
begin: "(^[ \\t]+)?(?=//)",
beginCaptures: {
"1" => {
name: "punctuation.whitespace.comment.leading"
}
},
end: "(?!\\G)",
patterns: [
{
begin: "//",
beginCaptures: {
"0" => {
name: "punctuation.definition.comment"
}
},
end: "(?=\\n)",
name: "comment.line.double-slash",
patterns: [
{
include: "#line_continuation_character"
}
]
}
]
}
]
}
cpp_grammar[:comments_context] = [
#
# file banner
# this matches emacs style file banners ex: /* = foo.c = */
# a file banner is a <comment start> <some spaces> <banner start> <some spaces>
# <comment contents> <banner end> <some spaces> <comment end>
# single line
newPattern(
should_fully_match: ["// ### test.c ###", "//=test.c - test util ="],
should_not_partial_match: ["// ### test.c #=#", "//=test.c - test util ~~~"],
match: /^/.maybe(@spaces).then(
match: newPattern(
match: /\/\//,
tag_as: "punctuation.definition.comment"
).maybe(
match: @spaces,
).then(
match: oneOrMoreOf(match: /[#;\/=*C~]+/, dont_back_track?: true).lookAheadToAvoid(/[#;\/=*C~]/),
tag_as: "meta.banner.character",
reference: "banner_part"
).maybe(@spaces).then(/.+/).maybe(@spaces).backReference("banner_part")
.maybe(@spaces).then(/(?:\n|$)/),
tag_as: "comment.line.double-slash",
),
# tag is a legacy name
tag_as: "meta.toc-list.banner.double-slash",
),
# multi line comment
newPattern(
should_fully_match: ["/* ### test.c ###*/", "/*=test.c - test util =*/"],
should_not_partial_match: ["/* ### test.c #=# */", "/*=test.c - test util ~~~*/"],
match: /^/.maybe(@spaces).then(
match: newPattern(
match: /\/\*/,
tag_as: "punctuation.definition.comment"
).maybe(
match: @spaces,
quantity_preference: :as_few_as_possible
).then(
match: oneOrMoreOf(match: /[#;\/=*C~]+/, dont_back_track?: true).lookAheadToAvoid(/[#;\/=*C~]/),
tag_as: "meta.banner.character",
reference: "banner_part"
).maybe(@spaces).then(/.+/).maybe(@spaces).backReference("banner_part")
.maybe(@spaces).then(/\*\//),
tag_as: "comment.line.block",
),
# tag is a legacy name
tag_as: "meta.toc-list.banner.block",
),
#
# single line comment
#
PatternRange.new(
start_pattern: maybe(
# consumes extra space character
match: newPattern(
match: @spaces,
dont_back_track?: true
),
tag_as: "punctuation.whitespace.comment.leading"
).lookBehindToAvoid(/[\\]/).then(
match: /\/\//,
tag_as: "comment.line.double-slash punctuation.definition.comment"
),
end_pattern: lookBehindToAvoid(/[\\]/).then(/\n/),
tag_content_as: "comment.line.double-slash",
includes: [
:line_continuation_characterm
]
),
#
# multi line comment
#
PatternRange.new(
start_pattern: newPattern(
should_fully_match: [" /*", "/*"],
should_partial_match: ["/***/"],
match: maybe(
# consumes extra space character
match: newPattern(
match: @spaces,
dont_back_track?: true
),
tag_as: "punctuation.whitespace.comment.leading"
).lookBehindToAvoid(/[\\]/).then(
match: /\/\*/,
tag_as: "comment.block punctuation.definition.comment.begin"
)),
end_pattern: lookBehindToAvoid(/[\\]/).then(
match: /\*\//,
tag_as: "comment.block punctuation.definition.comment.end"
),
tag_content_as: "comment.block",
includes: [
:line_continuation_character
]
),
]
cpp_grammar[:disabled] = {
begin: "^\\s*#\\s*if(n?def)?\\b.*$",
end: "^\\s*#\\s*endif\\b",
Expand Down
80 changes: 46 additions & 34 deletions syntaxes/cpp.tmLanguage.json
Original file line number Diff line number Diff line change
Expand Up @@ -9951,61 +9951,73 @@
"comments_context": {
"patterns": [
{
"match": "^\\s*((\\/\\/)\\s*((?>[#;\\/=*C~]+)(?![#;\\/=*C~]))\\s*.+\\s*\\3\\s*(?:\\n|$))",
"captures": {
"1": {
"name": "meta.toc-list.banner.block.cpp"
"name": "comment.line.double-slash.cpp"
},
"2": {
"name": "punctuation.definition.comment.cpp"
},
"3": {
"name": "meta.banner.character.cpp"
}
},
"match": "^/\\* =(\\s*.*?)\\s*= \\*/$\\n?",
"name": "comment.block.cpp"
"name": "meta.toc-list.banner.double-slash.cpp"
},
{
"begin": "/\\*",
"beginCaptures": {
"0": {
"name": "punctuation.definition.comment.begin.cpp"
}
},
"end": "\\*/",
"endCaptures": {
"0": {
"name": "punctuation.definition.comment.end.cpp"
"match": "^\\s*((\\/\\*)\\s*?((?>[#;\\/=*C~]+)(?![#;\\/=*C~]))\\s*.+\\s*\\3\\s*\\*\\/)",
"captures": {
"1": {
"name": "comment.line.block.cpp"
},
"2": {
"name": "punctuation.definition.comment.cpp"
},
"3": {
"name": "meta.banner.character.cpp"
}
},
"name": "comment.block.cpp"
"name": "meta.toc-list.banner.block.cpp"
},
{
"captures": {
"contentName": "comment.line.double-slash.cpp",
"begin": "((?>\\s+)?)(?<![\\\\])(\\/\\/)",
"beginCaptures": {
"1": {
"name": "meta.toc-list.banner.line.cpp"
"name": "punctuation.whitespace.comment.leading.cpp"
},
"2": {
"name": "comment.line.double-slash.cpp punctuation.definition.comment.cpp"
}
},
"match": "^// =(\\s*.*?)\\s*=\\s*$\\n?",
"name": "comment.line.banner.cpp"
"end": "(?<![\\\\])\\n",
"patterns": [
{
"include": "#line_continuation_characterm"
}
]
},
{
"begin": "(^[ \\t]+)?(?=//)",
"contentName": "comment.block.cpp",
"begin": "(((?>\\s+)?)(?<![\\\\])(\\/\\*))",
"beginCaptures": {
"1": {
"2": {
"name": "punctuation.whitespace.comment.leading.cpp"
},
"3": {
"name": "comment.block.cpp punctuation.definition.comment.begin.cpp"
}
},
"end": "(?<![\\\\])(\\*\\/)",
"endCaptures": {
"1": {
"name": "comment.block.cpp punctuation.definition.comment.end.cpp"
}
},
"end": "(?!\\G)",
"patterns": [
{
"begin": "//",
"beginCaptures": {
"0": {
"name": "punctuation.definition.comment.cpp"
}
},
"end": "(?=\\n)",
"name": "comment.line.double-slash.cpp",
"patterns": [
{
"include": "#line_continuation_character"
}
]
"include": "#line_continuation_character"
}
]
}
Expand Down
62 changes: 35 additions & 27 deletions syntaxes/cpp.tmLanguage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5333,39 +5333,47 @@
- include: "#function_body_context"
comments_context:
patterns:
- captures:
- match: "^\\s*((\\/\\/)\\s*((?>[#;\\/=*C~]+)(?![#;\\/=*C~]))\\s*.+\\s*\\3\\s*(?:\\n|$))"
captures:
'1':
name: meta.toc-list.banner.block.cpp
match: "^/\\* =(\\s*.*?)\\s*= \\*/$\\n?"
name: comment.block.cpp
- begin: "/\\*"
beginCaptures:
'0':
name: punctuation.definition.comment.begin.cpp
end: "\\*/"
endCaptures:
'0':
name: punctuation.definition.comment.end.cpp
name: comment.block.cpp
- captures:
name: comment.line.double-slash.cpp
'2':
name: punctuation.definition.comment.cpp
'3':
name: meta.banner.character.cpp
name: meta.toc-list.banner.double-slash.cpp
- match: "^\\s*((\\/\\*)\\s*?((?>[#;\\/=*C~]+)(?![#;\\/=*C~]))\\s*.+\\s*\\3\\s*\\*\\/)"
captures:
'1':
name: meta.toc-list.banner.line.cpp
match: "^// =(\\s*.*?)\\s*=\\s*$\\n?"
name: comment.line.banner.cpp
- begin: "(^[ \\t]+)?(?=//)"
name: comment.line.block.cpp
'2':
name: punctuation.definition.comment.cpp
'3':
name: meta.banner.character.cpp
name: meta.toc-list.banner.block.cpp
- contentName: comment.line.double-slash.cpp
begin: "((?>\\s+)?)(?<![\\\\])(\\/\\/)"
beginCaptures:
'1':
name: punctuation.whitespace.comment.leading.cpp
end: "(?!\\G)"
'2':
name: comment.line.double-slash.cpp punctuation.definition.comment.cpp
end: "(?<![\\\\])\\n"
patterns:
- begin: "//"
beginCaptures:
'0':
name: punctuation.definition.comment.cpp
end: "(?=\\n)"
name: comment.line.double-slash.cpp
patterns:
- include: "#line_continuation_character"
- include: "#line_continuation_characterm"
- contentName: comment.block.cpp
begin: "(((?>\\s+)?)(?<![\\\\])(\\/\\*))"
beginCaptures:
'2':
name: punctuation.whitespace.comment.leading.cpp
'3':
name: comment.block.cpp punctuation.definition.comment.begin.cpp
end: "(?<![\\\\])(\\*\\/)"
endCaptures:
'1':
name: comment.block.cpp punctuation.definition.comment.end.cpp
patterns:
- include: "#line_continuation_character"
disabled:
begin: "^\\s*#\\s*if(n?def)?\\b.*$"
end: "^\\s*#\\s*endif\\b"
Expand Down

0 comments on commit b140eae

Please sign in to comment.