From 67b1d7890b4bd1f5ae78c67eb35c0cdb77812246 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Tue, 17 Mar 2015 22:05:43 +0100 Subject: [PATCH 01/12] Add soft-wrap hanging indentation spaces --- src/display-buffer.coffee | 10 +++++++++- src/tokenized-line.coffee | 19 ++++++++++++------- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/display-buffer.coffee b/src/display-buffer.coffee index 5659d38a0..ecdd36c6d 100644 --- a/src/display-buffer.coffee +++ b/src/display-buffer.coffee @@ -69,12 +69,17 @@ class DisplayBuffer extends Model scrollPastEnd: atom.config.get('editor.scrollPastEnd', scope: scopeDescriptor) softWrap: atom.config.get('editor.softWrap', scope: scopeDescriptor) softWrapAtPreferredLineLength: atom.config.get('editor.softWrapAtPreferredLineLength', scope: scopeDescriptor) + softWrapHangingIndentationSpaces: atom.config.get('editor.softWrapHangingIndentationSpaces', scope: scopeDescriptor) preferredLineLength: atom.config.get('editor.preferredLineLength', scope: scopeDescriptor) subscriptions.add atom.config.onDidChange 'editor.softWrap', scope: scopeDescriptor, ({newValue}) => @configSettings.softWrap = newValue @updateWrappedScreenLines() + subscriptions.add atom.config.onDidChange 'editor.softWrapHangingIndentationSpaces', scope: scopeDescriptor, ({newValue}) => + @configSettings.softWrapHangingIndentationSpaces = newValue + @updateWrappedScreenLines() + subscriptions.add atom.config.onDidChange 'editor.softWrapAtPreferredLineLength', scope: scopeDescriptor, ({newValue}) => @configSettings.softWrapAtPreferredLineLength = newValue @updateWrappedScreenLines() if @isSoftWrapped() @@ -1157,7 +1162,10 @@ class DisplayBuffer extends Model softWraps = 0 if @isSoftWrapped() while wrapScreenColumn = tokenizedLine.findWrapColumn(@getSoftWrapColumn()) - [wrappedLine, tokenizedLine] = tokenizedLine.softWrapAt(wrapScreenColumn) + [wrappedLine, tokenizedLine] = tokenizedLine.softWrapAt( + wrapScreenColumn, + hangingIndentationSpaces: @configSettings.softWrapHangingIndentationSpaces + ) break if wrappedLine.hasOnlySoftWrapIndentation() screenLines.push(wrappedLine) softWraps++ diff --git a/src/tokenized-line.coffee b/src/tokenized-line.coffee index 3560c35a1..ab12d39c3 100644 --- a/src/tokenized-line.coffee +++ b/src/tokenized-line.coffee @@ -118,18 +118,23 @@ class TokenizedLine oddIndentLevel = @indentLevel - Math.floor(@indentLevel) Math.round(@tabLength * oddIndentLevel) - buildSoftWrapIndentationTokens: (token) -> + buildSoftWrapIndentationTokens: (token, hangingIndentationSpaces) -> indentTokens = [0...Math.floor(@indentLevel)].map => token.buildSoftWrapIndentationToken(@tabLength) if @getOddIndentationSpaces() - indentTokens.concat( - token.buildSoftWrapIndentationToken @getOddIndentationSpaces() + indentTokens.push( + token.buildSoftWrapIndentationToken(@getOddIndentationSpaces()) ) - else - indentTokens - softWrapAt: (column) -> + if hangingIndentationSpaces + indentTokens.push( + token.buildSoftWrapIndentationToken(hangingIndentationSpaces) + ) + + indentTokens + + softWrapAt: (column, {hangingIndentationSpaces}) -> return [new TokenizedLine([], '', [0, 0], [0, 0]), this] if column == 0 rightTokens = new Array(@tokens...) @@ -142,7 +147,7 @@ class TokenizedLine leftTextLength += nextToken.value.length leftTokens.push nextToken - indentationTokens = @buildSoftWrapIndentationTokens(leftTokens[0]) + indentationTokens = @buildSoftWrapIndentationTokens(leftTokens[0], hangingIndentationSpaces) leftFragment = new TokenizedLine( tokens: leftTokens From 6d39bd3657eb64bd1a1f4fb1589768bdf1553b72 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Tue, 17 Mar 2015 22:21:07 +0100 Subject: [PATCH 02/12] :white_check_mark: Verify that hanging indentation is tokenized --- spec/display-buffer-spec.coffee | 6 ++++++ src/tokenized-line.coffee | 4 +++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/spec/display-buffer-spec.coffee b/spec/display-buffer-spec.coffee index df7c13383..5a719198d 100644 --- a/spec/display-buffer-spec.coffee +++ b/spec/display-buffer-spec.coffee @@ -128,6 +128,12 @@ describe "DisplayBuffer", -> expect(displayBuffer.tokenizedLineForScreenRow(4).tokens[0].isSoftWrapIndentation).toBeTruthy() expect(displayBuffer.tokenizedLineForScreenRow(4).tokens[1].isSoftWrapIndentation).toBeTruthy() + it "correctly tokenizes hanging indentation spaces", -> + atom.config.set("editor.softWrapHangingIndentationSpaces", 3) + expect(displayBuffer.tokenizedLineForScreenRow(4).tokens[0].isSoftWrapIndentation).toBeTruthy() + expect(displayBuffer.tokenizedLineForScreenRow(4).tokens[1].isSoftWrapIndentation).toBeTruthy() + expect(displayBuffer.tokenizedLineForScreenRow(4).tokens[2].isSoftWrapIndentation).toBeTruthy() + describe "when the buffer changes", -> describe "when buffer lines are updated", -> describe "when whitespace is added after the max line length", -> diff --git a/src/tokenized-line.coffee b/src/tokenized-line.coffee index ab12d39c3..0437fbdcf 100644 --- a/src/tokenized-line.coffee +++ b/src/tokenized-line.coffee @@ -134,9 +134,11 @@ class TokenizedLine indentTokens - softWrapAt: (column, {hangingIndentationSpaces}) -> + softWrapAt: (column, options = {}) -> return [new TokenizedLine([], '', [0, 0], [0, 0]), this] if column == 0 + {hangingIndentationSpaces} = options + rightTokens = new Array(@tokens...) leftTokens = [] leftTextLength = 0 From c4d2e0eac8c62e2aafb5784813e7b2ae1a4c28cc Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Tue, 17 Mar 2015 22:42:51 +0100 Subject: [PATCH 03/12] Add default editor.softWrapHangingIndentationSpaces --- src/config-schema.coffee | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/config-schema.coffee b/src/config-schema.coffee index 1f205ecbf..74fc719a4 100644 --- a/src/config-schema.coffee +++ b/src/config-schema.coffee @@ -149,6 +149,9 @@ module.exports = softWrapAtPreferredLineLength: type: 'boolean' default: false + softWrapHangingIndentationSpaces: + type: 'integer' + default: 0 scrollSensitivity: type: 'integer' default: 40 From 6ac8af2a6e7799cbf72e2fc6998f9329159e40d0 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Tue, 17 Mar 2015 22:58:14 +0100 Subject: [PATCH 04/12] :white_check_mark: Check leading spaces as well --- spec/display-buffer-spec.coffee | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/spec/display-buffer-spec.coffee b/spec/display-buffer-spec.coffee index 5a719198d..1306174aa 100644 --- a/spec/display-buffer-spec.coffee +++ b/spec/display-buffer-spec.coffee @@ -128,8 +128,15 @@ describe "DisplayBuffer", -> expect(displayBuffer.tokenizedLineForScreenRow(4).tokens[0].isSoftWrapIndentation).toBeTruthy() expect(displayBuffer.tokenizedLineForScreenRow(4).tokens[1].isSoftWrapIndentation).toBeTruthy() + describe "when editor.softWrapHangingIndentationSpaces is set", -> + beforeEach -> + atom.config.set('editor.softWrapHangingIndentationSpaces', 3) + + it "further indents wrapped lines", -> + expect(displayBuffer.tokenizedLineForScreenRow(11).text).toBe " sort(left).concat(pivot).concat(sort(right)" + expect(displayBuffer.tokenizedLineForScreenRow(12).text).toBe " );" + it "correctly tokenizes hanging indentation spaces", -> - atom.config.set("editor.softWrapHangingIndentationSpaces", 3) expect(displayBuffer.tokenizedLineForScreenRow(4).tokens[0].isSoftWrapIndentation).toBeTruthy() expect(displayBuffer.tokenizedLineForScreenRow(4).tokens[1].isSoftWrapIndentation).toBeTruthy() expect(displayBuffer.tokenizedLineForScreenRow(4).tokens[2].isSoftWrapIndentation).toBeTruthy() From f0b9bb7ce3e75f716c0d0297bbcacee5fe289f81 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Wed, 18 Mar 2015 11:17:34 +0100 Subject: [PATCH 05/12] Show indent guides on hanging indentation too --- spec/display-buffer-spec.coffee | 1 + src/tokenized-line.coffee | 28 +++++++++++++--------------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/spec/display-buffer-spec.coffee b/spec/display-buffer-spec.coffee index 1306174aa..1b3ef3c8a 100644 --- a/spec/display-buffer-spec.coffee +++ b/spec/display-buffer-spec.coffee @@ -140,6 +140,7 @@ describe "DisplayBuffer", -> expect(displayBuffer.tokenizedLineForScreenRow(4).tokens[0].isSoftWrapIndentation).toBeTruthy() expect(displayBuffer.tokenizedLineForScreenRow(4).tokens[1].isSoftWrapIndentation).toBeTruthy() expect(displayBuffer.tokenizedLineForScreenRow(4).tokens[2].isSoftWrapIndentation).toBeTruthy() + expect(displayBuffer.tokenizedLineForScreenRow(4).tokens[3].isSoftWrapIndentation).toBeTruthy() describe "when the buffer changes", -> describe "when buffer lines are updated", -> diff --git a/src/tokenized-line.coffee b/src/tokenized-line.coffee index 0437fbdcf..14208e36b 100644 --- a/src/tokenized-line.coffee +++ b/src/tokenized-line.coffee @@ -111,28 +111,26 @@ class TokenizedLine return maxColumn - # Calculates how many trailing spaces in this line's indentation cannot fit in a single tab. + # For a given `indentLevel`, calculates how many trailing spaces cannot fit in a single tab. # - # Returns a {Number} representing the odd indentation spaces in this line. - getOddIndentationSpaces: -> - oddIndentLevel = @indentLevel - Math.floor(@indentLevel) + # indentLevel - {Number} + # Returns a {Number} representing the odd indentation spaces for `indentLevel`. + oddIndentationSpacesForIndentLevel: (indentLevel) -> + oddIndentLevel = indentLevel - Math.floor(indentLevel) Math.round(@tabLength * oddIndentLevel) buildSoftWrapIndentationTokens: (token, hangingIndentationSpaces) -> - indentTokens = [0...Math.floor(@indentLevel)].map => + hangingIndentLevel = hangingIndentationSpaces / @tabLength + indentLevel = @indentLevel + hangingIndentLevel + indentTokens = [0...Math.floor(indentLevel)].map => token.buildSoftWrapIndentationToken(@tabLength) - if @getOddIndentationSpaces() - indentTokens.push( - token.buildSoftWrapIndentationToken(@getOddIndentationSpaces()) + if oddIndentationSpaces = @oddIndentationSpacesForIndentLevel(indentLevel) + indentTokens.concat( + token.buildSoftWrapIndentationToken(oddIndentationSpaces) ) - - if hangingIndentationSpaces - indentTokens.push( - token.buildSoftWrapIndentationToken(hangingIndentationSpaces) - ) - - indentTokens + else + indentTokens softWrapAt: (column, options = {}) -> return [new TokenizedLine([], '', [0, 0], [0, 0]), this] if column == 0 From ae2c92a1dc15385ee0bf262c3199f8defd0d4c7a Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Wed, 18 Mar 2015 11:29:39 +0100 Subject: [PATCH 06/12] :memo: --- src/tokenized-line.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tokenized-line.coffee b/src/tokenized-line.coffee index 14208e36b..dfc8cac83 100644 --- a/src/tokenized-line.coffee +++ b/src/tokenized-line.coffee @@ -114,7 +114,7 @@ class TokenizedLine # For a given `indentLevel`, calculates how many trailing spaces cannot fit in a single tab. # # indentLevel - {Number} - # Returns a {Number} representing the odd indentation spaces for `indentLevel`. + # Returns a {Number} representing the odd indentation spaces in `indentLevel`. oddIndentationSpacesForIndentLevel: (indentLevel) -> oddIndentLevel = indentLevel - Math.floor(indentLevel) Math.round(@tabLength * oddIndentLevel) From e5fa44171df82962efb185d6b5d36ab2b8329f86 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Wed, 18 Mar 2015 14:15:52 +0100 Subject: [PATCH 07/12] :bug: Hanging indent must be a positive value --- src/config-schema.coffee | 1 + 1 file changed, 1 insertion(+) diff --git a/src/config-schema.coffee b/src/config-schema.coffee index 74fc719a4..92d80069f 100644 --- a/src/config-schema.coffee +++ b/src/config-schema.coffee @@ -152,6 +152,7 @@ module.exports = softWrapHangingIndentationSpaces: type: 'integer' default: 0 + minimum: 0 scrollSensitivity: type: 'integer' default: 40 From 7c33b9bf41681f0eb21a1264370e62f6056119a9 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Wed, 18 Mar 2015 14:18:27 +0100 Subject: [PATCH 08/12] :art: Rename to softWrapHangingIndent --- spec/display-buffer-spec.coffee | 4 ++-- src/config-schema.coffee | 2 +- src/display-buffer.coffee | 8 ++++---- src/tokenized-line.coffee | 8 ++++---- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/spec/display-buffer-spec.coffee b/spec/display-buffer-spec.coffee index 1b3ef3c8a..4f2046434 100644 --- a/spec/display-buffer-spec.coffee +++ b/spec/display-buffer-spec.coffee @@ -128,9 +128,9 @@ describe "DisplayBuffer", -> expect(displayBuffer.tokenizedLineForScreenRow(4).tokens[0].isSoftWrapIndentation).toBeTruthy() expect(displayBuffer.tokenizedLineForScreenRow(4).tokens[1].isSoftWrapIndentation).toBeTruthy() - describe "when editor.softWrapHangingIndentationSpaces is set", -> + describe "when editor.softWrapHangingIndent is set", -> beforeEach -> - atom.config.set('editor.softWrapHangingIndentationSpaces', 3) + atom.config.set('editor.softWrapHangingIndent', 3) it "further indents wrapped lines", -> expect(displayBuffer.tokenizedLineForScreenRow(11).text).toBe " sort(left).concat(pivot).concat(sort(right)" diff --git a/src/config-schema.coffee b/src/config-schema.coffee index 92d80069f..c2944911b 100644 --- a/src/config-schema.coffee +++ b/src/config-schema.coffee @@ -149,7 +149,7 @@ module.exports = softWrapAtPreferredLineLength: type: 'boolean' default: false - softWrapHangingIndentationSpaces: + softWrapHangingIndent: type: 'integer' default: 0 minimum: 0 diff --git a/src/display-buffer.coffee b/src/display-buffer.coffee index ecdd36c6d..c8ec23e07 100644 --- a/src/display-buffer.coffee +++ b/src/display-buffer.coffee @@ -69,15 +69,15 @@ class DisplayBuffer extends Model scrollPastEnd: atom.config.get('editor.scrollPastEnd', scope: scopeDescriptor) softWrap: atom.config.get('editor.softWrap', scope: scopeDescriptor) softWrapAtPreferredLineLength: atom.config.get('editor.softWrapAtPreferredLineLength', scope: scopeDescriptor) - softWrapHangingIndentationSpaces: atom.config.get('editor.softWrapHangingIndentationSpaces', scope: scopeDescriptor) + softWrapHangingIndent: atom.config.get('editor.softWrapHangingIndent', scope: scopeDescriptor) preferredLineLength: atom.config.get('editor.preferredLineLength', scope: scopeDescriptor) subscriptions.add atom.config.onDidChange 'editor.softWrap', scope: scopeDescriptor, ({newValue}) => @configSettings.softWrap = newValue @updateWrappedScreenLines() - subscriptions.add atom.config.onDidChange 'editor.softWrapHangingIndentationSpaces', scope: scopeDescriptor, ({newValue}) => - @configSettings.softWrapHangingIndentationSpaces = newValue + subscriptions.add atom.config.onDidChange 'editor.softWrapHangingIndent', scope: scopeDescriptor, ({newValue}) => + @configSettings.softWrapHangingIndent = newValue @updateWrappedScreenLines() subscriptions.add atom.config.onDidChange 'editor.softWrapAtPreferredLineLength', scope: scopeDescriptor, ({newValue}) => @@ -1164,7 +1164,7 @@ class DisplayBuffer extends Model while wrapScreenColumn = tokenizedLine.findWrapColumn(@getSoftWrapColumn()) [wrappedLine, tokenizedLine] = tokenizedLine.softWrapAt( wrapScreenColumn, - hangingIndentationSpaces: @configSettings.softWrapHangingIndentationSpaces + hangingIndent: @configSettings.softWrapHangingIndent ) break if wrappedLine.hasOnlySoftWrapIndentation() screenLines.push(wrappedLine) diff --git a/src/tokenized-line.coffee b/src/tokenized-line.coffee index dfc8cac83..a949a6e11 100644 --- a/src/tokenized-line.coffee +++ b/src/tokenized-line.coffee @@ -119,8 +119,8 @@ class TokenizedLine oddIndentLevel = indentLevel - Math.floor(indentLevel) Math.round(@tabLength * oddIndentLevel) - buildSoftWrapIndentationTokens: (token, hangingIndentationSpaces) -> - hangingIndentLevel = hangingIndentationSpaces / @tabLength + buildSoftWrapIndentationTokens: (token, hangingIndent) -> + hangingIndentLevel = hangingIndent / @tabLength indentLevel = @indentLevel + hangingIndentLevel indentTokens = [0...Math.floor(indentLevel)].map => token.buildSoftWrapIndentationToken(@tabLength) @@ -135,7 +135,7 @@ class TokenizedLine softWrapAt: (column, options = {}) -> return [new TokenizedLine([], '', [0, 0], [0, 0]), this] if column == 0 - {hangingIndentationSpaces} = options + {hangingIndent} = options rightTokens = new Array(@tokens...) leftTokens = [] @@ -147,7 +147,7 @@ class TokenizedLine leftTextLength += nextToken.value.length leftTokens.push nextToken - indentationTokens = @buildSoftWrapIndentationTokens(leftTokens[0], hangingIndentationSpaces) + indentationTokens = @buildSoftWrapIndentationTokens(leftTokens[0], hangingIndent) leftFragment = new TokenizedLine( tokens: leftTokens From 85595262156bfe30eb2fb3aaf184168f1c4dab05 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Wed, 18 Mar 2015 17:42:25 +0100 Subject: [PATCH 09/12] :white_check_mark: Write a more comprehensive spec --- spec/display-buffer-spec.coffee | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/display-buffer-spec.coffee b/spec/display-buffer-spec.coffee index 4f2046434..c61733836 100644 --- a/spec/display-buffer-spec.coffee +++ b/spec/display-buffer-spec.coffee @@ -133,6 +133,7 @@ describe "DisplayBuffer", -> atom.config.set('editor.softWrapHangingIndent', 3) it "further indents wrapped lines", -> + expect(displayBuffer.tokenizedLineForScreenRow(10).text).toBe " return " expect(displayBuffer.tokenizedLineForScreenRow(11).text).toBe " sort(left).concat(pivot).concat(sort(right)" expect(displayBuffer.tokenizedLineForScreenRow(12).text).toBe " );" From f5e1e40edd5fcc06734e013e22b9ad87ea43d79d Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Wed, 18 Mar 2015 17:44:35 +0100 Subject: [PATCH 10/12] Avoid named parameter to save an allocation --- src/display-buffer.coffee | 2 +- src/tokenized-line.coffee | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/display-buffer.coffee b/src/display-buffer.coffee index c8ec23e07..8e8bd8dcd 100644 --- a/src/display-buffer.coffee +++ b/src/display-buffer.coffee @@ -1164,7 +1164,7 @@ class DisplayBuffer extends Model while wrapScreenColumn = tokenizedLine.findWrapColumn(@getSoftWrapColumn()) [wrappedLine, tokenizedLine] = tokenizedLine.softWrapAt( wrapScreenColumn, - hangingIndent: @configSettings.softWrapHangingIndent + @configSettings.softWrapHangingIndent ) break if wrappedLine.hasOnlySoftWrapIndentation() screenLines.push(wrappedLine) diff --git a/src/tokenized-line.coffee b/src/tokenized-line.coffee index a949a6e11..98224e189 100644 --- a/src/tokenized-line.coffee +++ b/src/tokenized-line.coffee @@ -132,11 +132,9 @@ class TokenizedLine else indentTokens - softWrapAt: (column, options = {}) -> + softWrapAt: (column, hangingIndent) -> return [new TokenizedLine([], '', [0, 0], [0, 0]), this] if column == 0 - {hangingIndent} = options - rightTokens = new Array(@tokens...) leftTokens = [] leftTextLength = 0 From fbfe19825b545d71a29a0309e8da3d68da4325dc Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Wed, 18 Mar 2015 18:00:31 +0100 Subject: [PATCH 11/12] Even more descriptive specs :sparkles: --- spec/display-buffer-spec.coffee | 34 +++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/spec/display-buffer-spec.coffee b/spec/display-buffer-spec.coffee index c61733836..1703a24d1 100644 --- a/spec/display-buffer-spec.coffee +++ b/spec/display-buffer-spec.coffee @@ -124,9 +124,16 @@ describe "DisplayBuffer", -> expect(displayBuffer.tokenizedLineForScreenRow(3).tokens[1].isHardTab).toBeTruthy() describe "when a line is wrapped", -> - it "correctly tokenizes soft wrap indentation tokens", -> - expect(displayBuffer.tokenizedLineForScreenRow(4).tokens[0].isSoftWrapIndentation).toBeTruthy() - expect(displayBuffer.tokenizedLineForScreenRow(4).tokens[1].isSoftWrapIndentation).toBeTruthy() + it "breaks soft-wrap indentation into a token for each indentation level to support indent guides", -> + tokenizedLine = displayBuffer.tokenizedLineForScreenRow(4) + + expect(tokenizedLine.tokens[0].value).toBe(" ") + expect(tokenizedLine.tokens[0].isSoftWrapIndentation).toBeTruthy() + + expect(tokenizedLine.tokens[1].value).toBe(" ") + expect(tokenizedLine.tokens[1].isSoftWrapIndentation).toBeTruthy() + + expect(tokenizedLine.tokens[2].isSoftWrapIndentation).toBeFalsy() describe "when editor.softWrapHangingIndent is set", -> beforeEach -> @@ -137,11 +144,22 @@ describe "DisplayBuffer", -> expect(displayBuffer.tokenizedLineForScreenRow(11).text).toBe " sort(left).concat(pivot).concat(sort(right)" expect(displayBuffer.tokenizedLineForScreenRow(12).text).toBe " );" - it "correctly tokenizes hanging indentation spaces", -> - expect(displayBuffer.tokenizedLineForScreenRow(4).tokens[0].isSoftWrapIndentation).toBeTruthy() - expect(displayBuffer.tokenizedLineForScreenRow(4).tokens[1].isSoftWrapIndentation).toBeTruthy() - expect(displayBuffer.tokenizedLineForScreenRow(4).tokens[2].isSoftWrapIndentation).toBeTruthy() - expect(displayBuffer.tokenizedLineForScreenRow(4).tokens[3].isSoftWrapIndentation).toBeTruthy() + it "includes hanging indent when breaking soft-wrap indentation into tokens", -> + tokenizedLine = displayBuffer.tokenizedLineForScreenRow(4) + + expect(tokenizedLine.tokens[0].value).toBe(" ") + expect(tokenizedLine.tokens[0].isSoftWrapIndentation).toBeTruthy() + + expect(tokenizedLine.tokens[1].value).toBe(" ") + expect(tokenizedLine.tokens[1].isSoftWrapIndentation).toBeTruthy() + + expect(tokenizedLine.tokens[2].value).toBe(" ") # hanging indent + expect(tokenizedLine.tokens[2].isSoftWrapIndentation).toBeTruthy() + + expect(tokenizedLine.tokens[3].value).toBe(" ") # odd space + expect(tokenizedLine.tokens[3].isSoftWrapIndentation).toBeTruthy() + + expect(tokenizedLine.tokens[4].isSoftWrapIndentation).toBeFalsy() describe "when the buffer changes", -> describe "when buffer lines are updated", -> From 522e82ebf1eb71370f60af55b6970943677cf0e6 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Wed, 18 Mar 2015 18:07:07 +0100 Subject: [PATCH 12/12] :art: Refactor buildSoftWrapIndentationTokens ...as suggested by @nathansobo :sparkles: --- src/tokenized-line.coffee | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/src/tokenized-line.coffee b/src/tokenized-line.coffee index 98224e189..2a807d84a 100644 --- a/src/tokenized-line.coffee +++ b/src/tokenized-line.coffee @@ -111,26 +111,16 @@ class TokenizedLine return maxColumn - # For a given `indentLevel`, calculates how many trailing spaces cannot fit in a single tab. - # - # indentLevel - {Number} - # Returns a {Number} representing the odd indentation spaces in `indentLevel`. - oddIndentationSpacesForIndentLevel: (indentLevel) -> - oddIndentLevel = indentLevel - Math.floor(indentLevel) - Math.round(@tabLength * oddIndentLevel) - buildSoftWrapIndentationTokens: (token, hangingIndent) -> - hangingIndentLevel = hangingIndent / @tabLength - indentLevel = @indentLevel + hangingIndentLevel - indentTokens = [0...Math.floor(indentLevel)].map => - token.buildSoftWrapIndentationToken(@tabLength) + totalIndentSpaces = (@indentLevel * @tabLength) + hangingIndent + indentTokens = [] + while totalIndentSpaces > 0 + tokenLength = Math.min(@tabLength, totalIndentSpaces) + indentToken = token.buildSoftWrapIndentationToken(tokenLength) + indentTokens.push(indentToken) + totalIndentSpaces -= tokenLength - if oddIndentationSpaces = @oddIndentationSpacesForIndentLevel(indentLevel) - indentTokens.concat( - token.buildSoftWrapIndentationToken(oddIndentationSpaces) - ) - else - indentTokens + indentTokens softWrapAt: (column, hangingIndent) -> return [new TokenizedLine([], '', [0, 0], [0, 0]), this] if column == 0