Skip to content

Commit 63916a1

Browse files
committed
命令行工具会根据选项自动寻找配置
1 parent faf194e commit 63916a1

File tree

5 files changed

+384
-57
lines changed

5 files changed

+384
-57
lines changed

CHANGELOG.md

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
# Change Log
2+
3+
## 0.9.0
4+
5+
1. 命令行工具现在会自动寻找并使用最近的editorconfig配置
6+
2. 长字符串和长注释会采用配置的行尾格式化
7+
8+
## 0.8.7
9+
10+
重写命令行工具
11+
12+
## 0.8.5
13+
14+
修复一个bug:纯注释文件会被直接清空
15+
16+
## 0.8.4
17+
18+
`FIX` [#27](https://github.com/CppCXY/EmmyLuaCodeStyle/issues/27)
19+
## 0.8.3
20+
21+
`FIX` [#26](https://github.com/CppCXY/EmmyLuaCodeStyle/issues/26)
22+
23+
## 0.8.2
24+
25+
`FIX` [#25](https://github.com/CppCXY/EmmyLuaCodeStyle/issues/25)
26+
27+
## 0.8.1
28+
29+
`FIX` [#24](https://github.com/CppCXY/EmmyLuaCodeStyle/issues/24)
30+
31+
## 0.8.0
32+
33+
`Resolve` [#20](https://github.com/CppCXY/EmmyLuaCodeStyle/issues/20)
34+
`Resolve` [#17](https://github.com/CppCXY/EmmyLuaCodeStyle/issues/17)
35+
`Resolve` [#16](https://github.com/CppCXY/EmmyLuaCodeStyle/issues/16)
36+
`Resolve` [#15](https://github.com/CppCXY/EmmyLuaCodeStyle/issues/15)
37+
38+
主要更新的特性为允许设定单双引号风格,对齐时采用最小对齐,允许移除函数调用的括号
39+
40+
## 0.7.0
41+
42+
1. 修复存在某些语法错误的情况下仍然格式化的问题
43+
2. 支持if的条件表达式互相对齐
44+
3. table表达式内支持不同的连续有不同的对齐
45+
4. 形如t[#t+1]可以无空白包含
46+
5. sumneko_lua 集成了该插件的主要功能
47+
48+
49+
## 0.6.1
50+
51+
更新文档,更新editorconfig模板
52+
53+
54+
## 0.6.0
55+
56+
1. 提高测试的覆盖率,每一个格式化选项都有专门的测试
57+
2. 重构缩进算法,更好的支持使用tab作为缩进
58+
3. 提供许多与缩进有关的格式化选项
59+
4. 部分格式化选项重新命名了
60+
5. 修复许多bug
61+
62+
63+
## 0.5.1
64+
65+
修复linux下不可用的问题
66+
67+
## 0.5.0
68+
69+
将语言服务由单线程分离io线程和逻辑线程。io线程会尽可能快的读取消息,逻辑线程为单线程内无锁异步(不含asio自己控制的锁)
70+
采用增量更新,单次发包量大幅度降低,语言服务采用原地增量更新算法(极少重新申请空间储存代码)
71+
语言服务实现调度机制,代码诊断实现后端防抖,防抖区间暂定0.3秒。
72+
大幅度提高插件性能,代码诊断不再成为性能瓶颈,在10万行以内的代码不会影响插件的正常使用
73+
74+
## 0.4.5
75+
76+
修复由于linux下编译器版本为gcc11导致在大部分linux环境下不可用的问题
77+
78+
## 0.4.4
79+
80+
修复枚举不一致
81+
## 0.4.3
82+
83+
修复诊断bug
84+
85+
## 0.4.2
86+
87+
1. 修改格式化规则,连续调用表达式中如果存在单参数省略括号的表达形式,则该参数与下一个调用参数列表保持一个间距。
88+
2. 修复一个识别错误的bug
89+
90+
## 0.4.1
91+
92+
提供模块的特殊export.rule规则,使用形式待考虑
93+
94+
## 0.4.0
95+
96+
提供auto import 功能
97+
98+
## 0.3.2
99+
100+
修复label导致格式化结果不稳定的bug
101+
102+
## 0.3.1
103+
104+
现在修改配置会立即刷新所有打开的文件了
105+
106+
## 0.3.0
107+
108+
修复许多bug,重构迭代算法,增加测试用例
109+
110+
## 0.2.6
111+
112+
降低插件体积,插件实现分系统分发不同的包。
113+
114+
## 0.2.5
115+
116+
修复一个连续调用函数时格式化错误的问题
117+
118+
## 0.2.4
119+
120+
修复因为cin.readsome无法在linux和macosx上正确工作导致无法正确运行的问题,windows上仍然使用cin,cout
121+
linux和macosx上使用asio封装过的接口
122+
123+
## 0.2.2
124+
125+
优化节点的内存占用,优化常驻内存,采用mimalloc优化内存分配性能,优化内存碎片。
126+
127+
## 0.2.1
128+
129+
修复内存方面的问题,降低内存消耗
130+
131+
## 0.2.0
132+
133+
1. 重写.editorconfig读取规则,支持editorconfig的基本匹配规则和所有基本选项
134+
2. 支持基于lua语法特性和日常习惯的lua命名风格检测规则
135+
3. 语言服务实现内采用异常提前中断没必要的解析
136+
137+
## 0.1.18
138+
139+
修复命名风格检查未能正确工作的bug
140+
141+
## 0.1.17
142+
143+
1. 修改配置选项名称,和editorconfig使用的名称保持一直(建议重新导入配置)
144+
145+
2. 支持检测是否以新行结束文本
146+
147+
3. 支持命名风格检测(默认不开启)
148+
149+
## 0.1.16
150+
151+
代码诊断本地化
152+
153+
## 0.1.15
154+
155+
修复bug

CodeFormat/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ target_include_directories(CodeFormat PUBLIC
1414
target_sources(CodeFormat
1515
PUBLIC
1616
${CodeFormat_SOURCE_DIR}/src/CodeFormat.cpp
17+
${CodeFormat_SOURCE_DIR}/src/LuaFormat.cpp
1718
)
1819

1920
target_link_libraries(CodeFormat CodeService Util)

CodeFormat/src/CodeFormat.cpp

Lines changed: 24 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include <iostream>
22
#include <cstring>
33
#include <fstream>
4+
5+
#include "LuaFormat.h"
46
#include "CodeService/LuaEditorConfig.h"
57
#include "LuaParser/LuaParser.h"
68
#include "Util/format.h"
@@ -27,6 +29,9 @@ int main(int argc, char** argv)
2729
.Add<int>("stdin", "i", "Read from stdin and specify read size")
2830
.Add<std::string>("config", "c",
2931
"Specify .editorconfig file, it decides on the effect of formatting")
32+
.Add<std::string>("detect-config-root", "d",
33+
"Set the root directory for automatic detection of config,\n"
34+
" If this option is set, the config option has no effect ")
3035
.Add<std::string>("outfile", "o",
3136
"Specify output file")
3237
.EnableKeyValueArgs();
@@ -43,97 +48,59 @@ int main(int argc, char** argv)
4348
}
4449

4550
std::shared_ptr<LuaParser> parser = nullptr;
46-
51+
auto luaFormat = std::make_shared<LuaFormat>();
4752
if (cmd.HasOption("file"))
4853
{
49-
parser = LuaParser::LoadFromFile(cmd.Get<std::string>("file"));
50-
if (!parser)
54+
if(!luaFormat->SetInputFile(cmd.Get<std::string>("file")))
5155
{
52-
std::cerr << format("can not find file: {}", cmd.Get<std::string>("file")) << std::endl;
56+
std::cerr << format("Can not find file {}", cmd.Get<std::string>("file")) << std::endl;
5357
return -1;
5458
}
5559
}
5660
else if (cmd.HasOption("stdin"))
5761
{
5862
SET_BINARY_MODE();
5963
std::size_t size = cmd.Get<int>("stdin");
60-
61-
std::string buffer;
62-
buffer.resize(size);
63-
std::cin.get(buffer.data(), size, EOF);
64-
auto realSize = strnlen(buffer.data(), size);
65-
buffer.resize(realSize);
66-
parser = LuaParser::LoadFromBuffer(std::move(buffer));
64+
if(!luaFormat->ReadFromStdin(size))
65+
{
66+
return -1;
67+
}
6768
}
6869
else
6970
{
7071
std::cerr << "not special input file" << std::endl;
7172
return -1;
7273
}
7374

74-
std::shared_ptr<LuaCodeStyleOptions> options = nullptr;
75-
76-
if (cmd.HasOption("config"))
75+
if (cmd.HasOption("output"))
7776
{
78-
auto editorConfig = LuaEditorConfig::LoadFromFile(cmd.Get<std::string>("config"));
79-
if (cmd.HasOption("file"))
80-
{
81-
options = editorConfig->Generate(cmd.Get<std::string>("file"));
82-
}
83-
else
84-
{
85-
options = editorConfig->Generate(cmd.Get<std::string>(""));
86-
}
87-
}
88-
else
89-
{
90-
options = std::make_shared<LuaCodeStyleOptions>();
91-
if (!cmd.GetKeyValueOptions().empty())
92-
{
93-
LuaEditorConfig::ParseFromSection(options, cmd.GetKeyValueOptions());
94-
}
77+
luaFormat->SetOutputFile(cmd.Get<std::string>("output"));
9578
}
9679

97-
if (!cmd.HasOption("outfile"))
80+
if (cmd.HasOption("detect-config-root"))
9881
{
99-
options->end_of_line = "\n";
82+
luaFormat->AutoDetectConfigRoot(cmd.Get<std::string>("detect-config-root"));
10083
}
101-
102-
parser->BuildAstWithComment();
103-
104-
if (parser->HasError())
84+
else if(cmd.HasOption("config"))
10585
{
106-
return -1;
86+
luaFormat->SetConfigPath(cmd.Get<std::string>("config"));
10787
}
10888

109-
LuaFormatter formatter(parser, *options);
110-
formatter.BuildFormattedElement();
89+
luaFormat->SetDefaultOptions(cmd.GetKeyValueOptions());
11190

11291
if (cmd.GetTarget() == "format")
11392
{
114-
auto formattedText = formatter.GetFormattedText();
115-
if (cmd.HasOption("outfile"))
93+
if(!luaFormat->Reformat())
11694
{
117-
std::fstream f(cmd.Get<std::string>("outfile"), std::ios::out | std::ios::binary);
118-
f.write(formattedText.data(), formattedText.size());
119-
f.close();
120-
}
121-
else
122-
{
123-
std::cout.write(formattedText.data(), formattedText.size());
95+
std::cerr << format("Exist lua syntax error") << std::endl;
96+
return -1;
12497
}
12598
}
12699
else if (cmd.GetTarget() == "check")
127100
{
128-
auto diagnosis = formatter.GetDiagnosisInfos();
129-
for (auto& d : diagnosis)
101+
if (!luaFormat->Check() && cmd.Get<bool>("diagnosis-as-error"))
130102
{
131-
std::cout << format("{} from {}:{} to {}:{}", d.Message, d.Range.Start.Line, d.Range.Start.Character,
132-
d.Range.End.Line, d.Range.End.Character) << std::endl;
133-
}
134-
if (cmd.Get<bool>("diagnosis-as-error"))
135-
{
136-
return diagnosis.empty() ? 0 : -1;
103+
return -1;
137104
}
138105
}
139106
return 0;

0 commit comments

Comments
 (0)