Skip to content

Commit a339b3f

Browse files
committed
refactor c lib
1 parent fcdd734 commit a339b3f

File tree

9 files changed

+177
-96
lines changed

9 files changed

+177
-96
lines changed

CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +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)
10+
option(BuildCodeFormatCLib "Build CodeFormatCLib" ON)
1111
option(EnableTest "Test project" ON)
1212

1313
if(APPLE)
@@ -40,8 +40,8 @@ if(BuildCodeFormatServer)
4040
add_subdirectory(CodeFormatServer)
4141
endif()
4242

43-
if(BuildCodeFormatCSharpLib)
44-
add_subdirectory(CodeFormatCSharpLib)
43+
if(BuildCodeFormatCLib)
44+
add_subdirectory(CodeFormatCLib)
4545
endif()
4646

4747
if(EnableTest)

CodeFormatCLib/CMakeLists.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
cmake_minimum_required(VERSION 3.14)
2+
3+
project(CodeFormatCLib)
4+
5+
add_library(CodeFormatCLib SHARED)
6+
7+
set_target_properties(CodeFormatCLib PROPERTIES OUTPUT_NAME code_format_c)
8+
9+
add_dependencies(CodeFormatCLib CodeFormatCore)
10+
11+
target_include_directories(CodeFormatCLib
12+
PRIVATE
13+
include
14+
)
15+
16+
target_sources(CodeFormatCLib
17+
PRIVATE
18+
src/CodeFormatCLib.cpp
19+
src/CodeFormat.cpp
20+
)
21+
22+
target_link_libraries(CodeFormatCLib PUBLIC CodeFormatCore)
23+
24+
install(TARGETS CodeFormatCLib
25+
RUNTIME DESTINATION bin
26+
LIBRARY DESTINATION lib
27+
ARCHIVE DESTINATION lib
28+
)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#ifndef CODE_FORMAT_C_LIB_H
2+
#define CODE_FORMAT_C_LIB_H
3+
4+
#include <stdint.h> // 使用固定大小的整数类型
5+
6+
#if defined(_WIN32)
7+
#define EMMY_API __declspec(dllexport)
8+
#elif defined(__GNUC__)
9+
#define EMMY_API __attribute__((visibility("default")))
10+
#else
11+
#define EMMY_API
12+
#endif
13+
14+
#ifdef __cplusplus
15+
extern "C" {
16+
#endif
17+
18+
// 使用固定大小的类型,并确保结构体按字节对齐
19+
typedef struct RangeFormatResult {
20+
int32_t StartLine;
21+
int32_t StartCharacter;
22+
int32_t EndLine;
23+
int32_t EndCharacter;
24+
char *Text;
25+
} RangeFormatResult;
26+
27+
// 定义不透明指针类型
28+
typedef struct CodeFormatHandle CodeFormatHandle;
29+
30+
// 创建和销毁 CodeFormat 实例
31+
EMMY_API CodeFormatHandle* CreateCodeFormat();
32+
EMMY_API void DestroyCodeFormat(CodeFormatHandle* handle);
33+
34+
// 修改函数使用不透明指针
35+
EMMY_API char* ReformatLuaCode(CodeFormatHandle* handle, const char *code, const char *uri);
36+
EMMY_API RangeFormatResult RangeFormatLuaCode(CodeFormatHandle* handle, const char *code, const char *uri, int32_t startLine, int32_t startCol, int32_t endLine, int32_t endCol);
37+
EMMY_API void FreeReformatResult(char *ptr);
38+
EMMY_API void UpdateCodeStyle(CodeFormatHandle* handle, const char *workspaceUri, const char *configPath);
39+
EMMY_API void RemoveCodeStyle(CodeFormatHandle* handle, const char *workspaceUri);
40+
41+
#ifdef __cplusplus
42+
}
43+
#endif
44+
45+
#endif// CODE_FORMAT_C_LIB_H
Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ void CodeFormat::SupportNonStandardSymbol() {
5757
_supportNonStandardSymbol = true;
5858
}
5959

