Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit 529eab7

Browse files
committed
Merge pull request #2133 from TomMalbran/tom/line-comment
CSS and HTML Line Comment
2 parents 5453d95 + 642ff84 commit 529eab7

1 file changed

Lines changed: 82 additions & 18 deletions

File tree

src/editor/EditorCommandHandlers.js

Lines changed: 82 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -128,24 +128,6 @@ define(function (require, exports, module) {
128128
});
129129

130130
}
131-
132-
/**
133-
* Invokes a language-specific line-comment/uncomment handler
134-
* @param {?Editor} editor If unspecified, applies to the currently focused editor
135-
*/
136-
function lineComment(editor) {
137-
editor = editor || EditorManager.getFocusedEditor();
138-
if (!editor) {
139-
return;
140-
}
141-
142-
var mode = editor.getModeForSelection();
143-
144-
// Currently we only support languages with "//" commenting
145-
if (mode === "javascript" || mode === "less") {
146-
lineCommentSlashSlash(editor);
147-
}
148-
}
149131

150132

151133
/**
@@ -387,6 +369,66 @@ define(function (require, exports, module) {
387369
}
388370
}
389371

372+
373+
/**
374+
* Add or remove block-comment tokens to the selection, preserving selection
375+
* and cursor position. Applies to the currently focused Editor.
376+
*
377+
* The implementation uses blockCommentPrefixSuffix, with the exception of the case where
378+
* there is no selection on a uncommented and not empty line. In this case the whole lines gets
379+
* commented in a block-comment.
380+
*
381+
* @param {!Editor} editor
382+
* @param {!String} prefix
383+
* @param {!String} suffix
384+
*/
385+
function lineCommentPrefixSuffix(editor, prefix, suffix) {
386+
var sel = editor.getSelection(),
387+
selStart = sel.start,
388+
selEnd = sel.end,
389+
prefixExp = new RegExp("^" + StringUtils.regexEscape(prefix), "g"),
390+
isLineSelection = sel.start.ch === 0 && sel.end.ch === 0 && sel.start.line !== sel.end.line,
391+
isMultipleLine = sel.start.line !== sel.end.line,
392+
lineLength = editor.document.getLine(sel.start.line).length;
393+
394+
// Line selections already behave like we want to
395+
if (!isLineSelection) {
396+
// For a multiple line selection transform it to a multiple whole line selection
397+
if (isMultipleLine) {
398+
selStart = {line: sel.start.line, ch: 0};
399+
selEnd = {line: sel.end.line + 1, ch: 0};
400+
401+
// For one line selections, just start at column 0 and end at the end of the line
402+
} else {
403+
selStart = {line: sel.start.line, ch: 0};
404+
selEnd = {line: sel.end.line, ch: lineLength};
405+
}
406+
}
407+
408+
// If the selection includes a comment or is already a line selection, delegate to Block-Comment
409+
var ctx = TokenUtils.getInitialContext(editor._codeMirror, {line: selStart.line, ch: selStart.ch});
410+
var hasNext = _findNextBlockComment(ctx, selEnd, prefixExp);
411+
if (ctx.token.className === "comment" || hasNext || isLineSelection) {
412+
blockCommentPrefixSuffix(editor, prefix, suffix, false);
413+
414+
} else {
415+
// Set the new selection and comment it
416+
editor.setSelection(selStart, selEnd);
417+
blockCommentPrefixSuffix(editor, prefix, suffix, false);
418+
419+
// Restore the old selection taking into account the prefix change
420+
if (isMultipleLine) {
421+
sel.start.line++;
422+
sel.end.line++;
423+
} else {
424+
sel.start.ch += prefix.length;
425+
sel.end.ch += prefix.length;
426+
}
427+
editor.setSelection(sel.start, sel.end);
428+
}
429+
}
430+
431+
390432
/**
391433
* Invokes a language-specific block-comment/uncomment handler
392434
* @param {?Editor} editor If unspecified, applies to the currently focused editor
@@ -408,6 +450,28 @@ define(function (require, exports, module) {
408450
}
409451
}
410452

453+
/**
454+
* Invokes a language-specific line-comment/uncomment handler
455+
* @param {?Editor} editor If unspecified, applies to the currently focused editor
456+
*/
457+
function lineComment(editor) {
458+
editor = editor || EditorManager.getFocusedEditor();
459+
if (!editor) {
460+
return;
461+
}
462+
463+
var mode = editor.getModeForSelection();
464+
465+
// Currently we only support languages with "//" commenting
466+
if (mode === "javascript" || mode === "less") {
467+
lineCommentSlashSlash(editor);
468+
} else if (mode === "css") {
469+
lineCommentPrefixSuffix(editor, "/*", "*/");
470+
} else if (mode === "html") {
471+
lineCommentPrefixSuffix(editor, "<!--", "-->");
472+
}
473+
}
474+
411475

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

0 commit comments

Comments
 (0)