Skip to content

Commit 4f632be

Browse files
committed
Add LuaCheck class and Util functions, refactor
1 parent 234c0c8 commit 4f632be

File tree

10 files changed

+588
-511
lines changed

10 files changed

+588
-511
lines changed

CodeFormat/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@ add_dependencies(CodeFormat CodeFormatCore Util)
88

99
target_include_directories(CodeFormat PRIVATE
1010
src
11+
${LuaCodeStyle_SOURCE_DIR}/3rd/nlohmann_json/include
1112
)
1213

1314
target_sources(CodeFormat
1415
PRIVATE
1516
src/CodeFormat.cpp
1617
src/LuaFormat.cpp
18+
src/FormatContext.cpp
19+
src/LuaCheck.cpp
20+
src/Util.cpp
1721
)
1822

1923
target_link_libraries(CodeFormat CodeFormatCore Util)

CodeFormat/src/CodeFormat.cpp

Lines changed: 56 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
#include "Util/StringUtil.h"
88
#include "Util/format.h"
99

10+
11+
#include <LuaCheck.h>
12+
1013
// https://stackoverflow.com/questions/1598985/c-read-binary-stdin
1114
#ifdef _WIN32
1215

@@ -20,9 +23,9 @@
2023
#define SET_BINARY_MODE() ((void) 0)
2124
#endif
2225

23-
bool InitFormat(CommandLine &cmd, LuaFormat &format);
24-
bool InitCheck(CommandLine &cmd, LuaFormat &format);
25-
bool InitRangeFormat(CommandLine &cmd, LuaFormat &format);
26+
bool InitFormat(CommandLine &cmd, FormatContext &formatContext);
27+
bool InitCheck(CommandLine &cmd, FormatContext &formatContext);
28+
bool InitRangeFormat(CommandLine &cmd, FormatContext &formatContext);
2629

