Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fullDocs option for doc_comment #425

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function initEditor() {

server = new CodeMirror.TernServer({
defs: defs,
plugins: {requirejs: {}, doc_comment: true},
plugins: {requirejs: {}, doc_comment: {fullDocs: false}},
switchToDoc: function(name) { selectDoc(docID(name)); },
workerDeps: ["../../../acorn/acorn.js", "../../../acorn/acorn_loose.js",
"../../../acorn/util/walk.js", "../../../../lib/signal.js", "../../../../lib/tern.js",
Expand Down
4 changes: 4 additions & 0 deletions doc/src/manual.txt
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,10 @@ property.
To turn this plugin off, set `doc_comment: null` in your
<<option_plugins,plugin option>>.

The plugin understands the following configuration parameters:

`fullDocs`:: Can be set to `true` to return the full comment text instead of the first sentence.

[[plugin_node]]
==== Node.js plugin ====

Expand Down
21 changes: 15 additions & 6 deletions plugin/doc_comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@
})(function(infer, tern, comment, acorn, walk) {
"use strict";

tern.registerPlugin("doc_comment", function(server) {
var fullDocs=false;

tern.registerPlugin("doc_comment", function(server, options) {
server.jsdocTypedefs = Object.create(null);
server.on("reset", function() {
server.jsdocTypedefs = Object.create(null);
});
fullDocs = options && options.fullDocs;

return {
passes: {
Expand Down Expand Up @@ -93,11 +96,17 @@
type = null;
}

var first = comments[0], dot = first.search(/\.\s/);
if (dot > 5) first = first.slice(0, dot + 1);
first = first.trim().replace(/\s*\n\s*\*\s*|\s{1,}/g, " ");
if (aval instanceof infer.AVal) aval.doc = first;
if (type) type.doc = first;
var result = comments[comments.length - 1];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is an issue I've always had with tern but I thought it would be a difficult fix because I never looked at the code. Now that I looked at the code I realized how simple it is to fix this!

Example:

/*** section header ***/

/** test comments */
function test(){}

Why would it ever make sense to display the first comment when there are more than one? I have a lot of code similar to above and I end up with section header as my documentation for function test inside of tern. It makes sense to display the last comment (Unless there is a use case I'm not aware of).

if (fullDocs) result = result.trim().replace(/\n {0,}\* {0,}/g, "\n");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This removes all asterisks that are part of the JSDoc structure except the first one (which is removed later). This is important as it keeps asterisks that are part of the documentation; example of asterisk in documentation:

 /**
  * Solves equations of the form a * x = b
  */

This also preserves the newlines inside of the comments.

else{
var dot = result.search(/\.\s/);
if (dot > 5) result = result.slice(0, dot + 1);
result = result.trim().replace(/\s*\n\s*\*\s*|\s{1,}/g, " ");
}
result = result.replace(/^\s{0,}\*{1,}\s{0,}/, "");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Without this tern likes to return comments that start with an asterisk which also doesn't make sense. This fix applies to both fullDocs and the first sentence doc mode.

Example:

/** test comments */
function test(){}

Without this fix (current state of tern) the resulting comment is * test comments. With the fix the result is test comments. I can't think of any reason why someone would ever want a starting asterisk.


if (aval instanceof infer.AVal) aval.doc = result;
if (type) type.doc = result;
}

// Parses a subset of JSDoc-style comments in order to include the
Expand Down