FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
EmmyLuaCodeStyle/CodeFormatCSharpLib/src/CodeFormat.cpp at master · yaSebastian/EmmyLuaCodeStyle · GitHub
yaSebastian
EmmyLuaCodeStyle
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
EmmyLuaCodeStyle
/
CodeFormatCSharpLib
/
src
/
CodeFormat.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
230 lines (185 loc) · 6.56 KB
Breadcrumbs
EmmyLuaCodeStyle
/
CodeFormatCSharpLib
/
src
/
CodeFormat.cpp
Copy path
File metadata and controls
230 lines (185 loc) · 6.56 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
include
"
CodeFormat.h
"
#
include
"
CodeFormatCore/Config/LuaEditorConfig.h
"
#
include
"
CodeFormatCore/RangeFormat/RangeFormatBuilder.h
"
#
include
"
LuaParser/Parse/LuaParser.h
"
#
include
"
Util/StringUtil.h
"
CodeFormat &
CodeFormat::GetInstance
() {
static
CodeFormat instance;
return
instance;
}
CodeFormat::CodeFormat
()
: _supportNonStandardSymbol(
false
),
_supportCLikeComments(
false
) {
}
void
CodeFormat::UpdateCodeStyle
(
const
std::string &workspaceUri,
const
std::string &configPath) {
for
(
auto
&config: _configs) {
if
(config.
Workspace
== workspaceUri) {
config.
Editorconfig
=
LuaEditorConfig::OpenFile
(configPath);
return
;
}
}
auto
&config = _configs.
emplace_back
(
workspaceUri);
config.
Editorconfig
=
LuaEditorConfig::OpenFile
(configPath);
}
void
CodeFormat::UpdateDiagnosticStyle
(InfoTree &tree) {
_diagnosticStyle =
LuaDiagnosticStyle
();
_diagnosticStyle.
ParseTree
(tree);
_diagnosticStyle.
code_style_check
=
true
;
_diagnosticStyle.
name_style_check
=
false
;
_diagnosticStyle.
spell_check
=
false
;
}
void
CodeFormat::RemoveCodeStyle
(
const
std::string &workspaceUri) {
for
(
auto
it = _configs.
begin
(); it != _configs.
end
(); it++) {
if
(it->
Workspace
== workspaceUri) {
_configs.
erase
(it);
break
;
}
}
}
void
CodeFormat::SetDefaultCodeStyle
(ConfigMap &configMap) {
if
(!configMap.
empty
()) {
LuaStyle style;
style.
Parse
(configMap);
_defaultStyle = style;
}
}
void
CodeFormat::SupportNonStandardSymbol
() {
_supportNonStandardSymbol =
true
;
}
Result<
char
*>
CodeFormat::Reformat
(
const
std::string &uri, std::string &&text) {
auto
file = std::make_shared<LuaSource>(
std::move
(text));
LuaLexer
luaLexer
(file);
if
(_supportNonStandardSymbol) {
luaLexer.
SupportNonStandardSymbol
();
}
if
(_supportCLikeComments) {
luaLexer.
SupportCLikeComments
();
}
luaLexer.
Parse
();
LuaParser
p
(file,
std::move
(luaLexer.
GetTokens
()));
p.
Parse
();
if
(p.
HasError
()) {
return
ResultType::Err;
}
LuaSyntaxTree t;
t.
BuildTree
(p);
LuaStyle style =
GetStyle
(uri);
FormatBuilder
f
(style);
auto
result = f.
GetFormatResult
(t);
//
由于可能存在sso string,所以需要拷贝一份
char
*ptr =
new
char
[result.
size
() +
1
];
std::copy
(result.
begin
(), result.
end
(), ptr);
ptr[result.
size
()] =
'
\0
'
;
//
[result.size()] = '\0'
return
ptr;
}
Result<RangeFormatResult>
CodeFormat::RangeFormat
(
const
std::string &uri, FormatRange &range,
std::string &&text) {
auto
file = std::make_shared<LuaSource>(
std::move
(text));
LuaLexer
luaLexer
(file);
if
(_supportNonStandardSymbol) {
luaLexer.
SupportNonStandardSymbol
();
}
if
(_supportCLikeComments) {
luaLexer.
SupportCLikeComments
();
}
luaLexer.
Parse
();
LuaParser
p
(file,
std::move
(luaLexer.
GetTokens
()));
p.
Parse
();
if
(p.
HasError
()) {
return
ResultType::Err;
}
LuaSyntaxTree t;
t.
BuildTree
(p);
LuaStyle style =
GetStyle
(uri);
RangeFormatBuilder
f
(style, range);
auto
formattedText = f.
GetFormatResult
(t);
range = f.
GetReplaceRange
();
char
*ptr =
new
char
[formattedText.
size
() +
1
];
std::copy
(formattedText.
begin
(), formattedText.
end
(), ptr);
ptr[formattedText.
size
()] =
'
\0
'
;
//
[formattedText.size()] = '\0'
return
RangeFormatResult{
static_cast
<
int
>(range.
StartLine
),
static_cast
<
int
>(range.
StartCol
),
static_cast
<
int
>(range.
EndLine
),
static_cast
<
int
>(range.
EndCol
),
ptr};
}
Result<std::vector<LuaTypeFormat::Result>>
CodeFormat::TypeFormat
(
const
std::string &uri, std::
size_t
line, std::
size_t
character, std::string &&text) {
auto
file = std::make_shared<LuaSource>(
std::move
(text));
LuaLexer
luaLexer
(file);
if
(_supportNonStandardSymbol) {
luaLexer.
SupportNonStandardSymbol
();
}
if
(_supportCLikeComments) {
luaLexer.
SupportCLikeComments
();
}
luaLexer.
Parse
();
LuaParser
p
(file,
std::move
(luaLexer.
GetTokens
()));
p.
Parse
();
if
(p.
HasError
()) {
return
ResultType::Err;
}
LuaSyntaxTree t;
t.
BuildTree
(p);
LuaStyle style =
GetStyle
(uri);
LuaTypeFormatFeatures typeFormatOptions;
typeFormatOptions.
auto_complete_end
=
false
;
LuaTypeFormat
tf
(typeFormatOptions);
tf.
Analyze
(
"
\n
"
, line, character, t, style);
return
tf.
GetResult
();
}
Result<std::vector<LuaDiagnosticInfo>>
CodeFormat::Diagnostic
(
const
std::string &uri, std::string &&text) {
auto
file = std::make_shared<LuaSource>(
std::move
(text));
LuaLexer
luaLexer
(file);
if
(_supportNonStandardSymbol) {
luaLexer.
SupportNonStandardSymbol
();
}
if
(_supportCLikeComments) {
luaLexer.
SupportCLikeComments
();
}
luaLexer.
Parse
();
LuaParser
p
(file,
std::move
(luaLexer.
GetTokens
()));
p.
Parse
();
if
(p.
HasError
()) {
return
ResultType::Err;
}
LuaSyntaxTree t;
t.
BuildTree
(p);
LuaStyle style =
GetStyle
(uri);
DiagnosticBuilder
diagnosticBuilder
(style, _diagnosticStyle);
diagnosticBuilder.
CodeStyleCheck
(t);
return
MakeDiagnosticInfo
(diagnosticBuilder.
GetDiagnosticResults
(t), file);
}
LuaStyle &
CodeFormat::GetStyle
(
const
std::string &uri) {
std::shared_ptr<LuaEditorConfig> editorConfig =
nullptr
;
std::
size_t
matchProcess =
0
;
for
(
auto
&config: _configs) {
if
(
string_util::StartWith
(uri, config.
Workspace
)) {
if
(config.
Workspace
.
size
() > matchProcess) {
matchProcess = config.
Workspace
.
size
();
editorConfig = config.
Editorconfig
;
}
}
}
if
(editorConfig) {
return
editorConfig->
Generate
(uri);
}
return
_defaultStyle;
}
std::vector<LuaDiagnosticInfo>
CodeFormat::MakeDiagnosticInfo
(
const
std::vector<LuaDiagnostic> &diagnostics,
std::shared_ptr<LuaSource> file) {
std::vector<LuaDiagnosticInfo> results;
for
(
auto
&diagnostic: diagnostics) {
auto
&result = results.
emplace_back
();
result.
Type
= diagnostic.
Type
;
result.
Message
= diagnostic.
Message
;
result.
Data
= diagnostic.
Data
;
result.
Start
.
Line
= file->
GetLine
(diagnostic.
Range
.
StartOffset
);
result.
Start
.
Col
= file->
GetColumn
(diagnostic.
Range
.
StartOffset
);
result.
End
.
Line
= file->
GetLine
(diagnostic.
Range
.
GetEndOffset
());
result.
End
.
Col
= file->
GetColumn
(diagnostic.
Range
.
GetEndOffset
()) +
1
;
}
return
results;
}
Back
|
FazBrowse Home
|
New Git URL