Skip to content

Commit 604393d

Browse files
committed
Close #107
1 parent 668b88d commit 604393d

File tree

9 files changed

+196
-33
lines changed

9 files changed

+196
-33
lines changed

CodeFormat/src/CodeFormat.cpp

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
1-
#include <iostream>
21
#include <cstring>
32
#include <fstream>
3+
#include <iostream>
44

55
#include "LuaFormat.h"
6-
#include "Util/format.h"
76
#include "Util/CommandLine.h"
87
#include "Util/StringUtil.h"
8+
#include "Util/format.h"
99

1010
// https://stackoverflow.com/questions/1598985/c-read-binary-stdin
1111
#ifdef _WIN32
1212

13-
# include <io.h>
14-
# include <fcntl.h>
13+
#include <fcntl.h>
14+
#include <io.h>
1515

16-
# define SET_BINARY_MODE() _setmode(_fileno(stdin), _O_BINARY);\
16+
#define SET_BINARY_MODE() \
17+
_setmode(_fileno(stdin), _O_BINARY); \
1718
_setmode(_fileno(stdout), _O_BINARY)
1819
#else
19-
# define SET_BINARY_MODE() ((void)0)
20+
#define SET_BINARY_MODE() ((void) 0)
2021
#endif
2122

22-
bool InitFormat(CommandLine& cmd, LuaFormat& format);
23-
bool InitCheck(CommandLine& cmd, LuaFormat& format);
24-
bool InitRangeFormat(CommandLine& cmd, LuaFormat& format);
23+
bool InitFormat(CommandLine &cmd, LuaFormat &format);
24+
bool InitCheck(CommandLine &cmd, LuaFormat &format);
25+
bool InitRangeFormat(CommandLine &cmd, LuaFormat &format);
2526

2627
int main(int argc, char **argv) {
2728
CommandLine cmd;
@@ -34,8 +35,7 @@ int main(int argc, char **argv) {
3435
"\tCodeFormat check -w . -d --ignores \"Test/*.lua;src/**.lua\"\n"
3536
"\tCodeFormat check -w . -d --ignores-file \".gitignore\"\n"
3637
"\tCodeFormat rangeformat -i -d --rangeline 1:10\n"
37-
"\tCodeFormat rangeformat -i -d --rangeOffset 0:100\n"
38-
);
38+
"\tCodeFormat rangeformat -i -d --rangeOffset 0:100\n");
3939
cmd.AddTarget("format")
4040
.Add<std::string>("file", "f", "Specify the input file")
4141
.Add<bool>("overwrite", "ow", "Format overwrite the input file")
@@ -50,12 +50,11 @@ int main(int argc, char **argv) {
5050
.Add<std::string>("outfile", "o",
5151
"Specify output file")
5252
.Add<std::string>("ignores-file", "igf",
53-
"Specify which files to ignore through configuration file,for example \".gitignore\""
54-
)
53+
"Specify which files to ignore through configuration file,for example \".gitignore\"")
5554
.Add<std::string>("ignores", "ig",
5655
"Use file wildcards to specify how to ignore files\n"
57-
"\t\tseparated by ';'"
58-
)
56+
"\t\tseparated by ';'")
57+
.Add<bool>("non-standard", "", "Enable non-standard formatting")
5958
.EnableKeyValueArgs();
6059
cmd.AddTarget("rangeformat")
6160
.Add<std::string>("file", "f", "Specify the input file")
@@ -71,6 +70,7 @@ int main(int argc, char **argv) {
7170
"If true, all content will be output")
7271
.Add<std::string>("range-line", "", "the format is startline:endline, for eg: 1:10")
7372
.Add<std::string>("range-offset", "", "the format is startOffset:endOffset, for eg: 0:256")
73+
.Add<bool>("non-standard", "", "Enable non-standard rangeformatting")
7474
.EnableKeyValueArgs();
7575
cmd.AddTarget("check")
7676
.Add<std::string>("file", "f", "Specify the input file")
@@ -83,13 +83,12 @@ int main(int argc, char **argv) {
8383
"\t\tIf this option is set, the config option has no effect")
8484
.Add<bool>("diagnosis-as-error", "DAE", "if exist error or diagnosis info , return -1")
8585
.Add<std::string>("ignores-file", "igf",
86-
"Specify which files to ignore through configuration file,for example \".gitignore\""
87-
)
86+
"Specify which files to ignore through configuration file,for example \".gitignore\"")
8887
.Add<std::string>("ignores", "ig",
8988
"Use file wildcards to specify how to ignore files\n"
90-
"\t\tseparated by ';'"
91-
)
89+
"\t\tseparated by ';'")
9290
.Add<bool>("name-style", "ns", "Enable name-style check")
91+
.Add<bool>("non-standard", "", "Enable non-standard checking")
9392
.EnableKeyValueArgs();
9493

