Added support for function expressions

This commit is contained in:
dderaedt 2012-09-24 10:06:31 +02:00
parent e21ef92206
commit f2aa61ca57
3 changed files with 75 additions and 17 deletions

View file

@ -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 ```@return``` is generated since the extension only looks at the function signature to generate the annotation, not its body.

54
main.js
View file

@ -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);
}

34
test_dummy.js Normal file
View file

@ -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;
}
});