Skip to content

Commit 744a69e

Browse files
committed
更新code_format_lib
1 parent 8adf3ec commit 744a69e

File tree

6 files changed

+204
-21
lines changed

6 files changed

+204
-21
lines changed

3rd/uriparser/src/config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343

4444
#define PACKAGE_VERSION "0.9.5"
4545

46-
#define HAVE_WPRINTF
46+
/* #undef HAVE_WPRINTF */
4747
/* #undef HAVE_REALLOCARRAY */
4848

4949

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ endif()
3232
if(EnableTest)
3333
enable_testing()
3434
add_subdirectory(Test)
35-
endif()
35+
endif()

CodeFormatLib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ target_include_directories(CodeFormatLib PUBLIC
1919
target_sources(CodeFormatLib
2020
PUBLIC
2121
${CodeFormatLib_SOURCE_DIR}/src/CodeFormatLib.cpp
22+
${CodeFormatLib_SOURCE_DIR}/src/LuaCodeFormat.cpp
2223
)
2324

2425
if(NOT WIN32)

CodeFormatLib/src/CodeFormatLib.cpp

Lines changed: 81 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,113 @@
11
#include "lua.hpp"
2-
#include "CodeService/LuaFormatter.h"
2+
#include "LuaCodeFormat.h"
33

44
#ifdef MSVC
55
#define EXPORT __declspec(dllexport)
66
#else
77
#define EXPORT
88
#endif
99

10-
1110
int format(lua_State* L)
1211
{
1312
int top = lua_gettop(L);
1413

15-
if (top == 0)
14+
if (top < 2)
1615
{
1716
return 0;
1817
}
1918

20-
if (lua_isstring(L, 1))
19+
if (lua_isstring(L, 1) && lua_isstring(L, 2))
2120
{
22-
std::string buffer = lua_tostring(L, 1);
23-
auto parser = LuaParser::LoadFromBuffer(std::move(buffer));
24-
parser->BuildAstWithComment();
25-
26-
if (!parser->GetErrors().empty())
21+
try
22+
{
23+
std::string filename = lua_tostring(L, 1);
24+
std::string text = lua_tostring(L, 2);
25+
auto formattedText = LuaCodeFormat::GetInstance().Reformat(filename, std::move(text));
26+
if (formattedText.empty())
27+
{
28+
lua_pushboolean(L, false);
29+
return 1;
30+
}
31+
lua_pushboolean(L, true);
32+
lua_pushlstring(L, formattedText.c_str(), formattedText.size());
33+
return 2;
34+
}
35+
catch (std::exception& e)
2736
{
37+
std::string err = e.what();
38+
lua_settop(L, top);
2839
lua_pushboolean(L, false);
40+
lua_pushlstring(L, err.c_str(), err.size());
41+
return 2;
42+
}
43+
}
44+
return 0;
45+
}
46+
47+
enum class UpdateType
48+
{
49+
Created = 1,
50+
Changed = 2,
51+
Deleted = 3
52+
};
53+
54+
int range_format(lua_State* L)
55+
{
56+
return 0;
57+
}
58+
59+
int update_config(lua_State* L)
60+
{
61+
int top = lua_gettop(L);
62+
63+
if (top < 3)
64+
{
65+
return 0;
66+
}
67+
68+
if (lua_isinteger(L, 1) && lua_isstring(L, 2) && lua_isstring(L, 3))
69+
{
70+
try {
71+
auto type = static_cast<UpdateType>(lua_tointeger(L, 1));
72+
std::string workspaceUri = lua_tostring(L, 2);
73+
std::string configPath = lua_tostring(L, 3);
74+
switch (type)
75+
{
76+
case UpdateType::Created:
77+
case UpdateType::Changed:
78+
{
79+
LuaCodeFormat::GetInstance().UpdateCodeStyle(workspaceUri, configPath);
80+
break;
81+
}
82+
case UpdateType::Deleted:
83+
{
84+
LuaCodeFormat::GetInstance().RemoveCodeStyle(workspaceUri);
85+
break;
86+
}
87+
}
88+
89+
lua_pushboolean(L, true);
2990
return 1;
3091
}
31-
LuaFormatOptions options;
32-
LuaFormatter formatter(parser, options);
33-
formatter.BuildFormattedElement();
34-
35-
std::string formattedText = formatter.GetFormattedText();
36-
lua_pushboolean(L, true);
37-
lua_pushlstring(L, formattedText.c_str(), formattedText.size());
38-
return 2;
92+
catch (std::exception& e)
93+
{
94+
std::string err = e.what();
95+
lua_settop(L, top);
96+
lua_pushboolean(L, false);
97+
lua_pushlstring(L, err.c_str(), err.size());
98+
return 2;
99+
}
39100
}
101+
40102
return 0;
41103
}
42104

43105
static const luaL_Reg lib[] = {
44-
{"format",format},
106+
{"format", format},
107+
{"update_config", update_config},
45108
{nullptr, nullptr}
46109
};
47110

48-
49111
extern "C"
50112
EXPORT int luaopen_code_format(lua_State* L)
51113
{
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#include "LuaCodeFormat.h"
2+
3+
#include "CodeService/LuaEditorConfig.h"
4+
#include "CodeService/LuaFormatter.h"
5+
#include "LuaParser/LuaParser.h"
6+
7+
LuaCodeFormat& LuaCodeFormat::GetInstance()
8+
{
9+
static LuaCodeFormat instance;
10+
return instance;
11+
}
12+
13+
LuaCodeFormat::LuaCodeFormat()
14+
: _defaultOptions(std::make_shared<LuaCodeStyleOptions>())
15+
{
16+
}
17+
18+
void LuaCodeFormat::UpdateCodeStyle(const std::string& workspaceUri, const std::string& configPath)
19+
{
20+
auto editorconfig = LuaEditorConfig::LoadFromFile(configPath);
21+
22+
if (editorconfig == nullptr)
23+
{
24+
return;
25+
}
26+
27+
for (auto& pair : _editorConfigVector)
28+
{
29+
if (pair.first == workspaceUri)
30+
{
31+
pair.second = editorconfig;
32+
pair.second->SetWorkspace(workspaceUri);
33+
return;
34+
}
35+
}
36+
37+
_editorConfigVector.push_back({
38+
workspaceUri, editorconfig
39+
});
40+
_editorConfigVector.back().second->SetWorkspace(workspaceUri);
41+
}
42+
43+
void LuaCodeFormat::RemoveCodeStyle(const std::string& workspaceUri)
44+
{
45+
for(auto it = _editorConfigVector.begin();it != _editorConfigVector.end();++it)
46+
{
47+
if(it->first == workspaceUri)
48+
{
49+
_editorConfigVector.erase(it);
50+
return;
51+
}
52+
}
53+
}
54+
55+
std::string LuaCodeFormat::Reformat(const std::string& uri, std::string&& text)
56+
{
57+
auto parser = LuaParser::LoadFromBuffer(std::move(text));
58+
parser->BuildAstWithComment();
59+
60+
if (!parser->GetErrors().empty())
61+
{
62+
return "";
63+
}
64+
auto options = GetOptions(uri);
65+
LuaFormatter formatter(parser, *options);
66+
formatter.BuildFormattedElement();
67+
68+
return formatter.GetFormattedText();
69+
}
70+
71+
std::string LuaCodeFormat::RangeFormat(const std::string uri, LuaFormatRange range, std::string&& text)
72+
{
73+
return "";
74+
}
75+
76+
std::shared_ptr<LuaCodeStyleOptions> LuaCodeFormat::GetOptions(const std::string& uri)
77+
{
78+
std::size_t matchLength = 0;
79+
std::shared_ptr<LuaCodeStyleOptions> options = _defaultOptions;
80+
for (auto it = _editorConfigVector.begin(); it != _editorConfigVector.end(); it++)
81+
{
82+
if (uri.starts_with(it->first) && it->first.size() > matchLength)
83+
{
84+
matchLength = it->first.size();
85+
options = it->second->Generate(uri);
86+
}
87+
}
88+
89+
return options;
90+
}

CodeFormatLib/src/LuaCodeFormat.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#pragma once
2+
3+
#include <memory>
4+
#include <string>
5+
#include <string_view>
6+
7+
#include "CodeService/LuaCodeStyleOptions.h"
8+
#include "CodeService/LuaEditorConfig.h"
9+
#include "CodeService/LuaFormatRange.h"
10+
11+
class LuaCodeFormat
12+
{
13+
public:
14+
static LuaCodeFormat& GetInstance();
15+
16+
LuaCodeFormat();
17+
18+
void UpdateCodeStyle(const std::string& workspaceUri, const std::string& configPath);
19+
void RemoveCodeStyle(const std::string& workspaceUri);
20+
21+
std::string Reformat(const std::string& uri, std::string&& text);
22+
23+
std::string RangeFormat(const std::string uri, LuaFormatRange range, std::string&& text);
24+
25+
std::shared_ptr<LuaCodeStyleOptions> GetOptions(const std::string& uri);
26+
private:
27+
std::vector<std::pair<std::string, std::shared_ptr<LuaEditorConfig>>> _editorConfigVector;
28+
29+
std::shared_ptr<LuaCodeStyleOptions> _defaultOptions;
30+
};

0 commit comments

Comments
 (0)