From e1bebf60e48cb5ef9142ee02ef53c4baa28b1d15 Mon Sep 17 00:00:00 2001 From: Martin Aeschlimann Date: Mon, 19 Oct 2015 21:22:10 +0200 Subject: [PATCH] Support nested repositories --- release/main.js | 19 +- src/rule.ts | 5 +- src/types.ts | 2 + src/utils.ts | 9 + test-cases/suite1/fixtures/markdown.plist | 1108 +++++++++++++++++++++ test-cases/suite1/tests.json | 73 ++ 6 files changed, 1213 insertions(+), 3 deletions(-) create mode 100644 test-cases/suite1/fixtures/markdown.plist diff --git a/release/main.js b/release/main.js index 6229af9..cc0b38b 100644 --- a/release/main.js +++ b/release/main.js @@ -49,6 +49,19 @@ function cloneObj(obj) { } return r; } +function mergeObjects(target) { + var sources = []; + for (var _i = 1; _i < arguments.length; _i++) { + sources[_i - 1] = arguments[_i]; + } + sources.forEach(function (source) { + for (var key in source) { + target[key] = source[key]; + } + }); + return target; +} +exports.mergeObjects = mergeObjects; var CAPTURING_REGEX_SOURCE = /\$(\d+)|\${(\d+):\/(downcase|upcase)}/; var RegexSource = (function () { function RegexSource() { @@ -279,8 +292,7 @@ $load('./rule', function(require, module, exports) { var __extends = (this && this.__extends) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; function __() { this.constructor = d; } - __.prototype = b.prototype; - d.prototype = new __(); + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; var utils_1 = require('./utils'); var BACK_REFERENCING_END = /\\(\d+)/; @@ -664,6 +676,9 @@ var RuleFactory = (function () { return new MatchRule(desc.id, desc.name, desc.match, RuleFactory._compileCaptures(desc.captures, helper, repository)); } if (!desc.begin) { + if (desc.repository) { + repository = utils_1.mergeObjects({}, repository, desc.repository); + } return new IncludeOnlyRule(desc.id, desc.name, desc.contentName, RuleFactory._compilePatterns(desc.patterns, helper, repository)); } return new BeginEndRule(desc.id, desc.name, desc.contentName, desc.begin, RuleFactory._compileCaptures(desc.beginCaptures || desc.captures, helper, repository), desc.end, RuleFactory._compileCaptures(desc.endCaptures || desc.captures, helper, repository), desc.applyEndPatternLast, RuleFactory._compilePatterns(desc.patterns, helper, repository)); diff --git a/src/rule.ts b/src/rule.ts index dd1612a..f67208c 100644 --- a/src/rule.ts +++ b/src/rule.ts @@ -3,7 +3,7 @@ *--------------------------------------------------------*/ 'use strict'; -import {RegexSource} from './utils'; +import {RegexSource, mergeObjects} from './utils'; import {IRawGrammar, IRawRepository, IRawRule, IRawPattern, IRawCaptures} from './types'; import {OnigScanner, IOnigCaptureIndex} from 'oniguruma'; @@ -502,6 +502,9 @@ export class RuleFactory { } if (!desc.begin) { + if (desc.repository) { + repository = mergeObjects({}, repository, desc.repository); + } return new IncludeOnlyRule( desc.id, desc.name, diff --git a/src/types.ts b/src/types.ts index 1e7a46b..d30ba8d 100644 --- a/src/types.ts +++ b/src/types.ts @@ -39,6 +39,8 @@ export interface IRawRule { endCaptures?: IRawCaptures; patterns?: IRawPattern[]; + repository?: IRawRepository; + applyEndPatternLast?:boolean; } diff --git a/src/utils.ts b/src/utils.ts index c7b6967..d5ccb20 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -35,6 +35,15 @@ function cloneObj(obj:any): any { return r; } +export function mergeObjects(target:any, ...sources:any[]): any { + sources.forEach(source => { + for (let key in source) { + target[key] = source[key]; + } + }); + return target; +} + let CAPTURING_REGEX_SOURCE = /\$(\d+)|\${(\d+):\/(downcase|upcase)}/; export class RegexSource { diff --git a/test-cases/suite1/fixtures/markdown.plist b/test-cases/suite1/fixtures/markdown.plist new file mode 100644 index 0000000..8fed5b1 --- /dev/null +++ b/test-cases/suite1/fixtures/markdown.plist @@ -0,0 +1,1108 @@ + + + + + fileTypes + + md + mdown + markdown + markdn + + keyEquivalent + ^~M + name + Markdown + patterns + + + include + #block + + + repository + + block + + patterns + + + include + #separator + + + include + #heading + + + include + #blockquote + + + include + #lists + + + include + #raw_block + + + include + #link-def + + + include + #html + + + include + #paragraph + + + repository + + blockquote + + begin + (^|\G)(>) ? + captures + + 2 + + name + punctuation.definition.quote.markdown + + + name + markup.quote.markdown + patterns + + + include + #block + + + while + (^|\G)(>) ? + + heading + + begin + (?:^|\G)(#{1,6})\s*(?=[\S[^#]]) + captures + + 1 + + name + punctuation.definition.heading.markdown + + + contentName + entity.name.section.markdown + end + \s*(#{1,6})?$\n? + name + markup.heading.${1/(#)(#)?(#)?(#)?(#)?(#)?/${6:?6:${5:?5:${4:?4:${3:?3:${2:?2:1}}}}}/}.markdown + patterns + + + include + #inline + + + + heading-setext + + patterns + + + match + ^(={3,})(?=[ \t]*$\n?) + name + markup.heading.setext.1.markdown + + + match + ^(-{3,})(?=[ \t]*$\n?) + name + markup.heading.setext.2.markdown + + + + html + + patterns + + + begin + (?i)(^|\G)(?=<(p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math|ins|del).*</\2\s*>\s*$) + end + $ + patterns + + + include + text.html.basic + + + + + begin + (?i)(^|\G)(?=<(p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math|ins|del)) + patterns + + + include + text.html.basic + + + while + \G(?!</\2\s*>) + + + + link-def + + captures + + 1 + + name + punctuation.definition.constant.markdown + + 10 + + name + punctuation.definition.string.end.markdown + + 11 + + name + string.other.link.description.title.markdown + + 12 + + name + punctuation.definition.string.begin.markdown + + 13 + + name + punctuation.definition.string.end.markdown + + 2 + + name + constant.other.reference.link.markdown + + 3 + + name + punctuation.definition.constant.markdown + + 4 + + name + punctuation.separator.key-value.markdown + + 5 + + name + punctuation.definition.link.markdown + + 6 + + name + markup.underline.link.markdown + + 7 + + name + punctuation.definition.link.markdown + + 8 + + name + string.other.link.description.title.markdown + + 9 + + name + punctuation.definition.string.begin.markdown + + + match + ^(?x: + \s* # Leading whitespace + (\[)(.+?)(\])(:) # Reference name + [ \t]* # Optional whitespace + (<?)(\S+?)(>?) # The url + [ \t]* # Optional whitespace + (?: + ((\().+?(\))) # Match title in quotes… + | ((").+?(")) # or in parens. + )? # Title is optional + \s* # Optional whitespace + $ + ) + name + meta.link.reference.def.markdown + + list_paragraph + + begin + (^|\G)(?=\S)(?![*+-]\s|[0-9]+\.\s) + name + meta.paragraph.markdown + patterns + + + include + #inline + + + include + text.html.basic + + + include + #heading-setext + + + while + (^|\G)(?!\s*$|#|[ ]{0,3}([-*_][ ]{2,}){3,}[ \t]*$\n?|>|[ ]{0,3}[*+-]|[ ]{0,3}[0-9]+\.) + + lists + + patterns + + + begin + (^|\G)([ ]{0,3})([*+-])([ ]{1,3}|\t) + beginCaptures + + 3 + + name + punctuation.definition.list.markdown + + + comment + Currently does not support un-indented second lines. + name + markup.list.unnumbered.markdown + patterns + + + include + #list_paragraph + + + include + #block + + + while + \G([ ]{4}|\t|$) + + + begin + (^|\G)([ ]{0,3})([0-9]+\.)([ ]{1,3}|\t) + beginCaptures + + 3 + + name + punctuation.definition.list.markdown + + + name + markup.list.numbered.markdown + patterns + + + include + #list_paragraph + + + include + #block + + + while + \G([ ]{4}|\t|$) + + + + paragraph + + begin + (^|\G)(?=\S) + name + meta.paragraph.markdown + patterns + + + include + #inline + + + include + text.html.basic + + + include + #heading-setext + + + while + (^|\G)(?!\s*$|#|[ ]{0,3}([-*_][ ]{2,}){3,}[ \t]*$\n?|\s*\[.+?\]:|>) + + raw_block + + begin + (^|\G)([ ]{4}|\t) + name + markup.raw.block.markdown + while + (^|\G)([ ]{4}|\t) + + separator + + match + (^|\G)[ ]{0,3}([-*_])([ ]{0,2}\2){2,}[ \t]*$\n? + name + meta.separator.markdown + + + + inline + + patterns + + + include + #ampersand + + + include + #bracket + + + include + #bold + + + include + #italic + + + include + #raw + + + include + #escape + + + include + #image-inline + + + include + #image-ref + + + include + #link-email + + + include + #link-inet + + + include + #link-inline + + + include + #link-ref + + + include + #link-ref-literal + + + repository + + ampersand + + comment + + Markdown will convert this for us. We match it so that the + HTML grammar will not mark it up as invalid. + + match + &(?!([a-zA-Z0-9]+|#[0-9]+|#x[0-9a-fA-F]+);) + name + meta.other.valid-ampersand.markdown + + bold + + begin + (?x) + (\*\*|__)(?=\S) # Open + (?= + ( + <[^>]*+> # HTML tags + | (?<raw>`+)([^`]|(?!(?<!`)\k<raw>(?!`))`)*+\k<raw> + # Raw + | \\[\\`*_{}\[\]()#.!+\->]?+ # Escapes + | \[ + ( + (?<square> # Named group + [^\[\]\\] # Match most chars + | \\. # Escaped chars + | \[ \g<square>*+ \] # Nested brackets + )*+ + \] + ( + ( # Reference Link + [ ]? # Optional space + \[[^\]]*+\] # Ref name + ) + | ( # Inline Link + \( # Opening paren + [ \t]*+ # Optional whtiespace + <?(.*?)>? # URL + [ \t]*+ # Optional whtiespace + ( # Optional Title + (?<title>['"]) + (.*?) + \k<title> + )? + \) + ) + ) + ) + | (?!(?<=\S)\1). # Everything besides + # style closer + )++ + (?<=\S)\1 # Close + ) + + captures + + 1 + + name + punctuation.definition.bold.markdown + + + end + (?<=\S)(\1) + name + markup.bold.markdown + patterns + + + applyEndPatternLast + 1 + begin + (?=<[^>]*?>) + end + (?<=>) + patterns + + + include + text.html.basic + + + + + include + #escape + + + include + #ampersand + + + include + #bracket + + + include + #raw + + + include + #italic + + + include + #image-inline + + + include + #link-inline + + + include + #link-inet + + + include + #link-email + + + include + #image-ref + + + include + #link-ref-literal + + + include + #link-ref + + + + bracket + + comment + + Markdown will convert this for us. We match it so that the + HTML grammar will not mark it up as invalid. + + match + <(?![a-z/?\$!]) + name + meta.other.valid-bracket.markdown + + escape + + match + \\[-`*_#+.!(){}\[\]\\>] + name + constant.character.escape.markdown + + image-inline + + captures + + 1 + + name + punctuation.definition.string.begin.markdown + + 10 + + name + string.other.link.description.title.markdown + + 11 + + name + punctuation.definition.string.markdown + + 12 + + name + punctuation.definition.string.markdown + + 13 + + name + string.other.link.description.title.markdown + + 14 + + name + punctuation.definition.string.markdown + + 15 + + name + punctuation.definition.string.markdown + + 16 + + name + punctuation.definition.metadata.markdown + + 2 + + name + string.other.link.description.markdown + + 4 + + name + punctuation.definition.string.end.markdown + + 5 + + name + invalid.illegal.whitespace.markdown + + 6 + + name + punctuation.definition.metadata.markdown + + 7 + + name + punctuation.definition.link.markdown + + 8 + + name + markup.underline.link.image.markdown + + 9 + + name + punctuation.definition.link.markdown + + + match + (?x: + \! # Images start with ! + (\[)((?<square>[^\[\]\\]|\\.|\[\g<square>*+\])*+)(\]) + # Match the link text. + ([ ])? # Space not allowed + (\() # Opening paren for url + (<?)(\S+?)(>?) # The url + [ \t]* # Optional whitespace + (?: + ((\().+?(\))) # Match title in parens… + | ((").+?(")) # or in quotes. + )? # Title is optional + \s* # Optional whitespace + (\)) + ) + name + meta.image.inline.markdown + + image-ref + + captures + + 1 + + name + punctuation.definition.string.begin.markdown + + 2 + + name + string.other.link.description.markdown + + 4 + + name + punctuation.definition.string.begin.markdown + + 5 + + name + punctuation.definition.constant.markdown + + 6 + + name + constant.other.reference.link.markdown + + 7 + + name + punctuation.definition.constant.markdown + + + match + \!(\[)((?<square>[^\[\]\\]|\\.|\[\g<square>*+\])*+)(\])[ ]?(\[)(.*?)(\]) + name + meta.image.reference.markdown + + italic + + begin + (?x) + (\*|_)(?=\S) # Open + (?= + ( + <[^>]*+> # HTML tags + | (?<raw>`+)([^`]|(?!(?<!`)\k<raw>(?!`))`)*+\k<raw> + # Raw + | \\[\\`*_{}\[\]()#.!+\->]?+ # Escapes + | \[ + ( + (?<square> # Named group + [^\[\]\\] # Match most chars + | \\. # Escaped chars + | \[ \g<square>*+ \] # Nested brackets + )*+ + \] + ( + ( # Reference Link + [ ]? # Optional space + \[[^\]]*+\] # Ref name + ) + | ( # Inline Link + \( # Opening paren + [ \t]*+ # Optional whtiespace + <?(.*?)>? # URL + [ \t]*+ # Optional whtiespace + ( # Optional Title + (?<title>['"]) + (.*?) + \k<title> + )? + \) + ) + ) + ) + | \1\1 # Must be bold closer + | (?!(?<=\S)\1). # Everything besides + # style closer + )++ + (?<=\S)\1 # Close + ) + + captures + + 1 + + name + punctuation.definition.italic.markdown + + + end + (?<=\S)(\1)((?!\1)|(?=\1\1)) + name + markup.italic.markdown + patterns + + + applyEndPatternLast + 1 + begin + (?=<[^>]*?>) + end + (?<=>) + patterns + + + include + text.html.basic + + + + + include + #escape + + + include + #ampersand + + + include + #bracket + + + include + #raw + + + include + #bold + + + include + #image-inline + + + include + #link-inline + + + include + #link-inet + + + include + #link-email + + + include + #image-ref + + + include + #link-ref-literal + + + include + #link-ref + + + + link-email + + captures + + 1 + + name + punctuation.definition.link.markdown + + 2 + + name + markup.underline.link.markdown + + 4 + + name + punctuation.definition.link.markdown + + + match + (<)((?:mailto:)?[-.\w]+@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)(>) + name + meta.link.email.lt-gt.markdown + + link-inet + + captures + + 1 + + name + punctuation.definition.link.markdown + + 2 + + name + markup.underline.link.markdown + + 3 + + name + punctuation.definition.link.markdown + + + match + (<)((?:https?|ftp)://.*?)(>) + name + meta.link.inet.markdown + + link-inline + + captures + + 1 + + name + punctuation.definition.string.begin.markdown + + 10 + + name + string.other.link.description.title.markdown + + 11 + + name + punctuation.definition.string.begin.markdown + + 12 + + name + punctuation.definition.string.end.markdown + + 13 + + name + string.other.link.description.title.markdown + + 14 + + name + punctuation.definition.string.begin.markdown + + 15 + + name + punctuation.definition.string.end.markdown + + 16 + + name + punctuation.definition.metadata.markdown + + 2 + + name + string.other.link.title.markdown + + 4 + + name + punctuation.definition.string.end.markdown + + 5 + + name + invalid.illegal.whitespace.markdown + + 6 + + name + punctuation.definition.metadata.markdown + + 7 + + name + punctuation.definition.link.markdown + + 8 + + name + markup.underline.link.markdown + + 9 + + name + punctuation.definition.link.markdown + + + match + (?x: + (\[)((?<square>[^\[\]\\]|\\.|\[\g<square>*+\])*+)(\]) + # Match the link text. + ([ ])? # Space not allowed + (\() # Opening paren for url + (<?)(.*?)(>?) # The url + [ \t]* # Optional whitespace + (?: + ((\().+?(\))) # Match title in parens… + | ((").+?(")) # or in quotes. + )? # Title is optional + \s* # Optional whitespace + (\)) + ) + name + meta.link.inline.markdown + + link-ref + + captures + + 1 + + name + punctuation.definition.string.begin.markdown + + 2 + + name + string.other.link.title.markdown + + 4 + + name + punctuation.definition.string.end.markdown + + 5 + + name + punctuation.definition.constant.begin.markdown + + 6 + + name + constant.other.reference.link.markdown + + 7 + + name + punctuation.definition.constant.end.markdown + + + match + (\[)((?<square>[^\[\]\\]|\\.|\[\g<square>*+\])*+)(\])[ ]?(\[)([^\]]*+)(\]) + name + meta.link.reference.markdown + + link-ref-literal + + captures + + 1 + + name + punctuation.definition.string.begin.markdown + + 2 + + name + string.other.link.title.markdown + + 4 + + name + punctuation.definition.string.end.markdown + + 5 + + name + punctuation.definition.constant.begin.markdown + + 6 + + name + punctuation.definition.constant.end.markdown + + + match + (\[)((?<square>[^\[\]\\]|\\.|\[\g<square>*+\])*+)(\])[ ]?(\[)(\]) + name + meta.link.reference.literal.markdown + + raw + + captures + + 1 + + name + punctuation.definition.raw.markdown + + 2 + + 3 + + name + punctuation.definition.raw.markdown + + + match + (`+)([^`]|(?!(?<!`)\1(?!`))`)*+(\1) + name + markup.raw.inline.markdown + + + + + scopeName + text.html.markdown + uuid + 0A1D9874-B448-11D9-BD50-000D93B6E43C + + \ No newline at end of file diff --git a/test-cases/suite1/tests.json b/test-cases/suite1/tests.json index 8c06d9a..446716e 100644 --- a/test-cases/suite1/tests.json +++ b/test-cases/suite1/tests.json @@ -68,5 +68,78 @@ ] } ] + }, + { + "grammars": [ + "fixtures/markdown.plist" + ], + "grammarPath": "fixtures/markdown.plist", + "lines": [ + { + "line": "## This is *great* stuff", + "tokens": [ + { + "value": "##", + "scopes": [ + "text.html.markdown", + "markup.heading.${1/(#)(#)?(#)?(#)?(#)?(#)?/${6:?6:${5:?5:${4:?4:${3:?3:${2:?2:1}}}}}/}.markdown", + "punctuation.definition.heading.markdown" + ] + }, + { + "value": " ", + "scopes": [ + "text.html.markdown", + "markup.heading.${1/(#)(#)?(#)?(#)?(#)?(#)?/${6:?6:${5:?5:${4:?4:${3:?3:${2:?2:1}}}}}/}.markdown" + ] + }, + { + "value": "This is ", + "scopes": [ + "text.html.markdown", + "markup.heading.${1/(#)(#)?(#)?(#)?(#)?(#)?/${6:?6:${5:?5:${4:?4:${3:?3:${2:?2:1}}}}}/}.markdown", + "entity.name.section.markdown" + ] + }, + { + "value": "*", + "scopes": [ + "text.html.markdown", + "markup.heading.${1/(#)(#)?(#)?(#)?(#)?(#)?/${6:?6:${5:?5:${4:?4:${3:?3:${2:?2:1}}}}}/}.markdown", + "entity.name.section.markdown", + "markup.italic.markdown", + "punctuation.definition.italic.markdown" + ] + }, + { + "value": "great", + "scopes": [ + "text.html.markdown", + "markup.heading.${1/(#)(#)?(#)?(#)?(#)?(#)?/${6:?6:${5:?5:${4:?4:${3:?3:${2:?2:1}}}}}/}.markdown", + "entity.name.section.markdown", + "markup.italic.markdown" + ] + }, + { + "value": "*", + "scopes": [ + "text.html.markdown", + "markup.heading.${1/(#)(#)?(#)?(#)?(#)?(#)?/${6:?6:${5:?5:${4:?4:${3:?3:${2:?2:1}}}}}/}.markdown", + "entity.name.section.markdown", + "markup.italic.markdown", + "punctuation.definition.italic.markdown" + ] + }, + { + "value": " stuff", + "scopes": [ + "text.html.markdown", + "markup.heading.${1/(#)(#)?(#)?(#)?(#)?(#)?/${6:?6:${5:?5:${4:?4:${3:?3:${2:?2:1}}}}}/}.markdown", + "entity.name.section.markdown" + ] + } + ] + } + ] } ] \ No newline at end of file