pragmater 7.0.0 → 8.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,69 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Pragmater
4
- # Writes formatted pragma comments to source file.
5
- # :reek:TooManyInstanceVariables
6
- # :reek:MissingSafeMethod
7
- class Writer
8
- # rubocop:disable Metrics/ParameterLists
9
- def initialize file_path, new_comments, formatter: Formatter, commenter: Commenter
10
- @file_path = file_path
11
- @file_lines = File.readlines file_path
12
- @formatter = formatter
13
- @commenter = commenter
14
- @old_comments = file_comments
15
- @new_comments = new_comments
16
- end
17
- # rubocop:enable Metrics/ParameterLists
18
-
19
- def add
20
- comments = format commenter.new(old_comments, new_comments).add
21
- lines = comments + file_lines_without_comments
22
- insert_spacing! lines, comments
23
- write { lines.join }
24
- end
25
-
26
- def remove
27
- lines = format(commenter.new(old_comments, new_comments).remove) + file_lines_without_comments
28
- remove_spacing! lines
29
- write { lines.join }
30
- end
31
-
32
- private
33
-
34
- attr_reader :file_path, :file_lines, :new_comments, :old_comments, :formatter, :commenter
35
-
36
- def file_comments
37
- file_lines.select { |line| line =~ formatter.valid_formats }
38
- end
39
-
40
- def file_lines_without_comments
41
- file_lines.reject { |line| old_comments.include? line }
42
- end
43
-
44
- # :reek:UtilityFunction
45
- def format lines
46
- lines.map { |line| "#{line}\n" }
47
- end
48
-
49
- # :reek:UtilityFunction
50
- def insert_spacing! lines, comments
51
- comment_count = comments.size
52
-
53
- return if comments.empty?
54
- return if lines.size == 1
55
- return if lines[comment_count] == "\n"
56
-
57
- lines.insert comment_count, "\n"
58
- end
59
-
60
- # :reek:UtilityFunction
61
- def remove_spacing! lines
62
- lines.delete_at 0 if lines.first == "\n"
63
- end
64
-
65
- def write
66
- File.open(file_path, "w") { |file| file.write yield }
67
- end
68
- end
69
- end