Merge pull request #5 from aonic/master

Fixed indentation issue, and added PHP functions and hotkey support
This commit is contained in:
David Deraedt 2012-10-08 00:42:21 -07:00
commit 270e33123d
2 changed files with 76 additions and 25 deletions

View file

@ -5,11 +5,13 @@ A brackets extension to generate JSDoc annotations for your functions.
To install, place the ```annotate``` folder inside the ```brackets/src/extensions/user``` folder.
**Compatible with Brackets Sprint10**
**Compatible with Brackets Sprint10 and higher**
Usage
=====
Open a project in Brackets, place your cursor before a function definition, and select ```Annotate``` form the ```Edit``` menu.
Open a project in Brackets, place your cursor before, or on the line of a function definition, and select ```Annotate``` form the ```Edit``` menu.
Alternatively, use the keyboard shortcut `CTRL`+`Shift`+`D` on Windows, or `⌘`+`Shift`+`D` on Mac.
This will create a JSDoc like annotation according to the function signature. It will add ```@private``` if the function starts with an underscore. It will create a ```@param``` for each parameter.
@ -17,6 +19,4 @@ This will create a JSDoc like annotation according to the function signature. I
Known issues
=====
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.

93
main.js
View file

@ -30,9 +30,10 @@ define(function (require, exports, module) {
'use strict';
var CommandManager = brackets.getModule("command/CommandManager"),
EditorManager = brackets.getModule("editor/EditorManager"),
Menus = brackets.getModule("command/Menus");
var CommandManager = brackets.getModule("command/CommandManager"),
EditorManager = brackets.getModule("editor/EditorManager"),
KeyBindingManager = brackets.getModule("command/KeyBindingManager"),
Menus = brackets.getModule("command/Menus");
var EMPTY_MSG = "No function found";
@ -44,19 +45,38 @@ define(function (require, exports, module) {
function insert(input) {
var editor = EditorManager.getCurrentFullEditor();
var pos = editor.getCursorPos();
var pos = editor.getCursorPos();
pos.ch = 0;
editor._codeMirror.replaceRange(input, pos);
EditorManager.focusEditor();
EditorManager.focusEditor();
}
/**
* 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) {
var indexOf = input.indexOf(match),
prefix = "";
if (indexOf != -1) {
prefix = input.substr(0, indexOf);
}
return prefix;
}
function getTarget() {
var editor = EditorManager.getCurrentFullEditor();
var pos = editor.getCursorPos();
var pos = editor.getCursorPos();
pos.ch = 0;
// Take the text of the document, starting with the current cursor line
var txtFrom = editor._codeMirror.getRange(pos, {line: editor._codeMirror.lineCount() });
@ -72,13 +92,36 @@ define(function (require, exports, module) {
if (results[0] === "function") {
return {
name: results[1],
params: results.slice(2)
params: results.slice(2),
prefix: getPrefix(txtFrom, results[0])
};
}
else if (results[0] === "var" && results[2] === "function") {
else if (results[0] === "private" && results[1] === "function") {
return {
name: results[1],
params: results.slice(3)
params: results.slice(3),
prefix: getPrefix(txtFrom, results[0])
};
}
else if (results[0] === "public" && results[1] === "function") {
return {
name: results[1],
params: results.slice(3),
prefix: getPrefix(txtFrom, results[0])
};
}
else if (results[0] === "static" && results[1] === "function") {
return {
name: results[1],
params: results.slice(3),
prefix: getPrefix(txtFrom, results[0])
};
}
else if (results[0] === "var" && results[1] === "function") {
return {
name: results[1],
params: results.slice(3),
prefix: getPrefix(txtFrom, results[0])
};
}
else {
@ -88,32 +131,39 @@ define(function (require, exports, module) {
}
function generateComment(fname, params) {
/**
* Generate comment block
* @param string fname function name
* @param string params function parameters
* @param string prefix whitespace prefix for comment block lines
*/
function generateComment(fname, params, prefix) {
var output = "/**\n";
var output = [];
output.push("/**");
// Assume function is private if it starts with an underscore
if (fname.charAt(0) === "_") {
output += " * @private\n";
output.push(" * @private");
}
// Add description
output += " * Description\n";
output.push(" * Description");
// Add parameters
if (params.length > 0) {
var i;
for (i = 0; i < params.length; i++) {
var param = params[i];
output += " * @param {type} " + param + " Description\n";
output.push(" * @param {type} " + param + " Description");
}
}
// TODO use if 'return' is found in the function body?
//output += " * @return {type} ???\n";
output += " */\n";
output.push(" */");
return output;
return prefix + output.join("\n" + prefix) + "\n";
}
@ -127,16 +177,17 @@ define(function (require, exports, module) {
return;
}
var comment = generateComment(target.name, target.params);
var comment = generateComment(target.name, target.params, target.prefix);
insert(comment);
}
CommandManager.register(MENU_NAME, COMMAND_ID, annotate);
KeyBindingManager.addBinding(COMMAND_ID, "Ctrl-Shift-D");
var menu = Menus.getMenu(Menus.AppMenuBar.EDIT_MENU);
menu.addMenuDivider();
menu.addMenuItem(COMMAND_ID);//"menu-edit-annotate",
});
});