60-
Result<char *> CodeFormat::Reformat(const std::string &uri, std::string &&text) {
61-
auto file = std::make_shared<LuaSource>(std::move(text));
60+
char* CodeFormat::Reformat(const std::string &uri, const std::string &text, size_t &length) {
61+
auto file = std::make_shared<LuaSource>(text);
6262
LuaLexer luaLexer(file);
6363
if (_supportNonStandardSymbol) {
6464
luaLexer.SupportNonStandardSymbol();
@@ -69,11 +69,12 @@ Result<char *> CodeFormat::Reformat(const std::string &uri, std::string &&text)
6969

7070
luaLexer.Parse();
7171

72-
LuaParser p(file, std::move(luaLexer.GetTokens()));
72+
LuaParser p(file, luaLexer.GetTokens());
7373
p.Parse();
7474

7575
if (p.HasError()) {
76-
return ResultType::Err;
76+
length = 0;
77+
return nullptr;
7778
}
7879

7980
LuaSyntaxTree t;
@@ -84,13 +85,20 @@ Result<char *> CodeFormat::Reformat(const std::string &uri, std::string &&text)
8485
FormatBuilder f(style);
8586

8687
auto result = f.GetFormatResult(t);
87-
// 由于可能存在sso string,所以需要拷贝一份
88-
char *ptr = new char[result.size() + 1];
88+
length = result.size();
89+
char *ptr = new char[length + 1];
8990
std::copy(result.begin(), result.end(), ptr);
90-
ptr[result.size()] = '\0';// [result.size()] = '\0'
91+
ptr[length] = '\0';
9192
return ptr;
9293
}
9394

95+
void CodeFormat::FreeRangeFormatResult(RangeFormatResult &result) {
96+
if (result.Text) {
97+
delete[] result.Text;
98+
result.Text = nullptr;
99+
}
100+
}
101+
94102
Result<RangeFormatResult> CodeFormat::RangeFormat(const std::string &uri, FormatRange &range,
95103
std::string &&text) {
96104
auto file = std::make_shared<LuaSource>(std::move(text));
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ class CodeFormat {
3030

3131
void SupportNonStandardSymbol();
3232

33-
Result<char*> Reformat(const std::string &uri, std::string &&text);
33+
char* Reformat(const std::string &uri, const std::string &text, size_t &length); // 修改返回类型和参数
34+
RangeFormatResult RangeFormat(const std::string &uri, FormatRange &range, const std::string &text);
35+
void FreeRangeFormatResult(RangeFormatResult &result); // 提供释放函数
3436

35-
Result<RangeFormatResult> RangeFormat(const std::string &uri, FormatRange &range, std::string &&text);
37+
void SupportCLikeComments(); // 添加支持 C 语言注释的函数
3638

3739
Result<std::vector<LuaTypeFormat::Result>>
3840
TypeFormat(const std::string &uri, std::size_t line, std::size_t character, std::string &&text);
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include "CodeFormat.h"
2+
#include "CodeFormatCLib.h"
3+
#include "Types.h"
4+
5+
#if defined(_WIN32)
6+
#define EMMY_API __declspec(dllexport)
7+
#elif defined(__GNUC__)
8+
#define EMMY_API __attribute__((visibility("default")))
9+
#else
10+
#define EMMY_API
11+
#endif
12+
13+
14+
extern "C" {
15+
16+
// 定义不透明指针结构
17+
struct CodeFormatHandle {
18+
CodeFormat* instance;
19+
};
20+
21+
// 创建 CodeFormat 实例
22+
EMMY_API CodeFormatHandle* CreateCodeFormat() {
23+
CodeFormatHandle* handle = new CodeFormatHandle();
24+
handle->instance = &CodeFormat::GetInstance();
25+
return handle;
26+
}
27+
28+
// 销毁 CodeFormat 实例
29+
EMMY_API void DestroyCodeFormat(CodeFormatHandle* handle) {
30+
delete handle;
31+
}
32+
33+
EMMY_API char *ReformatLuaCode(CodeFormatHandle* handle, const char *code, const char *uri) {
34+
CodeFormat &codeFormat = *handle->instance;
35+
auto result = codeFormat.Reformat(uri, code);
36+
if (result.Type == ResultType::Ok) {
37+
return result.Data;
38+
} else {
39+
return nullptr;
40+
}
41+
}
42+
43+
EMMY_API RangeFormatResult RangeFormatLuaCode(CodeFormatHandle* handle, const char *code, const char *uri, int startLine, int startCol, int endLine, int endCol) {
44+
CodeFormat &codeFormat = *handle->instance;
45+
FormatRange range;
46+
range.StartLine = startLine;
47+
range.StartCol = startCol;
48+
range.EndLine = endLine;
49+
range.EndCol = endCol;
50+
auto result = codeFormat.RangeFormat(uri, range, code);
51+
if (result.Type == ResultType::Ok) {
52+
return result.Data;
53+
} else {
54+
return RangeFormatResult{};
55+
}
56+
}
57+
58+
EMMY_API void FreeReformatResult(char *ptr) {
59+
delete[] ptr;
60+
}
61+
62+
EMMY_API void UpdateCodeStyle(CodeFormatHandle* handle, const char *workspaceUri, const char *configPath) {
63+
CodeFormat &codeFormat = *handle->instance;
64+
codeFormat.UpdateCodeStyle(workspaceUri, configPath);
65+
}
66+
67+
EMMY_API void RemoveCodeStyle(CodeFormatHandle* handle, const char *workspaceUri) {
68+
CodeFormat &codeFormat = *handle->instance;
69+
codeFormat.RemoveCodeStyle(workspaceUri);
70+
}
71+
}
Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#include <stdint.h> // 使用固定大小的整数类型
4+
#include <stddef.h> // 对于指针大小
35

46
struct LuaConfig {
57
explicit LuaConfig(std::string_view workspace)
@@ -10,16 +12,16 @@ struct LuaConfig {
1012
};
1113

1214
struct Position {
13-
std::size_t Line;
14-
std::size_t Col;
15+
uint32_t Line;
16+
uint32_t Col;
1517
};
1618

1719
struct LuaDiagnosticInfo {
1820
DiagnosticType Type;
1921
Position Start;
2022
Position End;
21-
std::string Message;
22-
std::string Data;
23+
char* Message;
24+
char* Data;
2325
};
2426

2527
enum class ResultType {
@@ -41,9 +43,9 @@ class Result {
4143
};
4244

4345
struct RangeFormatResult {
44-
int StartLine = 0;
45-
int StartCharacter = 0;
46-
int EndLine = 0;
47-
int EndCharacter = 0;
48-
char *Text = nullptr;
46+
int32_t StartLine;
47+
int32_t StartCharacter;
48+
int32_t EndLine;
49+
int32_t EndCharacter;
50+
char *Text;
4951
};

CodeFormatCSharpLib/CMakeLists.txt

Lines changed: 0 additions & 23 deletions
This file was deleted.

CodeFormatCSharpLib/src/CodeFormatCSharpLib.cpp

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)