Skip to content

Commit 9524b48

Browse files
authored
Merge pull request #873 from viktorianer/fix-uncomment-lines-space-which-existed-before-the-comment-hash
Preserve Correct Indentation When Uncommenting Lines
2 parents e436b5f + 7cb2b07 commit 9524b48

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

Diff for: lib/thor/actions/file_manipulation.rb

+3-4
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,8 @@ def gsub_file(path, flag, *args, &block)
274274
end
275275
end
276276

277-
# Uncomment all lines matching a given regex. It will leave the space
278-
# which existed before the comment hash in tact but will remove any spacing
279-
# between the comment hash and the beginning of the line.
277+
# Uncomment all lines matching a given regex. Preserves indentation before
278+
# the comment hash and removes the hash and any immediate following space.
280279
#
281280
# ==== Parameters
282281
# path<String>:: path of the file to be changed
@@ -290,7 +289,7 @@ def gsub_file(path, flag, *args, &block)
290289
def uncomment_lines(path, flag, *args)
291290
flag = flag.respond_to?(:source) ? flag.source : flag
292291

293-
gsub_file(path, /^(\s*)#[[:blank:]]*(.*#{flag})/, '\1\2', *args)
292+
gsub_file(path, /^(\s*)#[[:blank:]]?(.*#{flag})/, '\1\2', *args)
294293
end
295294

296295
# Comment all lines matching a given regex. It will leave the space

Diff for: spec/actions/file_manipulation_spec.rb

+14-8
Original file line numberDiff line numberDiff line change
@@ -475,20 +475,26 @@ def file
475475
File.join(destination_root, "doc", "COMMENTER")
476476
end
477477

478-
unmodified_comments_file = /__start__\n # greenblue\n#\n# yellowblue\n#yellowred\n #greenred\norange\n purple\n ind#igo\n # ind#igo\n__end__/
478+
unmodified_comments_file = /__start__\n # greenblue\n#\n# yellowblue\n#yellowred\n #greenred\norange\n purple\n ind#igo\n # ind#igo\n # spaces_between\n__end__/
479479

480480
describe "#uncomment_lines" do
481481
it "uncomments all matching lines in the file" do
482482
action :uncomment_lines, "doc/COMMENTER", "green"
483-
expect(File.binread(file)).to match(/__start__\n greenblue\n#\n# yellowblue\n#yellowred\n greenred\norange\n purple\n ind#igo\n # ind#igo\n__end__/)
483+
expect(File.binread(file)).to match(/__start__\n greenblue\n#\n# yellowblue\n#yellowred\n greenred\norange\n purple\n ind#igo\n # ind#igo\n # spaces_between\n__end__/)
484484

485485
action :uncomment_lines, "doc/COMMENTER", "red"
486-
expect(File.binread(file)).to match(/__start__\n greenblue\n#\n# yellowblue\nyellowred\n greenred\norange\n purple\n ind#igo\n # ind#igo\n__end__/)
486+
expect(File.binread(file)).to match(/__start__\n greenblue\n#\n# yellowblue\nyellowred\n greenred\norange\n purple\n ind#igo\n # ind#igo\n # spaces_between\n__end__/)
487487
end
488488

489489
it "correctly uncomments lines with hashes in them" do
490490
action :uncomment_lines, "doc/COMMENTER", "ind#igo"
491-
expect(File.binread(file)).to match(/__start__\n # greenblue\n#\n# yellowblue\n#yellowred\n #greenred\norange\n purple\n ind#igo\n ind#igo\n__end__/)
491+
expect(File.binread(file)).to match(/__start__\n # greenblue\n#\n# yellowblue\n#yellowred\n #greenred\norange\n purple\n ind#igo\n ind#igo\n # spaces_between\n__end__/)
492+
end
493+
494+
it "will leave the space which existed before the comment hash in tact" do
495+
action :uncomment_lines, "doc/COMMENTER", "ind#igo"
496+
action :uncomment_lines, "doc/COMMENTER", "spaces_between"
497+
expect(File.binread(file)).to match(/__start__\n # greenblue\n#\n# yellowblue\n#yellowred\n #greenred\norange\n purple\n ind#igo\n ind#igo\n spaces_between\n__end__/)
492498
end
493499

494500
it "does not modify already uncommented lines in the file" do
@@ -499,22 +505,22 @@ def file
499505

500506
it "does not uncomment the wrong line when uncommenting lines preceded by blank commented line" do
501507
action :uncomment_lines, "doc/COMMENTER", "yellow"
502-
expect(File.binread(file)).to match(/__start__\n # greenblue\n#\nyellowblue\nyellowred\n #greenred\norange\n purple\n ind#igo\n # ind#igo\n__end__/)
508+
expect(File.binread(file)).to match(/__start__\n # greenblue\n#\nyellowblue\nyellowred\n #greenred\norange\n purple\n ind#igo\n # ind#igo\n # spaces_between\n__end__/)
503509
end
504510
end
505511

506512
describe "#comment_lines" do
507513
it "comments lines which are not commented" do
508514
action :comment_lines, "doc/COMMENTER", "orange"
509-
expect(File.binread(file)).to match(/__start__\n # greenblue\n#\n# yellowblue\n#yellowred\n #greenred\n# orange\n purple\n ind#igo\n # ind#igo\n__end__/)
515+
expect(File.binread(file)).to match(/__start__\n # greenblue\n#\n# yellowblue\n#yellowred\n #greenred\n# orange\n purple\n ind#igo\n # ind#igo\n # spaces_between\n__end__/)
510516

511517
action :comment_lines, "doc/COMMENTER", "purple"
512-
expect(File.binread(file)).to match(/__start__\n # greenblue\n#\n# yellowblue\n#yellowred\n #greenred\n# orange\n # purple\n ind#igo\n # ind#igo\n__end__/)
518+
expect(File.binread(file)).to match(/__start__\n # greenblue\n#\n# yellowblue\n#yellowred\n #greenred\n# orange\n # purple\n ind#igo\n # ind#igo\n # spaces_between\n__end__/)
513519
end
514520

515521
it "correctly comments lines with hashes in them" do
516522
action :comment_lines, "doc/COMMENTER", "ind#igo"
517-
expect(File.binread(file)).to match(/__start__\n # greenblue\n#\n# yellowblue\n#yellowred\n #greenred\norange\n purple\n # ind#igo\n # ind#igo\n__end__/)
523+
expect(File.binread(file)).to match(/__start__\n # greenblue\n#\n# yellowblue\n#yellowred\n #greenred\norange\n purple\n # ind#igo\n # ind#igo\n # spaces_between\n__end__/)
518524
end
519525

520526
it "does not modify already commented lines" do

Diff for: spec/fixtures/doc/COMMENTER

+1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ orange
88
purple
99
ind#igo
1010
# ind#igo
11+
# spaces_between
1112
__end__

0 commit comments

Comments
 (0)