forked from CppCXY/EmmyLuaCodeStyle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLuaCodeFormat.cpp
More file actions
248 lines (212 loc) · 6.11 KB
/
LuaCodeFormat.cpp
File metadata and controls
248 lines (212 loc) · 6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include "LuaCodeFormat.h"
#include "CodeService/LuaEditorConfig.h"
#include "CodeService/LuaFormatter.h"
#include "CodeService/NameStyle/NameStyleChecker.h"
#include "LuaParser/LuaParser.h"
#include "Util/StringUtil.h"
LuaCodeFormat& LuaCodeFormat::GetInstance()
{
static LuaCodeFormat instance;
return instance;
}
LuaCodeFormat::LuaCodeFormat()
: _defaultOptions(std::make_shared<LuaCodeStyleOptions>()),
_codeSpellChecker(std::make_shared<CodeSpellChecker>()),
_customParser(std::make_shared<LuaCustomParser>())
{
}
void LuaCodeFormat::UpdateCodeStyle(const std::string& workspaceUri, const std::string& configPath)
{
auto editorconfig = LuaEditorConfig::LoadFromFile(configPath);
if (editorconfig == nullptr)
{
return;
}
for (auto& pair : _editorConfigVector)
{
if (pair.first == workspaceUri)
{
pair.second = editorconfig;
pair.second->SetWorkspace(workspaceUri);
return;
}
}
_editorConfigVector.push_back({
workspaceUri, editorconfig
});
_editorConfigVector.back().second->SetWorkspace(workspaceUri);
}
void LuaCodeFormat::RemoveCodeStyle(const std::string& workspaceUri)
{
for (auto it = _editorConfigVector.begin(); it != _editorConfigVector.end(); ++it)
{
if (it->first == workspaceUri)
{
_editorConfigVector.erase(it);
return;
}
}
}
void LuaCodeFormat::SetDefaultCodeStyle(ConfigMap& configMap)
{
if (!configMap.empty())
{
LuaEditorConfig::ParseFromSection(_defaultOptions, configMap);
}
}
void LuaCodeFormat::SetSupportNonStandardSymbol(const std::string& tokenType,const std::vector<std::string>& tokens)
{
if (tokenType.size() == 1)
{
_customParser->SetTokens(tokenType.front(), tokens);
}
}
void LuaCodeFormat::LoadSpellDictionary(const std::string& path)
{
_codeSpellChecker->LoadDictionary(path);
}
void LuaCodeFormat::LoadSpellDictionaryFromBuffer(const std::string& buffer)
{
_codeSpellChecker->LoadDictionaryFromBuffer(buffer);
}
std::string LuaCodeFormat::Reformat(const std::string& uri, std::string&& text, ConfigMap& configMap)
{
auto parser = LuaParser::LoadFromBuffer(std::move(text));
if (_customParser->IsSupportCustomTokens()) {
parser->GetTokenParser()->SetCustomParser(_customParser);
}
parser->BuildAstWithComment();
if (parser->HasError())
{
return "";
}
auto options = GetOptions(uri);
auto tempOptions = CalculateOptions(uri, configMap);
LuaFormatter formatter(parser, tempOptions);
formatter.BuildFormattedElement();
return formatter.GetFormattedText();
}
std::string LuaCodeFormat::RangeFormat(const std::string& uri, LuaFormatRange& range, std::string&& text,
ConfigMap& configMap)
{
auto parser = LuaParser::LoadFromBuffer(std::move(text));
if (_customParser->IsSupportCustomTokens()) {
parser->GetTokenParser()->SetCustomParser(_customParser);
}
parser->BuildAstWithComment();
if (parser->HasError())
{
return "";
}
auto options = GetOptions(uri);
auto tempOptions = CalculateOptions(uri, configMap);
LuaFormatter formatter(parser, tempOptions);
formatter.BuildFormattedElement();
return formatter.GetRangeFormattedText(range);
}
std::pair<bool, std::vector<LuaDiagnosisInfo>> LuaCodeFormat::Diagnose(const std::string& uri, std::string&& text)
{
auto parser = LuaParser::LoadFromBuffer(std::move(text));
if (_customParser->IsSupportCustomTokens()) {
parser->GetTokenParser()->SetCustomParser(_customParser);
}
parser->BuildAstWithComment();
if (!parser->GetErrors().empty())
{
return std::make_pair(false, std::vector<LuaDiagnosisInfo>());
}
auto options = GetOptions(uri);
LuaFormatter formatter(parser, *options);
formatter.BuildFormattedElement();
DiagnosisContext ctx(parser, *options);
formatter.CalculateDiagnosisInfos(ctx);
if (options->enable_check_codestyle)
{
NameStyleChecker styleChecker(ctx);
styleChecker.Analysis();
}
ctx.DiagnoseLine();
return std::make_pair(true, ctx.GetDiagnosisInfos());
}
std::vector<LuaDiagnosisInfo> LuaCodeFormat::SpellCheck(const std::string& uri, std::string&& text,
const CodeSpellChecker::CustomDictionary& tempDict)
{
auto parser = LuaParser::LoadFromBuffer(std::move(text));
if (_customParser->IsSupportCustomTokens()) {
parser->GetTokenParser()->SetCustomParser(_customParser);
}
parser->GetTokenParser()->Parse();
auto options = GetOptions(uri);
DiagnosisContext ctx(parser, *options);
_codeSpellChecker->Analysis(ctx, tempDict);
return ctx.GetDiagnosisInfos();
}
std::vector<SuggestItem> LuaCodeFormat::SpellCorrect(const std::string& word)
{
std::string letterWord = word;
for (auto& c : letterWord)
{
c = std::tolower(c);
}
bool upperFirst = false;
if (std::isupper(word.front()))
{
upperFirst = true;
}
auto suggests = _codeSpellChecker->GetSuggests(letterWord);
for (auto& suggest : suggests)
{
if (!suggest.Term.empty())
{
if (upperFirst)
{
suggest.Term[0] = std::toupper(suggest.Term[0]);
}
}
}
return suggests;
}
std::shared_ptr<LuaCodeStyleOptions> LuaCodeFormat::GetOptions(const std::string& uri)
{
std::size_t matchLength = 0;
std::shared_ptr<LuaCodeStyleOptions> options = _defaultOptions;
for (auto it = _editorConfigVector.begin(); it != _editorConfigVector.end(); it++)
{
if (StringUtil::StartWith(uri, it->first) && it->first.size() > matchLength)
{
matchLength = it->first.size();
options = it->second->Generate(uri);
}
}
return options;
}
LuaCodeStyleOptions LuaCodeFormat::CalculateOptions(const std::string& uri, ConfigMap& configMap)
{
auto options = GetOptions(uri);
if (configMap.empty())
{
return *options;
}
else
{
LuaCodeStyleOptions tempOptions = *options;
if (configMap.count("insertSpaces"))
{
tempOptions.indent_style = configMap.at("insertSpaces") == "true"
? IndentStyle::Space
: IndentStyle::Tab;
}
if (configMap.count("tabSize"))
{
if (tempOptions.indent_style == IndentStyle::Tab)
{
tempOptions.tab_width = std::stoi(configMap.at("tabSize"));
}
else if (tempOptions.indent_style == IndentStyle::Space)
{
tempOptions.indent_size = std::stoi(configMap.at("tabSize"));
}
}
return tempOptions;
}
}