FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
EmmyLuaCodeStyle/CodeFormatLib/src/CodeFormatLib.cpp at master · Sedas25/EmmyLuaCodeStyle · GitHub
Sedas25
EmmyLuaCodeStyle
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
EmmyLuaCodeStyle
/
CodeFormatLib
/
src
/
CodeFormatLib.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
252 lines (219 loc) · 4.96 KB
Breadcrumbs
EmmyLuaCodeStyle
/
CodeFormatLib
/
src
/
CodeFormatLib.cpp
Copy path
File metadata and controls
252 lines (219 loc) · 4.96 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#includ
e
"lua.hp
p
"
#
include
"
LuaCodeFormat.h
"
#
ifdef
MSVC
#
define
EXPORT
__declspec
(dllexport)
#
else
#
define
EXPORT
#
endif
int
format
(lua_State* L)
{
int
top =
lua_gettop
(L);
if
(top <
2
)
{
return
0
;
}
if
(
lua_isstring
(L,
1
) &&
lua_isstring
(L,
2
))
{
try
{
std::string filename =
lua_tostring
(L,
1
);
std::string text =
lua_tostring
(L,
2
);
auto
formattedText =
LuaCodeFormat::GetInstance
().
Reformat
(filename,
std::move
(text));
if
(formattedText.
empty
())
{
lua_pushboolean
(L,
false
);
return
1
;
}
lua_pushboolean
(L,
true
);
lua_pushlstring
(L, formattedText.
c_str
(), formattedText.
size
());
return
2
;
}
catch
(std::exception& e)
{
std::string err = e.
what
();
lua_settop
(L, top);
lua_pushboolean
(L,
false
);
lua_pushlstring
(L, err.
c_str
(), err.
size
());
return
2
;
}
}
return
0
;
}
enum
class
UpdateType
{
Created =
1
,
Changed =
2
,
Deleted =
3
};
int
range_format
(lua_State* L)
{
int
top =
lua_gettop
(L);
if
(top <
4
)
{
return
0
;
}
if
(
lua_isstring
(L,
1
) &&
lua_isstring
(L,
2
) &&
lua_isinteger
(L,
3
) &&
lua_isinteger
(L,
4
))
{
try
{
std::string filename =
lua_tostring
(L,
1
);
std::string text =
lua_tostring
(L,
2
);
int
startLine =
lua_tointeger
(L,
3
);
int
endLine =
lua_tointeger
(L,
4
);
LuaFormatRange
range
(startLine, endLine);
auto
formattedText =
LuaCodeFormat::GetInstance
().
RangeFormat
(filename, range,
std::move
(text));
if
(formattedText.
empty
())
{
lua_pushboolean
(L,
false
);
return
1
;
}
lua_pushboolean
(L,
true
);
lua_pushlstring
(L, formattedText.
c_str
(), formattedText.
size
());
lua_pushinteger
(L, range.
StartLine
);
lua_pushinteger
(L, range.
EndLine
);
return
4
;
}
catch
(std::exception& e)
{
std::string err = e.
what
();
lua_settop
(L, top);
lua_pushboolean
(L,
false
);
lua_pushlstring
(L, err.
c_str
(), err.
size
());
return
2
;
}
}
return
0
;
}
int
update_config
(lua_State* L)
{
int
top =
lua_gettop
(L);
if
(top <
3
)
{
return
0
;
}
if
(
lua_isinteger
(L,
1
) &&
lua_isstring
(L,
2
) &&
lua_isstring
(L,
3
))
{
try
{
auto
type =
static_cast
<UpdateType>(
lua_tointeger
(L,
1
));
std::string workspaceUri =
lua_tostring
(L,
2
);
std::string configPath =
lua_tostring
(L,
3
);
switch
(type)
{
case
UpdateType::Created:
case
UpdateType::Changed:
{
LuaCodeFormat::GetInstance
().
UpdateCodeStyle
(workspaceUri, configPath);
break
;
}
case
UpdateType::Deleted:
{
LuaCodeFormat::GetInstance
().
RemoveCodeStyle
(workspaceUri);
break
;
}
}
lua_pushboolean
(L,
true
);
return
1
;
}
catch
(std::exception& e)
{
std::string err = e.
what
();
lua_settop
(L, top);
lua_pushboolean
(L,
false
);
lua_pushlstring
(L, err.
c_str
(), err.
size
());
return
2
;
}
}
return
0
;
}
int
diagnose_file
(lua_State* L)
{
int
top =
lua_gettop
(L);
if
(top <
2
)
{
return
0
;
}
if
(
lua_isstring
(L,
1
) &&
lua_isstring
(L,
2
))
{
try
{
std::string filename =
lua_tostring
(L,
1
);
std::string text =
lua_tostring
(L,
2
);
auto
result =
LuaCodeFormat::GetInstance
().
Diagnose
(filename,
std::move
(text));
if
(!result.
first
)
{
lua_pushboolean
(L,
false
);
return
1
;
}
auto
& diagnosticInfos = result.
second
;
lua_pushboolean
(L,
true
);
int
count =
1
;
lua_newtable
(L);
for
(
auto
& diagnosticInfo : diagnosticInfos)
{
//
诊断信息表
lua_newtable
(L);
//
message
{
lua_pushstring
(L,
"
message
"
);
lua_pushlstring
(L, diagnosticInfo.
Message
.
c_str
(), diagnosticInfo.
Message
.
size
());
lua_rawset
(L, -
3
);
}
//
range
{
lua_pushstring
(L,
"
range
"
);
//
range table
lua_newtable
(L);
lua_pushstring
(L,
"
start
"
);
//
start table
lua_newtable
(L);
lua_pushstring
(L,
"
line
"
);
lua_pushinteger
(L, diagnosticInfo.
Range
.
Start
.
Line
);
lua_rawset
(L, -
3
);
lua_pushstring
(L,
"
character
"
);
lua_pushinteger
(L, diagnosticInfo.
Range
.
Start
.
Character
);
lua_rawset
(L, -
3
);
lua_rawset
(L, -
3
);
//
set start = {}
lua_pushstring
(L,
"
end
"
);
//
end table
lua_newtable
(L);
lua_pushstring
(L,
"
line
"
);
lua_pushinteger
(L, diagnosticInfo.
Range
.
End
.
Line
);
lua_rawset
(L, -
3
);
lua_pushstring
(L,
"
character
"
);
lua_pushinteger
(L, diagnosticInfo.
Range
.
End
.
Character
);
lua_rawset
(L, -
3
);
lua_rawset
(L, -
3
);
//
set end = {}
lua_rawset
(L, -
3
);
//
set range = {}
}
//
不确认lua会不会把他改成宏,所以不要在这里用++count
lua_rawseti
(L, -
2
, count);
count++;
}
return
2
;
}
catch
(std::exception& e)
{
std::string err = e.
what
();
lua_settop
(L, top);
lua_pushboolean
(L,
false
);
lua_pushlstring
(L, err.
c_str
(), err.
size
());
return
2
;
}
}
return
0
;
}
static
const
luaL_Reg lib[] = {
{
"
format
"
, format},
{
"
range_format
"
, range_format},
{
"
update_config
"
, update_config},
{
"
diagnose_file
"
, diagnose_file},
{
nullptr
,
nullptr
}
};
extern
"
C
"
EXPORT
int
luaopen_code_format
(lua_State* L)
{
luaL_newlibtable
(L, lib);
luaL_setfuncs
(L, lib,
0
);
return
1
;
}
Back
|
FazBrowse Home
|
New Git URL