-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemp
More file actions
217 lines (214 loc) · 6.59 KB
/
Copy pathtemp
File metadata and controls
217 lines (214 loc) · 6.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
diff --git a/bin/elsefix.dart b/bin/elsefix.dart
index 1491257..a13986c 100644
--- a/bin/elsefix.dart
+++ b/bin/elsefix.dart
@@ -8,7 +8,8 @@ void main(List<String> args) async {
registerFlags(p);
try {
runMain(args, p);
- } catch (e) {
+ }
+ catch (e) {
print("Error: $e");
p.printFlags();
}
@@ -106,6 +107,13 @@ Future<String> handleLines(
) async {
String result = "";
bool acceptAll = false;
+ bool toStdout =
+ parsed["--stdout"] ??
+ parsed["-s"] ??
+ parsed["-"] ??
+ parsed["--stdin"] ??
+ false;
+ bool dryRun = parsed["--dry-run"] ?? parsed["-d"] ?? false;
for (int i = 0; i < lines.length; i++) {
String line = lines[i];
String newLine = "";
@@ -134,16 +142,123 @@ Future<String> handleLines(
newLine = fixed;
// default ('n' or anything else): leave newLine empty to keep original
}
- } else {
+ }
+ else {
newLine = handleLine(line, i, parsed, spaceType);
}
}
if (newLine.isEmpty) newLine = line;
- result += "$newLine\n";
+ // process mid-line else splits on each resulting sub-line
+ final List<String> subLines = newLine.split("\n");
+ final List<String> finalLines = [];
+ bool quit = false;
+ for (final String subLine in subLines) {
+ String current = subLine;
+ while (!quit) {
+ int indent = getIndentLevel(current, spaceType);
+ int? pos = midLineElsePos(current, indent);
+ if (pos == null) break;
+ String pad = "";
+ for (int k = 0; k < indent; k++) {
+ pad += spaceType == SpacingType.spaces ? " " : "\t";
+ }
+ String before = current.substring(0, pos).trimRight();
+ String after = "$pad${current.substring(pos)}";
+ String fixed = "$before\n$after";
+ if (interactive && !acceptAll) {
+ showInteractiveDiff([current], 0, fixed);
+ stderr.write("[y]es / [n]o / [A]ccept all / [q]uit: ");
+ String response = promptUser();
+ stderr.writeln("");
+ if (response == 'A') {
+ acceptAll = true;
+ finalLines.add(before);
+ current = after;
+ }
+ else if (response == 'y') {
+ finalLines.add(before);
+ current = after;
+ }
+ else if (response == 'q') {
+ quit = true;
+ }
+ else {
+ break; // 'n': keep current, stop splitting this sub-line
+ }
+ }
+ else {
+ if (!toStdout) {
+ print("Found:\n$_red- ${i + 1} | ${current.trimLeft()}$_reset");
+ print(
+ "Changing to:\n$_green+ ${i + 1} | ${before.trimLeft()}$_reset\n$_green+ ${i + 2} | ${after.trimLeft()}$_reset\n",
+ );
+ }
+ if (dryRun) break;
+ finalLines.add(before);
+ current = after;
+ }
+ }
+ finalLines.add(current);
+ }
+ result += "${finalLines.join("\n")}\n";
+ if (quit) {
+ for (int j = i + 1; j < lines.length; j++) {
+ result += "${lines[j]}\n";
+ }
+ return result;
+ }
}
return result;
}
+int? midLineElsePos(String line, int indentLevel) {
+ final pattern = RegExp(r'\belse\b');
+ for (final match in pattern.allMatches(line)) {
+ if (match.start <= indentLevel) continue;
+ if (line.substring(indentLevel, match.start).trim().isEmpty) continue;
+ bool inString = false;
+ bool inComment = false;
+ String? quoteChar;
+ for (int i = 0; i < match.start; i++) {
+ final c = line[i];
+ if (!inString && c == '/' && i + 1 < line.length && line[i + 1] == '/') {
+ inComment = true;
+ break;
+ }
+ if (!inString && (c == '"' || c == "'")) {
+ inString = true;
+ quoteChar = c;
+ }
+ else if (inString &&
+ c == quoteChar &&
+ (i == 0 || line[i - 1] != '\\')) {
+ inString = false;
+ quoteChar = null;
+ }
+ }
+ if (!inString && !inComment) return match.start;
+ }
+ return null;
+}
+
+List<String> splitMidLineElses(String line, SpacingType spaceType) {
+ final List<String> result = [];
+ String current = line;
+ while (true) {
+ int indent = getIndentLevel(current, spaceType);
+ int? pos = midLineElsePos(current, indent);
+ if (pos == null) break;
+ String pad = "";
+ for (int i = 0; i < indent; i++) {
+ pad += spaceType == SpacingType.spaces ? " " : "\t";
+ }
+ result.add(current.substring(0, pos).trimRight());
+ current = "$pad${current.substring(pos)}";
+ }
+ result.add(current);
+ return result;
+}
+
bool lineHasToken(String line, RegExp pattern) {
if (!pattern.hasMatch(line)) return false;
if (!line.contains('"') && !line.contains("'")) return true;
@@ -163,7 +278,7 @@ bool lineHasToken(String line, RegExp pattern) {
}
return true;
}
- return false;
+ return true;
}
void printUsage(Parser p) {
@@ -199,11 +314,11 @@ void registerFlags(Parser p) {
p.register("-s", "Print results to stdout.");
p.register(
"--dry-run",
- "Shows the output of the command without applying the changes..",
+ "Shows the output of the command without applying the changes.",
);
p.register(
"-d",
- "Shows the output of the command without applying the changes..",
+ "Shows the output of the command without applying the changes.",
);
p.register("--include-catch", "Also fix catch blocks.");
p.register("-c", "Also fix catch blocks.");
@@ -235,18 +350,20 @@ void runMain(List<String> args, Parser p) async {
if (useStdin) {
String text = await stdin.transform(utf8.decoder).join("\n");
lines = text.split("\n");
- } else if (filename.isNotEmpty) {
+ }
+ else if (filename.isNotEmpty) {
file = File(filename);
lines = file.readAsLinesSync();
- } else {
+ }
+ else {
printUsage(p);
}
final spaceType = detectSpacingType(lines);
bool includeCatch = parsed["-c"] ?? parsed["--include-catch"] ?? false;
- RegExp elsePattern = RegExp(r'}\s*\belse\b');
+ RegExp elsePattern = RegExp(r'}\s*[^\r\n]+\belse\b');
RegExp? catchPattern;
if (includeCatch) {
- catchPattern = RegExp(r'}\s*\bcatch\b');
+ catchPattern = RegExp(r'}\s*[^\r\n]+\bcatch\b');
}
bool toStdout = parsed["-s"] ?? parsed["--stdout"] ?? false;
String result = await handleLines(
@@ -260,7 +377,8 @@ void runMain(List<String> args, Parser p) async {
);
if (toStdout || useStdin) {
print(result);
- } else {
+ }
+ else {
file.writeAsStringSync(result);
}
}
@@ -290,3 +408,4 @@ void showInteractiveDiff(List<String> lines, int i, String fixed) {
}
enum SpacingType { spaces, tabs }
+