diff --git a/spec/language-mode-spec.coffee b/spec/language-mode-spec.coffee index 07b875a91..47fa30bdf 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,3]] + describe "coffeescript", -> beforeEach -> waitsForPromise -> diff --git a/src/language-mode.coffee b/src/language-mode.coffee index 96c1073c1..b5529a05e 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} = @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} 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()