Skip to content
Open
Show file tree
Hide file tree
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
55 changes: 54 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,61 @@ You'll get this:
*/
```

Text after cursor is inserted inside comment

```
/**|foo bar
```

And you'll get:

```
/**
* |foo bar
*/
```


Supports any language that uses `/* ... */`-style comments.

Simple predefined YUIDoc/Javadoc/PHPDoc-style comments for function and class. Just make sure the function/class defining row is right below when pressing Enter.

```
/**|
function foo(bar) {
```

To get:
```
/**
* function description
*
* @method foo
* @param {Type} bar
* @return {Type}
*/
function foo(bar) {
```

Or:
```
/**|
class Foo {
```

To get:
```
/**
* class description
*
* @class Foo
* @author Your Name <email>
* @constructor
*/
class Foo {
```



How to Install
==============
Expand All @@ -48,4 +101,4 @@ To install extensions:
MIT-licensed -- see `main.js` for details.

### Compatibility
Brackets Sprint 21 or newer (or Adobe Edge Code Preview 4 or newer).
Brackets Sprint 21 or newer (or Adobe Edge Code Preview 4 or newer).
60 changes: 49 additions & 11 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,14 @@
*/


/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50, white: true, unparam: true */
/*global define, brackets, $ */

define(function (require, exports, module) {
"use strict";

// Brackets modules
var EditorManager = brackets.getModule("editor/EditorManager"),
DocumentManager = brackets.getModule("document/DocumentManager"),
TokenUtils = brackets.getModule("utils/TokenUtils"),
KeyEvent = brackets.getModule("utils/KeyEvent"),
StringUtils = brackets.getModule("utils/StringUtils");

Expand All @@ -57,25 +55,29 @@ define(function (require, exports, module) {
//
// - pressing enter in *middle* of //-style comment should split it onto second line with // prefix


function handleEnterKey(editor) {
var cursor = editor.getCursorPos();
var token = editor._codeMirror.getTokenAt(cursor);
// console.log(token);

if (token.type === "comment") {
// But are we in a BLOCK comment?
// For now, we do a dumb approximation: does the line start with /* or *, and the last chars to left of cursor aren't */ ?
var line = editor.document.getLine(cursor.line);
var prefixMatch = line.match(/^\s*(\*|\/\*)/);
var followingText;
if (prefixMatch) {
if (!StringUtils.endsWith(token.string, "*/") || cursor.ch < token.end) {
var prefix, suffix;
var prefix, suffix, commentString = null;
if (prefixMatch[1] === "*") {
// Line other than first line
prefix = prefixMatch[0];
suffix = "";
} else {
// Get text after cursor
if (token.end > cursor.ch) {
followingText = token.string.substring(cursor.ch, token.end);
editor.document.replaceRange("", cursor, { line: cursor.line, ch: token.end });
}
// First line
prefix = prefixMatch[0].replace("/", " "); // don't reinsert /* on 2nd line

Expand All @@ -89,12 +91,51 @@ define(function (require, exports, module) {
} else {
suffix = "";
}

// if next line has function or class, insert some YUIDoc-like magic
var nextLine = editor.document.getLine(1 + cursor.line);
var reservedWord = nextLine.match(/class|function/) || null;

// has to have double asterisk and contain a keyword
if (line.match(/\/\*\*/) && reservedWord) {

var i = 0;
switch (reservedWord[0]) {
case "function":
var parts = nextLine.match(/(?:function+)(?:\s)(\w*)?\(([\w\s,]*)?\)/);
if (parts) {
commentString = "\n" + prefix + " function description\n" + prefix + "\n" + prefix + " @method " + parts[1];

// function variables
if (parts[2]) {
var variables = parts[2].replace(/\s/g, "").split(",");
for (i = 0; i < variables.length; i++) {
commentString += "\n" + prefix + " @param {Type} " + variables[i];
}
}
commentString += "\n" + prefix + " @return {Type} " + suffix;
}
break;
case "class":
var classname = nextLine.match(/class\s+(\w*)/);
if (classname) {
commentString = "\n" + prefix + " class description" + "\n" + prefix;
commentString += "\n" + prefix + " @class " + classname[1] || "";
commentString += "\n" + prefix + " @author Your Name <email>" + "\n" + prefix + " @constructor " + suffix;
}
break;
}
}
}
editor.document.replaceRange("\n" + prefix + " " + suffix, cursor);
editor.document.replaceRange(commentString || "\n" + prefix + " " + suffix, cursor);

cursor.line++;
cursor.ch = prefix.length + 1;
editor.setCursorPos(cursor.line, cursor.ch);
// If there was text, place inside comment
if (followingText) {
editor.document.replaceRange(followingText, editor.getCursorPos());
}
return true;
}
}
Expand All @@ -118,7 +159,6 @@ define(function (require, exports, module) {
}
}


// Attach Enter key listener
var editorHolder = $("#editor-holder")[0];
if (editorHolder) {
Expand All @@ -127,8 +167,6 @@ define(function (require, exports, module) {
console.warn("Unable to attach reasonable comments extension - assuming running in unit test window");
// (could verify that by looking at the path the way ExtensionLoader does, but seems like overkill)
}


// For unit tests
exports.handleEnterKey = handleEnterKey;
});
});