Skip to content

Commit d529fc4

Browse files
committed
update clib
1 parent 58f6c2f commit d529fc4

File tree

4 files changed

+45
-21
lines changed

4 files changed

+45
-21
lines changed

CodeFormatCLib/include/CodeFormatCLib.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,27 @@ struct RangeFormatResult {
2121
char *Text;
2222
};
2323

24-
EMMY_API char *ReformatLuaCode(const char *code, const char *uri);
24+
struct FormattingOptions {
25+
uint32_t indent_size;
26+
bool use_tabs;
27+
bool insert_final_newline;
28+
bool non_standard_symbol;
29+
};
30+
31+
EMMY_API char *
32+
ReformatLuaCode(const char *code, const char *uri, FormattingOptions options);
2533

26-
EMMY_API RangeFormatResult RangeFormatLuaCode(const char *code, const char *uri, int startLine, int startCol, int endLine, int endCol);
34+
EMMY_API RangeFormatResult RangeFormatLuaCode(const char *code, const char *uri,
35+
int32_t startLine, int32_t startCol, int32_t endLine, int32_t endCol,
36+
FormattingOptions options);
2737

2838
EMMY_API void FreeReformatResult(char *ptr);
2939

3040
EMMY_API void UpdateCodeStyle(const char *workspaceUri, const char *configPath);
3141

3242
EMMY_API void RemoveCodeStyle(const char *workspaceUri);
3343

34-
EMMY_API char* CheckCodeStyle(const char *code, const char *uri);
44+
EMMY_API char *CheckCodeStyle(const char *code, const char *uri);
3545
}
3646

3747

CodeFormatCLib/src/CodeFormat.cpp

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ void CodeFormat::SupportNonStandardSymbol() {
5757
_supportNonStandardSymbol = true;
5858
}
5959

