-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
refactor c lib
Showing
9 changed files
with
177 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
cmake_minimum_required(VERSION 3.14) | ||
|
||
project(CodeFormatCLib) | ||
|
||
add_library(CodeFormatCLib SHARED) | ||
|
||
set_target_properties(CodeFormatCLib PROPERTIES OUTPUT_NAME code_format_c) | ||
|
||
add_dependencies(CodeFormatCLib CodeFormatCore) | ||
|
||
target_include_directories(CodeFormatCLib | ||
PRIVATE | ||
include | ||
) | ||
|
||
target_sources(CodeFormatCLib | ||
PRIVATE | ||
src/CodeFormatCLib.cpp | ||
src/CodeFormat.cpp | ||
) | ||
|
||
target_link_libraries(CodeFormatCLib PUBLIC CodeFormatCore) | ||
|
||
install(TARGETS CodeFormatCLib | ||
RUNTIME DESTINATION bin | ||
LIBRARY DESTINATION lib | ||
ARCHIVE DESTINATION lib | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#ifndef CODE_FORMAT_C_LIB_H | ||
#define CODE_FORMAT_C_LIB_H | ||
|
||
#include <stdint.h> // 使用固定大小的整数类型 | ||
|
||
#if defined(_WIN32) | ||
#define EMMY_API __declspec(dllexport) | ||
#elif defined(__GNUC__) | ||
#define EMMY_API __attribute__((visibility("default"))) | ||
#else | ||
#define EMMY_API | ||
#endif | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
// 使用固定大小的类型,并确保结构体按字节对齐 | ||
typedef struct RangeFormatResult { | ||
int32_t StartLine; | ||
int32_t StartCharacter; | ||
int32_t EndLine; | ||
int32_t EndCharacter; | ||
char *Text; | ||
} RangeFormatResult; | ||
|
||
// 定义不透明指针类型 | ||
typedef struct CodeFormatHandle CodeFormatHandle; | ||
|
||
// 创建和销毁 CodeFormat 实例 | ||
EMMY_API CodeFormatHandle* CreateCodeFormat(); | ||
EMMY_API void DestroyCodeFormat(CodeFormatHandle* handle); | ||
|
||
// 修改函数使用不透明指针 | ||
EMMY_API char* ReformatLuaCode(CodeFormatHandle* handle, const char *code, const char *uri); | ||
EMMY_API RangeFormatResult RangeFormatLuaCode(CodeFormatHandle* handle, const char *code, const char *uri, int32_t startLine, int32_t startCol, int32_t endLine, int32_t endCol); | ||
EMMY_API void FreeReformatResult(char *ptr); | ||
EMMY_API void UpdateCodeStyle(CodeFormatHandle* handle, const char *workspaceUri, const char *configPath); | ||
EMMY_API void RemoveCodeStyle(CodeFormatHandle* handle, const char *workspaceUri); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif// CODE_FORMAT_C_LIB_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#include "CodeFormat.h" | ||
#include "CodeFormatCLib.h" | ||
#include "Types.h" | ||
|
||
#if defined(_WIN32) | ||
#define EMMY_API __declspec(dllexport) | ||
#elif defined(__GNUC__) | ||
#define EMMY_API __attribute__((visibility("default"))) | ||
#else | ||
#define EMMY_API | ||
#endif | ||
|
||
|
||
extern "C" { | ||
|
||
// 定义不透明指针结构 | ||
struct CodeFormatHandle { | ||
CodeFormat* instance; | ||
}; | ||
|
||
// 创建 CodeFormat 实例 | ||
EMMY_API CodeFormatHandle* CreateCodeFormat() { | ||
CodeFormatHandle* handle = new CodeFormatHandle(); | ||
handle->instance = &CodeFormat::GetInstance(); | ||
return handle; | ||
} | ||
|
||
// 销毁 CodeFormat 实例 | ||
EMMY_API void DestroyCodeFormat(CodeFormatHandle* handle) { | ||
delete handle; | ||
} | ||
|
||
EMMY_API char *ReformatLuaCode(CodeFormatHandle* handle, const char *code, const char *uri) { | ||
CodeFormat &codeFormat = *handle->instance; | ||
auto result = codeFormat.Reformat(uri, code); | ||
if (result.Type == ResultType::Ok) { | ||
return result.Data; | ||
} else { | ||
return nullptr; | ||
} | ||
} | ||
|
||
EMMY_API RangeFormatResult RangeFormatLuaCode(CodeFormatHandle* handle, const char *code, const char *uri, int startLine, int startCol, int endLine, int endCol) { | ||
CodeFormat &codeFormat = *handle->instance; | ||
FormatRange range; | ||
range.StartLine = startLine; | ||
range.StartCol = startCol; | ||
range.EndLine = endLine; | ||
range.EndCol = endCol; | ||
auto result = codeFormat.RangeFormat(uri, range, code); | ||
if (result.Type == ResultType::Ok) { | ||
return result.Data; | ||
} else { | ||
return RangeFormatResult{}; | ||
} | ||
} | ||
|
||
EMMY_API void FreeReformatResult(char *ptr) { | ||
delete[] ptr; | ||
} | ||
|
||
EMMY_API void UpdateCodeStyle(CodeFormatHandle* handle, const char *workspaceUri, const char *configPath) { | ||
CodeFormat &codeFormat = *handle->instance; | ||
codeFormat.UpdateCodeStyle(workspaceUri, configPath); | ||
} | ||
|
||
EMMY_API void RemoveCodeStyle(CodeFormatHandle* handle, const char *workspaceUri) { | ||
CodeFormat &codeFormat = *handle->instance; | ||
codeFormat.RemoveCodeStyle(workspaceUri); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.