From dc04aea593b4debc42ccd43d292b0e6d4dbeb0e4 Mon Sep 17 00:00:00 2001 From: Timo Sandberg Date: Tue, 14 Oct 2014 21:35:42 +0300 Subject: [PATCH 1/7] additional yuidoc like comments for class and function --- main.js | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/main.js b/main.js index f7ac06a..d29b9a1 100644 --- a/main.js +++ b/main.js @@ -61,7 +61,7 @@ define(function (require, exports, module) { function handleEnterKey(editor) { var cursor = editor.getCursorPos(); var token = editor._codeMirror.getTokenAt(cursor); -// console.log(token); + //console.log(token); if (token.type === "comment") { // But are we in a BLOCK comment? @@ -90,7 +90,38 @@ define(function (require, exports, module) { suffix = ""; } } - editor.document.replaceRange("\n" + prefix + " " + suffix, cursor); + // if next line has function or class, insert YUIDOC magic + var nextline = editor.document.getLine(cursor.line + 1); + var reservedword = nextline.match(/class|function/)[0]; + + if (reservedword) { + var append = "", i = 0; + switch (reservedword) { + case "function": + var parts = nextline.match(/(?:function+)(?:\s)(\w*)?\(([\w\s_,]+)\)/); + append += "\n" + prefix + " function description\n" + prefix; + append += "\n" + prefix + " @method " + parts[1]; + + if (parts[2]) { + var variables = parts[2].replace(" ", "").split(","); + for (i = 0; i < variables.length; i++) { + append += "\n" + prefix + " @param {Type} " + variables[i]; + } + } + break; + case "class": + var classname = nextline.match(/class\s+(\w*)/)[1]; + append += "\n" + prefix + " class description"; + append += "\n" + prefix; + append += "\n" + prefix + " @class " + classname || ""; + append += "\n" + prefix + " @author Your Name "; + append += "\n" + prefix + " @constructor"; + break; + } + editor.document.replaceRange(append + suffix, cursor); + } else { + editor.document.replaceRange("\n" + prefix + " " + suffix, cursor); + } cursor.line++; cursor.ch = prefix.length + 1; From 4de1a35455660d7f9dc531c915f2f608f41c9ecb Mon Sep 17 00:00:00 2001 From: Timo Sandberg Date: Wed, 15 Oct 2014 08:35:52 +0300 Subject: [PATCH 2/7] minor --- main.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/main.js b/main.js index d29b9a1..4fd92d9 100644 --- a/main.js +++ b/main.js @@ -21,7 +21,7 @@ */ -/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */ +/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50, white: true, unparam: true, todo: true */ /*global define, brackets, $ */ define(function (require, exports, module) { @@ -29,8 +29,8 @@ define(function (require, exports, module) { // Brackets modules var EditorManager = brackets.getModule("editor/EditorManager"), - DocumentManager = brackets.getModule("document/DocumentManager"), - TokenUtils = brackets.getModule("utils/TokenUtils"), + //DocumentManager = brackets.getModule("document/DocumentManager"), + //TokenUtils = brackets.getModule("utils/TokenUtils"), KeyEvent = brackets.getModule("utils/KeyEvent"), StringUtils = brackets.getModule("utils/StringUtils"); @@ -90,11 +90,12 @@ define(function (require, exports, module) { suffix = ""; } } - // if next line has function or class, insert YUIDOC magic + // if next line has function or class, insert YUIDoc-like magic var nextline = editor.document.getLine(cursor.line + 1); var reservedword = nextline.match(/class|function/)[0]; - if (reservedword) { + // has to start with /** and contain keyword + if (line.match(/\/\*\*/) && reservedword) { var append = "", i = 0; switch (reservedword) { case "function": @@ -102,6 +103,7 @@ define(function (require, exports, module) { append += "\n" + prefix + " function description\n" + prefix; append += "\n" + prefix + " @method " + parts[1]; + // function variables if (parts[2]) { var variables = parts[2].replace(" ", "").split(","); for (i = 0; i < variables.length; i++) { @@ -162,4 +164,4 @@ define(function (require, exports, module) { // For unit tests exports.handleEnterKey = handleEnterKey; -}); \ No newline at end of file +}); From 86a13fe27ceee51838e93b028780ea41a62543c4 Mon Sep 17 00:00:00 2001 From: Timo Sandberg Date: Wed, 15 Oct 2014 09:52:07 +0300 Subject: [PATCH 3/7] refactoring --- main.js | 66 ++++++++++++++++++++++++++++++++------------------------- 1 file changed, 37 insertions(+), 29 deletions(-) diff --git a/main.js b/main.js index 4fd92d9..cf6d27c 100644 --- a/main.js +++ b/main.js @@ -21,7 +21,7 @@ */ -/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50, white: true, unparam: true, todo: true */ +/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50, white: true, unparam: true */ /*global define, brackets, $ */ define(function (require, exports, module) { @@ -70,7 +70,7 @@ define(function (require, exports, module) { var prefixMatch = line.match(/^\s*(\*|\/\*)/); if (prefixMatch) { if (!StringUtils.endsWith(token.string, "*/") || cursor.ch < token.end) { - var prefix, suffix; + var prefix, suffix, commentString = null; if (prefixMatch[1] === "*") { // Line other than first line prefix = prefixMatch[0]; @@ -89,38 +89,46 @@ define(function (require, exports, module) { } else { suffix = ""; } - } - // if next line has function or class, insert YUIDoc-like magic - var nextline = editor.document.getLine(cursor.line + 1); - var reservedword = nextline.match(/class|function/)[0]; + + // if next line has function or class, insert some YUIDoc-like magic + var nextLine = editor.document.getLine(1 + cursor.line); + var reservedWord = nextLine.match(/class|function/) || null; - // has to start with /** and contain keyword - if (line.match(/\/\*\*/) && reservedword) { - var append = "", i = 0; - switch (reservedword) { - case "function": - var parts = nextline.match(/(?:function+)(?:\s)(\w*)?\(([\w\s_,]+)\)/); - append += "\n" + prefix + " function description\n" + prefix; - append += "\n" + prefix + " @method " + parts[1]; + // has to have double asterisk and contain a keyword + if (line.match(/\/\*\*/) && reservedWord) { + commentString = ""; + + var i = 0; + switch (reservedWord[0]) { + case "function": + var parts = nextLine.match(/(?:function+)(?:\s)(\w*)?\(([\w\s_,]+)\)/); + commentString += "\n" + prefix + " function description\n" + prefix; + commentString += "\n" + prefix + " @method " + parts[1]; - // function variables - if (parts[2]) { - var variables = parts[2].replace(" ", "").split(","); - for (i = 0; i < variables.length; i++) { - append += "\n" + prefix + " @param {Type} " + variables[i]; + // function variables + if (parts[2]) { + var variables = parts[2].replace(/\s/g, "").split(","); + for (i = 0; i < variables.length; i++) { + commentString += "\n" + prefix + " @param {Type} " + variables[i]; + } } + break; + case "class": + var classname = nextLine.match(/class\s+(\w*)/)[1]; + commentString += "\n" + prefix + " class description"; + commentString += "\n" + prefix; + commentString += "\n" + prefix + " @class " + classname || ""; + commentString += "\n" + prefix + " @author Your Name "; + commentString += "\n" + prefix + " @constructor"; + break; } - break; - case "class": - var classname = nextline.match(/class\s+(\w*)/)[1]; - append += "\n" + prefix + " class description"; - append += "\n" + prefix; - append += "\n" + prefix + " @class " + classname || ""; - append += "\n" + prefix + " @author Your Name "; - append += "\n" + prefix + " @constructor"; - break; + commentString += suffix; + } - editor.document.replaceRange(append + suffix, cursor); + } + + if (commentString) { + editor.document.replaceRange(commentString, cursor); } else { editor.document.replaceRange("\n" + prefix + " " + suffix, cursor); } From 6249d45aeb9c67f244ade84275d0391aa4bd42ff Mon Sep 17 00:00:00 2001 From: Timo Sandberg Date: Wed, 15 Oct 2014 17:57:42 +0300 Subject: [PATCH 4/7] refactoring --- README.md | 38 ++++++++++++++++++++++++++++++++++++++ main.js | 4 +--- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 71cfe4c..aaada4b 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,44 @@ You'll get this: Supports any language that uses `/* ... */`-style comments. +Simple predefined YUIDoc/Javadoc/PHPDoc-style comments for function and class. Just make sure the function/class defining row is right below when pressing Enter. + +``` + /**| + function foo(bar) { +``` + +To get: +``` + /** + * function description + * + * @method foo + * @param {Type} bar + * @return {Type} + */ + function foo(bar) { +``` + +Or: +``` + /**| + class Foo { +``` + +To get: +``` + /** + * class description + * + * @class Foo + * @author Your Name + * @constructor + */ + class Foo { +``` + + How to Install ============== diff --git a/main.js b/main.js index cf6d27c..252df2e 100644 --- a/main.js +++ b/main.js @@ -29,8 +29,6 @@ define(function (require, exports, module) { // Brackets modules var EditorManager = brackets.getModule("editor/EditorManager"), - //DocumentManager = brackets.getModule("document/DocumentManager"), - //TokenUtils = brackets.getModule("utils/TokenUtils"), KeyEvent = brackets.getModule("utils/KeyEvent"), StringUtils = brackets.getModule("utils/StringUtils"); @@ -61,7 +59,6 @@ define(function (require, exports, module) { function handleEnterKey(editor) { var cursor = editor.getCursorPos(); var token = editor._codeMirror.getTokenAt(cursor); - //console.log(token); if (token.type === "comment") { // But are we in a BLOCK comment? @@ -112,6 +109,7 @@ define(function (require, exports, module) { commentString += "\n" + prefix + " @param {Type} " + variables[i]; } } + commentString += "\n" + prefix + " @return {Type} "; break; case "class": var classname = nextLine.match(/class\s+(\w*)/)[1]; From db2405dfa050b323380a1df03b3739f71f82606b Mon Sep 17 00:00:00 2001 From: Timo Sandberg Date: Wed, 15 Oct 2014 19:06:25 +0300 Subject: [PATCH 5/7] cleanup --- README.md | 2 +- main.js | 46 ++++++++++++++++++---------------------------- 2 files changed, 19 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index aaada4b..dced609 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ To get: * @author Your Name * @constructor */ - class Foo { + class Foo { ``` diff --git a/main.js b/main.js index 252df2e..fd32d86 100644 --- a/main.js +++ b/main.js @@ -93,43 +93,36 @@ define(function (require, exports, module) { // has to have double asterisk and contain a keyword if (line.match(/\/\*\*/) && reservedWord) { - commentString = ""; var i = 0; switch (reservedWord[0]) { case "function": - var parts = nextLine.match(/(?:function+)(?:\s)(\w*)?\(([\w\s_,]+)\)/); - commentString += "\n" + prefix + " function description\n" + prefix; - commentString += "\n" + prefix + " @method " + parts[1]; + var parts = nextLine.match(/(?:function+)(?:\s)(\w*)?\(([\w\s,]*)?\)/); + if (parts) { + commentString = "\n" + prefix + " function description\n" + prefix + "\n" + prefix + " @method " + parts[1]; - // function variables - if (parts[2]) { - var variables = parts[2].replace(/\s/g, "").split(","); - for (i = 0; i < variables.length; i++) { - commentString += "\n" + prefix + " @param {Type} " + variables[i]; + // function variables + if (parts[2]) { + var variables = parts[2].replace(/\s/g, "").split(","); + for (i = 0; i < variables.length; i++) { + commentString += "\n" + prefix + " @param {Type} " + variables[i]; + } } + commentString += "\n" + prefix + " @return {Type} " + suffix; } - commentString += "\n" + prefix + " @return {Type} "; break; case "class": - var classname = nextLine.match(/class\s+(\w*)/)[1]; - commentString += "\n" + prefix + " class description"; - commentString += "\n" + prefix; - commentString += "\n" + prefix + " @class " + classname || ""; - commentString += "\n" + prefix + " @author Your Name "; - commentString += "\n" + prefix + " @constructor"; + var classname = nextLine.match(/class\s+(\w*)/); + if (classname) { + commentString = "\n" + prefix + " class description" + "\n" + prefix; + commentString += "\n" + prefix + " @class " + classname[1] || ""; + commentString += "\n" + prefix + " @author Your Name " + "\n" + prefix + " @constructor " + suffix; + } break; } - commentString += suffix; - } } - - if (commentString) { - editor.document.replaceRange(commentString, cursor); - } else { - editor.document.replaceRange("\n" + prefix + " " + suffix, cursor); - } + editor.document.replaceRange(commentString || "\n" + prefix + " " + suffix, cursor); cursor.line++; cursor.ch = prefix.length + 1; @@ -157,7 +150,6 @@ define(function (require, exports, module) { } } - // Attach Enter key listener var editorHolder = $("#editor-holder")[0]; if (editorHolder) { @@ -166,8 +158,6 @@ define(function (require, exports, module) { console.warn("Unable to attach reasonable comments extension - assuming running in unit test window"); // (could verify that by looking at the path the way ExtensionLoader does, but seems like overkill) } - - // For unit tests exports.handleEnterKey = handleEnterKey; -}); +}); \ No newline at end of file From 36e0c2d550266efc07e660268fe21fb8ad5cc8ec Mon Sep 17 00:00:00 2001 From: Timo Sandberg Date: Fri, 17 Oct 2014 11:10:00 +0300 Subject: [PATCH 6/7] place text inside comment --- main.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/main.js b/main.js index fd32d86..bb330b4 100644 --- a/main.js +++ b/main.js @@ -55,7 +55,6 @@ define(function (require, exports, module) { // // - pressing enter in *middle* of //-style comment should split it onto second line with // prefix - function handleEnterKey(editor) { var cursor = editor.getCursorPos(); var token = editor._codeMirror.getTokenAt(cursor); @@ -65,6 +64,7 @@ define(function (require, exports, module) { // For now, we do a dumb approximation: does the line start with /* or *, and the last chars to left of cursor aren't */ ? var line = editor.document.getLine(cursor.line); var prefixMatch = line.match(/^\s*(\*|\/\*)/); + var followingText; if (prefixMatch) { if (!StringUtils.endsWith(token.string, "*/") || cursor.ch < token.end) { var prefix, suffix, commentString = null; @@ -73,6 +73,11 @@ define(function (require, exports, module) { prefix = prefixMatch[0]; suffix = ""; } else { + // Get text after cursor + if (token.end > cursor.ch) { + followingText = token.string.substring(cursor.ch, token.end); + editor.document.replaceRange("", cursor, { line: cursor.line, ch: token.end }); + } // First line prefix = prefixMatch[0].replace("/", " "); // don't reinsert /* on 2nd line @@ -127,6 +132,10 @@ define(function (require, exports, module) { cursor.line++; cursor.ch = prefix.length + 1; editor.setCursorPos(cursor.line, cursor.ch); + // If there was text, place inside comment + if (followingText) { + editor.document.replaceRange(followingText, editor.getCursorPos()); + } return true; } } @@ -160,4 +169,4 @@ define(function (require, exports, module) { } // For unit tests exports.handleEnterKey = handleEnterKey; -}); \ No newline at end of file +}); From dfdc5be50ec1535f22018090c449aecb94de562c Mon Sep 17 00:00:00 2001 From: Timo Sandberg Date: Mon, 20 Oct 2014 10:46:26 +0300 Subject: [PATCH 7/7] Update README.md --- README.md | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index dced609..0a368cd 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,21 @@ You'll get this: */ ``` +Text after cursor is inserted inside comment + +``` + /**|foo bar +``` + +And you'll get: + +``` + /** + * |foo bar + */ +``` + + Supports any language that uses `/* ... */`-style comments. Simple predefined YUIDoc/Javadoc/PHPDoc-style comments for function and class. Just make sure the function/class defining row is right below when pressing Enter. @@ -86,4 +101,4 @@ To install extensions: MIT-licensed -- see `main.js` for details. ### Compatibility -Brackets Sprint 21 or newer (or Adobe Edge Code Preview 4 or newer). \ No newline at end of file +Brackets Sprint 21 or newer (or Adobe Edge Code Preview 4 or newer).