Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Decouple JSDoc from JavaScript grammar / Highlight examples - #496

Merged
winstliu merged 11 commits into
atom:masterfrom
Cutlery-Drawer:jsdoc
Mar 9, 2017
Merged

winstliu merged 11 commits into
atom:masterfrom
Cutlery-Drawer:jsdoc

Conversation

@Alhadis

@Alhadis Alhadis commented Mar 3, 2017

Copy link
Copy Markdown
Contributor

Fixes #495.

Description of the Change

This PR separates the patterns for highlighting JSDoc tags from the main JavaScript grammar. The lookahead patterns currently responsible for this are inflicting severe performance issues in certain cases, which is to say nothing of the maintainability issues they've always imposed.

Additionally, I've also included inline JS highlighting for @example tags:

Figure 1

Alternate Designs / Thoughts

  • Initially, I wrote each block tag to span multiple lines, but realised highlighting something like @access \n private really didn't justify the added complexity.

Benefits

Other than being much more readable and maintainable, I imagine this will boost the tokeniser's performance drastically. It might be interesting to benchmark.

Possible Drawbacks

In light of the countless intricacies of Closure Compiler, I've opted to match {types} and @param tags with an inclusive approach, rather than exclusive. Users will no longer be able to rely on what this grammar matches to indicate what syntax is valid. To be honest, I feel it's a fair trade-off... grammars shouldn't be responsible for validating input (at least beyond the scope of unclosed strings, etc).

@Alhadis

Alhadis commented Mar 3, 2017

Copy link
Copy Markdown
Contributor Author

/cc @50Wliu @bgriffith

@winstliu

winstliu commented Mar 6, 2017

Copy link
Copy Markdown
Contributor

Oh look, another huge PR to review from @Alhadis 😁. I may need to punt this for a while to get a language-c rewrite merged first, as that grammar is in pretty bad shape right now.

Comment thread grammars/jsdoc.cson Outdated
'3':
'name': 'entity.name.type.instance.jsdoc'
'4':
'name': 'punctuation.definition.begin.jsdoc'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.bracket.angle

Comment thread grammars/jsdoc.cson Outdated
'match': '''(?x)
((@)borrows) \\s+
((?:[^@\\s*/]|\\*[^/])++) # <that namepath>
\\s++ (as) \\s++ # as

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the possessive match?

Comment thread grammars/jsdoc.cson Outdated
# URL
(
(?=https?://)
(?:[^\\s*]|\\*[/])+

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the forwards slash in a character class? Did you perhaps forget a ^? I noticed it also looks different from all the other patterns similar to this one.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, fixed.

Comment thread grammars/jsdoc.cson Outdated
# JSDoc namepath
(
(?!https?://)
(?:[^@\\s*/]|\\*[^/])++

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why possessive?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I'm a possessive prick.

(removed)*+

Comment thread grammars/jsdoc.cson
[A-Za-z_$] # First character: non-numeric word character
[\\w$.\\[\\]]* # Rest of identifier
(?: # Possible list of additional identifiers
\\s* , \\s*

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the comma should be tokenized

Comment thread grammars/jsdoc.cson Outdated
]
}
]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 empty line

Comment thread spec/jsdoc-spec.coffee Outdated
runs ->
grammar = atom.grammars.grammarForScopeName("source.js")


Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 empty line

Comment thread spec/jsdoc-spec.coffee Outdated
expect(tokens[1]).toEqual value: ' Text ', scopes: ['source.js', 'comment.block.documentation.js']
expect(tokens[2]).toEqual value: '{@', scopes: ['source.js', 'comment.block.documentation.js', 'entity.name.type.instance.jsdoc', 'storage.type.class.jsdoc', 'punctuation.definition.inline.tag.begin.jsdoc']
expect(tokens[3]).toEqual value: 'link', scopes: ['source.js', 'comment.block.documentation.js', 'entity.name.type.instance.jsdoc', 'storage.type.class.jsdoc']
expect(tokens[4]).toEqual value: 'plain', scopes: ['source.js', 'comment.block.documentation.js', 'entity.name.type.instance.jsdoc', 'variable.other.description.jsdoc']

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is interesting...shouldn't it be tokenized as linkplain and not separately?

Comment thread spec/jsdoc-spec.coffee Outdated
expect(tokens[1]).toEqual value: ' Text ', scopes: ['source.js', 'comment.block.documentation.js']
expect(tokens[2]).toEqual value: '{@', scopes: ['source.js', 'comment.block.documentation.js', 'entity.name.type.instance.jsdoc', 'storage.type.class.jsdoc', 'punctuation.definition.inline.tag.begin.jsdoc']
expect(tokens[3]).toEqual value: 'link', scopes: ['source.js', 'comment.block.documentation.js', 'entity.name.type.instance.jsdoc', 'storage.type.class.jsdoc']
expect(tokens[4]).toEqual value: 'code', scopes: ['source.js', 'comment.block.documentation.js', 'entity.name.type.instance.jsdoc', 'variable.other.description.jsdoc']

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

Comment thread spec/jsdoc-spec.coffee
expect(tokens[5]).toEqual value: '{', scopes: ['source.js', 'comment.block.documentation.js', 'entity.name.type.instance.jsdoc', 'punctuation.definition.bracket.curly.begin.jsdoc']
expect(tokens[6]).toEqual value: '*', scopes: ['source.js', 'comment.block.documentation.js', 'entity.name.type.instance.jsdoc']
expect(tokens[7]).toEqual value: '}', scopes: ['source.js', 'comment.block.documentation.js', 'entity.name.type.instance.jsdoc', 'punctuation.definition.bracket.curly.end.jsdoc']
expect(tokens[9]).toEqual value: 'variable', scopes: ['source.js', 'comment.block.documentation.js', 'variab

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 empty line

@winstliu

winstliu commented Mar 8, 2017

Copy link
Copy Markdown
Contributor

Would it be worthwhile to tokenize default values as actual JS?

@Alhadis

Alhadis commented Mar 9, 2017

Copy link
Copy Markdown
Contributor Author

Possibly, but that would likely leave us at the mercy of runaway begin/end pairs if the terminating ] was matched by something else.

@Alhadis

Alhadis commented Mar 9, 2017

Copy link
Copy Markdown
Contributor Author

... never mind, I said nothing.

I shouldn't be allowed to communicate with humans until I've finished waking up.

@winstliu winstliu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: All I did was highlight instances of possessive matching. I'm unsure if any of those are intentional, if so, just ignore the comment.

Comment thread grammars/jsdoc.cson Outdated
'match': '''(?x)
((@)borrows) \\s+
((?:[^@\\s*/]|\\*[^/])+) # <that namepath>
\\s++ (as) \\s+ # as

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You forgot to remove the first possessive match on this line.

