Skip to content

Commit ebcd76a

Browse files
committed
support clike comments
1 parent 1b5763c commit ebcd76a

File tree

13 files changed

+127
-17
lines changed

13 files changed

+127
-17
lines changed

CodeFormat/src/CodeFormat.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ int main(int argc, char **argv) {
5858
"Use file wildcards to specify how to ignore files\n"
5959
"\t\tseparated by ';'")
6060
.Add<bool>("non-standard", "", "Enable non-standard formatting")
61+
.Add<bool>("c-like-comments", "", "Enable c-like-comments formatting")
6162
.EnableKeyValueArgs();
6263
cmd.AddTarget("rangeformat")
6364
.Add<std::string>("file", "f", "Specify the input file")
@@ -74,6 +75,7 @@ int main(int argc, char **argv) {
7475
.Add<std::string>("range-line", "", "the format is startline:endline, for eg: 1:10")
7576
.Add<std::string>("range-offset", "", "the format is startOffset:endOffset, for eg: 0:256")
7677
.Add<bool>("non-standard", "", "Enable non-standard rangeformatting")
78+
.Add<bool>("c-like-comments", "", "Enable c-like-comments formatting")
7779
.EnableKeyValueArgs();
7880
cmd.AddTarget("check")
7981
.Add<std::string>("file", "f", "Specify the input file")
@@ -93,6 +95,7 @@ int main(int argc, char **argv) {
9395
"\t\tseparated by ';'")
9496
.Add<bool>("name-style", "ns", "Enable name-style check")
9597
.Add<bool>("non-standard", "", "Enable non-standard checking")
98+
.Add<bool>("c-like-comments", "", "Enable c-like-comments formatting")
9699
.Add<bool>("dump-json", "", "Dump json format diagnosis info")
97100
.EnableKeyValueArgs();
98101

@@ -176,6 +179,10 @@ bool InitFormat(CommandLine &cmd, FormatContext &formatContext) {
176179
formatContext.EnableNonStandardLuaSupport();
177180
}
178181

182+
if (cmd.Get<bool>("c-like-comments")) {
183+
formatContext.EnableCLikeCommentsSupport();
184+
}
185+
179186
formatContext.SetDefaultStyleOptions(cmd.GetKeyValueOptions());
180187
return true;
181188
}
@@ -234,10 +241,14 @@ bool InitCheck(CommandLine &cmd, FormatContext &formatContext) {
234241
formatContext.EnableNameStyleCheckSupport();
235242
}
236243

237-
if(cmd.Get<bool>("dump-json")) {
244+
if (cmd.Get<bool>("dump-json")) {
238245
formatContext.EnableJsonDump();
239246
}
240247

248+
if (cmd.Get<bool>("c-like-comments")) {
249+
formatContext.EnableCLikeCommentsSupport();
250+
}
251+
241252
formatContext.SetDefaultStyleOptions(cmd.GetKeyValueOptions());
242253
return true;
243254
}

CodeFormat/src/FormatContext.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ void FormatContext::EnableJsonDump() {
157157
_dumpJson = true;
158158
}
159159

