Skip to content

Commit

Permalink
Added doc comment highlighting (#1541)
Browse files Browse the repository at this point in the history
This adds support for JavaDoc, JSDoc, TSDoc, and PHPDoc.
  • Loading branch information
RunDevelopment authored Feb 28, 2019
1 parent bb62860 commit 493d19e
Show file tree
Hide file tree
Showing 29 changed files with 1,246 additions and 6 deletions.
2 changes: 1 addition & 1 deletion components.js

Large diffs are not rendered by default.

39 changes: 38 additions & 1 deletion components.json
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,23 @@
"require": "clike",
"owner": "sherblot"
},
"javadoc": {
"title": "JavaDoc",
"require": ["markup", "java", "javadoclike"],
"peerDependencies": [
"scala"
],
"owner": "RunDevelopment"
},
"javadoclike": {
"title": "JavaDoc-like",
"peerDependencies": [
"java",
"javascript",
"php"
],
"owner": "RunDevelopment"
},
"javastacktrace": {
"title": "Java stack trace",
"owner": "RunDevelopment"
Expand All @@ -412,6 +429,15 @@
"require": "clike",
"owner": "thesave"
},
"jsdoc": {
"title": "JSDoc",
"require": ["javascript", "javadoclike"],
"peerDependencies": [
"actionscript",
"coffeescript"
],
"owner": "RunDevelopment"
},
"js-extras": {
"title": "JS Extras",
"require": "javascript",
Expand Down Expand Up @@ -518,6 +544,9 @@
"n4js": {
"title": "N4JS",
"require": "javascript",
"peerDependencies": [
"jsdoc"
],
"alias": "n4jsd",
"owner": "bsmith-n4"
},
Expand Down Expand Up @@ -595,6 +624,11 @@
"require": ["clike", "markup-templating"],
"owner": "milesj"
},
"phpdoc": {
"title": "PHPDoc",
"require": ["php", "javadoclike"],
"owner": "RunDevelopment"
},
"php-extras": {
"title": "PHP Extras",
"require": "php",
Expand Down Expand Up @@ -681,7 +715,10 @@
"jsx": {
"title": "React JSX",
"require": ["markup", "javascript"],
"peerDependencies": "js-extras",
"peerDependencies": [
"jsdoc",
"js-extras"
],
"owner": "vkbansal"
},
"tsx": {
Expand Down
54 changes: 54 additions & 0 deletions components/prism-javadoc.js
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));
1 change: 1 addition & 0 deletions components/prism-javadoc.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

86 changes: 86 additions & 0 deletions components/prism-javadoclike.js
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));
1 change: 1 addition & 0 deletions components/prism-javadoclike.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 73 additions & 0 deletions components/prism-jsdoc.js
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));
1 change: 1 addition & 0 deletions components/prism-jsdoc.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions components/prism-phpdoc.js
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));
1 change: 1 addition & 0 deletions components/prism-phpdoc.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion plugins/autoloader/prism-autoloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
}

// The dependencies map is built automatically with gulp
var lang_dependencies = /*languages_placeholder[*/{"javascript":"clike","actionscript":"javascript","arduino":"cpp","aspnet":["markup","csharp"],"bison":"c","c":"clike","csharp":"clike","cpp":"c","coffeescript":"javascript","crystal":"ruby","css-extras":"css","d":"clike","dart":"clike","django":"markup","erb":["ruby","markup-templating"],"fsharp":"clike","flow":"javascript","glsl":"clike","gml":"clike","go":"clike","groovy":"clike","haml":"ruby","handlebars":"markup-templating","haxe":"clike","java":"clike","jolie":"clike","js-extras":"javascript","jsonp":"json","json5":"json","kotlin":"clike","less":"css","markdown":"markup","markup-templating":"markup","n4js":"javascript","nginx":"clike","objectivec":"c","opencl":"cpp","parser":"markup","php":["clike","markup-templating"],"php-extras":"php","plsql":"sql","processing":"clike","protobuf":"clike","pug":"javascript","qore":"clike","jsx":["markup","javascript"],"tsx":["jsx","typescript"],"reason":"clike","ruby":"clike","sass":"css","scss":"css","scala":"java","smarty":"markup-templating","soy":"markup-templating","swift":"clike","tap":"yaml","textile":"markup","tt2":["clike","markup-templating"],"twig":"markup","typescript":"javascript","vala":"clike","vbnet":"basic","velocity":"markup","wiki":"markup","xeora":"markup","xquery":"markup"}/*]*/;
var lang_dependencies = /*languages_placeholder[*/{"javascript":"clike","actionscript":"javascript","arduino":"cpp","aspnet":["markup","csharp"],"bison":"c","c":"clike","csharp":"clike","cpp":"c","coffeescript":"javascript","crystal":"ruby","css-extras":"css","d":"clike","dart":"clike","django":"markup","erb":["ruby","markup-templating"],"fsharp":"clike","flow":"javascript","glsl":"clike","gml":"clike","go":"clike","groovy":"clike","haml":"ruby","handlebars":"markup-templating","haxe":"clike","java":"clike","javadoc":["markup","java","javadoclike"],"jolie":"clike","jsdoc":["javascript","javadoclike"],"js-extras":"javascript","jsonp":"json","json5":"json","kotlin":"clike","less":"css","markdown":"markup","markup-templating":"markup","n4js":"javascript","nginx":"clike","objectivec":"c","opencl":"cpp","parser":"markup","php":["clike","markup-templating"],"phpdoc":["php","javadoclike"],"php-extras":"php","plsql":"sql","processing":"clike","protobuf":"clike","pug":"javascript","qore":"clike","jsx":["markup","javascript"],"tsx":["jsx","typescript"],"reason":"clike","ruby":"clike","sass":"css","scss":"css","scala":"java","smarty":"markup-templating","soy":"markup-templating","swift":"clike","tap":"yaml","textile":"markup","tt2":["clike","markup-templating"],"twig":"markup","typescript":"javascript","vala":"clike","vbnet":"basic","velocity":"markup","wiki":"markup","xeora":"markup","xquery":"markup"}/*]*/;

var lang_data = {};

Expand Down
Loading

0 comments on commit 493d19e

Please sign in to comment.