Skip to content

Commit f0fcc25

Browse files
committed
codeformatlib支持范围格式化
1 parent 2eb0e2d commit f0fcc25

File tree

3 files changed

+64
-12
lines changed

3 files changed

+64
-12
lines changed

CodeFormatLib/src/CodeFormatLib.cpp

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,45 @@ enum class UpdateType
5353

5454
int range_format(lua_State* L)
5555
{
56+
int top = lua_gettop(L);
57+
58+
if (top < 4)
59+
{
60+
return 0;
61+
}
62+
63+
if (lua_isstring(L, 1) && lua_isstring(L, 2) && lua_isinteger(L, 3) && lua_isinteger(L, 4))
64+
{
65+
try
66+
{
67+
std::string filename = lua_tostring(L, 1);
68+
std::string text = lua_tostring(L, 2);
69+
int startLine = lua_tointeger(L, 3);
70+
int endLine = lua_tointeger(L, 4);
71+
72+
LuaFormatRange range(startLine, endLine);
73+
auto formattedText = LuaCodeFormat::GetInstance().RangeFormat(filename, range, std::move(text));
74+
if (formattedText.empty())
75+
{
76+
lua_pushboolean(L, false);
77+
return 1;
78+
}
79+
lua_pushboolean(L, true);
80+
lua_pushlstring(L, formattedText.c_str(), formattedText.size());
81+
lua_pushinteger(L, range.StartLine);
82+
lua_pushinteger(L, range.EndLine);
83+
84+
return 4;
85+
}
86+
catch (std::exception& e)
87+
{
88+
std::string err = e.what();
89+
lua_settop(L, top);
90+
lua_pushboolean(L, false);
91+
lua_pushlstring(L, err.c_str(), err.size());
92+
return 2;
93+
}
94+
}
5695
return 0;
5796
}
5897

@@ -67,23 +106,24 @@ int update_config(lua_State* L)
67106

68107
if (lua_isinteger(L, 1) && lua_isstring(L, 2) && lua_isstring(L, 3))
69108
{
70-
try {
109+
try
110+
{
71111
auto type = static_cast<UpdateType>(lua_tointeger(L, 1));
72112
std::string workspaceUri = lua_tostring(L, 2);
73113
std::string configPath = lua_tostring(L, 3);
74114
switch (type)
75115
{
76116
case UpdateType::Created:
77117
case UpdateType::Changed:
78-
{
79-
LuaCodeFormat::GetInstance().UpdateCodeStyle(workspaceUri, configPath);
80-
break;
81-
}
118+
{
119+
LuaCodeFormat::GetInstance().UpdateCodeStyle(workspaceUri, configPath);
120+
break;
121+
}
82122
case UpdateType::Deleted:
83-
{
84-
LuaCodeFormat::GetInstance().RemoveCodeStyle(workspaceUri);
85-
break;
86-
}
123+
{
124+
LuaCodeFormat::GetInstance().RemoveCodeStyle(workspaceUri);
125+
break;
126+
}
87127
}
88128

89129
lua_pushboolean(L, true);
@@ -104,6 +144,7 @@ int update_config(lua_State* L)
104144

105145
static const luaL_Reg lib[] = {
106146
{"format", format},
147+
{"range_format", range_format},
107148
{"update_config", update_config},
108149
{nullptr, nullptr}
109150
};

CodeFormatLib/src/LuaCodeFormat.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,20 @@ std::string LuaCodeFormat::Reformat(const std::string& uri, std::string&& text)
6969
return formatter.GetFormattedText();
7070
}
7171

72-
std::string LuaCodeFormat::RangeFormat(const std::string uri, LuaFormatRange range, std::string&& text)
72+
std::string LuaCodeFormat::RangeFormat(const std::string uri, LuaFormatRange& range, std::string&& text)
7373
{
74-
return "";
74+
auto parser = LuaParser::LoadFromBuffer(std::move(text));
75+
parser->BuildAstWithComment();
76+
77+
if (!parser->GetErrors().empty())
78+
{
79+
return "";
80+
}
81+
auto options = GetOptions(uri);
82+
LuaFormatter formatter(parser, *options);
83+
formatter.BuildRangeFormattedElement(range);
84+
85+
return formatter.GetFormattedText();
7586
}
7687

7788
std::shared_ptr<LuaCodeStyleOptions> LuaCodeFormat::GetOptions(const std::string& uri)

CodeFormatLib/src/LuaCodeFormat.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class LuaCodeFormat
2020

2121
std::string Reformat(const std::string& uri, std::string&& text);
2222

23-
std::string RangeFormat(const std::string uri, LuaFormatRange range, std::string&& text);
23+
std::string RangeFormat(const std::string uri, LuaFormatRange& range, std::string&& text);
2424

2525
std::shared_ptr<LuaCodeStyleOptions> GetOptions(const std::string& uri);
2626
private:

0 commit comments

Comments
 (0)