160+
void FormatContext::EnableCLikeCommentsSupport() {
161+
_isSupportCLikeComments = false;
162+
}
163+
160164
LuaStyle FormatContext::GetStyle(std::string_view path) const {
161165
std::shared_ptr<LuaEditorConfig> editorConfig = nullptr;
162166
std::size_t matchProcess = 0;
@@ -232,3 +236,7 @@ std::string FormatContext::GetInputPath() const {
232236
const LuaDiagnosticStyle &FormatContext::GetDiagnosticStyle() const {
233237
return _diagnosticStyle;
234238
}
239+
240+
bool FormatContext::IsCLikeCommentsSupport() const {
241+
return _isSupportCLikeComments;
242+
}

CodeFormat/src/FormatContext.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class FormatContext {
4141

4242
void EnableJsonDump();
4343

44+
void EnableCLikeCommentsSupport();
45+
4446
[[nodiscard]] LuaStyle GetStyle(std::string_view path) const;
4547

4648
[[nodiscard]] WorkMode GetWorkMode() const;
@@ -67,6 +69,7 @@ class FormatContext {
6769

6870
[[nodiscard]] const LuaDiagnosticStyle &GetDiagnosticStyle() const;
6971

72+
[[nodiscard]] bool IsCLikeCommentsSupport() const;
7073
private:
7174
WorkMode _workMode = WorkMode::File;
7275
std::string _inputPath;
@@ -81,5 +84,6 @@ class FormatContext {
8184
bool _isRangeLine = false;
8285
std::string _rangeStr;
8386
bool _isSupportNonStandardLua = false;
87+
bool _isSupportCLikeComments = false;
8488
bool _dumpJson = false;
8589
};

CodeFormat/src/LuaCheck.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ bool LuaCheck::CheckSingleFile(const FormatContext &context, std::string_view pa
5757
if (context.IsNonStandardLua()) {
5858
luaLexer.SupportNonStandardSymbol();
5959
}
60+
if (context.IsCLikeCommentsSupport()) {
61+
luaLexer.SupportCLikeComments();
62+
}
63+
6064
luaLexer.Parse();
6165

6266
LuaParser p(file, std::move(luaLexer.GetTokens()));

CodeFormat/src/LuaFormat.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include "Util/format.h"
1313
#include <fstream>
1414
#include <iostream>
15-
#include <sstream>
1615

1716
bool LuaFormat::Reformat(const FormatContext &context) {
1817
switch (context.GetWorkMode()) {
@@ -37,6 +36,10 @@ bool LuaFormat::ReformatSingleFile(const FormatContext &context, std::string_vie
3736
if (context.IsNonStandardLua()) {
3837
luaLexer.SupportNonStandardSymbol();
3938
}
39+
if (context.IsCLikeCommentsSupport()) {
40+
luaLexer.SupportCLikeComments();
41+
}
42+
4043
luaLexer.Parse();
4144

4245
LuaParser p(file, std::move(luaLexer.GetTokens()));
@@ -75,6 +78,9 @@ bool LuaFormat::RangeReformat(const FormatContext &context) {
7578
if (context.IsNonStandardLua()) {
7679
luaLexer.SupportNonStandardSymbol();
7780
}
81+
if (context.IsCLikeCommentsSupport()) {
82+
luaLexer.SupportCLikeComments();
83+
}
7884

7985
luaLexer.Parse();
8086

CodeFormatLib/src/CodeFormatLib.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ int range_format(lua_State *L) {
169169
return 0;
170170
}
171171

172-
173172
int type_format(lua_State *L) {
174173
int top = lua_gettop(L);
175174

@@ -293,7 +292,6 @@ int type_format(lua_State *L) {
293292
return 0;
294293
}
295294

296-
297295
int update_config(lua_State *L) {
298296
int top = lua_gettop(L);
299297

@@ -487,6 +485,24 @@ int set_nonstandard_symbol(lua_State *L) {
487485
return 0;
488486
}
489487

488+
int set_clike_comments_symbol(lua_State *L) {
489+
int top = lua_gettop(L);
490+
491+
try {
492+
LuaCodeFormat::GetInstance().SupportCLikeComments();
493+
lua_pushboolean(L, true);
494+
return 1;
495+
} catch (std::exception &e) {
496+
std::string err = e.what();
497+
lua_settop(L, top);
498+
lua_pushboolean(L, false);
499+
lua_pushlstring(L, err.c_str(), err.size());
500+
return 2;
501+
}
502+
503+
return 0;
504+
}
505+
490506
int spell_load_dictionary_from_path(lua_State *L) {
491507
int top = lua_gettop(L);
492508

@@ -739,6 +755,7 @@ static const luaL_Reg lib[] = {
739755
{"spell_analysis", spell_analysis },
740756
{"spell_suggest", spell_suggest },
741757
{"set_nonstandard_symbol", set_nonstandard_symbol },
758+
{"set_clike_comments_symbol", set_clike_comments_symbol },
742759
{"name_style_analysis", name_style_analysis },
743760
{"update_name_style_config", update_name_style_config },
744761
{nullptr, nullptr }

CodeFormatLib/src/LuaCodeFormat.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ LuaCodeFormat &LuaCodeFormat::GetInstance() {
1111
}
1212

1313
LuaCodeFormat::LuaCodeFormat()
14-
: _supportNonStandardSymbol(false) {
14+
: _supportNonStandardSymbol(false),
15+
_supportCLikeComments(false) {
1516
}
1617

1718
void LuaCodeFormat::UpdateCodeStyle(const std::string &workspaceUri, const std::string &configPath) {
@@ -58,6 +59,10 @@ void LuaCodeFormat::SupportNonStandardSymbol() {
5859
_supportNonStandardSymbol = true;
5960
}
6061

62+
void LuaCodeFormat::SupportCLikeComments() {
63+
_supportCLikeComments = true;
64+
}
65+
6166
void LuaCodeFormat::LoadSpellDictionary(const std::string &path) {
6267
_spellChecker.LoadDictionary(path);
6368
}
@@ -72,6 +77,10 @@ Result<std::string> LuaCodeFormat::Reformat(const std::string &uri, std::string
7277
if (_supportNonStandardSymbol) {
7378
luaLexer.SupportNonStandardSymbol();
7479
}
80+
if (_supportCLikeComments) {
81+
luaLexer.SupportCLikeComments();
82+
}
83+
7584
luaLexer.Parse();
7685

7786
LuaParser p(file, std::move(luaLexer.GetTokens()));
@@ -100,6 +109,10 @@ Result<std::string> LuaCodeFormat::RangeFormat(const std::string &uri, FormatRan
100109
if (_supportNonStandardSymbol) {
101110
luaLexer.SupportNonStandardSymbol();
102111
}
112+
if (_supportCLikeComments) {
113+
luaLexer.SupportCLikeComments();
114+
}
115+
103116
luaLexer.Parse();
104117

105118
LuaParser p(file, std::move(luaLexer.GetTokens()));
@@ -130,6 +143,10 @@ LuaCodeFormat::TypeFormat(const std::string &uri, std::size_t line, std::size_t
130143
if (_supportNonStandardSymbol) {
131144
luaLexer.SupportNonStandardSymbol();
132145
}
146+
if (_supportCLikeComments) {
147+
luaLexer.SupportCLikeComments();
148+
}
149+
133150
luaLexer.Parse();
134151

135152
LuaParser p(file, std::move(luaLexer.GetTokens()));
@@ -158,6 +175,10 @@ Result<std::vector<LuaDiagnosticInfo>> LuaCodeFormat::Diagnostic(const std::stri
158175
if (_supportNonStandardSymbol) {
159176
luaLexer.SupportNonStandardSymbol();
160177
}
178+
if (_supportCLikeComments) {
179+
luaLexer.SupportCLikeComments();
180+
}
181+
161182
luaLexer.Parse();
162183

163184
LuaParser p(file, std::move(luaLexer.GetTokens()));
@@ -184,6 +205,10 @@ Result<std::vector<LuaDiagnosticInfo>> LuaCodeFormat::SpellCheck(const std::stri
184205
if (_supportNonStandardSymbol) {
185206
luaLexer.SupportNonStandardSymbol();
186207
}
208+
if (_supportCLikeComments) {
209+
luaLexer.SupportCLikeComments();
210+
}
211+
187212
luaLexer.Parse();
188213

189214
LuaParser p(file, std::move(luaLexer.GetTokens()));
@@ -210,6 +235,10 @@ Result<std::vector<LuaDiagnosticInfo>> LuaCodeFormat::NameStyleCheck(const std::
210235
if (_supportNonStandardSymbol) {
211236
luaLexer.SupportNonStandardSymbol();
212237
}
238+
if (_supportCLikeComments) {
239+
luaLexer.SupportCLikeComments();
240+
}
241+
213242
luaLexer.Parse();
214243

215244
LuaParser p(file, std::move(luaLexer.GetTokens()));

CodeFormatLib/src/LuaCodeFormat.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class LuaCodeFormat {
3131

3232
void SupportNonStandardSymbol();
3333

34+
void SupportCLikeComments();
35+
3436
void LoadSpellDictionary(const std::string &path);
3537

3638
void LoadSpellDictionaryFromBuffer(const std::string &buffer);
@@ -65,4 +67,5 @@ class LuaCodeFormat {
6567
CodeSpellChecker _spellChecker;
6668
LuaDiagnosticStyle _diagnosticStyle;
6769
bool _supportNonStandardSymbol;
70+
bool _supportCLikeComments;
6871
};

LuaParser/include/LuaParser/Lexer/LuaLexer.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
class LuaLexer {
2020
public:
21-
explicit LuaLexer(std::shared_ptr<LuaSource> file);
21+
explicit LuaLexer(std::shared_ptr<LuaSource> source);
2222

2323
bool Parse();
2424

@@ -32,7 +32,7 @@ class LuaLexer {
3232

3333
void SupportNonStandardSymbol();
3434

35-
// void SetCustomParser(std::shared_ptr<LuaCustomParser> parser);
35+
void SupportCLikeComments();
3636
private:
3737
static std::map<std::string, LuaTokenKind, std::less<>> LuaReserved;
3838

@@ -52,12 +52,13 @@ class LuaLexer {
5252

5353
bool CurrentIsNewLine();
5454

55-
bool IsReserved(std::string_view text);
55+
static bool IsReserved(std::string_view text);
5656

5757
void TokenError(std::string_view message, TextRange range);
5858

5959
int _linenumber;
6060
bool _supportNonStandardSymbol;
61+
bool _supportCLikeComments;
6162
TextReader _reader;
6263
std::vector<LuaToken> _tokens;
6364
std::vector<LuaTokenError> _errors;

LuaParser/src/Lexer/LuaLexer.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,12 @@ std::map<std::string, LuaTokenKind, std::less<>> LuaLexer::LuaReserved = {
4242
{"::", TK_DBCOLON }
4343
};
4444

45-
LuaLexer::LuaLexer(std::shared_ptr<LuaSource> file)
45+
LuaLexer::LuaLexer(std::shared_ptr<LuaSource> source)
4646
: _linenumber(0),
4747
_supportNonStandardSymbol(false),
48-
_reader(file->GetSource()),
49-
_file(file) {
48+
_supportCLikeComments(false),
49+
_reader(source->GetSource()),
50+
_file(source) {
5051
}
5152

5253
bool LuaLexer::Parse() {
@@ -85,6 +86,13 @@ std::vector<LuaToken> &LuaLexer::GetTokens() {
8586
return _tokens;
8687
}
8788

89+
void LuaLexer::SupportNonStandardSymbol() {
90+
_supportNonStandardSymbol = true;
91+
}
92+
void LuaLexer::SupportCLikeComments(){
93+
_supportCLikeComments = true;
94+
}
95+
8896
LuaTokenKind LuaLexer::Lex() {
8997
_reader.ResetBuffer();
9098

@@ -204,9 +212,18 @@ LuaTokenKind LuaLexer::Lex() {
204212
}
205213
case '/': {
206214
_reader.SaveAndNext();
215+
216+
if(_supportCLikeComments) {
217+
while (!CurrentIsNewLine() && _reader.GetCurrentChar() != EOZ) {
218+
_reader.SaveAndNext();
219+
}
220+
return TK_SHORT_COMMENT;
221+
}
222+
207223
if (_reader.CheckNext1('=')) {
208224
return '=';
209225
}
226+
210227
return TK_IDIV;
211228
}
212229
case '*': {
@@ -607,7 +624,3 @@ bool LuaLexer::IsReserved(std::string_view text) {
607624
void LuaLexer::TokenError(std::string_view message, TextRange range) {
608625
_errors.emplace_back(message, range, 0);
609626
}
610-
611-
void LuaLexer::SupportNonStandardSymbol() {
612-
_supportNonStandardSymbol = true;
613-
}

0 commit comments

Comments
 (0)