Skip to content

Commit bdb82a2

Browse files
committed
fix build
1 parent a339b3f commit bdb82a2

File tree

5 files changed

+41
-87
lines changed

5 files changed

+41
-87
lines changed
Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef CODE_FORMAT_C_LIB_H
22
#define CODE_FORMAT_C_LIB_H
33

4-
#include <stdint.h> // 使用固定大小的整数类型
4+
#include <cstdint>// 使用固定大小的整数类型
55

66
#if defined(_WIN32)
77
#define EMMY_API __declspec(dllexport)
@@ -10,36 +10,20 @@
1010
#else
1111
#define EMMY_API
1212
#endif
13+
#include "Types.h"
1314

14-
#ifdef __cplusplus
1515
extern "C" {
16-
#endif
1716

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);
17+
EMMY_API char *ReformatLuaCode(const char *code, const char *uri);
18+
19+
EMMY_API RangeFormatResult RangeFormatLuaCode(const char *code, const char *uri, int startLine, int startCol, int endLine, int endCol);
20+
3721
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);
4022

41-
#ifdef __cplusplus
23+
EMMY_API void UpdateCodeStyle(const char *workspaceUri, const char *configPath);
24+
25+
EMMY_API void RemoveCodeStyle(const char *workspaceUri);
4226
}
43-
#endif
27+
4428

4529
#endif// CODE_FORMAT_C_LIB_H

CodeFormatCLib/src/CodeFormat.cpp

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

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

7070
luaLexer.Parse();
7171

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

7575
if (p.HasError()) {
76-
length = 0;
7776
return nullptr;
7877
}
7978

@@ -85,20 +84,13 @@ char* CodeFormat::Reformat(const std::string &uri, const std::string &text, size
8584
FormatBuilder f(style);
8685

8786
auto result = f.GetFormatResult(t);
88-
length = result.size();
87+
auto length = result.size();
8988
char *ptr = new char[length + 1];
9089
std::copy(result.begin(), result.end(), ptr);
9190
ptr[length] = '\0';
9291
return ptr;
9392
}
9493

95-
void CodeFormat::FreeRangeFormatResult(RangeFormatResult &result) {
96-
if (result.Text) {
97-
delete[] result.Text;
98-
result.Text = nullptr;
99-
}
100-
}
101-
10294
Result<RangeFormatResult> CodeFormat::RangeFormat(const std::string &uri, FormatRange &range,
10395
std::string &&text) {
10496
auto file = std::make_shared<LuaSource>(std::move(text));

CodeFormatCLib/src/CodeFormat.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,19 @@ class CodeFormat {
3030

3131
void SupportNonStandardSymbol();
3232

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); // 提供释放函数
33+
char *Reformat(const std::string &uri, std::string &&text);
3634

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

3939
Result<std::vector<LuaTypeFormat::Result>>
4040
TypeFormat(const std::string &uri, std::size_t line, std::size_t character, std::string &&text);
4141

4242
Result<std::vector<LuaDiagnosticInfo>> Diagnostic(const std::string &uri, std::string &&text);
4343

4444
LuaStyle &GetStyle(const std::string &uri);
45+
4546
private:
4647
std::vector<LuaDiagnosticInfo> MakeDiagnosticInfo(const std::vector<LuaDiagnostic> &diagnostics,
4748
std::shared_ptr<LuaSource> file);
@@ -53,4 +54,3 @@ class CodeFormat {
5354
bool _supportNonStandardSymbol;
5455
bool _supportCLikeComments;
5556
};
56-
Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,17 @@
1-
#include "CodeFormat.h"
21
#include "CodeFormatCLib.h"
2+
#include "CodeFormat.h"
33
#include "Types.h"
44

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-
145
extern "C" {
156

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;
7+
EMMY_API char *ReformatLuaCode(const char *code, const char *uri) {
8+
CodeFormat &codeFormat = CodeFormat::GetInstance();
359
auto result = codeFormat.Reformat(uri, code);
36-
if (result.Type == ResultType::Ok) {
37-
return result.Data;
38-
} else {
39-
return nullptr;
40-
}
10+
return result;
4111
}
4212

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;
13+
EMMY_API RangeFormatResult RangeFormatLuaCode(const char *code, const char *uri, int startLine, int startCol, int endLine, int endCol) {
14+
CodeFormat &codeFormat = CodeFormat::GetInstance();
4515
FormatRange range;
4616
range.StartLine = startLine;
4717
range.StartCol = startCol;
@@ -59,13 +29,13 @@ EMMY_API void FreeReformatResult(char *ptr) {
5929
delete[] ptr;
6030
}
6131

62-
EMMY_API void UpdateCodeStyle(CodeFormatHandle* handle, const char *workspaceUri, const char *configPath) {
63-
CodeFormat &codeFormat = *handle->instance;
32+
EMMY_API void UpdateCodeStyle(const char *workspaceUri, const char *configPath) {
33+
CodeFormat &codeFormat = CodeFormat::GetInstance();
6434
codeFormat.UpdateCodeStyle(workspaceUri, configPath);
6535
}
6636

67-
EMMY_API void RemoveCodeStyle(CodeFormatHandle* handle, const char *workspaceUri) {
68-
CodeFormat &codeFormat = *handle->instance;
37+
EMMY_API void RemoveCodeStyle(const char *workspaceUri) {
38+
CodeFormat &codeFormat = CodeFormat::GetInstance();
6939
codeFormat.RemoveCodeStyle(workspaceUri);
7040
}
7141
}

CodeFormatCLib/src/Types.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
#pragma once
22

3-
#include <stdint.h> // 使用固定大小的整数类型
4-
#include <stddef.h> // 对于指针大小
3+
#include <cstdint> // 使用固定大小的整数类型
4+
#include <cstddef> // 对于指针大小
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"
513

614
struct LuaConfig {
715
explicit LuaConfig(std::string_view workspace)
@@ -20,8 +28,8 @@ struct LuaDiagnosticInfo {
2028
DiagnosticType Type;
2129
Position Start;
2230
Position End;
23-
char* Message;
24-
char* Data;
31+
std::string Message;
32+
std::string Data;
2533
};
2634

2735
enum class ResultType {

0 commit comments

Comments
 (0)