Skip to content

Commit 3201d1e

Browse files
committed
typeformat支持补全end
1 parent 13ccea5 commit 3201d1e

File tree

16 files changed

+439
-66
lines changed

16 files changed

+439
-66
lines changed

CodeFormat/src/CodeFormat.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ int main(int argc, char** argv)
3636
);
3737
cmd.AddTarget("format")
3838
.Add<std::string>("file", "f", "Specify the input file")
39-
.Add<std::string>("overwrite", "ow", "Format overwrite the input file")
39+
.Add<bool>("overwrite", "ow", "Format overwrite the input file")
4040
.Add<std::string>("workspace", "w",
4141
"Specify workspace directory,if no input file is specified, bulk formatting is performed")
4242
.Add<int>("stdin", "i", "Read from stdin and specify read size")

CodeFormatLib/src/CodeFormatLib.cpp

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "lua.hpp"
22
#include "LuaCodeFormat.h"
3+
#include "CodeService/LuaTypeFormat.h"
34

45
#ifdef _MSC_VER
56
#define EXPORT __declspec(dllexport)
@@ -172,6 +173,117 @@ int range_format(lua_State* L)
172173
return 0;
173174
}
174175

176+
177+
int type_format(lua_State* L)
178+
{
179+
int top = lua_gettop(L);
180+
181+
if (top < 4)
182+
{
183+
return 0;
184+
}
185+
186+
if (lua_isstring(L, 1) && lua_isstring(L, 2) && lua_isinteger(L, 3) && lua_isinteger(L, 4))
187+
{
188+
try
189+
{
190+
std::string filename = lua_tostring(L, 1);
191+
std::string text = lua_tostring(L, 2);
192+
int line = lua_tointeger(L, 3);
193+
int character = lua_tointeger(L, 4);
194+
195+
LuaCodeFormat::ConfigMap configMap;
196+
197+
if (top == 5 && lua_istable(L, 5))
198+
{
199+
lua_pushnil(L);
200+
while (lua_next(L, -2) != 0)
201+
{
202+
auto key = luaToString(L, -2);
203+
auto value = luaToString(L, -1);
204+
205+
if (key != "nil")
206+
{
207+
configMap.insert({key, value});
208+
}
209+
210+
lua_pop(L, 1);
211+
}
212+
}
213+
auto typeFormat = LuaCodeFormat::GetInstance().TypeFormat(filename, line, character, std::move(text),
214+
configMap);
215+
216+
if(!typeFormat.HasFormatResult())
217+
{
218+
lua_pushboolean(L, false);
219+
return 1;
220+
}
221+
else
222+
{
223+
lua_pushboolean(L, false);
224+
auto result = typeFormat.GetResult();
225+
226+
// 结果
227+
lua_newtable(L);
228+
229+
//message
230+
{
231+
lua_pushstring(L, "newText");
232+
lua_pushlstring(L, result.Text.c_str(), result.Text.size());
233+
lua_rawset(L, -3);
234+
}
235+
236+
// range
237+
{
238+
lua_pushstring(L, "range");
239+
//range table
240+
lua_newtable(L);
241+
242+
lua_pushstring(L, "start");
243+
// start table
244+
lua_newtable(L);
245+
lua_pushstring(L, "line");
246+
lua_pushinteger(L, result.Range.StartLine);
247+
lua_rawset(L, -3);
248+
249+
lua_pushstring(L, "character");
250+
lua_pushinteger(L, result.Range.StartCharacter);
251+
lua_rawset(L, -3);
252+
253+
lua_rawset(L, -3); // set start = {}
254+
255+
lua_pushstring(L, "end");
256+
// end table
257+
lua_newtable(L);
258+
lua_pushstring(L, "line");
259+
lua_pushinteger(L, result.Range.EndLine);
260+
lua_rawset(L, -3);
261+
262+
lua_pushstring(L, "character");
263+
lua_pushinteger(L, result.Range.EndCharacter);
264+
lua_rawset(L, -3);
265+
266+
lua_rawset(L, -3); // set end = {}
267+
268+
lua_rawset(L, -3); // set range = {}
269+
}
270+
271+
return 2;
272+
}
273+
}
274+
catch (std::exception& e)
275+
{
276+
std::string err = e.what();
277+
lua_settop(L, top);
278+
lua_pushboolean(L, false);
279+
lua_pushlstring(L, err.c_str(), err.size());
280+
return 2;
281+
}
282+
}
283+
return 0;
284+
}
285+
286+
175287
int update_config(lua_State* L)
176288
{
177289
int top = lua_gettop(L);

CodeFormatLib/src/LuaCodeFormat.cpp

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ void LuaCodeFormat::SetDefaultCodeStyle(ConfigMap& configMap)
6464
}
6565
}
6666

