Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.
Merged
Changes from 1 commit
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
83 changes: 65 additions & 18 deletions src/editor/EditorCommandHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,24 +128,6 @@ define(function (require, exports, module) {
});

}

/**
* Invokes a language-specific line-comment/uncomment handler
* @param {?Editor} editor If unspecified, applies to the currently focused editor
*/
function lineComment(editor) {
editor = editor || EditorManager.getFocusedEditor();
if (!editor) {
return;
}

var mode = editor.getModeForSelection();

// Currently we only support languages with "//" commenting
if (mode === "javascript" || mode === "less") {
lineCommentSlashSlash(editor);
}
}


/**
Expand Down Expand Up @@ -388,6 +370,49 @@ define(function (require, exports, module) {
}
}


/**
* Add or remove block-comment tokens to the selection, preserving selection
* and cursor position. Applies to the currently focused Editor.
*
* The implementation uses blockCommentPrefixSuffix, with the exception of the case where
* there is no selection on a uncommented and not empty line. In this case the whole lines gets
* commented in a bock-comment.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: bock

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reminder: fix this typo

*
* @param {!Editor} editor
* @param {!String} prefix
* @param {!String} suffix
*/
function lineCommentPrefixSuffix(editor, prefix, suffix) {
var sel = editor.getSelection(),
ctx = TokenUtils.getInitialContext(editor._codeMirror, {line: sel.start.line, ch: sel.start.ch}),
line = editor.document.getLine(sel.start.line),
hasSelection = (sel.start.line !== sel.end.line) || (sel.start.ch !== sel.end.ch);

// If we are not inside a comment and there is no selection and the line is not empty,
// we comment the complete line from the first non-empty character
if (ctx.token.className !== "comment" && !hasSelection && line.trim().length > 0) {
var start = line.length - line.replace(/^\s+/, "").length,
end = line.length;

// Make a new selection of the line so that the prefix and suffix get placed where we want
editor.setSelection({line: sel.start.line, ch: start}, {line: sel.end.line, ch: end});
blockCommentPrefixSuffix(editor, prefix, suffix, false);

// Restore the selection. Moving the cursor if needed
if (sel.start.ch > start) {
sel.start.ch += prefix.length;
sel.end.ch += prefix.length;
}
editor.setSelection(sel.start, sel.end);

// if not, just do a normal block-comment
} else {
blockCommentPrefixSuffix(editor, prefix, suffix, false);
}
}


/**
* Invokes a language-specific block-comment/uncomment handler
* @param {?Editor} editor If unspecified, applies to the currently focused editor
Expand All @@ -409,6 +434,28 @@ define(function (require, exports, module) {
}
}

/**
* Invokes a language-specific line-comment/uncomment handler
* @param {?Editor} editor If unspecified, applies to the currently focused editor
*/
function lineComment(editor) {
editor = editor || EditorManager.getFocusedEditor();
if (!editor) {
return;
}

var mode = editor.getModeForSelection();

// Currently we only support languages with "//" commenting
if (mode === "javascript" || mode === "less") {
lineCommentSlashSlash(editor);
} else if (mode === "css") {
lineCommentPrefixSuffix(editor, "/*", "*/");
} else if (mode === "html") {
lineCommentPrefixSuffix(editor, "<!--", "-->");
}
}


/**
* Duplicates the selected text, or current line if no selection. The cursor/selection is left
Expand Down