From 6958e0af1074390b1d380f7a9cbceca0bfcf991a Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Mon, 6 Oct 2014 18:10:24 -0700 Subject: [PATCH] Scope editor.normalizeIndentOnPaste --- spec/text-editor-spec.coffee | 42 +++++++++++++++++++++++++++++++----- src/text-editor.coffee | 2 +- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/spec/text-editor-spec.coffee b/spec/text-editor-spec.coffee index 55c466cc8..509feacbf 100644 --- a/spec/text-editor-spec.coffee +++ b/spec/text-editor-spec.coffee @@ -3126,14 +3126,15 @@ describe "TextEditor", -> expect(editor.tokenizedLineForScreenRow(0).tokens.length).toBeGreaterThan 1 describe "auto-indent", -> - copyText = (text, {startColumn}={}) -> + copyText = (text, {startColumn, textEditor}={}) -> startColumn ?= 0 - editor.setCursorBufferPosition([0, 0]) - editor.insertText(text) + textEditor ?= editor + textEditor.setCursorBufferPosition([0, 0]) + textEditor.insertText(text) numberOfNewlines = text.match(/\n/g)?.length endColumn = text.match(/[^\n]*$/)[0]?.length - editor.getLastSelection().setBufferRange([[0,startColumn], [numberOfNewlines,endColumn]]) - editor.cutSelectedText() + textEditor.getLastSelection().setBufferRange([[0,startColumn], [numberOfNewlines,endColumn]]) + textEditor.cutSelectedText() describe "editor.autoIndent", -> describe "when editor.autoIndent is false (default)", -> @@ -3289,6 +3290,37 @@ describe "TextEditor", -> 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 "does not normalize the indentation level for coffee files, but does for js files", -> + 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") diff --git a/src/text-editor.coffee b/src/text-editor.coffee index 9e5d68bcf..a0b561861 100644 --- a/src/text-editor.coffee +++ b/src/text-editor.coffee @@ -2448,7 +2448,7 @@ class TextEditor extends Model return - else if atom.config.get("editor.normalizeIndentOnPaste") and metadata?.indentBasis? + else if atom.config.get(@scopesAtCursor(), "editor.normalizeIndentOnPaste") and metadata?.indentBasis? if !@getLastCursor().hasPrecedingCharactersOnLine() or containsNewlines options.indentBasis ?= metadata.indentBasis