From f2aa61ca57662c1ef62968490fbe03a1f837574c Mon Sep 17 00:00:00 2001 From: dderaedt Date: Mon, 24 Sep 2012 10:06:31 +0200 Subject: [PATCH] Added support for function expressions --- README.md | 4 +--- main.js | 54 ++++++++++++++++++++++++++++++++++++++------------- test_dummy.js | 34 ++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 17 deletions(-) create mode 100644 test_dummy.js diff --git a/README.md b/README.md index af65bce..fb36bd4 100755 --- a/README.md +++ b/README.md @@ -17,8 +17,6 @@ This will create a JSDoc like annotation according to the function signature. I Known issues ===== -Will only work with function declared with the form ```function name ([parameters]) {}```. - Annotations are not correctly indented. -No ```@return``` is generated since the extension only looks at the function signature to generate the annotation, not its body. \ No newline at end of file +No ```@return``` is generated since the extension only looks at the function signature to generate the annotation, not its body. diff --git a/main.js b/main.js index dde2e02..4361235 100644 --- a/main.js +++ b/main.js @@ -39,21 +39,20 @@ define(function (require, exports, module) { var COMMAND_ID = "annotate.annotate"; var MENU_NAME = "Annotate function"; + function insert(input) { - + var editor = EditorManager.getCurrentFullEditor(); var pos = editor.getCursorPos(); editor._codeMirror.replaceRange(input, pos); - EditorManager.focusEditor(); - + EditorManager.focusEditor(); } - function _generateComment() { - - var output = "/**\n"; + + function getTarget() { var editor = EditorManager.getCurrentFullEditor(); var pos = editor.getCursorPos(); @@ -69,13 +68,31 @@ define(function (require, exports, module) { var results = txtFrom.match(re); // The first word found should be "function", and next ones parameters - if (results[0] !== "function") { - window.alert(EMPTY_MSG); - return; + + if (results[0] === "function") { + return { + name: results[1], + params: results.slice(2) + }; + } + else if (results[0] === "var" && results[2] === "function") { + return { + name: results[1], + params: results.slice(3) + }; } + else { + return null; + } + + } + + + function generateComment(fname, params) { + + var output = "/**\n"; // Assume function is private if it starts with an underscore - var fname = results[1]; if (fname.charAt(0) === "_") { output += " * @private\n"; } @@ -84,10 +101,10 @@ define(function (require, exports, module) { output += " * Description\n"; // Add parameters - if (results.length > 2) { + if (params.length > 0) { var i; - for (i = 2; i < results.length; i++) { - var param = results[i]; + for (i = 0; i < params.length; i++) { + var param = params[i]; output += " * @param {type} " + param + " Description\n"; } } @@ -100,9 +117,18 @@ define(function (require, exports, module) { } + function annotate() { - var comment = _generateComment(); + var target = getTarget(); + + if(target === null) { + window.alert(EMPTY_MSG); + return; + } + + var comment = generateComment(target.name, target.params); + insert(comment); } diff --git a/test_dummy.js b/test_dummy.js new file mode 100644 index 0000000..e303e90 --- /dev/null +++ b/test_dummy.js @@ -0,0 +1,34 @@ + +/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */ +/*global define, $, brackets, window */ + +/** This file is only used to be tested against annotate */ +define(function (require, exports, module) { + + 'use strict'; + + + var greetings = "Hello world"), + + + function declaration(input) { + + var content = "stuff"; + } + + + var expression = function(p1, p2) { + + var content = "stuff"; + + }; + + + function noParams() { + + return null; + + } + + +}); \ No newline at end of file