Skip to content

Commit b9401ce

Browse files
committed
support used by other language
1 parent 22e4509 commit b9401ce

File tree

7 files changed

+408
-1
lines changed

7 files changed

+408
-1
lines changed

CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ set(CMAKE_CXX_STANDARD 20)
77
option(BuildAsLuaLib "Build for lua dll" OFF)
88
option(BuildCodeFormat "Build CodeFormat" ON)
99
option(BuildCodeFormatServer "Build CodeFormatServer" ON)
10+
option(BuildCodeFormatCSharpLib "Build CodeFormatCSharpLib" ON)
1011
option(EnableTest "Test project" ON)
1112

1213
if(APPLE)
@@ -22,7 +23,6 @@ if(APPLE)
2223
endif ()
2324

2425
add_subdirectory(LuaParser)
25-
#add_subdirectory(LuaCompile)
2626
add_subdirectory(CodeFormatCore)
2727
add_subdirectory(Util)
2828

@@ -38,6 +38,10 @@ if(BuildCodeFormatServer)
3838
add_subdirectory(CodeFormatServer)
3939
endif()
4040

41+
if(BuildCodeFormatCSharpLib)
42+
add_subdirectory(CodeFormatCSharpLib)
43+
endif()
44+
4145
if(EnableTest)
4246
enable_testing()
4347
add_subdirectory(3rd/googletest-1.13.0)

