From 5ec38f84c57af4804becc748ec106292a5d147ae Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Mon, 17 Nov 2014 15:50:06 -0800 Subject: [PATCH 1/2] Reorganize editor normalizeIndentOnPaste specs --- spec/text-editor-spec.coffee | 186 +++++++++++++++++------------------ 1 file changed, 93 insertions(+), 93 deletions(-) diff --git a/spec/text-editor-spec.coffee b/spec/text-editor-spec.coffee index dc6525989..6e11bb981 100644 --- a/spec/text-editor-spec.coffee +++ b/spec/text-editor-spec.coffee @@ -2567,13 +2567,23 @@ describe "TextEditor", -> ]) describe ".pasteText()", -> + copyText = (text, {startColumn, textEditor}={}) -> + startColumn ?= 0 + textEditor ?= editor + textEditor.setCursorBufferPosition([0, 0]) + textEditor.insertText(text) + numberOfNewlines = text.match(/\n/g)?.length + endColumn = text.match(/[^\n]*$/)[0]?.length + textEditor.getLastSelection().setBufferRange([[0,startColumn], [numberOfNewlines,endColumn]]) + textEditor.cutSelectedText() + it "pastes text into the buffer", -> atom.clipboard.write('first') editor.pasteText() expect(editor.lineTextForBufferRow(0)).toBe "var first = function () {" expect(editor.lineTextForBufferRow(1)).toBe " var first = function(items) {" - describe "when config.autoIndentOnPaste is true", -> + describe "when `autoIndentOnPaste` is true", -> beforeEach -> atom.config.set("editor.autoIndentOnPaste", true) @@ -2600,8 +2610,81 @@ describe "TextEditor", -> console.log JSON.stringify(editor.lineTextForBufferRow(1)) expect(editor.lineTextForBufferRow(1)).toBe(" y(); z();") - describe "when config.autoIndentOnPaste is false", -> + describe "when `autoIndentOnPaste` is false and `normalizeIndentOnPaste` is true", -> beforeEach -> + atom.config.set('editor.autoIndentOnPaste', false) + atom.config.set('editor.normalizeIndentOnPaste', true) + + describe "when the inserted text contains no newlines", -> + it "does not adjust the indentation level of the text", -> + editor.setCursorBufferPosition([5, 2]) + editor.insertText("foo", indentBasis: 5) + expect(editor.lineTextForBufferRow(5)).toBe " foo current = items.shift();" + + it "does not adjust the whitespace if there are preceding characters", -> + copyText(" foo") + editor.setCursorBufferPosition([5, 30]) + editor.pasteText() + + expect(editor.lineTextForBufferRow(5)).toBe " current = items.shift(); foo" + + describe "when the inserted text contains newlines", -> + describe "when the cursor is preceded only by whitespace characters", -> + it "normalizes indented lines to the cursor's current indentation level", -> + copyText(" while (true) {\n foo();\n }\n", {startColumn: 2}) + editor.setCursorBufferPosition([3, 4]) + editor.pasteText() + + expect(editor.lineTextForBufferRow(3)).toBe " while (true) {" + expect(editor.lineTextForBufferRow(4)).toBe " foo();" + expect(editor.lineTextForBufferRow(5)).toBe " }" + expect(editor.lineTextForBufferRow(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}) + editor.setCursorBufferPosition([1, Infinity]) + editor.pasteText() + + expect(editor.lineTextForBufferRow(1)).toBe " var sort = function(items) {while (true) {" + expect(editor.lineTextForBufferRow(2)).toBe " foo();" + expect(editor.lineTextForBufferRow(3)).toBe " }" + expect(editor.lineTextForBufferRow(4)).toBe "" + + describe 'when scoped settings are used', -> + coffeeEditor = null + beforeEach -> + waitsForPromise -> + atom.packages.activatePackage('language-coffee-script') + waitsForPromise -> + atom.project.open('coffee.coffee', autoIndent: false).then (o) -> coffeeEditor = o + + runs -> + atom.config.set('.source.js', 'editor.normalizeIndentOnPaste', true) + atom.config.set('.source.coffee', 'editor.normalizeIndentOnPaste', false) + + afterEach: -> + atom.packages.deactivatePackages() + atom.packages.unloadPackages() + + it "normalizes the indentation level based on scoped settings", -> + copyText(" while (true) {\n foo();\n }\n", {startColumn: 2, textEditor: coffeeEditor}) + coffeeEditor.setCursorBufferPosition([4, 4]) + coffeeEditor.pasteText() + expect(coffeeEditor.lineTextForBufferRow(4)).toBe " while (true) {" + expect(coffeeEditor.lineTextForBufferRow(5)).toBe " foo();" + expect(coffeeEditor.lineTextForBufferRow(6)).toBe " }" + + copyText(" while (true) {\n foo();\n }\n", {startColumn: 2}) + editor.setCursorBufferPosition([3, 4]) + editor.pasteText() + expect(editor.lineTextForBufferRow(3)).toBe " while (true) {" + expect(editor.lineTextForBufferRow(4)).toBe " foo();" + expect(editor.lineTextForBufferRow(5)).toBe " }" + + describe "when `autoIndentOnPaste` and `normalizeIndentOnPaste` are both false", -> + beforeEach -> + atom.config.set('editor.normalizeIndentOnPaste', false) atom.config.set("editor.autoIndentOnPaste", false) it "does not auto-indent the pasted text", -> @@ -2611,6 +2694,14 @@ describe "TextEditor", -> expect(editor.lineTextForBufferRow(5)).toBe("console.log(x);") expect(editor.lineTextForBufferRow(6)).toBe("console.log(y);") + it "does not normalize the indentation level of the text", -> + copyText(" function() {\nvar cool = 1;\n }\n") + editor.setCursorBufferPosition([5, 2]) + editor.pasteText() + expect(editor.lineTextForBufferRow(5)).toBe " function() {" + expect(editor.lineTextForBufferRow(6)).toBe "var cool = 1;" + expect(editor.lineTextForBufferRow(7)).toBe " }" + describe 'when the clipboard has many selections', -> it "pastes each selection separately into the buffer", -> atom.clipboard.write('first\nsecond', {selections: ['first', 'second'] }) @@ -3264,16 +3355,6 @@ describe "TextEditor", -> expect(editor.tokenizedLineForScreenRow(0).tokens.length).toBeGreaterThan 1 describe "auto-indent", -> - copyText = (text, {startColumn, textEditor}={}) -> - startColumn ?= 0 - textEditor ?= editor - textEditor.setCursorBufferPosition([0, 0]) - textEditor.insertText(text) - numberOfNewlines = text.match(/\n/g)?.length - endColumn = text.match(/[^\n]*$/)[0]?.length - textEditor.getLastSelection().setBufferRange([[0,startColumn], [numberOfNewlines,endColumn]]) - textEditor.cutSelectedText() - describe "editor.autoIndent", -> describe "when editor.autoIndent is false (default)", -> describe "when `indent` is triggered", -> @@ -3404,87 +3485,6 @@ describe "TextEditor", -> coffeeEditor.insertText("\n") expect(coffeeEditor.lineTextForBufferRow(2)).toBe "" - describe "editor.normalizeIndentOnPaste", -> - beforeEach -> - atom.config.set('editor.autoIndentOnPaste', false) - atom.config.set('editor.normalizeIndentOnPaste', true) - - it "does not normalize the indentation level of the text when editor.normalizeIndentOnPaste is false", -> - copyText(" function() {\nvar cool = 1;\n }\n") - atom.config.set('editor.normalizeIndentOnPaste', false) - editor.setCursorBufferPosition([5, 2]) - editor.pasteText() - expect(editor.lineTextForBufferRow(5)).toBe " function() {" - expect(editor.lineTextForBufferRow(6)).toBe "var cool = 1;" - expect(editor.lineTextForBufferRow(7)).toBe " }" - - describe "when the inserted text contains no newlines", -> - it "does not adjust the indentation level of the text", -> - editor.setCursorBufferPosition([5, 2]) - editor.insertText("foo", indentBasis: 5) - expect(editor.lineTextForBufferRow(5)).toBe " foo current = items.shift();" - - it "does not adjust the whitespace if there are preceding characters", -> - copyText(" foo") - editor.setCursorBufferPosition([5, 30]) - editor.pasteText() - - expect(editor.lineTextForBufferRow(5)).toBe " current = items.shift(); foo" - - describe "when the inserted text contains newlines", -> - describe "when the cursor is preceded only by whitespace characters", -> - it "normalizes indented lines to the cursor's current indentation level", -> - copyText(" while (true) {\n foo();\n }\n", {startColumn: 2}) - editor.setCursorBufferPosition([3, 4]) - editor.pasteText() - - expect(editor.lineTextForBufferRow(3)).toBe " while (true) {" - expect(editor.lineTextForBufferRow(4)).toBe " foo();" - expect(editor.lineTextForBufferRow(5)).toBe " }" - expect(editor.lineTextForBufferRow(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}) - editor.setCursorBufferPosition([1, Infinity]) - editor.pasteText() - - expect(editor.lineTextForBufferRow(1)).toBe " var sort = function(items) {while (true) {" - expect(editor.lineTextForBufferRow(2)).toBe " foo();" - expect(editor.lineTextForBufferRow(3)).toBe " }" - expect(editor.lineTextForBufferRow(4)).toBe "" - - describe 'when scoped settings are used', -> - coffeeEditor = null - beforeEach -> - waitsForPromise -> - atom.packages.activatePackage('language-coffee-script') - waitsForPromise -> - atom.project.open('coffee.coffee', autoIndent: false).then (o) -> coffeeEditor = o - - runs -> - atom.config.set('.source.js', 'editor.normalizeIndentOnPaste', true) - atom.config.set('.source.coffee', 'editor.normalizeIndentOnPaste', false) - - afterEach: -> - atom.packages.deactivatePackages() - atom.packages.unloadPackages() - - it "normalizes the indentation level based on scoped settings", -> - copyText(" while (true) {\n foo();\n }\n", {startColumn: 2, textEditor: coffeeEditor}) - coffeeEditor.setCursorBufferPosition([4, 4]) - coffeeEditor.pasteText() - expect(coffeeEditor.lineTextForBufferRow(4)).toBe " while (true) {" - expect(coffeeEditor.lineTextForBufferRow(5)).toBe " foo();" - expect(coffeeEditor.lineTextForBufferRow(6)).toBe " }" - - copyText(" while (true) {\n foo();\n }\n", {startColumn: 2}) - editor.setCursorBufferPosition([3, 4]) - editor.pasteText() - expect(editor.lineTextForBufferRow(3)).toBe " while (true) {" - expect(editor.lineTextForBufferRow(4)).toBe " foo();" - expect(editor.lineTextForBufferRow(5)).toBe " }" - it "autoIndentSelectedRows auto-indents the selection", -> editor.setCursorBufferPosition([2, 0]) editor.insertText("function() {\ninside=true\n}\n i=1\n") From 24432018ecbb0fcb42aa21ff9aa48b08edbe763a Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Mon, 17 Nov 2014 16:53:11 -0800 Subject: [PATCH 2/2] Reorganize editor auto-indent specs --- spec/text-editor-spec.coffee | 272 +++++++++++++++++------------------ 1 file changed, 136 insertions(+), 136 deletions(-) diff --git a/spec/text-editor-spec.coffee b/spec/text-editor-spec.coffee index 6e11bb981..4c754f327 100644 --- a/spec/text-editor-spec.coffee +++ b/spec/text-editor-spec.coffee @@ -2851,6 +2851,18 @@ describe "TextEditor", -> expect(editor.getSelectedBufferRange()).toEqual [[0, 1], [3, 0]] + describe ".autoIndentSelectedRows", -> + it "auto-indents the selection", -> + editor.setCursorBufferPosition([2, 0]) + editor.insertText("function() {\ninside=true\n}\n i=1\n") + editor.getLastSelection().setBufferRange([[2,0], [6,0]]) + editor.autoIndentSelectedRows() + + expect(editor.lineTextForBufferRow(2)).toBe " function() {" + expect(editor.lineTextForBufferRow(3)).toBe " inside=true" + expect(editor.lineTextForBufferRow(4)).toBe " }" + expect(editor.lineTextForBufferRow(5)).toBe " i=1" + describe ".toggleLineCommentsInSelection()", -> it "toggles comments on the selected lines", -> editor.setSelectedBufferRange([[4, 5], [7, 5]]) @@ -3354,147 +3366,135 @@ describe "TextEditor", -> expect(editor.getGrammar()).toBe jsGrammar expect(editor.tokenizedLineForScreenRow(0).tokens.length).toBeGreaterThan 1 - describe "auto-indent", -> - describe "editor.autoIndent", -> - describe "when editor.autoIndent is false (default)", -> - describe "when `indent` is triggered", -> - it "does not auto-indent the line", -> - editor.setCursorBufferPosition([1, 30]) - editor.insertText("\n ") - expect(editor.lineTextForBufferRow(2)).toBe " " - - atom.config.set("editor.autoIndent", false) - editor.indent() - expect(editor.lineTextForBufferRow(2)).toBe " " - - describe "when editor.autoIndent is true", -> - beforeEach -> - atom.config.set("editor.autoIndent", true) - - describe "when `indent` is triggered", -> - it "auto-indents the line", -> - editor.setCursorBufferPosition([1, 30]) - editor.insertText("\n ") - expect(editor.lineTextForBufferRow(2)).toBe " " - - atom.config.set("editor.autoIndent", true) - editor.indent() - expect(editor.lineTextForBufferRow(2)).toBe " " - - describe "when a newline is added", -> - describe "when the line preceding the newline adds a new level of indentation", -> - it "indents the newline to one additional level of indentation beyond the preceding line", -> - editor.setCursorBufferPosition([1, Infinity]) - editor.insertText('\n') - expect(editor.indentationForBufferRow(2)).toBe editor.indentationForBufferRow(1) + 1 - - describe "when the line preceding the newline does't add a level of indentation", -> - it "indents the new line to the same level a as the preceding line", -> - editor.setCursorBufferPosition([5, 14]) - editor.insertText('\n') - expect(editor.indentationForBufferRow(6)).toBe editor.indentationForBufferRow(5) - - describe "when the line preceding the newline is a comment", -> - it "maintains the indent of the commented line", -> - editor.setCursorBufferPosition([0, 0]) - editor.insertText(' //') - editor.setCursorBufferPosition([0, Infinity]) - editor.insertText('\n') - expect(editor.indentationForBufferRow(1)).toBe 2 - - it "does not indent the line preceding the newline", -> - editor.setCursorBufferPosition([2, 0]) - editor.insertText(' var this-line-should-be-indented-more\n') - expect(editor.indentationForBufferRow(1)).toBe 1 - - atom.config.set("editor.autoIndent", true) - editor.setCursorBufferPosition([2, Infinity]) - editor.insertText('\n') - expect(editor.indentationForBufferRow(1)).toBe 1 - expect(editor.indentationForBufferRow(2)).toBe 1 - - describe "when the cursor is before whitespace", -> - it "retains the whitespace following the cursor on the new line", -> - editor.setText(" var sort = function() {}") - editor.setCursorScreenPosition([0, 23]) - editor.insertNewline() - - expect(buffer.lineForRow(0)).toBe ' var sort = function()' - expect(buffer.lineForRow(1)).toBe ' {}' - expect(editor.getCursorScreenPosition()).toEqual [1, 2] - - describe "when inserted text matches a decrease indent pattern", -> - describe "when the preceding line matches an increase indent pattern", -> - it "decreases the indentation to match that of the preceding line", -> - editor.setCursorBufferPosition([1, Infinity]) - editor.insertText('\n') - expect(editor.indentationForBufferRow(2)).toBe editor.indentationForBufferRow(1) + 1 - editor.insertText('}') - expect(editor.indentationForBufferRow(2)).toBe editor.indentationForBufferRow(1) - - describe "when the preceding line doesn't match an increase indent pattern", -> - it "decreases the indentation to be one level below that of the preceding line", -> - editor.setCursorBufferPosition([3, Infinity]) - editor.insertText('\n ') - expect(editor.indentationForBufferRow(4)).toBe editor.indentationForBufferRow(3) - editor.insertText('}') - expect(editor.indentationForBufferRow(4)).toBe editor.indentationForBufferRow(3) - 1 - - it "doesn't break when decreasing the indentation on a row that has no indentation", -> - editor.setCursorBufferPosition([12, Infinity]) - editor.insertText("\n}; # too many closing brackets!") - expect(editor.lineTextForBufferRow(13)).toBe "}; # too many closing brackets!" - - describe "when inserted text does not match a decrease indent pattern", -> - it "does not decrease the indentation", -> - editor.setCursorBufferPosition([12, 0]) - editor.insertText(' ') - expect(editor.lineTextForBufferRow(12)).toBe ' };' - editor.insertText('\t\t') - expect(editor.lineTextForBufferRow(12)).toBe ' \t\t};' - - describe "when the current line does not match a decrease indent pattern", -> - it "leaves the line unchanged", -> - editor.setCursorBufferPosition([2, 4]) - expect(editor.indentationForBufferRow(2)).toBe editor.indentationForBufferRow(1) + 1 - editor.insertText('foo') - expect(editor.indentationForBufferRow(2)).toBe editor.indentationForBufferRow(1) + 1 - - describe 'when scoped settings are used', -> - coffeeEditor = null - beforeEach -> - waitsForPromise -> - atom.packages.activatePackage('language-coffee-script') - waitsForPromise -> - atom.project.open('coffee.coffee', autoIndent: false).then (o) -> coffeeEditor = o - - runs -> - atom.config.set('.source.js', 'editor.autoIndent', true) - atom.config.set('.source.coffee', 'editor.autoIndent', false) - - afterEach: -> - atom.packages.deactivatePackages() - atom.packages.unloadPackages() - - it "does not auto-indent the line for javascript files", -> + describe "editor.autoIndent", -> + describe "when editor.autoIndent is false (default)", -> + describe "when `indent` is triggered", -> + it "does not auto-indent the line", -> editor.setCursorBufferPosition([1, 30]) - editor.insertText("\n") + editor.insertText("\n ") + expect(editor.lineTextForBufferRow(2)).toBe " " + + atom.config.set("editor.autoIndent", false) + editor.indent() + expect(editor.lineTextForBufferRow(2)).toBe " " + + describe "when editor.autoIndent is true", -> + beforeEach -> + atom.config.set("editor.autoIndent", true) + + describe "when `indent` is triggered", -> + it "auto-indents the line", -> + editor.setCursorBufferPosition([1, 30]) + editor.insertText("\n ") + expect(editor.lineTextForBufferRow(2)).toBe " " + + atom.config.set("editor.autoIndent", true) + editor.indent() expect(editor.lineTextForBufferRow(2)).toBe " " - coffeeEditor.setCursorBufferPosition([1, 18]) - coffeeEditor.insertText("\n") - expect(coffeeEditor.lineTextForBufferRow(2)).toBe "" + describe "when a newline is added", -> + describe "when the line preceding the newline adds a new level of indentation", -> + it "indents the newline to one additional level of indentation beyond the preceding line", -> + editor.setCursorBufferPosition([1, Infinity]) + editor.insertText('\n') + expect(editor.indentationForBufferRow(2)).toBe editor.indentationForBufferRow(1) + 1 - it "autoIndentSelectedRows auto-indents the selection", -> - editor.setCursorBufferPosition([2, 0]) - editor.insertText("function() {\ninside=true\n}\n i=1\n") - editor.getLastSelection().setBufferRange([[2,0], [6,0]]) - editor.autoIndentSelectedRows() + describe "when the line preceding the newline does't add a level of indentation", -> + it "indents the new line to the same level a as the preceding line", -> + editor.setCursorBufferPosition([5, 14]) + editor.insertText('\n') + expect(editor.indentationForBufferRow(6)).toBe editor.indentationForBufferRow(5) - expect(editor.lineTextForBufferRow(2)).toBe " function() {" - expect(editor.lineTextForBufferRow(3)).toBe " inside=true" - expect(editor.lineTextForBufferRow(4)).toBe " }" - expect(editor.lineTextForBufferRow(5)).toBe " i=1" + describe "when the line preceding the newline is a comment", -> + it "maintains the indent of the commented line", -> + editor.setCursorBufferPosition([0, 0]) + editor.insertText(' //') + editor.setCursorBufferPosition([0, Infinity]) + editor.insertText('\n') + expect(editor.indentationForBufferRow(1)).toBe 2 + + it "does not indent the line preceding the newline", -> + editor.setCursorBufferPosition([2, 0]) + editor.insertText(' var this-line-should-be-indented-more\n') + expect(editor.indentationForBufferRow(1)).toBe 1 + + atom.config.set("editor.autoIndent", true) + editor.setCursorBufferPosition([2, Infinity]) + editor.insertText('\n') + expect(editor.indentationForBufferRow(1)).toBe 1 + expect(editor.indentationForBufferRow(2)).toBe 1 + + describe "when the cursor is before whitespace", -> + it "retains the whitespace following the cursor on the new line", -> + editor.setText(" var sort = function() {}") + editor.setCursorScreenPosition([0, 23]) + editor.insertNewline() + + expect(buffer.lineForRow(0)).toBe ' var sort = function()' + expect(buffer.lineForRow(1)).toBe ' {}' + expect(editor.getCursorScreenPosition()).toEqual [1, 2] + + describe "when inserted text matches a decrease indent pattern", -> + describe "when the preceding line matches an increase indent pattern", -> + it "decreases the indentation to match that of the preceding line", -> + editor.setCursorBufferPosition([1, Infinity]) + editor.insertText('\n') + expect(editor.indentationForBufferRow(2)).toBe editor.indentationForBufferRow(1) + 1 + editor.insertText('}') + expect(editor.indentationForBufferRow(2)).toBe editor.indentationForBufferRow(1) + + describe "when the preceding line doesn't match an increase indent pattern", -> + it "decreases the indentation to be one level below that of the preceding line", -> + editor.setCursorBufferPosition([3, Infinity]) + editor.insertText('\n ') + expect(editor.indentationForBufferRow(4)).toBe editor.indentationForBufferRow(3) + editor.insertText('}') + expect(editor.indentationForBufferRow(4)).toBe editor.indentationForBufferRow(3) - 1 + + it "doesn't break when decreasing the indentation on a row that has no indentation", -> + editor.setCursorBufferPosition([12, Infinity]) + editor.insertText("\n}; # too many closing brackets!") + expect(editor.lineTextForBufferRow(13)).toBe "}; # too many closing brackets!" + + describe "when inserted text does not match a decrease indent pattern", -> + it "does not decrease the indentation", -> + editor.setCursorBufferPosition([12, 0]) + editor.insertText(' ') + expect(editor.lineTextForBufferRow(12)).toBe ' };' + editor.insertText('\t\t') + expect(editor.lineTextForBufferRow(12)).toBe ' \t\t};' + + describe "when the current line does not match a decrease indent pattern", -> + it "leaves the line unchanged", -> + editor.setCursorBufferPosition([2, 4]) + expect(editor.indentationForBufferRow(2)).toBe editor.indentationForBufferRow(1) + 1 + editor.insertText('foo') + expect(editor.indentationForBufferRow(2)).toBe editor.indentationForBufferRow(1) + 1 + + describe 'when scoped settings are used', -> + coffeeEditor = null + beforeEach -> + waitsForPromise -> + atom.packages.activatePackage('language-coffee-script') + waitsForPromise -> + atom.project.open('coffee.coffee', autoIndent: false).then (o) -> coffeeEditor = o + + runs -> + atom.config.set('.source.js', 'editor.autoIndent', true) + atom.config.set('.source.coffee', 'editor.autoIndent', false) + + afterEach: -> + atom.packages.deactivatePackages() + atom.packages.unloadPackages() + + it "does not auto-indent the line for javascript files", -> + editor.setCursorBufferPosition([1, 30]) + editor.insertText("\n") + expect(editor.lineTextForBufferRow(2)).toBe " " + + coffeeEditor.setCursorBufferPosition([1, 18]) + coffeeEditor.insertText("\n") + expect(coffeeEditor.lineTextForBufferRow(2)).toBe "" describe "soft and hard tabs", -> afterEach ->