Skip to content

Commit a1f2121

Browse files
committed
refactor editorconfig read file
1 parent 9e39a4f commit a1f2121

File tree

9 files changed

+136
-67
lines changed

9 files changed

+136
-67
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
[中文文档](./CHANGELOG_CN.md)
44

5+
56
## 1.5.0
67

78
`NEW` `---@format disable-next` now supports working at any position

CodeFormat/src/FormatContext.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ void FormatContext::SetOutputFilePath(std::string_view path) {
4141
void FormatContext::SetConfigFilePath(std::string_view configPath) {
4242
if (!configPath.empty()) {
4343
auto &config = _configs.emplace_back("");
44-
config.Editorconfig = LuaEditorConfig::LoadFromFile(std::string(configPath));
45-
config.Editorconfig->Parse();
44+
config.Editorconfig = LuaEditorConfig::OpenFile(std::string(configPath));
4645
}
4746
}
4847

@@ -134,8 +133,7 @@ void FormatContext::EnableAutoDetectConfig() {
134133
for (auto &file: files) {
135134
std::filesystem::path filePath(file);
136135
auto &config = _configs.emplace_back(filePath.parent_path().string());
137-
config.Editorconfig = LuaEditorConfig::LoadFromFile(std::string(file));
138-
config.Editorconfig->Parse();
136+
config.Editorconfig = LuaEditorConfig::OpenFile(std::string(file));
139137
}
140138
}
141139
}

CodeFormatCore/include/CodeFormatCore/Config/LuaEditorConfig.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,13 @@ class LuaEditorConfig {
1818
std::map<std::string, std::string, std::less<>> ConfigMap;
1919
};
2020

21-
static std::shared_ptr<LuaEditorConfig> LoadFromFile(const std::string &path);
22-
23-
explicit LuaEditorConfig(std::string &&source);
24-
25-
void Parse();
21+
static std::shared_ptr<LuaEditorConfig> OpenFile(const std::string &path);
2622

2723
LuaStyle &Generate(std::string_view fileUri);
2824

2925
private:
30-
std::string _source;
26+
void Parse(std::string_view text);
27+
3128
std::vector<Section> _sections;
3229
std::map<std::string, LuaStyle, std::less<>> _styleMap;
3330
};

CodeFormatCore/src/Config/LuaEditorConfig.cpp

Lines changed: 85 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,48 +7,95 @@
77

88
#include "Util/StringUtil.h"
99