Comment thread grammars/jsdoc.cson Outdated
}
{
'name': 'entity.name.type.instance.jsdoc'
'match': '(?:[^@\\s*/]|\\*[^/])++'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possessive

Comment thread grammars/jsdoc.cson Outdated
\\s+
(
[^@\\s<>*/]
(?:[^@<>*/]|\\*[^/])*+

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possessive

Comment thread grammars/jsdoc.cson Outdated
}
{
# Highlighted JavaScript example
'match': '[^\\s@*](?:[^*]|\\*[^/])*+'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possessive

Comment thread grammars/jsdoc.cson Outdated
(?:
\\s*
(<)
([^>\\s]++)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possessive

@Alhadis

Alhadis commented Mar 9, 2017

Copy link
Copy Markdown
Contributor Author

Fixed.

@winstliu
winstliu merged commit 2ba9a98 into atom:master Mar 9, 2017
@Alhadis

Alhadis commented Mar 10, 2017 •

Copy link
Copy Markdown
Contributor Author

@50Wliu Urgh, I forgot to accommodate arrays when highlighting default values as JavaScript. Something like this will only be partially highlighted:

/** @param {Object} [thing={a: [], b: [0, 2], c: "String"}] [Not Highlighted] [] [] [] */
                                ^ Terminates here

Currently working on a fix, although this is surprisingly tricky to get right...

@winstliu

Copy link
Copy Markdown
Contributor

If it's too difficult/tricky to achieve just revert the embedded highlighting all together.

@Alhadis

Alhadis commented Mar 10, 2017

Copy link
Copy Markdown
Contributor Author

No no, so far, so good... it's just that if users get too carried away with really convoluted complex values, they might get flaky-looking highlighting.

Then again, if they're stuffing that much into a default value, what else do you expect? 😀 Heck, this might even discourage an anti-pattern, haha.

@Alhadis

Alhadis commented Mar 10, 2017

Copy link
Copy Markdown
Contributor Author

Okay, fixed. Pleeeease don't chide me over the odd-looking way I've implemented this... :D Some repetition was necessary...

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Catastrophic backtracking in JSDoc param types

2 participants