2012-06-13 16:12:30 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2012 Adobe Systems Incorporated. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
|
|
|
|
|
/*global define, $, brackets, window */
|
|
|
|
|
|
|
|
|
|
/** extension to generate JSDoc annotations for functions */
|
|
|
|
|
define(function (require, exports, module) {
|
|
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
|
2012-10-07 17:45:02 +00:00
|
|
|
var CommandManager = brackets.getModule("command/CommandManager"),
|
|
|
|
|
EditorManager = brackets.getModule("editor/EditorManager"),
|
|
|
|
|
KeyBindingManager = brackets.getModule("command/KeyBindingManager"),
|
|
|
|
|
Menus = brackets.getModule("command/Menus");
|
2012-06-13 16:12:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
var EMPTY_MSG = "No function found";
|
|
|
|
|
var COMMAND_ID = "annotate.annotate";
|
|
|
|
|
var MENU_NAME = "Annotate function";
|
2014-03-26 05:27:39 +00:00
|
|
|
|
|
|
|
|
var REGEX_PATTERNS = {
|
|
|
|
|
comment: '\\/\\*.*\\*\\/',
|
|
|
|
|
jsVariable: '[$A-Za-z_][0-9A-Za-z_$]*'
|
|
|
|
|
};
|
2012-06-13 16:12:30 +00:00
|
|
|
|
2013-07-19 08:02:56 +00:00
|
|
|
function insert(input) {
|
2014-03-26 05:27:39 +00:00
|
|
|
|
2012-06-13 16:12:30 +00:00
|
|
|
var editor = EditorManager.getCurrentFullEditor();
|
2012-10-07 17:45:02 +00:00
|
|
|
var pos = editor.getCursorPos();
|
2013-07-19 08:02:56 +00:00
|
|
|
pos.ch = 0;
|
2014-03-26 05:27:39 +00:00
|
|
|
|
2012-09-21 09:06:04 +00:00
|
|
|
editor._codeMirror.replaceRange(input, pos);
|
2012-06-13 16:12:30 +00:00
|
|
|
|
2012-10-07 17:27:01 +00:00
|
|
|
EditorManager.focusEditor();
|
2014-03-26 05:27:39 +00:00
|
|
|
|
2012-10-07 17:27:01 +00:00
|
|
|
}
|
2014-03-26 05:27:39 +00:00
|
|
|
|
2012-10-07 17:27:01 +00:00
|
|
|
/**
|
|
|
|
|
* get the whitespace characters from line start to beginning of function def
|
|
|
|
|
* @param string input lines from start of the function definition
|
|
|
|
|
* @param string match function definition start
|
|
|
|
|
*/
|
|
|
|
|
function getPrefix(input, match) {
|
2014-03-26 05:27:39 +00:00
|
|
|
|
2012-10-07 17:27:01 +00:00
|
|
|
var indexOf = input.indexOf(match),
|
2012-10-07 17:45:02 +00:00
|
|
|
prefix = "";
|
2013-07-19 08:02:56 +00:00
|
|
|
if (indexOf !== -1) {
|
2014-03-26 05:27:39 +00:00
|
|
|
prefix = input.substr(0, indexOf).replace(/[^\s\n]/g, ' ');
|
2012-10-07 17:27:01 +00:00
|
|
|
}
|
2014-03-26 05:27:39 +00:00
|
|
|
|
2012-10-07 17:27:01 +00:00
|
|
|
return prefix;
|
2014-03-26 05:27:39 +00:00
|
|
|
|
2012-06-13 16:12:30 +00:00
|
|
|
}
|
2014-03-26 05:27:39 +00:00
|
|
|
|
2012-09-24 08:06:31 +00:00
|
|
|
function getTarget() {
|
2014-03-26 05:27:39 +00:00
|
|
|
|
|
|
|
|
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'),
|
2013-11-21 17:44:56 +00:00
|
|
|
|
2014-03-26 16:13:22 +00:00
|
|
|
functionExpresionRegex = new RegExp('^[a-z0-9]*\\s*\\n*(var|'+ REGEX_PATTERNS.jsVariable + '.)?\\s*'+ REGEX_PATTERNS.jsVariable + '\\s*=\\s*function\\s*\\(\\s*(' +
|
2014-03-26 05:27:39 +00:00
|
|
|
REGEX_PATTERNS.jsVariable + '\\s*(,\\s*)?)*\\s*\\)\\s*','g');
|
2013-11-21 17:44:56 +00:00
|
|
|
|
2014-03-26 05:27:39 +00:00
|
|
|
pos.ch = 0;
|
|
|
|
|
|
2012-06-13 16:12:30 +00:00
|
|
|
// Take the text of the document, starting with the current cursor line
|
|
|
|
|
var txtFrom = editor._codeMirror.getRange(pos, {line: editor._codeMirror.lineCount() });
|
2014-03-26 05:27:39 +00:00
|
|
|
|
|
|
|
|
//checks if there is a return value
|
|
|
|
|
var returnsValue = txtFrom.substr( txtFrom.indexOf('{'), txtFrom.indexOf('}')).search('return') !== -1;
|
|
|
|
|
|
2012-06-13 16:12:30 +00:00
|
|
|
txtFrom = txtFrom.substr(0, txtFrom.indexOf("{"));
|
2013-11-21 17:44:56 +00:00
|
|
|
|
2014-03-26 05:27:39 +00:00
|
|
|
//take any comment off
|
2014-03-26 15:01:45 +00:00
|
|
|
txtFrom = txtFrom.replace(new RegExp(REGEX_PATTERNS.comment,'g'), '');
|
2014-03-26 05:27:39 +00:00
|
|
|
|
|
|
|
|
var results = txtFrom.match(new RegExp(REGEX_PATTERNS.jsVariable,'g'));
|
|
|
|
|
switch(true) {
|
|
|
|
|
case functionExpresionRegex.test(txtFrom):
|
2012-10-07 17:27:01 +00:00
|
|
|
return {
|
2014-03-26 05:27:39 +00:00
|
|
|
//check for 'var'
|
2014-03-26 16:13:22 +00:00
|
|
|
name:results[results.indexOf('function')-1],
|
|
|
|
|
params:results.slice(results.indexOf('function')+1),
|
2014-03-26 05:27:39 +00:00
|
|
|
prefix: getPrefix(txtFrom, results[0]),
|
|
|
|
|
returnsValue:returnsValue
|
2013-07-19 08:02:56 +00:00
|
|
|
};
|
2014-03-26 05:27:39 +00:00
|
|
|
case functionDeclarationRegex.test(txtFrom):
|
2014-03-26 16:13:22 +00:00
|
|
|
//console.log(results[1]);
|
|
|
|
|
return {
|
2014-03-26 05:27:39 +00:00
|
|
|
name:results[1],
|
|
|
|
|
params:results.slice(2),
|
|
|
|
|
prefix: getPrefix(txtFrom, results[0]),
|
|
|
|
|
returnsValue:returnsValue
|
2013-07-19 08:02:56 +00:00
|
|
|
};
|
2014-03-26 05:27:39 +00:00
|
|
|
default:
|
2012-09-24 08:06:31 +00:00
|
|
|
return null;
|
2012-06-13 16:12:30 +00:00
|
|
|
}
|
2012-09-24 08:06:31 +00:00
|
|
|
}
|
2014-03-26 05:27:39 +00:00
|
|
|
|
|
|
|
|
|
2012-10-07 17:27:01 +00:00
|
|
|
/**
|
|
|
|
|
* Generate comment block
|
|
|
|
|
* @param string fname function name
|
|
|
|
|
* @param string params function parameters
|
|
|
|
|
* @param string prefix whitespace prefix for comment block lines
|
|
|
|
|
*/
|
2014-03-26 05:27:39 +00:00
|
|
|
function generateComment(fname, params,returnsValue, prefix) {
|
|
|
|
|
|
2012-10-07 17:27:01 +00:00
|
|
|
var output = [];
|
|
|
|
|
output.push("/**");
|
2014-03-26 05:27:39 +00:00
|
|
|
|
2012-06-13 16:12:30 +00:00
|
|
|
// Assume function is private if it starts with an underscore
|
|
|
|
|
if (fname.charAt(0) === "_") {
|
2012-10-07 17:27:01 +00:00
|
|
|
output.push(" * @private");
|
2012-06-13 16:12:30 +00:00
|
|
|
}
|
2014-03-26 05:27:39 +00:00
|
|
|
|
2012-06-13 16:12:30 +00:00
|
|
|
// Add description
|
2012-10-07 17:27:01 +00:00
|
|
|
output.push(" * Description");
|
2014-03-26 05:27:39 +00:00
|
|
|
|
2012-06-13 16:12:30 +00:00
|
|
|
// Add parameters
|
2012-09-24 08:06:31 +00:00
|
|
|
if (params.length > 0) {
|
2012-06-13 16:12:30 +00:00
|
|
|
var i;
|
2012-09-24 08:06:31 +00:00
|
|
|
for (i = 0; i < params.length; i++) {
|
|
|
|
|
var param = params[i];
|
2012-10-07 17:27:01 +00:00
|
|
|
output.push(" * @param {type} " + param + " Description");
|
2012-06-13 16:12:30 +00:00
|
|
|
}
|
|
|
|
|
}
|
2014-03-26 05:27:39 +00:00
|
|
|
|
|
|
|
|
if (returnsValue) output.push(" * @returns {type} Description");
|
2013-11-21 17:44:56 +00:00
|
|
|
|
2012-06-13 16:12:30 +00:00
|
|
|
// TODO use if 'return' is found in the function body?
|
|
|
|
|
//output += " * @return {type} ???\n";
|
2012-10-07 17:27:01 +00:00
|
|
|
output.push(" */");
|
2014-03-26 05:27:39 +00:00
|
|
|
|
2014-03-26 14:50:18 +00:00
|
|
|
return prefix + output.join("\n" + prefix) + "\n";
|
2012-06-13 16:12:30 +00:00
|
|
|
}
|
|
|
|
|
|
2014-03-26 05:27:39 +00:00
|
|
|
|
|
|
|
|
|
2012-06-13 16:12:30 +00:00
|
|
|
function annotate() {
|
2014-03-26 05:27:39 +00:00
|
|
|
|
2012-09-24 08:06:31 +00:00
|
|
|
var target = getTarget();
|
2014-03-26 05:27:39 +00:00
|
|
|
|
2013-07-19 08:02:56 +00:00
|
|
|
if (target === null) {
|
2012-09-24 08:06:31 +00:00
|
|
|
window.alert(EMPTY_MSG);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2014-03-26 05:27:39 +00:00
|
|
|
|
|
|
|
|
var comment = generateComment(target.name, target.params, target.returnsValue, target.prefix);
|
|
|
|
|
|
2012-06-13 16:12:30 +00:00
|
|
|
insert(comment);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CommandManager.register(MENU_NAME, COMMAND_ID, annotate);
|
2013-07-19 08:15:00 +00:00
|
|
|
KeyBindingManager.addBinding(COMMAND_ID, "Ctrl-Alt-A");
|
2012-06-13 16:12:30 +00:00
|
|
|
|
|
|
|
|
var menu = Menus.getMenu(Menus.AppMenuBar.EDIT_MENU);
|
|
|
|
|
menu.addMenuDivider();
|
2014-03-26 05:27:39 +00:00
|
|
|
menu.addMenuItem(COMMAND_ID);//"menu-edit-annotate",
|
2012-06-13 16:12:30 +00:00
|
|
|
|
2014-03-26 16:13:22 +00:00
|
|
|
});
|