10-
std::shared_ptr<LuaEditorConfig> LuaEditorConfig::LoadFromFile(const std::string &path) {
11-
std::fstream fin(path, std::ios::in);
10+
#ifdef WIN32
11+
#include <Windows.h>
12+
std::wstring utf8ToWideChar(const std::string &utf8Str) {
13+
int wideCharSize = MultiByteToWideChar(CP_UTF8, 0, utf8Str.c_str(), -1, NULL, 0);
14+
std::wstring wideStr(wideCharSize, L'\0');
15+
MultiByteToWideChar(CP_UTF8, 0, utf8Str.c_str(), -1, &wideStr[0], wideCharSize);
16+
return wideStr;
17+
}
18+
#endif
19+
20+
std::shared_ptr<LuaEditorConfig> LuaEditorConfig::OpenFile(const std::string &path) {
21+
//FIX windows下读取中文路径
22+
#ifdef WIN32
23+
std::fstream fin(utf8ToWideChar(path), std::ios::in | std::ios::binary);
24+
#else
25+
std::fstream fin(path, std::ios::in | std::ios::binary);
26+
#endif
1227
if (fin.is_open()) {
13-
std::stringstream s;
14-
s << fin.rdbuf();
15-
auto editorConfig = std::make_shared<LuaEditorConfig>(s.str());
16-
return editorConfig;
28+
fin.seekg(0, std::ios::end);
29+
auto size = fin.tellg();
30+
std::string s(size, ' ');
31+
fin.seekg(0);
32+
fin.read(s.data(), size);
33+
auto config = std::make_shared<LuaEditorConfig>();
34+
config->Parse(s);
35+
return config;
1736
}
1837

1938
return nullptr;
2039
}
2140

22-
LuaEditorConfig::LuaEditorConfig(std::string &&source)
23-
: _source(source) {
41+
static bool IsWhiteSpaces(int c) {
42+
return c > 0 && std::isspace(c);
2443
}
2544

26-
void LuaEditorConfig::Parse() {
27-
auto lines = string_util::Split(_source, "\n");
28-
29-
std::regex comment = std::regex(R"(^\s*(;|#))");
30-
std::regex luaSection = std::regex(R"(^\s*\[\s*([^\]]+)\s*\]\s*$)");
31-
std::regex valueRegex = std::regex(R"(^\s*([\w\d_\\.]+)\s*=\s*(.+)$)");
32-
bool sectionFounded = false;
33-
34-
for (auto &lineView: lines) {
35-
std::string line(lineView);
36-
if (std::regex_search(line, comment)) {
37-
continue;
38-
}
39-
40-
std::smatch m;
41-
42-
if (std::regex_search(line, m, luaSection)) {
43-
auto pattern = m.str(1);
44-
sectionFounded = (pattern.find("lua") != std::string::npos) || pattern == "*";
45-
_sections.emplace_back(pattern);
46-
continue;
47-
}
45+
static bool IsEndOfLine(int c){
46+
return c == '\r' || c == '\n';
47+
}
4848

49-
if (sectionFounded) {
50-
if (std::regex_search(line, m, valueRegex)) {
51-
_sections.back().ConfigMap.insert({m.str(1), std::string(string_util::TrimSpace(m.str(2)))});
49+
void LuaEditorConfig::Parse(std::string_view text) {
50+
auto reader = TextReader(text);
51+
_sections.emplace_back("");
52+
while (!reader.IsEof()) {
53+
reader.ResetBuffer();
54+
switch (reader.GetCurrentChar()) {
55+
// comment
56+
case ';':
57+
case '#':
58+
//skip empty line
59+
case '\r':
60+
case '\n': {
61+
reader.NextLine();
62+
break;
63+
}
64+
case '\t':
65+
case ' ': {
66+
reader.EatWhile(IsWhiteSpaces);
67+
break;
68+
}
69+
case '[': {
70+
reader.NextChar();
71+
reader.EatWhile([](char ch) { return ch != ']' && !IsEndOfLine(ch); });
72+
auto pattern = string_util::TrimSpace(reader.GetSaveText());
73+
if (pattern.empty() || reader.GetCurrentChar() != ']') {
74+
reader.NextLine();
75+
break;
76+
}
77+
reader.NextLine();
78+
_sections.emplace_back(pattern);
79+
break;
80+
}
81+
default: {
82+
reader.EatWhile([](char ch) { return ch != '=' && !IsEndOfLine(ch); });
83+
auto key = string_util::TrimSpace(reader.GetSaveText());
84+
if (key.empty() || reader.GetCurrentChar() != '=') {
85+
reader.NextLine();
86+
break;
87+
}
88+
89+
reader.NextChar();
90+
reader.ResetBuffer();
91+
reader.EatWhile([](char ch) { return !IsEndOfLine(ch); });
92+
auto value = string_util::TrimSpace(reader.GetSaveText());
93+
if (value.empty()) {
94+
reader.NextLine();
95+
break;
96+
}
97+
_sections.back().ConfigMap.insert({std::string(key), std::string(value)});
98+
break;
5299
}
53100
}
54101
}
@@ -60,8 +107,8 @@ LuaStyle &LuaEditorConfig::Generate(std::string_view filePath) {
60107
for (std::size_t i = 0; i != _sections.size(); i++) {
61108
auto &pattern = _sections[i].Pattern;
62109

63-
// [*] [*.lua]
64-
if (pattern.GetPattern() == "*" || pattern.GetPattern() == "*.lua") {
110+
// "" [*] [*.lua]
111+
if (pattern.GetPattern().empty() || pattern.GetPattern() == "*" || pattern.GetPattern() == "*.lua") {
65112
patternSection.push_back(i);
66113
}
67114
// [{test.lua,lib.lua}]
@@ -85,7 +132,9 @@ LuaStyle &LuaEditorConfig::Generate(std::string_view filePath) {
85132
auto &luaStyle = _styleMap.at(patternKey);
86133
for (auto i: patternSection) {
87134
auto &configMap = _sections[i].ConfigMap;
88-
luaStyle.Parse(configMap);
135+
if (!configMap.empty()) {
136+
luaStyle.Parse(configMap);
137+
}
89138
}
90139

91140
return luaStyle;

CodeFormatCore/src/Format/Analyzer/FormatDocAnalyze.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ void FormatDocAnalyze::Query(FormatState &f, LuaSyntaxNode syntaxNode, const Lua
3434
resolve.SetOriginRange(it->second);
3535
}
3636

37-
bool IsWhiteSpaces(int c) {
37+
static bool IsWhiteSpaces(int c) {
3838
return c > 0 && std::isspace(c);
3939
}
4040

41-
bool ActionStart(int c) {
41+
static bool ActionStart(int c) {
4242
return c > 0 && std::isalpha(c);
4343
}
4444

45-
bool ActionContinue(int c) {
45+
static bool ActionContinue(int c) {
4646
return c > 0 && (std::isalnum(c) || c == '-');
4747
}
4848

CodeFormatLib/src/LuaCodeFormat.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,14 @@ LuaCodeFormat::LuaCodeFormat()
1818
void LuaCodeFormat::UpdateCodeStyle(const std::string &workspaceUri, const std::string &configPath) {
1919
for (auto &config: _configs) {
2020
if (config.Workspace == workspaceUri) {
21-
config.Editorconfig = LuaEditorConfig::LoadFromFile(configPath);
22-
config.Editorconfig->Parse();
21+
config.Editorconfig = LuaEditorConfig::OpenFile(configPath);
2322
return;
2423
}
2524
}
2625

2726
auto &config = _configs.emplace_back(
2827
workspaceUri);
29-
config.Editorconfig = LuaEditorConfig::LoadFromFile(configPath);
30-
config.Editorconfig->Parse();
28+
config.Editorconfig = LuaEditorConfig::OpenFile(configPath);
3129
}
3230

3331
void LuaCodeFormat::UpdateDiagnosticStyle(InfoTree &tree) {

CodeFormatServer/src/Service/ConfigService.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,14 @@ void ConfigService::LoadEditorconfig(std::string_view workspace, std::string_vie
3333
std::string path(filePath);
3434
for (auto &config: _styleConfigs) {
3535
if (config.Workspace == workspace) {
36-
config.Editorconfig = LuaEditorConfig::LoadFromFile(path);
37-
config.Editorconfig->Parse();
36+
config.Editorconfig = LuaEditorConfig::OpenFile(path);
3837
return;
3938
}
4039
}
4140

4241
auto &config = _styleConfigs.emplace_back(
4342
std::string(workspace));
44-
config.Editorconfig = LuaEditorConfig::LoadFromFile(path);
45-
config.Editorconfig->Parse();
43+
config.Editorconfig = LuaEditorConfig::OpenFile(path);
4644
}
4745

4846
void ConfigService::LoadLanguageTranslator(std::string_view filePath) {

LuaParser/include/LuaParser/Lexer/TextReader.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ class TextReader {
77
public:
88
explicit TextReader(std::string_view text);
99

10-
int NextChar();
10+
void NextChar();
11+
12+
void NextLine();
1113

1214
void SaveAndNext();
1315

@@ -41,8 +43,9 @@ class TextReader {
4143

4244
bool IsEof() const;
4345

44-
bool HasSaveText() const;
46+
bool IsEol() const;
4547

48+
bool HasSaveText() const;
4649
private:
4750
std::string_view _text;
4851

LuaParser/src/Lexer/TextReader.cpp

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,40 @@ TextReader::TextReader(std::string_view text)
1010
_currentIndex(0) {
1111
}
1212

13-
int TextReader::NextChar() {
14-
if (_currentIndex < (_text.size() - 1)) {
15-
return _text[++_currentIndex];
13+
void TextReader::NextChar() {
14+
if (_currentIndex + 1 < _text.size()) {
15+
++_currentIndex;
1616
} else {
1717
++_currentIndex;
18-
return EOZ;
18+
if (_currentIndex >= _text.size()) {
19+
_isEof = true;
20+
}
1921
}
2022
}
2123

22-
void TextReader::SaveAndNext() {
23-
Save();
24-
int ch = NextChar();
25-
if (ch == EOZ) {
24+
void TextReader::NextLine() {
25+
while (_currentIndex < _text.size()) {
26+
char ch = _text[_currentIndex];
27+
if (ch == '\n' || ch == '\r') {
28+
_currentIndex++;
29+
if (ch == '\r' && _currentIndex < _text.size() && _text[_currentIndex] == '\n') {
30+
_currentIndex++;
31+
}
32+
break;
33+
}
34+
_currentIndex++;
35+
}
36+
37+
if (_currentIndex >= _text.size()) {
2638
_isEof = true;
2739
}
2840
}
2941

42+
void TextReader::SaveAndNext() {
43+
Save();
44+
NextChar();
45+
}
46+
3047
void TextReader::Save() {
3148
if (!_hasSaveText) {
3249
_hasSaveText = true;
@@ -100,3 +117,11 @@ std::size_t TextReader::EatWhen(int ch) {
100117
}
101118
return count;
102119
}
120+
121+
bool TextReader::IsEol() const {
122+
if (_currentIndex < _text.size()) {
123+
char ch = _text[_currentIndex];
124+
return ch == '\n' || ch == '\r';
125+
}
126+
return true;
127+
}

0 commit comments

Comments
 (0)