diff --git a/CHANGELOG.md b/CHANGELOG.md index 61765affb..846cac2fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,5 @@ +* Fixed: Using toggle comment shortcut respects indentation level + * Fixed: Search never completing in the command panel * Fixed: cmd-n now works when no windows are open diff --git a/spec/app/edit-session-spec.coffee b/spec/app/edit-session-spec.coffee index e9e74dd52..47f8ac23d 100644 --- a/spec/app/edit-session-spec.coffee +++ b/spec/app/edit-session-spec.coffee @@ -1717,10 +1717,10 @@ describe "EditSession", -> editSession.setSelectedBufferRange([[4, 5], [7, 5]]) editSession.toggleLineCommentsInSelection() - expect(buffer.lineForRow(4)).toBe "// while(items.length > 0) {" - expect(buffer.lineForRow(5)).toBe "// current = items.shift();" - expect(buffer.lineForRow(6)).toBe "// current < pivot ? left.push(current) : right.push(current);" - expect(buffer.lineForRow(7)).toBe "// }" + expect(buffer.lineForRow(4)).toBe " // while(items.length > 0) {" + expect(buffer.lineForRow(5)).toBe " // current = items.shift();" + expect(buffer.lineForRow(6)).toBe " // current < pivot ? left.push(current) : right.push(current);" + expect(buffer.lineForRow(7)).toBe " // }" expect(editSession.getSelectedBufferRange()).toEqual [[4, 8], [7, 8]] editSession.toggleLineCommentsInSelection() @@ -1732,9 +1732,9 @@ describe "EditSession", -> it "does not comment the last line of a non-empty selection if it ends at column 0", -> editSession.setSelectedBufferRange([[4, 5], [7, 0]]) editSession.toggleLineCommentsInSelection() - expect(buffer.lineForRow(4)).toBe "// while(items.length > 0) {" - expect(buffer.lineForRow(5)).toBe "// current = items.shift();" - expect(buffer.lineForRow(6)).toBe "// current < pivot ? left.push(current) : right.push(current);" + expect(buffer.lineForRow(4)).toBe " // while(items.length > 0) {" + expect(buffer.lineForRow(5)).toBe " // current = items.shift();" + expect(buffer.lineForRow(6)).toBe " // current < pivot ? left.push(current) : right.push(current);" expect(buffer.lineForRow(7)).toBe " }" it "uncomments lines if all lines match the comment regex", -> diff --git a/spec/app/language-mode-spec.coffee b/spec/app/language-mode-spec.coffee index efb45b138..be7ebb14f 100644 --- a/spec/app/language-mode-spec.coffee +++ b/spec/app/language-mode-spec.coffee @@ -14,19 +14,27 @@ describe "LanguageMode", -> editSession = project.open('sample.js', autoIndent: false) {buffer, languageMode} = editSession + describe ".minIndentLevelForRowRange(startRow, endRow)", -> + it "returns the minimum indent level for the given row range", -> + expect(languageMode.minIndentLevelForRowRange(4, 7)).toBe 2 + expect(languageMode.minIndentLevelForRowRange(5, 7)).toBe 2 + expect(languageMode.minIndentLevelForRowRange(5, 6)).toBe 3 + expect(languageMode.minIndentLevelForRowRange(9, 11)).toBe 1 + expect(languageMode.minIndentLevelForRowRange(10, 10)).toBe 0 + describe ".toggleLineCommentsForBufferRows(start, end)", -> it "comments/uncomments lines in the given range", -> languageMode.toggleLineCommentsForBufferRows(4, 7) - expect(buffer.lineForRow(4)).toBe "// while(items.length > 0) {" - expect(buffer.lineForRow(5)).toBe "// current = items.shift();" - expect(buffer.lineForRow(6)).toBe "// current < pivot ? left.push(current) : right.push(current);" - expect(buffer.lineForRow(7)).toBe "// }" + expect(buffer.lineForRow(4)).toBe " // while(items.length > 0) {" + expect(buffer.lineForRow(5)).toBe " // current = items.shift();" + expect(buffer.lineForRow(6)).toBe " // current < pivot ? left.push(current) : right.push(current);" + expect(buffer.lineForRow(7)).toBe " // }" languageMode.toggleLineCommentsForBufferRows(4, 5) expect(buffer.lineForRow(4)).toBe " while(items.length > 0) {" expect(buffer.lineForRow(5)).toBe " current = items.shift();" - expect(buffer.lineForRow(6)).toBe "// current < pivot ? left.push(current) : right.push(current);" - expect(buffer.lineForRow(7)).toBe "// }" + expect(buffer.lineForRow(6)).toBe " // current < pivot ? left.push(current) : right.push(current);" + expect(buffer.lineForRow(7)).toBe " // }" describe "fold suggestion", -> describe ".doesBufferRowStartFold(bufferRow)", -> @@ -58,17 +66,28 @@ describe "LanguageMode", -> describe ".toggleLineCommentsForBufferRows(start, end)", -> it "comments/uncomments lines in the given range", -> - languageMode.toggleLineCommentsForBufferRows(4, 7) - expect(buffer.lineForRow(4)).toBe "# pivot = items.shift()" - expect(buffer.lineForRow(5)).toBe "# left = []" - expect(buffer.lineForRow(6)).toBe "# right = []" - expect(buffer.lineForRow(7)).toBe "# " + languageMode.toggleLineCommentsForBufferRows(4, 6) + expect(buffer.lineForRow(4)).toBe " # pivot = items.shift()" + expect(buffer.lineForRow(5)).toBe " # left = []" + expect(buffer.lineForRow(6)).toBe " # right = []" languageMode.toggleLineCommentsForBufferRows(4, 5) expect(buffer.lineForRow(4)).toBe " pivot = items.shift()" expect(buffer.lineForRow(5)).toBe " left = []" - expect(buffer.lineForRow(6)).toBe "# right = []" - expect(buffer.lineForRow(7)).toBe "# " + expect(buffer.lineForRow(6)).toBe " # right = []" + + it "comments/uncomments lines when empty line", -> + languageMode.toggleLineCommentsForBufferRows(4, 7) + expect(buffer.lineForRow(4)).toBe " # pivot = items.shift()" + expect(buffer.lineForRow(5)).toBe " # left = []" + expect(buffer.lineForRow(6)).toBe " # right = []" + expect(buffer.lineForRow(7)).toBe " # " + + languageMode.toggleLineCommentsForBufferRows(4, 5) + expect(buffer.lineForRow(4)).toBe " pivot = items.shift()" + expect(buffer.lineForRow(5)).toBe " left = []" + expect(buffer.lineForRow(6)).toBe " # right = []" + expect(buffer.lineForRow(7)).toBe " # " describe "fold suggestion", -> describe ".doesBufferRowStartFold(bufferRow)", -> diff --git a/src/app/language-mode.coffee b/src/app/language-mode.coffee index 204e3f7ef..9bf809f9d 100644 --- a/src/app/language-mode.coffee +++ b/src/app/language-mode.coffee @@ -81,8 +81,10 @@ class LanguageMode columnEnd = columnStart + match[2].length buffer.change([[row, columnStart], [row, columnEnd]], "") else + indent = @minIndentLevelForRowRange(start, end) + indentString = @editSession.buildIndentString(indent) for row in [start..end] - buffer.insert([row, 0], commentStartString) + buffer.change([[row, 0], [row, indentString.length]], indentString + commentStartString) # Folds all the foldable lines in the buffer. foldAll: -> @@ -180,6 +182,17 @@ class LanguageMode desiredIndentLevel + # Calculate a minimum indent level for a range of lines excluding empty lines. + # + # startRow - The row {Number} to start at + # endRow - The row {Number} to end at + # + # Returns a {Number} of the indent level of the block of lines. + minIndentLevelForRowRange: (startRow, endRow) -> + indents = (@editSession.indentationForBufferRow(row) for row in [startRow..endRow] when not @editSession.isBufferRowBlank(row)) + indents = [0] unless indents.length + Math.min(indents...) + # Indents all the rows between two buffer row numbers. # # startRow - The row {Number} to start at