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

Commit 8affe4a

Browse files
committed
Merge pull request #7081 from adobe/nj/issue-7075
Don't delete the last line if the selection ends right at the beginning
2 parents 7ebb751 + e4b17bb commit 8affe4a

2 files changed

Lines changed: 32 additions & 6 deletions

File tree

src/editor/EditorCommandHandlers.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -592,20 +592,29 @@ define(function (require, exports, module) {
592592
var from,
593593
to,
594594
sel = editor.getSelection(),
595-
doc = editor.document;
596-
595+
doc = editor.document,
596+
endLine;
597+
597598
from = {line: sel.start.line, ch: 0};
598-
to = {line: sel.end.line + 1, ch: 0};
599-
if (to.line === editor.getLastVisibleLine() + 1) {
599+
600+
if (sel.end.line === editor.getLastVisibleLine()) {
600601
// Instead of deleting the newline after the last line, delete the newline
601602
// before the first line--unless this is the entire visible content of the editor,
602603
// in which case just delete the line content.
603604
if (from.line > editor.getFirstVisibleLine()) {
604605
from.line -= 1;
605606
from.ch = doc.getLine(from.line).length;
606607
}
607-
to.line -= 1;
608-
to.ch = doc.getLine(to.line).length;
608+
to = {line: sel.end.line, ch: doc.getLine(sel.end.line).length};
609+
} else {
610+
// Don't delete the line that the selection ends on if the selection
611+
// starts on a previous line and ends right at the beginning of the
612+
// last line.
613+
endLine = sel.end.line + 1;
614+
if (sel.start.line < sel.end.line && sel.end.ch === 0) {
615+
endLine--;
616+
}
617+
to = {line: endLine, ch: 0};
609618
}
610619

611620
doc.replaceRange("", from, to);

test/spec/EditorCommandHandlers-test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2185,6 +2185,23 @@ define(function (require, exports, module) {
21852185
var expectedText = defaultContent.split("\n").slice(3).join("\n");
21862186
expect(myDocument.getText()).toEqual(expectedText);
21872187
});
2188+
2189+
it("should not delete an extra line after a whole selected line", function () {
2190+
myEditor.setSelection({line: 1, ch: 0}, {line: 2, ch: 0});
2191+
CommandManager.execute(Commands.EDIT_DELETE_LINES, myEditor);
2192+
2193+
var expectedText = defaultContent.split("\n");
2194+
expectedText.splice(1, 1);
2195+
expect(myDocument.getText()).toEqual(expectedText.join("\n"));
2196+
});
2197+
2198+
it("should not delete an extra line after several whole lines that are selected", function () {
2199+
myEditor.setSelection({line: 0, ch: 0}, {line: 3, ch: 0});
2200+
CommandManager.execute(Commands.EDIT_DELETE_LINES, myEditor);
2201+
2202+
var expectedText = defaultContent.split("\n").slice(3).join("\n");
2203+
expect(myDocument.getText()).toEqual(expectedText);
2204+
});
21882205

21892206
it("should delete multiple lines ending at the bottom when selection spans them", function () {
21902207
myEditor.setSelection({line: 5, ch: 5}, {line: 7, ch: 0});

0 commit comments

Comments
 (0)