From 74caf89dd1a74103796e798bf74f1a4c4741e427 Mon Sep 17 00:00:00 2001 From: Sean Lee Date: Fri, 20 Mar 2015 14:23:37 +0800 Subject: [PATCH 1/5] :bug: fix rowRangeForParagraphAtBufferRow using \S --- spec/language-mode-spec.coffee | 8 ++++++++ src/language-mode.coffee | 6 +++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/spec/language-mode-spec.coffee b/spec/language-mode-spec.coffee index 07b875a91..d23bd5506 100644 --- a/spec/language-mode-spec.coffee +++ b/spec/language-mode-spec.coffee @@ -124,6 +124,11 @@ describe "LanguageMode", -> // lines var sort = function(items) {}; // comment line after fn + + var nosort = function(items) { + return item; + } + }; ''' @@ -144,6 +149,9 @@ describe "LanguageMode", -> range = languageMode.rowRangeForParagraphAtBufferRow(15) expect(range).toEqual [[15,0], [15,26]] + range = languageMode.rowRangeForParagraphAtBufferRow(18) + expect(range).toEqual [[17,0], [19,1]] + describe "coffeescript", -> beforeEach -> waitsForPromise -> diff --git a/src/language-mode.coffee b/src/language-mode.coffee index 96c1073c1..267383ff4 100644 --- a/src/language-mode.coffee +++ b/src/language-mode.coffee @@ -194,7 +194,7 @@ class LanguageMode # Right now, a paragraph is a block of text bounded by and empty line or a # block of text that is not the same type (comments next to source code). rowRangeForParagraphAtBufferRow: (bufferRow) -> - return unless /\w/.test(@editor.lineTextForBufferRow(bufferRow)) + return unless /\S/.test(@editor.lineTextForBufferRow(bufferRow)) if @isLineCommentedAtBufferRow(bufferRow) isOriginalRowComment = true @@ -207,14 +207,14 @@ class LanguageMode startRow = bufferRow while startRow > firstRow break if @isLineCommentedAtBufferRow(startRow - 1) isnt isOriginalRowComment - break unless /\w/.test(@editor.lineTextForBufferRow(startRow - 1)) + break unless /\S/.test(@editor.lineTextForBufferRow(startRow - 1)) startRow-- endRow = bufferRow lastRow = @editor.getLastBufferRow() while endRow < lastRow break if @isLineCommentedAtBufferRow(endRow + 1) isnt isOriginalRowComment - break unless /\w/.test(@editor.lineTextForBufferRow(endRow + 1)) + break unless /\S/.test(@editor.lineTextForBufferRow(endRow + 1)) endRow++ new Range([startRow, 0], [endRow, @editor.lineTextForBufferRow(endRow).length]) From 2872000d13279b09c1624b526fb822875cccb3f3 Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Mon, 13 Apr 2015 16:26:24 -0700 Subject: [PATCH 2/5] Fix paragraph computation in blocks of single line comments. Closes #6050 Closes #5963 --- spec/language-mode-spec.coffee | 4 ++-- src/language-mode.coffee | 43 +++++++++++++++++++++++----------- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/spec/language-mode-spec.coffee b/spec/language-mode-spec.coffee index d23bd5506..47fa30bdf 100644 --- a/spec/language-mode-spec.coffee +++ b/spec/language-mode-spec.coffee @@ -126,7 +126,7 @@ describe "LanguageMode", -> // comment line after fn var nosort = function(items) { - return item; + return item; } }; @@ -150,7 +150,7 @@ describe "LanguageMode", -> expect(range).toEqual [[15,0], [15,26]] range = languageMode.rowRangeForParagraphAtBufferRow(18) - expect(range).toEqual [[17,0], [19,1]] + expect(range).toEqual [[17,0], [19,3]] describe "coffeescript", -> beforeEach -> diff --git a/src/language-mode.coffee b/src/language-mode.coffee index 267383ff4..bff2c55a0 100644 --- a/src/language-mode.coffee +++ b/src/language-mode.coffee @@ -24,14 +24,8 @@ class LanguageMode # endRow - The row {Number} to end at toggleLineCommentsForBufferRows: (start, end) -> scope = @editor.scopeDescriptorForBufferPosition([start, 0]) - commentStartEntry = atom.config.getAll('editor.commentStart', {scope})[0] - - return unless commentStartEntry? - - commentEndEntry = _.find atom.config.getAll('editor.commentEnd', {scope}), (entry) -> - entry.scopeSelector is commentStartEntry.scopeSelector - commentStartString = commentStartEntry?.value - commentEndString = commentEndEntry?.value + {commentStartString, commentEndString} = @getCommentStartAndEndStrings(scope) + return unless commentStartString? buffer = @editor.buffer commentStartRegexString = _.escapeRegExp(commentStartString).replace(/(\s+)$/, '(?:$1)?') @@ -190,11 +184,24 @@ class LanguageMode return false unless 0 <= bufferRow <= @editor.getLastBufferRow() @editor.displayBuffer.tokenizedBuffer.tokenizedLineForRow(bufferRow).isComment() - # Find a row range for a 'paragraph' around specified bufferRow. - # Right now, a paragraph is a block of text bounded by and empty line or a - # block of text that is not the same type (comments next to source code). + # Find a row range for a 'paragraph' around specified bufferRow. A paragraph + # is a block of text bounded by and empty line or a block of text that is not + # the same type (comments next to source code). rowRangeForParagraphAtBufferRow: (bufferRow) -> - return unless /\S/.test(@editor.lineTextForBufferRow(bufferRow)) + scope = @editor.scopeDescriptorForBufferPosition([bufferRow, 0]) + {commentStartString, commentEndString} = @getCommentStartAndEndStrings(scope) + commentStartRegex = null + if commentStartString? and not commentEndString? + commentStartRegexString = _.escapeRegExp(commentStartString).replace(/(\s+)$/, '(?:$1)?') + commentStartRegex = new OnigRegExp("^(\\s*)(#{commentStartRegexString})") + + filterLineComments = (line) -> + if commentStartRegex? + matches = commentStartRegex.searchSync(line) + line = line.substring(matches[0].end) if matches?.length + line + + return unless /\S/.test(filterLineComments(@editor.lineTextForBufferRow(bufferRow))) if @isLineCommentedAtBufferRow(bufferRow) isOriginalRowComment = true @@ -207,14 +214,14 @@ class LanguageMode startRow = bufferRow while startRow > firstRow break if @isLineCommentedAtBufferRow(startRow - 1) isnt isOriginalRowComment - break unless /\S/.test(@editor.lineTextForBufferRow(startRow - 1)) + break unless /\S/.test(filterLineComments(@editor.lineTextForBufferRow(startRow - 1))) startRow-- endRow = bufferRow lastRow = @editor.getLastBufferRow() while endRow < lastRow break if @isLineCommentedAtBufferRow(endRow + 1) isnt isOriginalRowComment - break unless /\S/.test(@editor.lineTextForBufferRow(endRow + 1)) + break unless /\S/.test(filterLineComments(@editor.lineTextForBufferRow(endRow + 1))) endRow++ new Range([startRow, 0], [endRow, @editor.lineTextForBufferRow(endRow).length]) @@ -319,3 +326,11 @@ class LanguageMode foldEndRegexForScopeDescriptor: (scopeDescriptor) -> @getRegexForProperty(scopeDescriptor, 'editor.foldEndPattern') + + getCommentStartAndEndStrings: (scope) -> + commentStartEntry = atom.config.getAll('editor.commentStart', {scope})[0] + commentEndEntry = _.find atom.config.getAll('editor.commentEnd', {scope}), (entry) -> + entry.scopeSelector is commentStartEntry.scopeSelector + commentStartString = commentStartEntry?.value + commentEndString = commentEndEntry?.value + {commentStartString, commentEndString} From d38f6bb1d38871b71c4905ba7c4c0661d3cf62c8 Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Mon, 13 Apr 2015 16:26:50 -0700 Subject: [PATCH 3/5] Add toString to `ScopeDescriptor` --- src/scope-descriptor.coffee | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/scope-descriptor.coffee b/src/scope-descriptor.coffee index d7ec263fd..7940cc630 100644 --- a/src/scope-descriptor.coffee +++ b/src/scope-descriptor.coffee @@ -44,3 +44,6 @@ class ScopeDescriptor scope = ".#{scope}" unless scope[0] is '.' scope .join(' ') + + toString: -> + @getScopeChain() From 8f2fbbfd9d329ab264004d40961e5b7fc04d1ff9 Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Mon, 13 Apr 2015 16:29:10 -0700 Subject: [PATCH 4/5] Rename method to be more consistent --- src/language-mode.coffee | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/language-mode.coffee b/src/language-mode.coffee index bff2c55a0..d48c68d58 100644 --- a/src/language-mode.coffee +++ b/src/language-mode.coffee @@ -24,7 +24,7 @@ class LanguageMode # endRow - The row {Number} to end at toggleLineCommentsForBufferRows: (start, end) -> scope = @editor.scopeDescriptorForBufferPosition([start, 0]) - {commentStartString, commentEndString} = @getCommentStartAndEndStrings(scope) + {commentStartString, commentEndString} = @commentStartAndEndStringsForScope(scope) return unless commentStartString? buffer = @editor.buffer @@ -189,7 +189,7 @@ class LanguageMode # the same type (comments next to source code). rowRangeForParagraphAtBufferRow: (bufferRow) -> scope = @editor.scopeDescriptorForBufferPosition([bufferRow, 0]) - {commentStartString, commentEndString} = @getCommentStartAndEndStrings(scope) + {commentStartString, commentEndString} = @commentStartAndEndStringsForScope(scope) commentStartRegex = null if commentStartString? and not commentEndString? commentStartRegexString = _.escapeRegExp(commentStartString).replace(/(\s+)$/, '(?:$1)?') @@ -327,7 +327,7 @@ class LanguageMode foldEndRegexForScopeDescriptor: (scopeDescriptor) -> @getRegexForProperty(scopeDescriptor, 'editor.foldEndPattern') - getCommentStartAndEndStrings: (scope) -> + commentStartAndEndStringsForScope: (scope) -> commentStartEntry = atom.config.getAll('editor.commentStart', {scope})[0] commentEndEntry = _.find atom.config.getAll('editor.commentEnd', {scope}), (entry) -> entry.scopeSelector is commentStartEntry.scopeSelector From f6c1f95b65e8a8831b7d02b90849801b63cd9dc3 Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Mon, 13 Apr 2015 16:32:26 -0700 Subject: [PATCH 5/5] Rename method for clarity --- src/language-mode.coffee | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/language-mode.coffee b/src/language-mode.coffee index d48c68d58..b5529a05e 100644 --- a/src/language-mode.coffee +++ b/src/language-mode.coffee @@ -195,13 +195,13 @@ class LanguageMode commentStartRegexString = _.escapeRegExp(commentStartString).replace(/(\s+)$/, '(?:$1)?') commentStartRegex = new OnigRegExp("^(\\s*)(#{commentStartRegexString})") - filterLineComments = (line) -> + filterCommentStart = (line) -> if commentStartRegex? matches = commentStartRegex.searchSync(line) line = line.substring(matches[0].end) if matches?.length line - return unless /\S/.test(filterLineComments(@editor.lineTextForBufferRow(bufferRow))) + return unless /\S/.test(filterCommentStart(@editor.lineTextForBufferRow(bufferRow))) if @isLineCommentedAtBufferRow(bufferRow) isOriginalRowComment = true @@ -214,14 +214,14 @@ class LanguageMode startRow = bufferRow while startRow > firstRow break if @isLineCommentedAtBufferRow(startRow - 1) isnt isOriginalRowComment - break unless /\S/.test(filterLineComments(@editor.lineTextForBufferRow(startRow - 1))) + break unless /\S/.test(filterCommentStart(@editor.lineTextForBufferRow(startRow - 1))) startRow-- endRow = bufferRow lastRow = @editor.getLastBufferRow() while endRow < lastRow break if @isLineCommentedAtBufferRow(endRow + 1) isnt isOriginalRowComment - break unless /\S/.test(filterLineComments(@editor.lineTextForBufferRow(endRow + 1))) + break unless /\S/.test(filterCommentStart(@editor.lineTextForBufferRow(endRow + 1))) endRow++ new Range([startRow, 0], [endRow, @editor.lineTextForBufferRow(endRow).length])