2730
int main(int argc, char **argv) {
2831
CommandLine cmd;
@@ -74,6 +77,7 @@ int main(int argc, char **argv) {
7477
.EnableKeyValueArgs();
7578
cmd.AddTarget("check")
7679
.Add<std::string>("file", "f", "Specify the input file")
80+
.Add<bool>("stdin", "i", "Read from stdin")
7781
.Add<std::string>("workspace", "w",
7882
"Specify workspace directory, if no input file is specified, bulk checking is performed")
7983
.Add<std::string>("config", "c",
@@ -89,6 +93,7 @@ int main(int argc, char **argv) {
8993
"\t\tseparated by ';'")
9094
.Add<bool>("name-style", "ns", "Enable name-style check")
9195
.Add<bool>("non-standard", "", "Enable non-standard checking")
96+
.Add<bool>("dump-json", "", "Dump json format diagnosis info")
9297
.EnableKeyValueArgs();
9398

9499

@@ -97,21 +102,21 @@ int main(int argc, char **argv) {
97102
return -1;
98103
}
99104

100-
LuaFormat format;
105+
FormatContext formatContext;
101106
if (cmd.GetTarget() == "format") {
102-
InitFormat(cmd, format);
103-
if (!format.Reformat()) {
107+
InitFormat(cmd, formatContext);
108+
if (LuaFormat format; !format.Reformat(formatContext)) {
104109
// special return code for intellij
105110
return 1;
106111
}
107112
} else if (cmd.GetTarget() == "check") {
108-
InitCheck(cmd, format);
109-
if (!format.Check() && cmd.Get<bool>("diagnosis-as-error")) {
113+
InitCheck(cmd, formatContext);
114+
if (LuaCheck check; !check.Check(formatContext) && cmd.Get<bool>("diagnosis-as-error")) {
110115
return -1;
111116
}
112117
} else if (cmd.GetTarget() == "rangeformat") {
113-
InitRangeFormat(cmd, format);
114-
if (!format.RangeReformat()) {
118+
InitRangeFormat(cmd, formatContext);
119+
if (LuaFormat format; !format.RangeReformat(formatContext)) {
115120
// special return code for intellij
116121
return 1;
117122
}
@@ -120,37 +125,30 @@ int main(int argc, char **argv) {
120125
return 0;
121126
}
122127

123-
bool InitFormat(CommandLine &cmd, LuaFormat &format) {
128+
bool InitFormat(CommandLine &cmd, FormatContext &formatContext) {
124129
if (cmd.HasOption("file") || cmd.HasOption("stdin")) {
125130
if (cmd.HasOption("file")) {
126-
format.SetInputFile(cmd.Get<std::string>("file"));
131+
formatContext.SetInputFilePath(cmd.Get<std::string>("file"));
127132
}
128133

129134
if (cmd.HasOption("file") && !cmd.HasOption("stdin")) {
130-
format.SetWorkMode(WorkMode::File);
131-
if (!format.ReadFromInput()) {
132-
std::cerr << util::format("Can not find file {}", cmd.Get<std::string>("file")) << std::endl;
133-
return false;
134-
}
135+
formatContext.SetWorkMode(WorkMode::File);
135136
} else {
136-
format.SetWorkMode(WorkMode::Stdin);
137+
formatContext.SetWorkMode(WorkMode::Stdin);
137138
SET_BINARY_MODE();
138-
if (!format.ReadFromStdin()) {
139-
return false;
140-
}
141139
}
142140

143141
if (cmd.HasOption("outfile")) {
144-
format.SetOutputFile(cmd.Get<std::string>("outfile"));
142+
formatContext.SetOutputFilePath(cmd.Get<std::string>("outfile"));
145143
} else if (cmd.HasOption("overwrite")) {
146-
format.SetOutputFile(cmd.Get<std::string>("file"));
144+
formatContext.SetOutputFilePath(cmd.Get<std::string>("file"));
147145
}
148146
} else {
149-
format.SetWorkMode(WorkMode::Workspace);
147+
formatContext.SetWorkMode(WorkMode::Workspace);
150148
}
151149

152150
if (cmd.HasOption("workspace")) {
153-
format.SetWorkspace(cmd.Get<std::string>("workspace"));
151+
formatContext.SetWorkspacePath(cmd.Get<std::string>("workspace"));
154152
}
155153

156154
if (cmd.HasOption("ignores")) {
@@ -159,55 +157,52 @@ bool InitFormat(CommandLine &cmd, LuaFormat &format) {
159157
for (auto pattern: patterns) {
160158
auto patternNoSpace = string_util::TrimSpace(pattern);
161159
if (!patternNoSpace.empty()) {
162-
format.AddIgnores(patternNoSpace);
160+
formatContext.AddIgnorePattern(patternNoSpace);
163161
}
164162
}
165163
}
166164

167165
if (cmd.HasOption("ignores-file")) {
168-
format.AddIgnoresByFile(cmd.Get<std::string>("ignores-file"));
166+
formatContext.AddIgnorePatternsFromFile(cmd.Get<std::string>("ignores-file"));
169167
}
170168

171169
if (cmd.Get<bool>("detect-config")) {
172-
format.AutoDetectConfig();
170+
formatContext.EnableAutoDetectConfig();
173171
} else if (cmd.HasOption("config")) {
174-
format.SetConfigPath(cmd.Get<std::string>("config"));
172+
formatContext.SetConfigFilePath(cmd.Get<std::string>("config"));
175173
}
176174

177175
if (cmd.Get<bool>("non-standard")) {
178-
format.SupportNonStandardLua();
176+
formatContext.EnableNonStandardLuaSupport();
179177
}
180178

181-
format.SetDefaultStyle(cmd.GetKeyValueOptions());
179+
formatContext.SetDefaultStyleOptions(cmd.GetKeyValueOptions());
182180
return true;
183181
}
184182

185-
bool InitCheck(CommandLine &cmd, LuaFormat &format) {
183+
bool InitCheck(CommandLine &cmd, FormatContext &formatContext) {
186184

187185
if (cmd.HasOption("file") || cmd.HasOption("stdin")) {
188186
if (cmd.HasOption("file")) {
189-
format.SetInputFile(cmd.Get<std::string>("file"));
187+
formatContext.SetInputFilePath(cmd.Get<std::string>("file"));
190188
}
191189

192190
if (cmd.HasOption("file") && !cmd.HasOption("stdin")) {
193-
format.SetWorkMode(WorkMode::File);
194-
if (!format.ReadFromInput()) {
195-
std::cerr << util::format("Can not find file {}", cmd.Get<std::string>("file")) << std::endl;
196-
return false;
197-
}
191+
formatContext.SetWorkMode(WorkMode::File);
198192
} else {
199-
format.SetWorkMode(WorkMode::Stdin);
193+
formatContext.SetWorkMode(WorkMode::Stdin);
200194
SET_BINARY_MODE();
201-
if (!format.ReadFromStdin()) {
202-
return false;
203-
}
195+
}
196+
197+
if (cmd.HasOption("outfile")) {
198+
formatContext.SetOutputFilePath(cmd.Get<std::string>("outfile"));
204199
}
205200
} else {
206-
format.SetWorkMode(WorkMode::Workspace);
201+
formatContext.SetWorkMode(WorkMode::Workspace);
207202
}
208203

209204
if (cmd.HasOption("workspace")) {
210-
format.SetWorkspace(cmd.Get<std::string>("workspace"));
205+
formatContext.SetWorkspacePath(cmd.Get<std::string>("workspace"));
211206
}
212207

213208
if (cmd.HasOption("ignores")) {
@@ -216,93 +211,45 @@ bool InitCheck(CommandLine &cmd, LuaFormat &format) {
216211
for (auto pattern: patterns) {
217212
auto patternNoSpace = string_util::TrimSpace(pattern);
218213
if (!patternNoSpace.empty()) {
219-
format.AddIgnores(patternNoSpace);
214+
formatContext.AddIgnorePattern(patternNoSpace);
220215
}
221216
}
222217
}
223218

224219
if (cmd.HasOption("ignores-file")) {
225-
format.AddIgnoresByFile(cmd.Get<std::string>("ignores-file"));
220+
formatContext.AddIgnorePatternsFromFile(cmd.Get<std::string>("ignores-file"));
226221
}
227222

228223
if (cmd.Get<bool>("detect-config")) {
229-
format.AutoDetectConfig();
224+
formatContext.EnableAutoDetectConfig();
230225
} else if (cmd.HasOption("config")) {
231-
format.SetConfigPath(cmd.Get<std::string>("config"));
232-
}
233-
234-
format.SetDefaultStyle(cmd.GetKeyValueOptions());
235-
236-
if (cmd.Get<bool>("name-style")) {
237-
format.SupportNameStyleCheck();
226+
formatContext.SetConfigFilePath(cmd.Get<std::string>("config"));
238227
}
239228

240229
if (cmd.Get<bool>("non-standard")) {
241-
format.SupportNonStandardLua();
230+
formatContext.EnableNonStandardLuaSupport();
242231
}
243-
return true;
244-
}
245-
246-
bool InitRangeFormat(CommandLine &cmd, LuaFormat &format) {
247-
if (cmd.HasOption("file") || cmd.HasOption("stdin")) {
248-
if (cmd.HasOption("file")) {
249-
format.SetInputFile(cmd.Get<std::string>("file"));
250-
}
251232

252-
if (cmd.HasOption("file") && !cmd.HasOption("stdin")) {
253-
format.SetWorkMode(WorkMode::File);
254-
if (!format.ReadFromInput()) {
255-
std::cerr << util::format("Can not find file {}", cmd.Get<std::string>("file")) << std::endl;
256-
return false;
257-
}
258-
} else {
259-
format.SetWorkMode(WorkMode::Stdin);
260-
SET_BINARY_MODE();
261-
if (!format.ReadFromStdin()) {
262-
return false;
263-
}
264-
}
265-
} else {
266-
format.SetWorkMode(WorkMode::Workspace);
267-
}
268-
269-
if (cmd.HasOption("workspace")) {
270-
format.SetWorkspace(cmd.Get<std::string>("workspace"));
271-
}
272-
273-
if (cmd.HasOption("ignores")) {
274-
auto ignores = cmd.Get<std::string>("ignores");
275-
auto patterns = string_util::Split(ignores, ";");
276-
for (auto pattern: patterns) {
277-
auto patternNoSpace = string_util::TrimSpace(pattern);
278-
if (!patternNoSpace.empty()) {
279-
format.AddIgnores(patternNoSpace);
280-
}
281-
}
233+
if (cmd.Get<bool>("name-style")) {
234+
formatContext.EnableNameStyleCheckSupport();
282235
}
283236

284-
if (cmd.HasOption("ignores-file")) {
285-
format.AddIgnoresByFile(cmd.Get<std::string>("ignores-file"));
237+
if(cmd.Get<bool>("dump-json")) {
238+
formatContext.EnableJsonDump();
286239
}
287240

288-
if (cmd.Get<bool>("detect-config")) {
289-
format.AutoDetectConfig();
290-
} else if (cmd.HasOption("config")) {
291-
format.SetConfigPath(cmd.Get<std::string>("config"));
292-
}
241+
formatContext.SetDefaultStyleOptions(cmd.GetKeyValueOptions());
242+
return true;
243+
}
293244

294-
format.SetDefaultStyle(cmd.GetKeyValueOptions());
295-
if (cmd.Get<bool>("complete-output")) {
296-
format.SupportCompleteOutputRange();
297-
}
245+
bool InitRangeFormat(CommandLine &cmd, FormatContext &formatContext) {
246+
InitFormat(cmd, formatContext);
298247

299248
if (cmd.HasOption("range-line")) {
300-
format.SetFormatRange(true, cmd.Get<std::string>("range-line"));
249+
formatContext.SetFormatRange(true, cmd.Get<std::string>("range-line"));
301250
} else if (cmd.HasOption("range-offset")) {
302-
format.SetFormatRange(false, cmd.Get<std::string>("range-offset"));
303-
}
304-
if (cmd.Get<bool>("non-standard")) {
305-
format.SupportNonStandardLua();
251+
formatContext.SetFormatRange(false, cmd.Get<std::string>("range-offset"));
306252
}
253+
307254
return true;
308255
}

0 commit comments

Comments
 (0)