60-
char *CodeFormat::Reformat(const std::string &uri, std::string &&text) {
60+
char *CodeFormat::Reformat(const std::string &uri, std::string &&text, FormattingOptions options) {
6161
auto file = LuaSource::From(std::move(text));
6262
LuaLexer luaLexer(file);
63-
if (_supportNonStandardSymbol) {
63+
if (_supportNonStandardSymbol || options.non_standard_symbol) {
6464
luaLexer.SupportNonStandardSymbol();
6565
}
66-
if (_supportCLikeComments) {
66+
if (_supportCLikeComments || options.non_standard_symbol) {
6767
luaLexer.SupportCLikeComments();
6868
}
6969

@@ -80,7 +80,7 @@ char *CodeFormat::Reformat(const std::string &uri, std::string &&text) {
8080
t.BuildTree(p);
8181

8282
LuaStyle style = GetStyle(uri);
83-
83+
SetupStyle(style, options);
8484
FormatBuilder f(style);
8585

8686
auto result = f.GetFormatResult(t);
@@ -92,13 +92,13 @@ char *CodeFormat::Reformat(const std::string &uri, std::string &&text) {
9292
}
9393

9494
Result<RangeFormatResult> CodeFormat::RangeFormat(const std::string &uri, FormatRange &range,
95-
std::string &&text) {
95+
std::string &&text, FormattingOptions options) {
9696
auto file = std::make_shared<LuaSource>(std::move(text));
9797
LuaLexer luaLexer(file);
98-
if (_supportNonStandardSymbol) {
98+
if (_supportNonStandardSymbol || options.non_standard_symbol) {
9999
luaLexer.SupportNonStandardSymbol();
100100
}
101-
if (_supportCLikeComments) {
101+
if (_supportCLikeComments || options.non_standard_symbol) {
102102
luaLexer.SupportCLikeComments();
103103
}
104104

@@ -115,6 +115,7 @@ Result<RangeFormatResult> CodeFormat::RangeFormat(const std::string &uri, Format
115115
t.BuildTree(p);
116116

117117
LuaStyle style = GetStyle(uri);
118+
SetupStyle(style, options);
118119

119120
RangeFormatBuilder f(style, range);
120121

@@ -125,13 +126,25 @@ Result<RangeFormatResult> CodeFormat::RangeFormat(const std::string &uri, Format
125126
std::copy(formattedText.begin(), formattedText.end(), ptr);
126127
ptr[formattedText.size()] = '\0';// [formattedText.size()] = '\0'
127128
return RangeFormatResult{
128-
static_cast<int>(range.StartLine),
129-
static_cast<int>(range.StartCol),
130-
static_cast<int>(range.EndLine),
131-
static_cast<int>(range.EndCol),
129+
static_cast<int32_t>(range.StartLine),
130+
static_cast<int32_t>(range.StartCol),
131+
static_cast<int32_t>(range.EndLine),
132+
static_cast<int32_t>(range.EndCol),
132133
ptr};
133134
}
134135

136+
void CodeFormat::SetupStyle(LuaStyle &style, const FormattingOptions &options) {
137+
if (options.use_tabs) {
138+
style.indent_style = IndentStyle::Tab;
139+
style.tab_width = options.indent_size;
140+
} else {
141+
style.indent_style = IndentStyle::Space;
142+
style.indent_size = options.indent_size;
143+
}
144+
145+
style.insert_final_newline = options.insert_final_newline;
146+
}
147+
135148
Result<std::vector<LuaTypeFormat::Result>>
136149
CodeFormat::TypeFormat(const std::string &uri, std::size_t line, std::size_t character, std::string &&text) {
137150
auto file = std::make_shared<LuaSource>(std::move(text));
@@ -157,7 +170,6 @@ CodeFormat::TypeFormat(const std::string &uri, std::size_t line, std::size_t cha
157170

158171
LuaStyle style = GetStyle(uri);
159172

160-
161173
LuaTypeFormatFeatures typeFormatOptions;
162174
typeFormatOptions.auto_complete_end = false;
163175
LuaTypeFormat tf(typeFormatOptions);

CodeFormatCLib/src/CodeFormat.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ class CodeFormat {
3131

3232
void SupportNonStandardSymbol();
3333

34-
char *Reformat(const std::string &uri, std::string &&text);
34+
char *Reformat(const std::string &uri, std::string &&text, FormattingOptions options);
3535

36-
Result<RangeFormatResult> RangeFormat(const std::string &uri, FormatRange &range, std::string &&text);
36+
Result<RangeFormatResult> RangeFormat(const std::string &uri, FormatRange &range, std::string &&text, FormattingOptions options);
3737

3838
void SupportCLikeComments();// 添加支持 C 语言注释的函数
3939

@@ -48,6 +48,8 @@ class CodeFormat {
4848
std::vector<LuaDiagnosticInfo> MakeDiagnosticInfo(const std::vector<LuaDiagnostic> &diagnostics,
4949
std::shared_ptr<LuaSource> file);
5050

51+
void SetupStyle(LuaStyle &style, const FormattingOptions &options);
52+
5153
std::vector<LuaConfig> _configs;
5254
LuaStyle _defaultStyle;
5355
CodeSpellChecker _spellChecker;

CodeFormatCLib/src/CodeFormatCLib.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@
44

55
extern "C" {
66

7-
EMMY_API char *ReformatLuaCode(const char *code, const char *uri) {
7+
EMMY_API char *ReformatLuaCode(const char *code, const char *uri, FormattingOptions options) {
88
CodeFormat &codeFormat = CodeFormat::GetInstance();
9-
auto result = codeFormat.Reformat(uri, code);
9+
auto result = codeFormat.Reformat(uri, code, options);
1010
return result;
1111
}
1212

13-
EMMY_API RangeFormatResult RangeFormatLuaCode(const char *code, const char *uri, int startLine, int startCol, int endLine, int endCol) {
13+
EMMY_API RangeFormatResult RangeFormatLuaCode(const char *code, const char *uri, int startLine, int startCol, int endLine, int endCol, FormattingOptions options) {
1414
CodeFormat &codeFormat = CodeFormat::GetInstance();
1515
FormatRange range;
1616
range.StartLine = startLine;
1717
range.StartCol = startCol;
1818
range.EndLine = endLine;
1919
range.EndCol = endCol;
20-
auto result = codeFormat.RangeFormat(uri, range, code);
20+
auto result = codeFormat.RangeFormat(uri, range, code, options);
2121
if (result.Type == ResultType::Ok) {
2222
return result.Data;
2323
} else {

0 commit comments

Comments
 (0)