Skip to content

Commit cee0322

Browse files
committed
限制typeformat
1 parent 2265c0a commit cee0322

File tree

9 files changed

+81
-14
lines changed

9 files changed

+81
-14
lines changed

CodeFormatLib/src/CodeFormatLib.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,26 @@ int type_format(lua_State* L)
210210
lua_pop(L, 1);
211211
}
212212
}
213+
214+
LuaCodeFormat::ConfigMap stringTypeOptions;
215+
if (top == 6 && lua_istable(L, 6))
216+
{
217+
lua_pushnil(L);
218+
while (lua_next(L, -2) != 0)
219+
{
220+
auto key = luaToString(L, -2);
221+
auto value = luaToString(L, -1);
222+
223+
if (key != "nil")
224+
{
225+
stringTypeOptions.insert({ key, value });
226+
}
227+
228+
lua_pop(L, 1);
229+
}
230+
}
213231
auto typeFormat = LuaCodeFormat::GetInstance().TypeFormat(filename, line, character, std::move(text),
214-
configMap);
232+
configMap, stringTypeOptions);
215233

216234
if (!typeFormat.HasFormatResult())
217235
{

CodeFormatLib/src/LuaCodeFormat.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ std::string LuaCodeFormat::RangeFormat(const std::string& uri, LuaFormatRange& r
126126
}
127127

128128
LuaTypeFormat LuaCodeFormat::TypeFormat(const std::string& uri, int line, int character, std::string&& text,
129-
ConfigMap& configMap)
129+
ConfigMap& configMap, ConfigMap& stringTypeOptions)
130130
{
131131
auto parser = LuaParser::LoadFromBuffer(std::move(text));
132132
if (_customParser->IsSupportCustomTokens())
@@ -136,7 +136,8 @@ LuaTypeFormat LuaCodeFormat::TypeFormat(const std::string& uri, int line, int ch
136136
parser->BuildAstWithComment();
137137
auto options = GetOptions(uri);
138138
auto tempOptions = CalculateOptions(uri, configMap);
139-
LuaTypeFormat typeFormat(parser, tempOptions);
139+
auto typeOptions = LuaTypeFormatOptions::ParseFrom(stringTypeOptions);
140+
LuaTypeFormat typeFormat(parser, tempOptions, typeOptions);
140141
typeFormat.Analysis("\n", line, character);
141142

142143
return typeFormat;

CodeFormatLib/src/LuaCodeFormat.h

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

1414
class LuaCodeFormat
1515
{
@@ -32,7 +32,8 @@ class LuaCodeFormat
3232

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

35-
LuaTypeFormat TypeFormat(const std::string& uri, int line, int character, std::string&& text, ConfigMap& configMap);
35+
LuaTypeFormat TypeFormat(const std::string& uri, int line, int character, std::string&& text,
36+
ConfigMap& configMap, ConfigMap& stringTypeOptions);
3637

3738
std::pair<bool, std::vector<LuaDiagnosisInfo>> Diagnose(const std::string& uri, std::string&& text);
3839

CodeFormatServer/src/LanguageService.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +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"
16+
#include "CodeService/TypeFormat/LuaTypeFormat.h"
1717
#include "Util/Url.h"
1818
#include "Util/format.h"
1919
#include "LuaParser/LuaIdentify.h"
@@ -320,7 +320,8 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnTypeFormatting(
320320
auto position = param->position;
321321

322322
auto result = std::make_shared<vscode::DocumentFormattingResult>();
323-
LuaTypeFormat typeFormat(parser, *options);
323+
LuaTypeFormatOptions typeOptions;
324+
LuaTypeFormat typeFormat(parser, *options, typeOptions);
324325
typeFormat.Analysis("\n", position.line, position.character);
325326

326327
if(!typeFormat.HasFormatResult())

CodeService/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ 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
3837

38+
${CodeService_SOURCE_DIR}/src/TypeFormat/LuaTypeFormat.cpp
39+
${CodeService_SOURCE_DIR}/src/TypeFormat/LuaTypeFormatOptions.cpp
3940
${CodeService_SOURCE_DIR}/src/FormatElement/IndentElement.cpp
4041
${CodeService_SOURCE_DIR}/src/FormatElement/KeepLineElement.cpp
4142
${CodeService_SOURCE_DIR}/src/FormatElement/MinLineElement.cpp

CodeService/src/LuaTypeFormat.cpp renamed to CodeService/src/TypeFormat/LuaTypeFormat.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "CodeService/LuaTypeFormat.h"
1+
#include "CodeService/TypeFormat/LuaTypeFormat.h"
22
#include <algorithm>
33
#include "LuaParser/LuaTokenTypeDetail.h"
44
#include "CodeService/FormatElement/SerializeContext.h"
@@ -27,9 +27,11 @@ int FindTokenIndexBeforePosition(std::vector<LuaToken>& tokens, int offset)
2727
return static_cast<int>(pos - tokens.begin()) - 1;
2828
}
2929

30-
LuaTypeFormat::LuaTypeFormat(std::shared_ptr<LuaParser> luaParser, LuaCodeStyleOptions& options)
30+
LuaTypeFormat::LuaTypeFormat(std::shared_ptr<LuaParser> luaParser, LuaCodeStyleOptions& options,
31+
LuaTypeFormatOptions& typeOptions)
3132
: _parser(luaParser),
3233
_options(options),
34+
_typeOptions(typeOptions),
3335
_hasResult(false)
3436
{
3537
}
@@ -84,6 +86,11 @@ void LuaTypeFormat::AnalysisReturn(int line, int character)
8486

8587
void LuaTypeFormat::CompleteMissToken(int line, int character, LuaError& luaError)
8688
{
89+
if(!_typeOptions.auto_complete_end)
90+
{
91+
return;
92+
}
93+
8794
LuaCodeStyleOptions temp = _options;
8895
temp.insert_final_newline = true;
8996
switch (luaError.MissToken)
@@ -190,6 +197,11 @@ void LuaTypeFormat::CompleteMissToken(int line, int character, LuaError& luaErro
190197

191198
void LuaTypeFormat::FormatLine(int line)
192199
{
200+
if(!_typeOptions.format_line)
201+
{
202+
return;
203+
}
204+
193205
LuaCodeStyleOptions temp = _options;
194206
temp.insert_final_newline = true;
195207
temp.remove_expression_list_finish_comma = false;
@@ -213,7 +225,7 @@ void LuaTypeFormat::FormatLine(int line)
213225
break;
214226
}
215227
}
216-
if(!formatText.empty() && formatText.back() != '\n')
228+
if (!formatText.empty() && formatText.back() != '\n')
217229
{
218230
formatText.push_back('\n');
219231
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include "CodeService/TypeFormat/LuaTypeFormatOptions.h"
2+
3+
LuaTypeFormatOptions LuaTypeFormatOptions::ParseFrom(std::map<std::string, std::string, std::less<>>& stringOptions)
4+
{
5+
LuaTypeFormatOptions options;
6+
7+
if(stringOptions.count("auto_complete_end"))
8+
{
9+
options.auto_complete_end = stringOptions["auto_complete_end"] == "true";
10+
}
11+
12+
if(stringOptions.count("format_line"))
13+
{
14+
options.format_line = stringOptions["format_line"] == "true";
15+
}
16+
17+
return options;
18+
}

include/CodeService/LuaTypeFormat.h renamed to include/CodeService/TypeFormat/LuaTypeFormat.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#pragma once
22

3-
#include "LuaFormatter.h"
4-
#include "LuaFormatRange.h"
3+
#include "CodeService/LuaFormatter.h"
4+
#include "CodeService/LuaFormatRange.h"
5+
#include "LuaTypeFormatOptions.h"
56

67
class LuaTypeFormat
78
{
@@ -14,7 +15,7 @@ class LuaTypeFormat
1415
};
1516

1617

17-
LuaTypeFormat(std::shared_ptr<LuaParser> luaParser, LuaCodeStyleOptions& options);
18+
LuaTypeFormat(std::shared_ptr<LuaParser> luaParser, LuaCodeStyleOptions& options, LuaTypeFormatOptions& typeOptions);
1819
~LuaTypeFormat() = default;
1920

2021
void Analysis(std::string_view trigger, int line, int character);
@@ -30,6 +31,7 @@ class LuaTypeFormat
3031

3132
std::shared_ptr<LuaParser> _parser;
3233
LuaCodeStyleOptions& _options;
34+
LuaTypeFormatOptions& _typeOptions;
3335
bool _hasResult;
3436
Result _result;
3537
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
3+
#include <map>
4+
#include <string>
5+
6+
class LuaTypeFormatOptions
7+
{
8+
public:
9+
static LuaTypeFormatOptions ParseFrom(std::map<std::string, std::string, std::less<>>& stringOptions);
10+
11+
bool format_line = true;
12+
bool auto_complete_end = true;
13+
};

0 commit comments

Comments
 (0)