mirror of
https://github.com/Hopiu/annotate-extension.git
synced 2026-03-17 00:10:23 +00:00
Merge pull request #21 from ajorquera/master
Change to use REGEX. Added return feature.
This commit is contained in:
commit
71cc72fd93
1 changed files with 65 additions and 99 deletions
164
main.js
164
main.js
|
|
@ -39,144 +39,108 @@ define(function (require, exports, module) {
|
|||
var EMPTY_MSG = "No function found";
|
||||
var COMMAND_ID = "annotate.annotate";
|
||||
var MENU_NAME = "Annotate function";
|
||||
|
||||
|
||||
|
||||
var REGEX_PATTERNS = {
|
||||
comment: '\\/\\*.*\\*\\/',
|
||||
jsVariable: '[$A-Za-z_][0-9A-Za-z_$]*'
|
||||
};
|
||||
|
||||
function insert(input) {
|
||||
|
||||
|
||||
var editor = EditorManager.getCurrentFullEditor();
|
||||
var pos = editor.getCursorPos();
|
||||
pos.ch = 0;
|
||||
|
||||
|
||||
editor._codeMirror.replaceRange(input, pos);
|
||||
|
||||
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);
|
||||
prefix = input.substr(0, indexOf).replace(/[^\s\n]/g, ' ');
|
||||
}
|
||||
|
||||
|
||||
return prefix;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
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)?\\s*'+ REGEX_PATTERNS.jsVariable + '\\s*=\\s*function\\s*\\(\\s*(' +
|
||||
REGEX_PATTERNS.jsVariable + '\\s*(,\\s*)?)*\\s*\\)\\s*','g');
|
||||
|
||||
var editor = EditorManager.getCurrentFullEditor();
|
||||
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() });
|
||||
|
||||
// For now, we generate annotations from the signature only (missing return statements)
|
||||
|
||||
//checks if there is a return value
|
||||
var returnsValue = txtFrom.substr( txtFrom.indexOf('{'), txtFrom.indexOf('}')).search('return') !== -1;
|
||||
|
||||
txtFrom = txtFrom.substr(0, txtFrom.indexOf("{"));
|
||||
|
||||
// Look for words
|
||||
var re = /[$,\w]+/g;
|
||||
var results = txtFrom.match(re);
|
||||
|
||||
// The first word found should be "function", and next ones parameters
|
||||
|
||||
if (results[0] === "function") {
|
||||
return {
|
||||
name: results[1],
|
||||
params: results.slice(2),
|
||||
prefix: getPrefix(txtFrom, results[0])
|
||||
};
|
||||
} else if (results[0] === "private" && results[1] === "function") {
|
||||
return {
|
||||
name: results[1],
|
||||
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[2] === "function") {
|
||||
//take any comment off
|
||||
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 {
|
||||
name: results[1],
|
||||
params: results.slice(3),
|
||||
prefix: getPrefix(txtFrom, results[0])
|
||||
//check for 'var'
|
||||
name:results[results.indexOf('var') === -1 ? 0:1],
|
||||
params:results.slice(results.indexOf('var') === -1 ? 2:3),
|
||||
prefix: getPrefix(txtFrom, results[0]),
|
||||
returnsValue:returnsValue
|
||||
};
|
||||
} else if(results[0] === "this" && results[2] === "function"){
|
||||
|
||||
return {
|
||||
name: results[1],
|
||||
params: results.slice(3),
|
||||
prefix: getPrefix(txtFrom, results[0])
|
||||
case functionDeclarationRegex.test(txtFrom):
|
||||
console.log(results[1]);
|
||||
return {
|
||||
name:results[1],
|
||||
params:results.slice(2),
|
||||
prefix: getPrefix(txtFrom, results[0]),
|
||||
returnsValue:returnsValue
|
||||
};
|
||||
} else if (results[1] === "function") {
|
||||
|
||||
return {
|
||||
name: results[0],
|
||||
params: results.slice(2),
|
||||
prefix: getPrefix(txtFrom, results[0])
|
||||
};
|
||||
} else if (results[1] === "function") {
|
||||
return {
|
||||
name: results[0],
|
||||
params: results.slice(2),
|
||||
prefix: getPrefix(txtFrom, results[0])
|
||||
};
|
||||
} else if(txtFrom.indexOf('.prototype.')){
|
||||
var i = 1;
|
||||
for(;i<results.length; i+=1){
|
||||
if(results[i] === 'function'){
|
||||
return {
|
||||
name: results[i-1],
|
||||
params: results.slice(i+1),
|
||||
prefix: getPrefix(txtFrom, results[0])
|
||||
};
|
||||
}
|
||||
}
|
||||
} else {
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
|
||||
function generateComment(fname, params,returnsValue, prefix) {
|
||||
|
||||
var output = [];
|
||||
output.push("/**");
|
||||
|
||||
|
||||
// Assume function is private if it starts with an underscore
|
||||
if (fname.charAt(0) === "_") {
|
||||
output.push(" * @private");
|
||||
}
|
||||
|
||||
|
||||
// Add description
|
||||
output.push(" * Description");
|
||||
|
||||
|
||||
// Add parameters
|
||||
if (params.length > 0) {
|
||||
var i;
|
||||
|
|
@ -185,27 +149,29 @@ define(function (require, exports, module) {
|
|||
output.push(" * @param {type} " + param + " Description");
|
||||
}
|
||||
}
|
||||
|
||||
if (returnsValue) output.push(" * @returns {type} Description");
|
||||
|
||||
// TODO use if 'return' is found in the function body?
|
||||
//output += " * @return {type} ???\n";
|
||||
output.push(" */");
|
||||
|
||||
return prefix + output.join("\n" + prefix) + "\n";
|
||||
|
||||
return prefix + output.join(prefix) + "\n";
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
function annotate() {
|
||||
|
||||
|
||||
var target = getTarget();
|
||||
|
||||
|
||||
if (target === null) {
|
||||
window.alert(EMPTY_MSG);
|
||||
return;
|
||||
}
|
||||
|
||||
var comment = generateComment(target.name, target.params, target.prefix);
|
||||
|
||||
|
||||
var comment = generateComment(target.name, target.params, target.returnsValue, target.prefix);
|
||||
|
||||
insert(comment);
|
||||
}
|
||||
|
||||
|
|
@ -215,6 +181,6 @@ define(function (require, exports, module) {
|
|||
|
||||
var menu = Menus.getMenu(Menus.AppMenuBar.EDIT_MENU);
|
||||
menu.addMenuDivider();
|
||||
menu.addMenuItem(COMMAND_ID);//"menu-edit-annotate",
|
||||
menu.addMenuItem(COMMAND_ID);//"menu-edit-annotate",
|
||||
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue