Skip to content

Commit

Permalink
refactor c lib
Browse files Browse the repository at this point in the history
  • Loading branch information
CppCXY committed Dec 26, 2024
1 parent fcdd734 commit a339b3f
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 96 deletions.
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ set(CMAKE_CXX_STANDARD 20)
option(BuildAsLuaLib "Build for lua dll" OFF)
option(BuildCodeFormat "Build CodeFormat" ON)
option(BuildCodeFormatServer "Build CodeFormatServer" ON)
option(BuildCodeFormatCSharpLib "Build CodeFormatCSharpLib" ON)
option(BuildCodeFormatCLib "Build CodeFormatCLib" ON)
option(EnableTest "Test project" ON)

if(APPLE)
Expand Down Expand Up @@ -40,8 +40,8 @@ if(BuildCodeFormatServer)
add_subdirectory(CodeFormatServer)
endif()

if(BuildCodeFormatCSharpLib)
add_subdirectory(CodeFormatCSharpLib)
if(BuildCodeFormatCLib)
add_subdirectory(CodeFormatCLib)
endif()

if(EnableTest)
Expand Down
28 changes: 28 additions & 0 deletions CodeFormatCLib/CMakeLists.txt
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
)
45 changes: 45 additions & 0 deletions CodeFormatCLib/include/CodeFormatCLib.h
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
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ void CodeFormat::SupportNonStandardSymbol() {
_supportNonStandardSymbol = true;
}

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

luaLexer.Parse();

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

if (p.HasError()) {
return ResultType::Err;
length = 0;
return nullptr;
}

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

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

void CodeFormat::FreeRangeFormatResult(RangeFormatResult &result) {
if (result.Text) {
delete[] result.Text;
result.Text = nullptr;
}
}

Result<RangeFormatResult> CodeFormat::RangeFormat(const std::string &uri, FormatRange &range,
std::string &&text) {
auto file = std::make_shared<LuaSource>(std::move(text));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ class CodeFormat {

void SupportNonStandardSymbol();

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

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

Result<std::vector<LuaTypeFormat::Result>>
TypeFormat(const std::string &uri, std::size_t line, std::size_t character, std::string &&text);
Expand Down
71 changes: 71 additions & 0 deletions CodeFormatCLib/src/CodeFormatCLib.cpp
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);
}
}
20 changes: 11 additions & 9 deletions CodeFormatCSharpLib/src/Types.h → CodeFormatCLib/src/Types.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

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

struct LuaConfig {
explicit LuaConfig(std::string_view workspace)
Expand All @@ -10,16 +12,16 @@ struct LuaConfig {
};

struct Position {
std::size_t Line;
std::size_t Col;
uint32_t Line;
uint32_t Col;
};

struct LuaDiagnosticInfo {
DiagnosticType Type;
Position Start;
Position End;
std::string Message;
std::string Data;
char* Message;
char* Data;
};

enum class ResultType {
Expand All @@ -41,9 +43,9 @@ class Result {
};

struct RangeFormatResult {
int StartLine = 0;
int StartCharacter = 0;
int EndLine = 0;
int EndCharacter = 0;
char *Text = nullptr;
int32_t StartLine;
int32_t StartCharacter;
int32_t EndLine;
int32_t EndCharacter;
char *Text;
};
23 changes: 0 additions & 23 deletions CodeFormatCSharpLib/CMakeLists.txt

This file was deleted.

52 changes: 0 additions & 52 deletions CodeFormatCSharpLib/src/CodeFormatCSharpLib.cpp

This file was deleted.

0 comments on commit a339b3f

Please sign in to comment.