CodeFormatCSharpLib/CMakeLists.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
cmake_minimum_required(VERSION 3.14)
2+
3+
project(CodeFormatCSharpLib)
4+
5+
add_library(CodeFormatCSharpLib SHARED)
6+
7+
set_target_properties(CodeFormatCSharpLib PROPERTIES OUTPUT_NAME code_format_csharp)
8+
9+
add_dependencies(CodeFormatCSharpLib CodeFormatCore)
10+
11+
target_sources(CodeFormatCSharpLib
12+
PRIVATE
13+
src/CodeFormatCSharpLib.cpp
14+
src/CodeFormat.cpp
15+
)
16+
17+
if (NOT WIN32)
18+
target_compile_options(CodeFormatCSharpLib PUBLIC -fPIC)
19+
endif ()
20+
21+
target_link_libraries(CodeFormatCSharpLib PUBLIC CodeFormatCore)
Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
#include "CodeFormat.h"
2+
3+
#include "CodeFormatCore/Config/LuaEditorConfig.h"
4+
#include "CodeFormatCore/RangeFormat/RangeFormatBuilder.h"
5+
#include "LuaParser/Parse/LuaParser.h"
6+
#include "Util/StringUtil.h"
7+
8+
CodeFormat &CodeFormat::GetInstance() {
9+
static CodeFormat instance;
10+
return instance;
11+
}
12+
13+
CodeFormat::CodeFormat()
14+
: _supportNonStandardSymbol(false),
15+
_supportCLikeComments(false) {
16+
}
17+
18+
void CodeFormat::UpdateCodeStyle(const std::string &workspaceUri, const std::string &configPath) {
19+
for (auto &config: _configs) {
20+
if (config.Workspace == workspaceUri) {
21+
config.Editorconfig = LuaEditorConfig::OpenFile(configPath);
22+
return;
23+
}
24+
}
25+
26+
auto &config = _configs.emplace_back(
27+
workspaceUri);
28+
config.Editorconfig = LuaEditorConfig::OpenFile(configPath);
29+
}
30+
31+
void CodeFormat::UpdateDiagnosticStyle(InfoTree &tree) {
32+
_diagnosticStyle = LuaDiagnosticStyle();
33+
_diagnosticStyle.ParseTree(tree);
34+
_diagnosticStyle.code_style_check = true;
35+
_diagnosticStyle.name_style_check = false;
36+
_diagnosticStyle.spell_check = false;
37+
}
38+
39+
void CodeFormat::RemoveCodeStyle(const std::string &workspaceUri) {
40+
for (auto it = _configs.begin(); it != _configs.end(); it++) {
41+
if (it->Workspace == workspaceUri) {
42+
_configs.erase(it);
43+
break;
44+
}
45+
}
46+
}
47+
48+
void CodeFormat::SetDefaultCodeStyle(ConfigMap &configMap) {
49+
if (!configMap.empty()) {
50+
LuaStyle style;
51+
style.Parse(configMap);
52+
_defaultStyle = style;
53+
}
54+
}
55+
56+
void CodeFormat::SupportNonStandardSymbol() {
57+
_supportNonStandardSymbol = true;
58+
}
59+
60+
Result<char*> CodeFormat::Reformat(const std::string &uri, std::string &&text) {
61+
auto file = std::make_shared<LuaSource>(std::move(text));
62+
LuaLexer luaLexer(file);
63+
if (_supportNonStandardSymbol) {
64+
luaLexer.SupportNonStandardSymbol();
65+
}
66+
if (_supportCLikeComments) {
67+
luaLexer.SupportCLikeComments();
68+
}
69+
70+
luaLexer.Parse();
71+
72+
LuaParser p(file, std::move(luaLexer.GetTokens()));
73+
p.Parse();
74+
75+
if (p.HasError()) {
76+
return ResultType::Err;
77+
}
78+
79+
LuaSyntaxTree t;
80+
t.BuildTree(p);
81+
82+
LuaStyle style = GetStyle(uri);
83+
84+
FormatBuilder f(style);
85+
86+
auto result = f.GetFormatResult(t);
87+
// 由于可能存在sso string,所以需要拷贝一份
88+
char* ptr = new char[result.size() + 1];
89+
std::copy(result.begin(), result.end(), ptr);
90+
ptr[result.size()] = '\0'; // [result.size()] = '\0'
91+
return ptr;
92+
}
93+
94+
Result<RangeFormatResult> CodeFormat::RangeFormat(const std::string &uri, FormatRange &range,
95+
std::string &&text) {
96+
auto file = std::make_shared<LuaSource>(std::move(text));
97+
LuaLexer luaLexer(file);
98+
if (_supportNonStandardSymbol) {
99+
luaLexer.SupportNonStandardSymbol();
100+
}
101+
if (_supportCLikeComments) {
102+
luaLexer.SupportCLikeComments();
103+
}
104+
105+
luaLexer.Parse();
106+
107+
LuaParser p(file, std::move(luaLexer.GetTokens()));
108+
p.Parse();
109+
110+
if (p.HasError()) {
111+
return ResultType::Err;
112+
}
113+
114+
LuaSyntaxTree t;
115+
t.BuildTree(p);
116+
117+
LuaStyle style = GetStyle(uri);
118+
119+
RangeFormatBuilder f(style, range);
120+
121+
auto formattedText = f.GetFormatResult(t);
122+
range = f.GetReplaceRange();
123+
124+
char* ptr = new char[formattedText.size() + 1];
125+
std::copy(formattedText.begin(), formattedText.end(), ptr);
126+
ptr[formattedText.size()] = '\0'; // [formattedText.size()] = '\0'
127+
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),
132+
ptr
133+
};
134+
}
135+
136+
Result<std::vector<LuaTypeFormat::Result>>
137+
CodeFormat::TypeFormat(const std::string &uri, std::size_t line, std::size_t character, std::string &&text) {
138+
auto file = std::make_shared<LuaSource>(std::move(text));
139+
LuaLexer luaLexer(file);
140+
if (_supportNonStandardSymbol) {
141+
luaLexer.SupportNonStandardSymbol();
142+
}
143+
if (_supportCLikeComments) {
144+
luaLexer.SupportCLikeComments();
145+
}
146+
147+
luaLexer.Parse();
148+
149+
LuaParser p(file, std::move(luaLexer.GetTokens()));
150+
p.Parse();
151+
152+
if (p.HasError()) {
153+
return ResultType::Err;
154+
}
155+
156+
LuaSyntaxTree t;
157+
t.BuildTree(p);
158+
159+
LuaStyle style = GetStyle(uri);
160+
161+
162+
LuaTypeFormatFeatures typeFormatOptions;
163+
typeFormatOptions.auto_complete_end = false;
164+
LuaTypeFormat tf(typeFormatOptions);
165+
tf.Analyze("\n", line, character, t, style);
166+
return tf.GetResult();
167+
}
168+
169+
Result<std::vector<LuaDiagnosticInfo>> CodeFormat::Diagnostic(const std::string &uri, std::string &&text) {
170+
auto file = std::make_shared<LuaSource>(std::move(text));
171+
LuaLexer luaLexer(file);
172+
if (_supportNonStandardSymbol) {
173+
luaLexer.SupportNonStandardSymbol();
174+
}
175+
if (_supportCLikeComments) {
176+
luaLexer.SupportCLikeComments();
177+
}
178+
179+
luaLexer.Parse();
180+
181+
LuaParser p(file, std::move(luaLexer.GetTokens()));
182+
p.Parse();
183+
184+
if (p.HasError()) {
185+
return ResultType::Err;
186+
}
187+
188+
LuaSyntaxTree t;
189+
t.BuildTree(p);
190+
191+
LuaStyle style = GetStyle(uri);
192+
193+
DiagnosticBuilder diagnosticBuilder(style, _diagnosticStyle);
194+
diagnosticBuilder.CodeStyleCheck(t);
195+
return MakeDiagnosticInfo(diagnosticBuilder.GetDiagnosticResults(t), file);
196+
}
197+
198+
LuaStyle &CodeFormat::GetStyle(const std::string &uri) {
199+
std::shared_ptr<LuaEditorConfig> editorConfig = nullptr;
200+
std::size_t matchProcess = 0;
201+
for (auto &config: _configs) {
202+
if (string_util::StartWith(uri, config.Workspace)) {
203+
if (config.Workspace.size() > matchProcess) {
204+
matchProcess = config.Workspace.size();
205+
editorConfig = config.Editorconfig;
206+
}
207+
}
208+
}
209+
210+
if (editorConfig) {
211+
return editorConfig->Generate(uri);
212+
}
213+
return _defaultStyle;
214+
}
215+
216+
std::vector<LuaDiagnosticInfo> CodeFormat::MakeDiagnosticInfo(const std::vector<LuaDiagnostic> &diagnostics,
217+
std::shared_ptr<LuaSource> file) {
218+
std::vector<LuaDiagnosticInfo> results;
219+
for (auto &diagnostic: diagnostics) {
220+
auto &result = results.emplace_back();
221+
result.Type = diagnostic.Type;
222+
result.Message = diagnostic.Message;
223+
result.Data = diagnostic.Data;
224+
result.Start.Line = file->GetLine(diagnostic.Range.StartOffset);
225+
result.Start.Col = file->GetColumn(diagnostic.Range.StartOffset);
226+
result.End.Line = file->GetLine(diagnostic.Range.GetEndOffset());
227+
result.End.Col = file->GetColumn(diagnostic.Range.GetEndOffset()) + 1;
228+
}
229+
230+
return results;
231+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#pragma once
2+
3+
#include <memory>
4+
#include <string>
5+
#include <string_view>
6+
7+
#include "CodeFormatCore/Config/LuaEditorConfig.h"
8+
#include "CodeFormatCore/Config/LuaStyle.h"
9+
#include "CodeFormatCore/Diagnostic/DiagnosticBuilder.h"
10+
#include "CodeFormatCore/Diagnostic/Spell/CodeSpellChecker.h"
11+
#include "CodeFormatCore/Format/FormatBuilder.h"
12+
#include "CodeFormatCore/TypeFormat/LuaTypeFormat.h"
13+
#include "Types.h"
14+
15+
class CodeFormat {
16+
public:
17+
using ConfigMap = std::map<std::string, std::string, std::less<>>;
18+
19+
static CodeFormat &GetInstance();
20+
21+
CodeFormat();
22+
23+
void UpdateCodeStyle(const std::string &workspaceUri, const std::string &configPath);
24+
25+
void UpdateDiagnosticStyle(InfoTree &tree);
26+
27+
void RemoveCodeStyle(const std::string &workspaceUri);
28+
29+
void SetDefaultCodeStyle(ConfigMap &configMap);
30+
31+
void SupportNonStandardSymbol();
32+
33+
Result<char*> Reformat(const std::string &uri, std::string &&text);
34+
35+
Result<RangeFormatResult> RangeFormat(const std::string &uri, FormatRange &range, std::string &&text);
36+
37+
Result<std::vector<LuaTypeFormat::Result>>
38+
TypeFormat(const std::string &uri, std::size_t line, std::size_t character, std::string &&text);
39+
40+
Result<std::vector<LuaDiagnosticInfo>> Diagnostic(const std::string &uri, std::string &&text);
41+
42+
LuaStyle &GetStyle(const std::string &uri);
43+
private:
44+
std::vector<LuaDiagnosticInfo> MakeDiagnosticInfo(const std::vector<LuaDiagnostic> &diagnostics,
45+
std::shared_ptr<LuaSource> file);
46+
47+
std::vector<LuaConfig> _configs;
48+
LuaStyle _defaultStyle;
49+
CodeSpellChecker _spellChecker;
50+
LuaDiagnosticStyle _diagnosticStyle;
51+
bool _supportNonStandardSymbol;
52+
bool _supportCLikeComments;
53+
};
54+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include "CodeFormat.h"
2+
3+
#if defined(_WIN32)
4+
#define EMMY_API __declspec(dllexport)
5+
#elif defined(__GNUC__)
6+
#define EMMY_API __attribute__((visibility("default")))
7+
#else
8+
#define EMMY_API
9+
#endif
10+
11+
12+
extern "C" {
13+
14+
EMMY_API char *ReformatLuaCode(const char *code, const char *uri) {
15+
CodeFormat &codeFormat = CodeFormat::GetInstance();
16+
auto result = codeFormat.Reformat(uri, code);
17+
if (result.Type == ResultType::Ok) {
18+
return result.Data;
19+
} else {
20+
return nullptr;
21+
}
22+
}
23+
24+
EMMY_API RangeFormatResult RangeFormatLuaCode(const char *code, const char *uri, int startLine, int startCol, int endLine, int endCol) {
25+
CodeFormat &codeFormat = CodeFormat::GetInstance();
26+
FormatRange range;
27+
range.StartLine = startLine;
28+
range.StartCol = startCol;
29+
range.EndLine = endLine;
30+
range.EndCol = endCol;
31+
auto result = codeFormat.RangeFormat(uri, range, code);
32+
if (result.Type == ResultType::Ok) {
33+
return result.Data;
34+
} else {
35+
return RangeFormatResult{};
36+
}
37+
}
38+
39+
EMMY_API void FreeReformatResult(char *ptr) {
40+
delete[] ptr;
41+
}
42+
43+
EMMY_API void UpdateCodeStyle(const char *workspaceUri, const char *configPath) {
44+
CodeFormat &codeFormat = CodeFormat::GetInstance();
45+
codeFormat.UpdateCodeStyle(workspaceUri, configPath);
46+
}
47+
}

0 commit comments

Comments
 (0)