FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
EmmyLuaCodeStyle/CodeFormatLib/src/LuaCodeFormat.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
/
LuaCodeFormat.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
118 lines (99 loc) · 2.82 KB
Breadcrumbs
EmmyLuaCodeStyle
/
CodeFormatLib
/
src
/
LuaCodeFormat.cpp
Copy path
File metadata and controls
118 lines (99 loc) · 2.82 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
#
include
"
LuaCodeFormat.h
"
#
include
"
CodeService/LuaEditorConfig.h
"
#
include
"
CodeService/LuaFormatter.h
"
#
include
"
LuaParser/LuaParser.h
"
#
include
"
Util/StringUtil.h
"
LuaCodeFormat&
LuaCodeFormat::GetInstance
()
{
static
LuaCodeFormat instance;
return
instance;
}
LuaCodeFormat::LuaCodeFormat
()
: _defaultOptions(std::make_shared<LuaCodeStyleOptions>())
{
}
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
;
}
}
}
std::string
LuaCodeFormat::Reformat
(
const
std::string& uri, std::string&& text)
{
auto
parser =
LuaParser::LoadFromBuffer
(
std::move
(text));
parser->
BuildAstWithComment
();
if
(!parser->
GetErrors
().
empty
())
{
return
"
"
;
}
auto
options =
GetOptions
(uri);
LuaFormatter
formatter
(parser, *options);
formatter.
BuildFormattedElement
();
return
formatter.
GetFormattedText
();
}
std::string
LuaCodeFormat::RangeFormat
(
const
std::string& uri, LuaFormatRange& range, std::string&& text)
{
auto
parser =
LuaParser::LoadFromBuffer
(
std::move
(text));
parser->
BuildAstWithComment
();
if
(!parser->
GetErrors
().
empty
())
{
return
"
"
;
}
auto
options =
GetOptions
(uri);
LuaFormatter
formatter
(parser, *options);
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));
parser->
BuildAstWithComment
();
if
(!parser->
GetErrors
().
empty
())
{
return
std::make_pair
(
false
, std::vector<LuaDiagnosisInfo>());
}
auto
options =
GetOptions
(uri);
LuaFormatter
formatter
(parser, *options);
formatter.
BuildFormattedElement
();
return
std::make_pair
(
true
, formatter.
GetDiagnosisInfos
());
}
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;
}
Back
|
FazBrowse Home
|
New Git URL