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

duplicateText on last line of file adds new line; tests #1166

Closed
wants to merge 1 commit into from
Closed
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
85 changes: 43 additions & 42 deletions src/editor/EditorCommandHandlers.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
/*
* Copyright (c) 2012 Adobe Systems Incorporated. All rights reserved.
*
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*
*/

/*jslint vars: true, plusplus: true, devel: true, browser: true, nomen: true, indent: 4, maxerr: 50 */
Expand All @@ -30,37 +30,37 @@
*/
define(function (require, exports, module) {
'use strict';

// Load dependent modules
var Commands = require("command/Commands"),
Strings = require("strings"),
CommandManager = require("command/CommandManager"),
EditorManager = require("editor/EditorManager");


/**
* Add or remove line-comment tokens to all the lines in the selected range, preserving selection
* and cursor position. Applies to currently focused Editor.
*
*
* If all non-whitespace lines are already commented out, then we uncomment; otherwise we comment
* out. Commenting out adds "//" to at column 0 of every line. Uncommenting removes the first "//"
* on each line (if any - empty lines might not have one).
*/
function lineCommentSlashSlash(editor) {

var doc = editor.document;
var sel = editor.getSelection();
var startLine = sel.start.line;
var endLine = sel.end.line;

// Is a range of text selected? (vs just an insertion pt)
var hasSelection = (startLine !== endLine) || (sel.start.ch !== sel.end.ch);

// In full-line selection, cursor pos is start of next line - but don't want to modify that line
if (sel.end.ch === 0 && hasSelection) {
endLine--;
}

// Decide if we're commenting vs. un-commenting
// Are there any non-blank lines that aren't commented out? (We ignore blank lines because
// some editors like Sublime don't comment them out)
Expand All @@ -75,22 +75,22 @@ define(function (require, exports, module) {
break;
}
}

// Make the edit
doc.batchOperation(function () {

if (containsUncommented) {
// Comment out - prepend "//" to each line
for (i = startLine; i <= endLine; i++) {
doc.replaceRange("//", {line: i, ch: 0});
}

// Make sure selection includes "//" that was added at start of range
if (sel.start.ch === 0 && hasSelection) {
// use *current* selection end, which has been updated for our text insertions
editor.setSelection({line: startLine, ch: 0}, editor.getSelection().end);
}

} else {
// Uncomment - remove first "//" on each line (if any)
for (i = startLine; i <= endLine; i++) {
Expand All @@ -102,7 +102,7 @@ define(function (require, exports, module) {
}
}
});

}

/**
Expand All @@ -114,16 +114,16 @@ define(function (require, exports, module) {
if (!editor) {
return;
}

var mode = editor.getModeForSelection();

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


/**
* Duplicates the selected text, or current line if no selection. The cursor/selection is left
* on the second copy.
Expand All @@ -133,20 +133,21 @@ define(function (require, exports, module) {
if (!editor) {
return;
}

var sel = editor.getSelection();

var hasSelection = (sel.start.line !== sel.end.line) || (sel.start.ch !== sel.end.ch);


var doc = editor.document,
sel = editor.getSelection(),
hasSelection = (sel.start.line !== sel.end.line) || (sel.start.ch !== sel.end.ch),
delimiter = "";

if (!hasSelection) {
sel.start.ch = 0;
sel.end = {line: sel.start.line + 1, ch: 0};
delimiter = typeof doc.getLine(sel.end.line) == "undefined" ? "\n" : delimiter;
}

// Make the edit
var doc = editor.document;

var selectedText = doc.getRange(sel.start, sel.end);

var selectedText = doc.getRange(sel.start, sel.end) + delimiter;
doc.replaceRange(selectedText, sel.start);
}

Expand All @@ -158,10 +159,10 @@ define(function (require, exports, module) {
if (!editor) {
return;
}

editor._codeMirror.execCommand("indentMore");
}

/**
* Unindent a line of text if no selection. Otherwise, unindent all lines in selection.
*/
Expand All @@ -170,10 +171,10 @@ define(function (require, exports, module) {
if (!editor) {
return;
}

editor._codeMirror.execCommand("indentLess");
}

// Register commands
CommandManager.register(Strings.CMD_INDENT, Commands.EDIT_INDENT, indentText);
CommandManager.register(Strings.CMD_UNINDENT, Commands.EDIT_UNINDENT, unidentText);
Expand Down
Loading