-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathCodeFormat.cpp
More file actions
270 lines (231 loc) · 10.4 KB
/
CodeFormat.cpp
File metadata and controls
270 lines (231 loc) · 10.4 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include <cstring>
#include <fstream>
#include <iostream>
#include "LuaFormat.h"
#include "Util/CommandLine.h"
#include "Util/StringUtil.h"
#include "Util/format.h"
#include <LuaCheck.h>
// https://stackoverflow.com/questions/1598985/c-read-binary-stdin
#ifdef _WIN32
#include <fcntl.h>
#include <io.h>
#define SET_BINARY_MODE() \
_setmode(_fileno(stdin), _O_BINARY); \
_setmode(_fileno(stdout), _O_BINARY)
#else
#define SET_BINARY_MODE() ((void) 0)
#endif
bool InitFormat(CommandLine &cmd, FormatContext &formatContext);
bool InitCheck(CommandLine &cmd, FormatContext &formatContext);
bool InitRangeFormat(CommandLine &cmd, FormatContext &formatContext);
int main(int argc, char **argv) {
CommandLine cmd;
cmd.SetUsage(
"Usage:\n"
"CodeFormat [check/format/rangeformat] [options]\n"
"for example:\n"
"\tCodeFormat check -w . -d\n"
"\tCodeFormat format -f test.lua -d\n"
"\tCodeFormat check -w . -d --ignores \"Test/*.lua;src/**.lua\"\n"
"\tCodeFormat check -w . -d --ignores-file \".gitignore\"\n"
"\tCodeFormat rangeformat -i -d --rangeline 1:10\n"
"\tCodeFormat rangeformat -i -d --rangeOffset 0:100\n");
cmd.AddTarget("format")
.Add<std::string>("file", "f", "Specify the input file")
.Add<bool>("overwrite", "ow", "Format overwrite the input file")
.Add<std::string>("workspace", "w",
"Specify workspace directory,if no input file is specified, bulk formatting is performed")
.Add<bool>("stdin", "i", "Read from stdin")
.Add<std::string>("config", "c",
"Specify .editorconfig file, it decides on the effect of formatting")
.Add<bool>("detect-config", "d",
"Configuration will be automatically detected,\n"
"\t\tIf this option is set, the config option has no effect ")
.Add<std::string>("outfile", "o",
"Specify output file")
.Add<std::string>("ignores-file", "igf",
"Specify which files to ignore through configuration file,for example \".gitignore\"")
.Add<std::string>("ignores", "ig",
"Use file wildcards to specify how to ignore files\n"
"\t\tseparated by ';'")
.Add<bool>("non-standard", "", "Enable non-standard formatting")
.Add<bool>("c-like-comments", "", "Enable c-like-comments formatting")
.EnableKeyValueArgs();
cmd.AddTarget("rangeformat")
.Add<std::string>("file", "f", "Specify the input file")
.Add<std::string>("workspace", "w",
"Specify workspace directory, It can be used to automatically check the configuration")
.Add<bool>("stdin", "i", "Read from stdin")
.Add<std::string>("config", "c",
"Specify .editorconfig file, it decides on the effect of formatting")
.Add<bool>("detect-config", "d",
"Configuration will be automatically detected,\n"
"\t\tIf this option is set, the config option has no effect ")
.Add<bool>("complete-output", "",
"If true, all content will be output")
.Add<std::string>("range-line", "", "the format is startline:endline, for eg: 1:10")
.Add<std::string>("range-offset", "", "the format is startOffset:endOffset, for eg: 0:256")
.Add<bool>("non-standard", "", "Enable non-standard rangeformatting")
.Add<bool>("c-like-comments", "", "Enable c-like-comments formatting")
.EnableKeyValueArgs();
cmd.AddTarget("check")
.Add<std::string>("file", "f", "Specify the input file")
.Add<bool>("stdin", "i", "Read from stdin")
.Add<std::string>("workspace", "w",
"Specify workspace directory, if no input file is specified, bulk checking is performed")
.Add<std::string>("config", "c",
"Specify editorconfig file, it decides on the effect of formatting or diagnosis")
.Add<bool>("detect-config", "d",
"Configuration will be automatically detected,\n"
"\t\tIf this option is set, the config option has no effect")
.Add<bool>("diagnosis-as-error", "DAE", "if exist error or diagnosis info , return -1")
.Add<std::string>("ignores-file", "igf",
"Specify which files to ignore through configuration file,for example \".gitignore\"")
.Add<std::string>("ignores", "ig",
"Use file wildcards to specify how to ignore files\n"
"\t\tseparated by ';'")
.Add<bool>("name-style", "ns", "Enable name-style check")
.Add<bool>("non-standard", "", "Enable non-standard checking")
.Add<bool>("c-like-comments", "", "Enable c-like-comments formatting")
.Add<bool>("dump-json", "", "Dump json format diagnosis info")
.EnableKeyValueArgs();
if (!cmd.Parse(argc, argv)) {
cmd.PrintUsage();
return -1;
}
FormatContext formatContext;
if (cmd.GetTarget() == "format") {
InitFormat(cmd, formatContext);
if (LuaFormat format; !format.Reformat(formatContext)) {
// special return code for intellij
return 1;
}
} else if (cmd.GetTarget() == "check") {
InitCheck(cmd, formatContext);
if (LuaCheck check; !check.Check(formatContext) && cmd.Get<bool>("diagnosis-as-error")) {
return -1;
}
} else if (cmd.GetTarget() == "rangeformat") {
InitRangeFormat(cmd, formatContext);
if (LuaFormat format; !format.RangeReformat(formatContext)) {
// special return code for intellij
return 1;
}
}
return 0;
}
bool InitFormat(CommandLine &cmd, FormatContext &formatContext) {
if (cmd.HasOption("file") || cmd.HasOption("stdin")) {
if (cmd.HasOption("file")) {
formatContext.SetInputFilePath(cmd.Get<std::string>("file"));
}
if (cmd.HasOption("file") && !cmd.HasOption("stdin")) {
formatContext.SetWorkMode(WorkMode::File);
} else {
formatContext.SetWorkMode(WorkMode::Stdin);
SET_BINARY_MODE();
}
if (cmd.HasOption("outfile")) {
formatContext.SetOutputFilePath(cmd.Get<std::string>("outfile"));
} else if (cmd.HasOption("overwrite")) {
formatContext.SetOutputFilePath(cmd.Get<std::string>("file"));
}
} else {
formatContext.SetWorkMode(WorkMode::Workspace);
}
if (cmd.HasOption("workspace")) {
formatContext.SetWorkspacePath(cmd.Get<std::string>("workspace"));
}
if (cmd.HasOption("ignores")) {
auto ignores = cmd.Get<std::string>("ignores");
auto patterns = string_util::Split(ignores, ";");
for (auto pattern: patterns) {
auto patternNoSpace = string_util::TrimSpace(pattern);
if (!patternNoSpace.empty()) {
formatContext.AddIgnorePattern(patternNoSpace);
}
}
}
if (cmd.HasOption("ignores-file")) {
formatContext.AddIgnorePatternsFromFile(cmd.Get<std::string>("ignores-file"));
}
if (cmd.Get<bool>("detect-config")) {
formatContext.EnableAutoDetectConfig();
} else if (cmd.HasOption("config")) {
formatContext.SetConfigFilePath(cmd.Get<std::string>("config"));
}
if (cmd.Get<bool>("non-standard")) {
formatContext.EnableNonStandardLuaSupport();
}
if (cmd.Get<bool>("c-like-comments")) {
formatContext.EnableCLikeCommentsSupport();
}
formatContext.SetDefaultStyleOptions(cmd.GetKeyValueOptions());
return true;
}
bool InitCheck(CommandLine &cmd, FormatContext &formatContext) {
if (cmd.HasOption("file") || cmd.HasOption("stdin")) {
if (cmd.HasOption("file")) {
formatContext.SetInputFilePath(cmd.Get<std::string>("file"));
}
if (cmd.HasOption("file") && !cmd.HasOption("stdin")) {
formatContext.SetWorkMode(WorkMode::File);
} else {
formatContext.SetWorkMode(WorkMode::Stdin);
SET_BINARY_MODE();
}
if (cmd.HasOption("outfile")) {
formatContext.SetOutputFilePath(cmd.Get<std::string>("outfile"));
}
} else {
formatContext.SetWorkMode(WorkMode::Workspace);
}
if (cmd.HasOption("workspace")) {
formatContext.SetWorkspacePath(cmd.Get<std::string>("workspace"));
}
if (cmd.HasOption("ignores")) {
auto ignores = cmd.Get<std::string>("ignores");
auto patterns = string_util::Split(ignores, ";");
for (auto pattern: patterns) {
auto patternNoSpace = string_util::TrimSpace(pattern);
if (!patternNoSpace.empty()) {
formatContext.AddIgnorePattern(patternNoSpace);
}
}
}
if (cmd.HasOption("ignores-file")) {
formatContext.AddIgnorePatternsFromFile(cmd.Get<std::string>("ignores-file"));
}
if (cmd.Get<bool>("detect-config")) {
formatContext.EnableAutoDetectConfig();
} else if (cmd.HasOption("config")) {
formatContext.SetConfigFilePath(cmd.Get<std::string>("config"));
}
if (cmd.Get<bool>("non-standard")) {
formatContext.EnableNonStandardLuaSupport();
}
if (cmd.Get<bool>("name-style")) {
formatContext.EnableNameStyleCheckSupport();
}
if (cmd.Get<bool>("dump-json")) {
formatContext.EnableJsonDump();
}
if (cmd.Get<bool>("c-like-comments")) {
formatContext.EnableCLikeCommentsSupport();
}
formatContext.SetDefaultStyleOptions(cmd.GetKeyValueOptions());
return true;
}
bool InitRangeFormat(CommandLine &cmd, FormatContext &formatContext) {
InitFormat(cmd, formatContext);
if (cmd.HasOption("range-line")) {
formatContext.SetFormatRange(true, cmd.Get<std::string>("range-line"));
} else if (cmd.HasOption("range-offset")) {
formatContext.SetFormatRange(false, cmd.Get<std::string>("range-offset"));
}
if (cmd.HasOption("complete-output")) {
formatContext.EnableCompleteOutputRangeSupport();
}
return true;
}