Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.
Merged
Changes from 3 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
97 changes: 79 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,63 @@ 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(),
selStart = sel.start,
selEnd = sel.end,
prefixExp = new RegExp("^" + StringUtils.regexEscape(prefix), "g"),
lineSelection = sel.start.ch === 0 && sel.end.ch === 0 && sel.start.line !== sel.end.line,
multipleLine = sel.start.line !== sel.end.line,
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you change these variable names to something like isLineSelection and isMultipleLine to indicate that they are boolean values?

lineLength = editor.document.getLine(sel.start.line).length;

// For a multiple line selection transform it to a multiple whole line selection
if (!lineSelection && multipleLine) {
selStart = {line: sel.start.line, ch: 0};
selEnd = {line: sel.end.line + 1, ch: 0};

// For one line selections, just start at column 0 and end at the end of the line
} else if (!lineSelection) {
selStart = {line: sel.start.line, ch: 0};
selEnd = {line: sel.end.line, ch: lineLength};
}
Copy link
Contributor

Choose a reason for hiding this comment

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

You can nest these conditions so you are only checking the value of lineSelection once, and the code is a bit more clear:

if (!lineSelection) {
    if (multipleLine) {
        // For a multiple line selection transform it to a multiple whole line selection

    } else {
        // For one line selections, just start at column 0 and end at the end of the line

    }
}


// If the selection includes a comment or is already a line selection, delegate to Block-Comment
var ctx = TokenUtils.getInitialContext(editor._codeMirror, {line: selStart.line, ch: selStart.ch});
var hasNext = _findNextBlockComment(ctx, selEnd, prefixExp);
if (ctx.token.className === "comment" || hasNext || lineSelection) {
blockCommentPrefixSuffix(editor, prefix, suffix, false);

} else {
// Set the new selection and comment it
editor.setSelection(selStart, selEnd);
blockCommentPrefixSuffix(editor, prefix, suffix, false);

// Restore the old selection taking into account the prefix change
if (multipleLine) {
sel.start.line++;
sel.end.line++;
} else {
sel.start.ch += prefix.length;
sel.end.ch += prefix.length;
}
editor.setSelection(sel.start, sel.end);
}
}


/**
* Invokes a language-specific block-comment/uncomment handler
* @param {?Editor} editor If unspecified, applies to the currently focused editor
Expand All @@ -409,6 +448,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