diff --git a/spec/editor-spec.coffee b/spec/editor-spec.coffee index ac2d24ddf..91a282f55 100644 --- a/spec/editor-spec.coffee +++ b/spec/editor-spec.coffee @@ -2594,6 +2594,10 @@ describe "Editor", -> atom.workspace.open('sample-with-tabs.coffee', softTabs: true).then (editor) -> expect(editor.getSoftTabs()).toBeFalsy() + waitsForPromise -> + atom.workspace.open('sample-with-tabs-and-initial-comment.js', softTabs: true).then (editor) -> + expect(editor.getSoftTabs()).toBeFalsy() + waitsForPromise -> atom.workspace.open(null, softTabs: false).then (editor) -> expect(editor.getSoftTabs()).toBeFalsy() diff --git a/spec/fixtures/sample-with-tabs-and-initial-comment.js b/spec/fixtures/sample-with-tabs-and-initial-comment.js new file mode 100644 index 000000000..996d4dff0 --- /dev/null +++ b/spec/fixtures/sample-with-tabs-and-initial-comment.js @@ -0,0 +1,8 @@ +/** + * Look, this is a comment. Don't go making assumtions that I want soft tabs + * because this block comment has leading spaces, Geez. + */ + +if (beNice) { + console.log('Thank you for being nice.'); +} diff --git a/src/editor.coffee b/src/editor.coffee index 98dbd5d5a..1e0d2768e 100644 --- a/src/editor.coffee +++ b/src/editor.coffee @@ -160,7 +160,7 @@ class Editor extends Model @displayBuffer ?= new DisplayBuffer({buffer, tabLength, softWrap}) @buffer = @displayBuffer.buffer - @softTabs = @buffer.usesSoftTabs() ? @softTabs ? atom.config.get('editor.softTabs') ? true + @softTabs = @usesSoftTabs() ? @softTabs ? atom.config.get('editor.softTabs') ? true for marker in @findMarkers(@getSelectionMarkerAttributes()) marker.setAttributes(preserveFolds: true) @@ -317,6 +317,19 @@ class Editor extends Model # Public: Set the on-screen length of tab characters. setTabLength: (tabLength) -> @displayBuffer.setTabLength(tabLength) + # Public: Determine if the buffer uses hard or soft tabs. + # + # Returns `true` if the first non-comment line with leading whitespace starts + # with a space character. Returns `false` if it starts with a hard tab (`\t`). + # + # Returns a {Boolean}, + usesSoftTabs: -> + for bufferRow in [0..@buffer.getLastRow()] + continue if @displayBuffer.tokenizedBuffer.lineForScreenRow(bufferRow).isComment() + if match = @buffer.lineForRow(bufferRow).match(/^\s/) + return match[0][0] != '\t' + undefined + # Public: Clip the given {Point} to a valid position in the buffer. # # If the given {Point} describes a position that is actually reachable by the