diff --git a/language_tags/cpp.txt b/language_tags/cpp.txt index f0ff4ef2..32fa6c81 100644 --- a/language_tags/cpp.txt +++ b/language_tags/cpp.txt @@ -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 @@ -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 @@ -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 diff --git a/source/languages/cpp/generate.rb b/source/languages/cpp/generate.rb index c6074639..77785c23 100644 --- a/source/languages/cpp/generate.rb +++ b/source/languages/cpp/generate.rb @@ -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 + # + # 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", diff --git a/syntaxes/cpp.tmLanguage.json b/syntaxes/cpp.tmLanguage.json index 9d4d47c4..26fd8311 100644 --- a/syntaxes/cpp.tmLanguage.json +++ b/syntaxes/cpp.tmLanguage.json @@ -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+)?)(?\\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+)?)(?\\s+)?)(?