From 49aeef99b6057a10d42850751fa20121741df792 Mon Sep 17 00:00:00 2001 From: probablycorey Date: Mon, 13 May 2013 08:53:22 -0700 Subject: [PATCH] Add config option editor.normalizeIndentOnPaste --- spec/app/edit-session-spec.coffee | 88 ++++++++++++++++++------------- src/app/edit-session.coffee | 7 +-- src/app/editor.coffee | 1 + 3 files changed, 55 insertions(+), 41 deletions(-) diff --git a/spec/app/edit-session-spec.coffee b/spec/app/edit-session-spec.coffee index 8833ab445..956d327db 100644 --- a/spec/app/edit-session-spec.coffee +++ b/spec/app/edit-session-spec.coffee @@ -2323,7 +2323,28 @@ describe "EditSession", -> editSession.pasteText() expect(editSession.lineForBufferRow(10)).toBe " var number" - describe "editor.normalizePastedText", -> + describe "editor.normalizeIndentOnPaste", -> + beforeEach -> + config.set('editor.normalizeIndentOnPaste', true) + + it "does not normalize the indentation level of the text when editor.autoIndentOnPaste is true", -> + copyText(" function() {\nvar cool = 1;\n }\n") + config.set('editor.autoIndentOnPaste', true) + editSession.setCursorBufferPosition([5, ]) + editSession.pasteText() + expect(editSession.lineForBufferRow(5)).toBe " function() {" + expect(editSession.lineForBufferRow(6)).toBe " var cool = 1;" + expect(editSession.lineForBufferRow(7)).toBe " }" + + it "does not normalize the indentation level of the text when editor.normalizeIndentOnPaste is false", -> + copyText(" function() {\nvar cool = 1;\n }\n") + config.set('editor.normalizeIndentOnPaste', false) + editSession.setCursorBufferPosition([5, 2]) + editSession.pasteText() + expect(editSession.lineForBufferRow(5)).toBe " function() {" + expect(editSession.lineForBufferRow(6)).toBe "var cool = 1;" + expect(editSession.lineForBufferRow(7)).toBe " }" + describe "when the inserted text contains no newlines", -> it "does not adjust the indentation level of the text", -> editSession.setCursorBufferPosition([5, 2]) @@ -2331,48 +2352,39 @@ describe "EditSession", -> expect(editSession.lineForBufferRow(5)).toBe " foo current = items.shift();" describe "when the inserted text contains newlines", -> - it "does not normalize the indentation level of the text when editor.autoIndentOnPaste is true", -> - copyText(" function() {\nvar cool = 1;\n }\n") - config.set('editor.autoIndentOnPaste', true) - editSession.setCursorBufferPosition([5, 2]) - editSession.pasteText() - expect(editSession.lineForBufferRow(5)).toBe " function() {" - expect(editSession.lineForBufferRow(6)).toBe " var cool = 1;" - expect(editSession.lineForBufferRow(7)).toBe " }" + describe "when copied text includes whitespace on first line", -> + describe "when cursor is preceded by whitespace and followed non-whitespace", -> + it "normalizes indented lines to the cursor's current indentation level", -> + copyText(" while (true) {\n foo();\n }\n", {startColumn: 4}) + editSession.setCursorBufferPosition([3, 4]) + editSession.pasteText() - describe "when copied text includes whitespace on first line", -> - describe "when cursor is preceded whitespace and followed non-whitespace", -> - it "normalizes indented lines to the cursor's current indentation level", -> - copyText(" while (true) {\n foo();\n }\n", {startColumn: 4}) - editSession.setCursorBufferPosition([3, 4]) - editSession.pasteText() + expect(editSession.lineForBufferRow(3)).toBe " while (true) {" + expect(editSession.lineForBufferRow(4)).toBe " foo();" + expect(editSession.lineForBufferRow(5)).toBe " }" + expect(editSession.lineForBufferRow(6)).toBe "var pivot = items.shift(), current, left = [], right = [];" - expect(editSession.lineForBufferRow(3)).toBe " while (true) {" - expect(editSession.lineForBufferRow(4)).toBe " foo();" - expect(editSession.lineForBufferRow(5)).toBe " }" - expect(editSession.lineForBufferRow(6)).toBe "var pivot = items.shift(), current, left = [], right = [];" + describe "when cursor is preceded by whitespace and followed by whitespace", -> + it "normalizes indented lines to the cursor's current indentation level", -> + copyText(" while (true) {\n foo();\n }\n", {startColumn: 0}) + editSession.setCursorBufferPosition([3, 4]) + editSession.pasteText() - describe "when cursor is preceded whitespace and followed by whitespace", -> - it "normalizes indented lines to the cursor's current indentation level", -> - copyText(" while (true) {\n foo();\n }\n", {startColumn: 0}) - editSession.setCursorBufferPosition([3, 4]) - editSession.pasteText() + expect(editSession.lineForBufferRow(3)).toBe " while (true) {" + expect(editSession.lineForBufferRow(4)).toBe " foo();" + expect(editSession.lineForBufferRow(5)).toBe " }" + expect(editSession.lineForBufferRow(6)).toBe "var pivot = items.shift(), current, left = [], right = [];" - expect(editSession.lineForBufferRow(3)).toBe " while (true) {" - expect(editSession.lineForBufferRow(4)).toBe " foo();" - expect(editSession.lineForBufferRow(5)).toBe " }" - expect(editSession.lineForBufferRow(6)).toBe "var pivot = items.shift(), current, left = [], right = [];" + describe "when the cursor is preceded by non-whitespace characters", -> + it "normalizes the indentation level of all lines based on the level of the existing first line", -> + copyText(" while (true) {\n foo();\n }\n", {startColumn: 0}) + editSession.setCursorBufferPosition([1, Infinity]) + editSession.pasteText() - describe "when the cursor is preceded by non-whitespace characters", -> - it "normalizes the indentation level of all lines based on the level of the existing first line", -> - copyText(" while (true) {\n foo();\n }\n", {startColumn: 0}) - editSession.setCursorBufferPosition([1, Infinity]) - editSession.pasteText() - - expect(editSession.lineForBufferRow(1)).toBe " var sort = function(items) { while (true) {" - expect(editSession.lineForBufferRow(2)).toBe " foo();" - expect(editSession.lineForBufferRow(3)).toBe " }" - expect(editSession.lineForBufferRow(4)).toBe "" + expect(editSession.lineForBufferRow(1)).toBe " var sort = function(items) { while (true) {" + expect(editSession.lineForBufferRow(2)).toBe " foo();" + expect(editSession.lineForBufferRow(3)).toBe " }" + expect(editSession.lineForBufferRow(4)).toBe "" it "autoIndentSelectedRows auto-indents the selection", -> editSession.setCursorBufferPosition([2, 0]) diff --git a/src/app/edit-session.coffee b/src/app/edit-session.coffee index b7baf0275..37b762ef1 100644 --- a/src/app/edit-session.coffee +++ b/src/app/edit-session.coffee @@ -475,10 +475,11 @@ class EditSession # # options - A set of options equivalent to {Selection.insertText}. pasteText: (options={}) -> - options.autoIndent ?= @shouldAutoIndentPastedText() - [text, metadata] = pasteboard.read() - _.extend(options, metadata) if metadata + + options.autoIndent ?= @shouldAutoIndentPastedText() + if config.get('editor.normalizeIndentOnPaste') and metadata + options.indentBasis ?= metadata.indentBasis @insertText(text, options) diff --git a/src/app/editor.coffee b/src/app/editor.coffee index 42888db14..2b75fb588 100644 --- a/src/app/editor.coffee +++ b/src/app/editor.coffee @@ -22,6 +22,7 @@ class Editor extends View showLineNumbers: true autoIndent: true autoIndentOnPaste: false + normalizeIndentOnPaste: false nonWordCharacters: "./\\()\"':,.;<>~!@#$%^&*|+=[]{}`~?-" preferredLineLength: 80