From 9cf642e910ed8c1fca84075246ca4696abbe2ef2 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 29 Apr 2014 16:12:22 -0700 Subject: [PATCH] Preserve indentation when commenting Previously this happened only when the language did not have a comment end pattern. --- spec/language-mode-spec.coffee | 4 ++-- src/language-mode.coffee | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/spec/language-mode-spec.coffee b/spec/language-mode-spec.coffee index e445460c7..0dcf8a0b8 100644 --- a/spec/language-mode-spec.coffee +++ b/spec/language-mode-spec.coffee @@ -184,13 +184,13 @@ describe "LanguageMode", -> languageMode.toggleLineCommentsForBufferRows(2, 2) expect(buffer.lineForRow(0)).toBe "/*body {" expect(buffer.lineForRow(1)).toBe " font-size: 1234px;*/" - expect(buffer.lineForRow(2)).toBe "/* width: 110%;*/" + expect(buffer.lineForRow(2)).toBe " /*width: 110%;*/" expect(buffer.lineForRow(3)).toBe " font-weight: bold !important;" languageMode.toggleLineCommentsForBufferRows(0, 1) expect(buffer.lineForRow(0)).toBe "body {" expect(buffer.lineForRow(1)).toBe " font-size: 1234px;" - expect(buffer.lineForRow(2)).toBe "/* width: 110%;*/" + expect(buffer.lineForRow(2)).toBe " /*width: 110%;*/" expect(buffer.lineForRow(3)).toBe " font-weight: bold !important;" it "uncomments lines with leading whitespace", -> diff --git a/src/language-mode.coffee b/src/language-mode.coffee index 3f3193857..83d50a74b 100644 --- a/src/language-mode.coffee +++ b/src/language-mode.coffee @@ -60,7 +60,8 @@ class LanguageMode buffer.setTextInRange([[end, endColumn], [end, endLength]], "") else buffer.transact -> - buffer.insert([start, 0], commentStartString) + indentLength = buffer.lineForRow(start).match(/^\s*/)?[0].length ? 0 + buffer.insert([start, indentLength], commentStartString) buffer.insert([end, buffer.lineLengthForRow(end)], commentEndString) else if shouldUncomment and start isnt end