Detect softTabs/hardTabs when an EditSession is created

This commit is contained in:
Corey Johnson & Nathan Sobo
2012-10-26 12:09:51 -06:00
parent 3a067bcd71
commit 17d74c4883
4 changed files with 28 additions and 3 deletions

View File

@@ -628,3 +628,12 @@ describe 'Buffer', ->
buffer.delete([[3, 0], [5, 0]])
expect(destroyHandler).toHaveBeenCalled()
expect(buffer.getAnchors().indexOf(anchor)).toBe -1
describe ".usesSoftTabs()", ->
it "returns true if the first indented line begins with tabs", ->
buffer.setText("function() {\n foo();\n}")
expect(buffer.usesSoftTabs()).toBeTruthy()
buffer.setText("function() {\n\tfoo();\n}")
expect(buffer.usesSoftTabs()).toBeFalsy()
buffer.setText("")
expect(buffer.usesSoftTabs()).toBeUndefined()

View File

@@ -10,7 +10,6 @@ describe "EditSession", ->
buffer.setText(buffer.getText().replace(/[ ]{2}/g, "\t"))
beforeEach ->
buffer = new Buffer()
editSession = fixturesProject.buildEditSessionForPath('sample.js', autoIndent: false)
buffer = editSession.buffer
lineLengths = buffer.getLines().map (line) -> line.length
@@ -1745,3 +1744,14 @@ describe "EditSession", ->
editSession.setSelectedBufferRange([[0, 1], [0, 4]])
editSession.transpose()
expect(editSession.lineForBufferRow(0)).toBe 'xcbaz'
describe "soft-tabs detection", ->
it "assign soft / hard tabs based on the contents of the buffer, or uses the default if unknown", ->
editSession = fixturesProject.buildEditSessionForPath('sample.js', softTabs: false)
expect(editSession.softTabs).toBeTruthy()
editSession = fixturesProject.buildEditSessionForPath('sample-with-tabs.coffee', softTabs: true)
expect(editSession.softTabs).toBeFalsy()
editSession = fixturesProject.buildEditSessionForPath(null, softTabs: false)
expect(editSession.softTabs).toBeFalsy()

View File

@@ -352,6 +352,12 @@ class Buffer
return row unless @isRowBlank(row)
null
usesSoftTabs: ->
for line in @getLines()
if match = line.match(/^\s/)
return match[0][0] != '\t'
undefined
logLines: (start=0, end=@getLastRow())->
for row in [start..end]
line = @lineForRow(row)

View File

@@ -39,9 +39,9 @@ class EditSession
softTabs: true
softWrap: false
constructor: ({@project, @buffer, @tabLength, @autoIndent, @softTabs, @softWrap }) ->
constructor: ({@project, @buffer, @tabLength, @autoIndent, softTabs, @softWrap }) ->
@id = @constructor.idCounter++
@softTabs ?= true
@softTabs = @buffer.usesSoftTabs() ? softTabs ? true
@languageMode = new LanguageMode(this, @buffer.getExtension())
@displayBuffer = new DisplayBuffer(@buffer, { @languageMode, @tabLength })
@tokenizedBuffer = @displayBuffer.tokenizedBuffer