-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added doc comment highlighting (#1541)
This adds support for JavaDoc, JSDoc, TSDoc, and PHPDoc.
- Loading branch information
1 parent
bb62860
commit 493d19e
Showing
29 changed files
with
1,246 additions
and
6 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
(function (Prism) { | ||
|
||
var codeLines = { | ||
'code': { | ||
pattern: /(^(\s*(?:\*\s*)*)).*[^*\s].+$/m, | ||
lookbehind: true, | ||
inside: Prism.languages.java, | ||
alias: 'language-java' | ||
} | ||
}; | ||
|
||
Prism.languages.javadoc = Prism.languages.extend('javadoclike', {}); | ||
Prism.languages.insertBefore('javadoc', 'keyword', { | ||
'class-name': [ | ||
{ | ||
pattern: /(@(?:exception|throws|see|link|linkplain|value)\s+(?:[a-z\d]+\.)*)[A-Z](?:\w*[a-z]\w*)?(?:\.[A-Z](?:\w*[a-z]\w*)?)*/, | ||
lookbehind: true, | ||
inside: { | ||
'punctuation': /\./ | ||
} | ||
}, | ||
{ | ||
// @param <T> the first generic type parameter | ||
pattern: /(@param\s+)<[A-Z]\w*>/, | ||
lookbehind: true, | ||
inside: { | ||
'punctuation': /[.<>]/ | ||
} | ||
} | ||
], | ||
'namespace': { | ||
pattern: /(@(?:exception|throws|see|link|linkplain)\s+)(?:[a-z\d]+\.)+/, | ||
lookbehind: true, | ||
inside: { | ||
'punctuation': /\./ | ||
} | ||
}, | ||
'code-section': [ | ||
{ | ||
pattern: /(\{@code\s+)(?:[^{}]|\{[^{}]*\})+?(?=\s*\})/, | ||
lookbehind: true, | ||
inside: codeLines | ||
}, | ||
{ | ||
pattern: /(<(code|tt)>\s*)[\s\S]+?(?=\s*<\/\2>)/, | ||
lookbehind: true, | ||
inside: codeLines | ||
} | ||
], | ||
'tag': Prism.languages.markup.tag, | ||
}); | ||
|
||
Prism.languages.javadoclike.addSupport('java', Prism.languages.javadoc); | ||
}(Prism)); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
(function (Prism) { | ||
|
||
var javaDocLike = Prism.languages.javadoclike = { | ||
'parameter': { | ||
pattern: /(^\s*(?:\/{3}|\*|\/\*\*)\s*@(?:param|arg|arguments)\s+)\w+/m, | ||
lookbehind: true | ||
}, | ||
'keyword': { | ||
// keywords are the first word in a line preceded be an `@` or surrounded by curly braces. | ||
// @word, {@word} | ||
pattern: /(^\s*(?:\/{3}|\*|\/\*\*)\s*|\{)@[a-z][a-zA-Z-]+\b/m, | ||
lookbehind: true | ||
}, | ||
'punctuation': /[{}]/ | ||
}; | ||
|
||
|
||
/** | ||
* Adds doc comment support to the given language and calls a given callback on each doc comment pattern. | ||
* | ||
* @param {string} lang the language add doc comment support to. | ||
* @param {(pattern: {inside: {rest: undefined}}) => void} callback the function called with each doc comment pattern as argument. | ||
*/ | ||
function docCommentSupport(lang, callback) { | ||
var tokenName = 'doc-comment'; | ||
|
||
var grammar = Prism.languages[lang]; | ||
if (!grammar) { | ||
return; | ||
} | ||
var token = grammar[tokenName]; | ||
|
||
if (!token) { | ||
// add doc comment: /** */ | ||
var definition = {}; | ||
definition[tokenName] = { | ||
pattern: /(^|[^\\])\/\*\*[^/][\s\S]*?(?:\*\/|$)/, | ||
alias: 'comment' | ||
}; | ||
|
||
grammar = Prism.languages.insertBefore(lang, 'comment', definition); | ||
token = grammar[tokenName]; | ||
} | ||
|
||
if (token instanceof RegExp) { // convert regex to object | ||
token = grammar[tokenName] = { pattern: token }; | ||
} | ||
|
||
if (Prism.util.type(token) === 'Array') { | ||
for (var i = 0, l = token.length; i < l; i++) { | ||
if (token[i] instanceof RegExp) { | ||
token[i] = { pattern: token[i] }; | ||
} | ||
callback(token[i]); | ||
} | ||
} else { | ||
callback(token); | ||
} | ||
} | ||
|
||
/** | ||
* Adds doc-comment support to the given languages for the given documentation language. | ||
* | ||
* @param {string[]|string} languages | ||
* @param {Object} docLanguage | ||
*/ | ||
function addSupport(languages, docLanguage) { | ||
if (typeof languages === 'string') { | ||
languages = [languages]; | ||
} | ||
|
||
languages.forEach(function (lang) { | ||
docCommentSupport(lang, function (pattern) { | ||
if (!pattern.inside) { | ||
pattern.inside = {}; | ||
} | ||
pattern.inside.rest = docLanguage; | ||
}); | ||
}); | ||
} | ||
|
||
Object.defineProperty(javaDocLike, 'addSupport', { value: addSupport }); | ||
|
||
javaDocLike.addSupport(['java', 'javascript', 'php'], javaDocLike); | ||
|
||
}(Prism)); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
(function (Prism) { | ||
|
||
var javascript = Prism.languages.javascript; | ||
|
||
var type = /{(?:[^{}]|{(?:[^{}]|{[^{}]*})*})+}/.source; | ||
var parameterPrefix = '(@(?:param|arg|argument|property)\\s+(?:' + type + '\\s+)?)'; | ||
|
||
Prism.languages.jsdoc = Prism.languages.extend('javadoclike', { | ||
'parameter': { | ||
// @param {string} foo - foo bar | ||
pattern: RegExp(parameterPrefix + /[$\w\xA0-\uFFFF.]+(?=\s|$)/.source), | ||
lookbehind: true, | ||
inside: { | ||
'punctuation': /\./ | ||
} | ||
} | ||
}); | ||
|
||
Prism.languages.insertBefore('jsdoc', 'keyword', { | ||
'optional-parameter': { | ||
// @param {string} [baz.foo="bar"] foo bar | ||
pattern: RegExp(parameterPrefix + /\[[$\w\xA0-\uFFFF.]+(?:=[^[\]]+)?\](?=\s|$)/.source), | ||
lookbehind: true, | ||
inside: { | ||
'parameter': { | ||
pattern: /(^\[)[$\w\xA0-\uFFFF\.]+/, | ||
lookbehind: true, | ||
inside: { | ||
'punctuation': /\./ | ||
} | ||
}, | ||
'code': { | ||
pattern: /(=)[\s\S]*(?=\]$)/, | ||
lookbehind: true, | ||
inside: javascript, | ||
alias: 'language-javascript' | ||
}, | ||
'punctuation': /[=[\]]/ | ||
} | ||
}, | ||
'class-name': [ | ||
{ | ||
pattern: RegExp('(@[a-z]+\\s+)' + type), | ||
lookbehind: true, | ||
inside: { | ||
'punctuation': /[.,:?=<>|{}()[\]]/ | ||
} | ||
}, | ||
{ | ||
pattern: /(@(?:augments|extends|class|interface|memberof!?|this)\s+)[A-Z]\w*(?:\.[A-Z]\w*)*/, | ||
lookbehind: true, | ||
inside: { | ||
'punctuation': /\./ | ||
} | ||
} | ||
], | ||
'example': { | ||
pattern: /(@example\s+)[^@]+?(?=\s*(?:\*\s*)?(?:@\w|\*\/))/, | ||
lookbehind: true, | ||
inside: { | ||
'code': { | ||
pattern: /^(\s*(?:\*\s*)?).+$/m, | ||
lookbehind: true, | ||
inside: javascript, | ||
alias: 'language-javascript' | ||
} | ||
} | ||
} | ||
}); | ||
|
||
Prism.languages.javadoclike.addSupport('javascript', Prism.languages.jsdoc); | ||
|
||
}(Prism)); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
(function (Prism) { | ||
|
||
var typeExpression = /(?:[a-zA-Z]\w*|[|\\[\]])+/.source; | ||
|
||
Prism.languages.phpdoc = Prism.languages.extend('javadoclike', { | ||
'parameter': { | ||
pattern: RegExp('(@(?:global|param|property(?:-read|-write)?|var)\\s+(?:' + typeExpression + '\\s+)?)\\$\\w+'), | ||
lookbehind: true | ||
} | ||
}); | ||
|
||
Prism.languages.insertBefore('phpdoc', 'keyword', { | ||
'class-name': [ | ||
{ | ||
pattern: RegExp('(@(?:global|package|param|property(?:-read|-write)?|return|subpackage|throws|var)\\s+)' + typeExpression), | ||
lookbehind: true, | ||
inside: { | ||
'keyword': /\b(?:callback|resource|boolean|integer|double|object|string|array|false|float|mixed|bool|null|self|true|void|int)\b/, | ||
'punctuation': /[|\\[\]()]/ | ||
} | ||
} | ||
], | ||
}); | ||
|
||
Prism.languages.javadoclike.addSupport('php', Prism.languages.phpdoc); | ||
|
||
}(Prism)); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.