Skip to content
Open
Changes from all 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
59 changes: 28 additions & 31 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ define(function (require, exports, module) {
var CommandManager = brackets.getModule("command/CommandManager"),
EditorManager = brackets.getModule("editor/EditorManager"),
KeyBindingManager = brackets.getModule("command/KeyBindingManager"),
Menus = brackets.getModule("command/Menus");
Menus = brackets.getModule("command/Menus"),
JSUtils = brackets.getModule("language/JSUtils");


var EMPTY_MSG = "No function found";
Expand Down Expand Up @@ -77,13 +78,7 @@ define(function (require, exports, module) {
function getTarget() {

var editor = EditorManager.getCurrentFullEditor(),
pos = editor.getCursorPos(),
functionDeclarationRegex = new RegExp('^[a-z0-9]*\\s*\\n*\\bfunction\\b\\s*' + REGEX_PATTERNS.jsVariable + '\\s*\\(\\s*(' +
REGEX_PATTERNS.jsVariable + '\\s*,?)*\\s*\\)','g'),

functionExpresionRegex = new RegExp('^[a-z0-9]*\\s*\\n*(var|(' + REGEX_PATTERNS.jsVariable + '.)*(' + REGEX_PATTERNS.jsVariable + ')?)?\\s*'+ REGEX_PATTERNS.jsVariable + '\\s*(=|:)\\s*function\\s*\\(\\s*(' +
REGEX_PATTERNS.jsVariable + '\\s*(,\\s*)?)*\\s*\\)\\s*','g');

pos = editor.getCursorPos();
pos.ch = 0;

// Take the text of the document, starting with the current cursor line
Expand All @@ -98,26 +93,28 @@ define(function (require, exports, module) {
txtFrom = txtFrom.replace(new RegExp(REGEX_PATTERNS.comment,'g'), '');

var results = txtFrom.match(new RegExp(REGEX_PATTERNS.jsVariable,'g'));
switch(true) {
case functionExpresionRegex.test(txtFrom):
return {
//check for 'var'
name:results[results.indexOf('function')-1],
params:results.slice(results.indexOf('function')+1),
prefix: getPrefix(txtFrom, results[0]),
returnsValue:returnsValue
};
case functionDeclarationRegex.test(txtFrom):
//console.log(results[1]);
return {
name:results[1],
params:results.slice(2),
prefix: getPrefix(txtFrom, results[0]),
returnsValue:returnsValue
};
default:
return null;

var functionList = JSUtils.findAllMatchingFunctionsInText (txtFrom, "*");

if (functionList.length === 0)
return;

// we interested only in first function
var functionName = functionList[0].name;

var result = {
name: functionName,
params: results.slice(results.indexOf('function')+1),
prefix: getPrefix(txtFrom, results[0]),
returnsValue: returnsValue
};
if (results.indexOf ('function') > results.indexOf (functionName)) { // …funct = function (…
return result;
} else if (results.indexOf ('function') < results.indexOf (functionName)) { // function funct (…
result.params = results.slice(results.indexOf (functionName)+1);
return result;
}
return;
}


Expand All @@ -138,18 +135,18 @@ define(function (require, exports, module) {
}

// Add description
output.push(" * Description");
output.push(" * [Description of "+fname+""]");

// Add parameters
if (params.length > 0) {
var i;
for (i = 0; i < params.length; i++) {
var param = params[i];
output.push(" * @param {type} " + param + " Description");
output.push(" * @param {type} " + param + " [Description]");
}
}

if (returnsValue) output.push(" * @returns {type} Description");
if (returnsValue) output.push(" * @returns {type} [Description]");

// TODO use if 'return' is found in the function body?
//output += " * @return {type} ???\n";
Expand Down Expand Up @@ -182,4 +179,4 @@ define(function (require, exports, module) {
menu.addMenuDivider();
menu.addMenuItem(COMMAND_ID);//"menu-edit-annotate",

});
});