67-
void LuaCodeFormat::SetSupportNonStandardSymbol(const std::string& tokenType,const std::vector<std::string>& tokens)
67+
void LuaCodeFormat::SetSupportNonStandardSymbol(const std::string& tokenType, const std::vector<std::string>& tokens)
6868
{
6969
if (tokenType.size() == 1)
7070
{
71-
_customParser->SetTokens(tokenType.front(), tokens);
71+
_customParser->SetTokens(tokenType.front(), tokens);
7272
}
7373
}
7474

@@ -85,7 +85,8 @@ void LuaCodeFormat::LoadSpellDictionaryFromBuffer(const std::string& buffer)
8585
std::string LuaCodeFormat::Reformat(const std::string& uri, std::string&& text, ConfigMap& configMap)
8686
{
8787
auto parser = LuaParser::LoadFromBuffer(std::move(text));
88-
if (_customParser->IsSupportCustomTokens()) {
88+
if (_customParser->IsSupportCustomTokens())
89+
{
8990
parser->GetTokenParser()->SetCustomParser(_customParser);
9091
}
9192
parser->BuildAstWithComment();
@@ -105,7 +106,8 @@ std::string LuaCodeFormat::RangeFormat(const std::string& uri, LuaFormatRange& r
105106
ConfigMap& configMap)
106107
{
107108
auto parser = LuaParser::LoadFromBuffer(std::move(text));
108-
if (_customParser->IsSupportCustomTokens()) {
109+
if (_customParser->IsSupportCustomTokens())
110+
{
109111
parser->GetTokenParser()->SetCustomParser(_customParser);
110112
}
111113
parser->BuildAstWithComment();
@@ -123,10 +125,29 @@ std::string LuaCodeFormat::RangeFormat(const std::string& uri, LuaFormatRange& r
123125
return formatter.GetRangeFormattedText(range);
124126
}
125127

128+
LuaTypeFormat LuaCodeFormat::TypeFormat(const std::string& uri, int line, int character, std::string&& text,
129+
ConfigMap& configMap)
130+
{
131+
auto parser = LuaParser::LoadFromBuffer(std::move(text));
132+
if (_customParser->IsSupportCustomTokens())
133+
{
134+
parser->GetTokenParser()->SetCustomParser(_customParser);
135+
}
136+
parser->BuildAstWithComment();
137+
auto options = GetOptions(uri);
138+
auto tempOptions = CalculateOptions(uri, configMap);
139+
LuaTypeFormat typeFormat(parser, tempOptions);
140+
typeFormat.Analysis("\n", line, character);
141+
142+
return typeFormat;
143+
}
144+
145+
126146
std::pair<bool, std::vector<LuaDiagnosisInfo>> LuaCodeFormat::Diagnose(const std::string& uri, std::string&& text)
127147
{
128148
auto parser = LuaParser::LoadFromBuffer(std::move(text));
129-
if (_customParser->IsSupportCustomTokens()) {
149+
if (_customParser->IsSupportCustomTokens())
150+
{
130151
parser->GetTokenParser()->SetCustomParser(_customParser);
131152
}
132153
parser->BuildAstWithComment();
@@ -157,7 +178,8 @@ std::vector<LuaDiagnosisInfo> LuaCodeFormat::SpellCheck(const std::string& uri,
157178
const CodeSpellChecker::CustomDictionary& tempDict)
158179
{
159180
auto parser = LuaParser::LoadFromBuffer(std::move(text));
160-
if (_customParser->IsSupportCustomTokens()) {
181+
if (_customParser->IsSupportCustomTokens())
182+
{
161183
parser->GetTokenParser()->SetCustomParser(_customParser);
162184
}
163185
parser->GetTokenParser()->Parse();

CodeFormatLib/src/LuaCodeFormat.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "CodeService/LuaEditorConfig.h"
1010
#include "CodeService/LuaFormatRange.h"
1111
#include "CodeService/Spell/CodeSpellChecker.h"
12+
#include "CodeService/LuaTypeFormat.h"
1213

1314
class LuaCodeFormat
1415
{
@@ -31,6 +32,8 @@ class LuaCodeFormat
3132

3233
std::string RangeFormat(const std::string& uri, LuaFormatRange& range, std::string&& text, ConfigMap& configMap);
3334

35+
LuaTypeFormat TypeFormat(const std::string& uri, int line, int character, std::string&& text, ConfigMap& configMap);
36+
3437
std::pair<bool, std::vector<LuaDiagnosisInfo>> Diagnose(const std::string& uri, std::string&& text);
3538

3639
std::vector<LuaDiagnosisInfo> SpellCheck(const std::string& uri, std::string&& text,

CodeFormatServer/src/LanguageService.cpp

Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "CodeFormatServer/Service/CommandService.h"
1414
#include "CodeFormatServer/Service/ModuleService.h"
1515
#include "CodeFormatServer/Service/CompletionService.h"
16+
#include "CodeService/LuaTypeFormat.h"
1617
#include "Util/Url.h"
1718
#include "Util/format.h"
1819
#include "LuaParser/LuaIdentify.h"
@@ -315,57 +316,27 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnTypeFormatting(
315316
std::shared_ptr<vscode::TextDocumentPositionParams> param)
316317
{
317318
auto parser = LanguageClient::GetInstance().GetFileParser(param->textDocument.uri);
318-
319+
auto options = LanguageClient::GetInstance().GetOptions(param->textDocument.uri);
319320
auto position = param->position;
320321

321322
auto result = std::make_shared<vscode::DocumentFormattingResult>();
322-
if (!parser || parser->IsEmptyLine(static_cast<int>(position.line) - 1))
323-
{
324-
result->hasError = true;
325-
return result;
326-
}
327-
328-
auto options = LanguageClient::GetInstance().GetOptions(param->textDocument.uri);
323+
LuaTypeFormat typeFormat(parser, *options);
324+
typeFormat.Analysis("\n", position.line, position.character);
329325

330-
if (parser->HasError())
326+
if(!typeFormat.HasFormatResult())
331327
{
332328
result->hasError = true;
333329
return result;
334330
}
335331

336-
LuaCodeStyleOptions tempOptions = *options;
337-
tempOptions.insert_final_newline = true;
332+
auto& formatResult = typeFormat.GetResult();
333+
auto& formatRange = formatResult.Range;
338334

339-
LuaFormatRange formattedRange(static_cast<int>(position.line) - 1, static_cast<int>(position.line) - 1);
340-
341-
auto formatResult = LanguageClient::GetInstance().GetService<CodeFormatService>()->RangeFormat(
342-
formattedRange, parser, tempOptions);
343-
344-
// workaround TODO 实现真正的typeformat
345-
if (!formatResult.ends_with('\n'))
346-
{
347-
switch (options->end_of_line)
348-
{
349-
case EndOfLine::CRLF:
350-
{
351-
formatResult.append("\r\n");
352-
break;
353-
}
354-
case EndOfLine::LF:
355-
{
356-
formatResult.push_back('\n');
357-
}
358-
default:
359-
{
360-
break;
361-
}
362-
}
363-
}
364335
auto& edit = result->edits.emplace_back();
365-
edit.newText = std::move(formatResult);
336+
edit.newText = std::move(formatResult.Text);
366337
edit.range = vscode::Range(
367-
vscode::Position(formattedRange.StartLine, formattedRange.StartCharacter),
368-
vscode::Position(formattedRange.EndLine + 1, formattedRange.EndCharacter)
338+
vscode::Position(formatRange.StartLine, formatRange.StartCharacter),
339+
vscode::Position(formatRange.EndLine, formatRange.EndCharacter)
369340
);
370341
return result;
371342
}

CodeService/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ target_sources(CodeService
3434
${LuaCodeStyle_SOURCE_DIR}/include/CodeService/FormatElement/TextDefineType.h
3535
PRIVATE
3636
${CodeService_SOURCE_DIR}/src/LuaFormatter.cpp
37+
${CodeService_SOURCE_DIR}/src/LuaTypeFormat.cpp
3738

3839
${CodeService_SOURCE_DIR}/src/FormatElement/IndentElement.cpp
3940
${CodeService_SOURCE_DIR}/src/FormatElement/KeepLineElement.cpp

0 commit comments

Comments
 (0)