Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow mixing of tabs and spaces if an explicit tabstop size is set #4303

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions src/lexer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ exports.Lexer = class Lexer
@tokens = [] # Stream of parsed tokens in the form `['TYPE', value, location data]`.
@seenFor = no # Used to recognize FORIN and FOROF tokens.

@tabSize = opts.tabSize or 1 # Width of tab stop

@chunkLine =
opts.line or 0 # The start line for the current @chunk.
@chunkColumn =
Expand Down Expand Up @@ -318,31 +320,43 @@ exports.Lexer = class Lexer
return 0 unless match = MULTI_DENT.exec @chunk
indent = match[0]
@seenFor = no
size = indent.length - 1 - indent.lastIndexOf '\n'
width = size = indent.length - 1 - indent.lastIndexOf '\n'

if @tabSize > 1
width = 0
beforeStop = 0
for i in [1..size]
if indent[i] isnt '\t'
beforeStop++
if indent[i] is '\t' or beforeStop is @tabSize
width += @tabSize
beforeStop = 0
width += beforeStop

noNewlines = @unfinished()
if size - @indebt is @indent
if width - @indebt is @indent
if noNewlines then @suppressNewlines() else @newlineToken 0
return indent.length

if size > @indent
if width > @indent
if noNewlines
@indebt = size - @indent
@indebt = width - @indent
@suppressNewlines()
return indent.length
unless @tokens.length
@baseIndent = @indent = size
@baseIndent = @indent = width
return indent.length
diff = size - @indent + @outdebt
diff = width - @indent + @outdebt
@token 'INDENT', diff, indent.length - size, size
@indents.push diff
@ends.push {tag: 'OUTDENT'}
@outdebt = @indebt = 0
@indent = size
else if size < @baseIndent
@indent = width
else if width < @baseIndent
@error 'missing indentation', offset: indent.length
else
@indebt = 0
@outdentToken @indent - size, noNewlines, indent.length
@outdentToken @indent - width, noNewlines, indent.length
indent.length

# Record an outdent token or multiple tokens, if we happen to be moving back
Expand Down
8 changes: 8 additions & 0 deletions test/formatting.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,11 @@ test "#1275: allow indentation before closing brackets", ->
a = 1
)
eq 1, a

test "allow mixing of spaces and tabs for indentation by passing a width for tabs to the lexer", ->
doesNotThrow -> CoffeeScript.compile '''
new Layer
x: 0
y: 1
z: 2
''', tabSize: 2