9594

@@ -110,7 +109,7 @@ int main(int argc, char **argv) {
110109
if (!format.Check() && cmd.Get<bool>("diagnosis-as-error")) {
111110
return -1;
112111
}
113-
} else if(cmd.GetTarget() == "rangeformat") {
112+
} else if (cmd.GetTarget() == "rangeformat") {
114113
InitRangeFormat(cmd, format);
115114
if (!format.RangeReformat()) {
116115
// special return code for intellij
@@ -121,7 +120,7 @@ int main(int argc, char **argv) {
121120
return 0;
122121
}
123122

124-
bool InitFormat(CommandLine &cmd, LuaFormat& format) {
123+
bool InitFormat(CommandLine &cmd, LuaFormat &format) {
125124
if (cmd.HasOption("file") || cmd.HasOption("stdin")) {
126125
if (cmd.HasOption("file")) {
127126
format.SetInputFile(cmd.Get<std::string>("file"));
@@ -175,11 +174,15 @@ bool InitFormat(CommandLine &cmd, LuaFormat& format) {
175174
format.SetConfigPath(cmd.Get<std::string>("config"));
176175
}
177176

177+
if (cmd.Get<bool>("non-standard")) {
178+
format.SupportNonStandardLua();
179+
}
180+
178181
format.SetDefaultStyle(cmd.GetKeyValueOptions());
179182
return true;
180183
}
181184

182-
bool InitCheck(CommandLine &cmd, LuaFormat& format) {
185+
bool InitCheck(CommandLine &cmd, LuaFormat &format) {
183186

184187
if (cmd.HasOption("file") || cmd.HasOption("stdin")) {
185188
if (cmd.HasOption("file")) {
@@ -230,13 +233,17 @@ bool InitCheck(CommandLine &cmd, LuaFormat& format) {
230233

231234
format.SetDefaultStyle(cmd.GetKeyValueOptions());
232235

233-
if(cmd.Get<bool>("name-style")){
236+
if (cmd.Get<bool>("name-style")) {
234237
format.SupportNameStyleCheck();
235238
}
239+
240+
if (cmd.Get<bool>("non-standard")) {
241+
format.SupportNonStandardLua();
242+
}
236243
return true;
237244
}
238245

239-
bool InitRangeFormat(CommandLine &cmd, LuaFormat& format) {
246+
bool InitRangeFormat(CommandLine &cmd, LuaFormat &format) {
240247
if (cmd.HasOption("file") || cmd.HasOption("stdin")) {
241248
if (cmd.HasOption("file")) {
242249
format.SetInputFile(cmd.Get<std::string>("file"));
@@ -285,16 +292,17 @@ bool InitRangeFormat(CommandLine &cmd, LuaFormat& format) {
285292
}
286293

287294
format.SetDefaultStyle(cmd.GetKeyValueOptions());
288-
if(cmd.Get<bool>("complete-output")) {
295+
if (cmd.Get<bool>("complete-output")) {
289296
format.SupportCompleteOutputRange();
290297
}
291298

292-
if(cmd.HasOption("range-line")) {
299+
if (cmd.HasOption("range-line")) {
293300
format.SetFormatRange(true, cmd.Get<std::string>("range-line"));
294-
}
295-
else if (cmd.HasOption("range-offset")){
301+
} else if (cmd.HasOption("range-offset")) {
296302
format.SetFormatRange(false, cmd.Get<std::string>("range-offset"));
297303
}
298-
304+
if (cmd.Get<bool>("non-standard")) {
305+
format.SupportNonStandardLua();
306+
}
299307
return true;
300308
}

CodeFormat/src/LuaFormat.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
LuaFormat::LuaFormat()
2020
: _mode(WorkMode::File),
2121
_isRangeLine(false),
22-
_isCompleteOutputRangeFormat(false) {
22+
_isCompleteOutputRangeFormat(false),
23+
_isSupportNonStandardLua(false) {
2324
_diagnosticStyle.name_style_check = false;
2425
}
2526

@@ -156,6 +157,10 @@ bool LuaFormat::Reformat() {
156157
bool LuaFormat::ReformatSingleFile(std::string_view inputPath, std::string_view outPath, std::string &&sourceText) {
157158
auto file = std::make_shared<LuaFile>(std::move(sourceText));
158159
LuaLexer luaLexer(file);
160+
if (_isSupportNonStandardLua) {
161+
luaLexer.SupportNonStandardSymbol();
162+
}
163+
159164
luaLexer.Parse();
160165

161166
LuaParser p(file, std::move(luaLexer.GetTokens()));
@@ -192,6 +197,10 @@ bool LuaFormat::ReformatSingleFile(std::string_view inputPath, std::string_view
192197
bool LuaFormat::RangeReformat() {
193198
auto file = std::make_shared<LuaFile>(std::move(_inputFileText));
194199
LuaLexer luaLexer(file);
200+
if (_isSupportNonStandardLua) {
201+
luaLexer.SupportNonStandardSymbol();
202+
}
203+
195204
luaLexer.Parse();
196205

197206
LuaParser p(file, std::move(luaLexer.GetTokens()));
@@ -311,6 +320,9 @@ LuaStyle LuaFormat::GetStyle(std::string_view path) {
311320
bool LuaFormat::CheckSingleFile(std::string_view inputPath, std::string &&sourceText) {
312321
auto file = std::make_shared<LuaFile>(std::move(sourceText));
313322
LuaLexer luaLexer(file);
323+
if (_isSupportNonStandardLua) {
324+
luaLexer.SupportNonStandardSymbol();
325+
}
314326
luaLexer.Parse();
315327

316328
LuaParser p(file, std::move(luaLexer.GetTokens()));
@@ -447,3 +459,6 @@ void LuaFormat::SetFormatRange(bool rangeLine, std::string_view rangeStr) {
447459
_rangeStr = std::string(rangeStr);
448460
}
449461

462+
void LuaFormat::SupportNonStandardLua() {
463+
_isSupportNonStandardLua = true;
464+
}

CodeFormat/src/LuaFormat.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class LuaFormat {
4949

5050
void SetFormatRange(bool rangeLine, std::string_view rangeStr);
5151

52+
void SupportNonStandardLua();
5253
private:
5354
std::optional<std::string> ReadFile(std::string_view path);
5455

@@ -78,4 +79,5 @@ class LuaFormat {
7879
bool _isCompleteOutputRangeFormat;
7980
bool _isRangeLine;
8081
std::string _rangeStr;
82+
bool _isSupportNonStandardLua;
8183
};

CodeFormatCore/src/Format/Analyzer/AlignAnalyzer.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,12 @@ void AlignAnalyzer::AnalyzeContinuousArrayTableField(FormatState &f, LuaSyntaxNo
297297
void AlignAnalyzer::AnalyzeArrayTableAlign(FormatState &f, std::vector<LuaSyntaxNode> &arrayTable, const LuaSyntaxTree &t) {
298298
std::vector<std::vector<LuaSyntaxNode>> arrayTableFieldVec;
299299
std::size_t maxAlign = 0;
300+
auto &file = t.GetFile();
300301
for (auto &table: arrayTable) {
302+
if (file.CheckCurrentLineUnicodeBefore(table.GetTextRange(t).GetEndOffset())) {
303+
return;
304+
}
305+
301306
auto tableFieldList = table.GetChildSyntaxNode(LuaSyntaxNodeKind::TableFieldList, t);
302307
auto tableFields = tableFieldList.GetChildSyntaxNodes(LuaSyntaxNodeKind::TableField, t);
303308
if (tableFields.size() > maxAlign) {
@@ -351,14 +356,17 @@ void AlignAnalyzer::ResolveAlignGroup(FormatState &f, std::size_t groupIndex, Al
351356
switch (group.Strategy) {
352357
case AlignStrategy::AlignToEqWhenExtraSpace: {
353358
bool allowAlign = false;
359+
auto &file = t.GetFile();
354360
for (auto i: group.SyntaxGroup) {
355361
auto node = LuaSyntaxNode(i);
356362
auto eq = node.GetChildToken('=', t);
357363
if (eq.IsToken(t)) {
358364
auto diff = eq.GetTextRange(t).StartOffset - eq.GetPrevToken(t).GetTextRange(t).GetEndOffset();
359365
if (diff > 2) {
360366
allowAlign = true;
361-
break;
367+
}
368+
if (file.CheckCurrentLineUnicodeBefore(eq.GetTextRange(t).StartOffset)) {
369+
return;
362370
}
363371
}
364372
}
@@ -382,11 +390,15 @@ void AlignAnalyzer::ResolveAlignGroup(FormatState &f, std::size_t groupIndex, Al
382390
}
383391
case AlignStrategy::AlignToEqAlways: {
384392
std::size_t maxDis = 0;
393+
auto &file = t.GetFile();
385394
for (auto i: group.SyntaxGroup) {
386395
auto node = LuaSyntaxNode(i);
387396
auto eq = node.GetChildToken('=', t);
388397
if (eq.IsToken(t)) {
389398
auto prev = eq.GetPrevToken(t);
399+
if (file.CheckCurrentLineUnicodeBefore(eq.GetTextRange(t).StartOffset)) {
400+
return;
401+
}
390402
auto newPos = prev.GetTextRange(t).GetEndOffset() + 2 - node.GetTextRange(t).StartOffset;
391403
if (newPos > maxDis) {
392404
maxDis = newPos;
@@ -421,6 +433,10 @@ void AlignAnalyzer::ResolveAlignGroup(FormatState &f, std::size_t groupIndex, Al
421433
for (auto i: group.SyntaxGroup) {
422434
auto comment = LuaSyntaxNode(i);
423435
if (comment.IsToken(t)) {
436+
if (file.CheckCurrentLineUnicodeBefore(comment.GetTextRange(t).StartOffset)) {
437+
return;
438+
}
439+
424440
auto prev = comment.GetPrevToken(t);
425441
auto newPos =
426442
file.GetColumn(prev.GetTextRange(t).GetEndOffset()) +

LuaParser/include/LuaParser/File/LuaFile.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class LuaFile
1515

1616
std::size_t GetColumn(std::size_t offset) const;
1717

18+
bool CheckCurrentLineUnicodeBefore(std::size_t offset) const;
19+
1820
std::size_t GetLineOffset(std::size_t offset) const;
1921

2022
std::size_t GetOffset(std::size_t line, std::size_t character) const;

LuaParser/src/File/LuaFile.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,21 @@ std::size_t LuaFile::GetColumn(std::size_t offset) const {
5757
return 0;
5858
}
5959

60+
bool LuaFile::CheckCurrentLineUnicodeBefore(std::size_t offset) const {
61+
auto line = GetLine(offset);
62+
63+
auto lineStartOffset = _lineOffsetVec[line];
64+
65+
if (offset > lineStartOffset) {
66+
for (std::size_t i = lineStartOffset; i < offset; i++) {
67+
if (_source[i] & 0x80) {
68+
return true;
69+
}
70+
}
71+
}
72+
return false;
73+
}
74+
6075
std::size_t LuaFile::GetLineOffset(std::size_t offset) const {
6176
auto line = GetLine(offset);
6277

LuaParser/src/Lexer/LuaLexer.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,16 @@ LuaTokenKind LuaLexer::Lex() {
356356
}
357357
case '?': {
358358
_reader.SaveAndNext();
359-
if(_supportNonStandardSymbol && _reader.CheckNext1('.')) {
360-
return '.';
359+
if(_supportNonStandardSymbol) {
360+
if(_reader.CheckNext1('.')) {
361+
return '.';
362+
}
363+
else if(_reader.CheckNext1(':')) {
364+
return ':';
365+
}
366+
else if(_reader.CheckNext1('[')){
367+
return '[';
368+
}
361369
}
362370

363371
return '?';

0 commit comments

Comments
 (0)