Merge pull request #6364 from atom/seansay-fix-row-range-for-p

Fix rowRangeForParagraphAtBufferRow using \S -- Round two
This commit is contained in:
Ben Ogle
2015-04-13 17:48:56 -07:00
3 changed files with 40 additions and 14 deletions

View File

@@ -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,3]]
describe "coffeescript", ->
beforeEach ->
waitsForPromise ->

View File

@@ -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} = @commentStartAndEndStringsForScope(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 /\w/.test(@editor.lineTextForBufferRow(bufferRow))
scope = @editor.scopeDescriptorForBufferPosition([bufferRow, 0])
{commentStartString, commentEndString} = @commentStartAndEndStringsForScope(scope)
commentStartRegex = null
if commentStartString? and not commentEndString?
commentStartRegexString = _.escapeRegExp(commentStartString).replace(/(\s+)$/, '(?:$1)?')
commentStartRegex = new OnigRegExp("^(\\s*)(#{commentStartRegexString})")
filterCommentStart = (line) ->
if commentStartRegex?
matches = commentStartRegex.searchSync(line)
line = line.substring(matches[0].end) if matches?.length
line
return unless /\S/.test(filterCommentStart(@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 /\w/.test(@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 /\w/.test(@editor.lineTextForBufferRow(endRow + 1))
break unless /\S/.test(filterCommentStart(@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')
commentStartAndEndStringsForScope: (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}

View File

@@ -44,3 +44,6 @@ class ScopeDescriptor
scope = ".#{scope}" unless scope[0] is '.'
scope
.join(' ')
toString: ->
@getScopeChain()