@@ -53,6 +53,45 @@ enum class UpdateType
5353
5454int 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
105145static const luaL_Reg lib[] = {
106146 {" format" , format},
147+ {" range_format" , range_format},
107148 {" update_config" , update_config},
108149 {nullptr , nullptr }
109150};
0 commit comments