Skip to content

Commit 58caad2

Browse files
committed
优化范围格式化和type格式化, 支持对format disable的处理
1 parent 029b9b1 commit 58caad2

File tree

9 files changed

+117
-70
lines changed

9 files changed

+117
-70
lines changed

CodeFormatLib/src/CodeFormatLib.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ int range_format(lua_State *L) {
146146
return 1;
147147
}
148148
auto &formattedText = formattedTextResult.Data;
149+
if(formattedText.empty()){
150+
lua_pushboolean(L, false);
151+
return 1;
152+
}
153+
149154
lua_pushboolean(L, true);
150155
lua_pushlstring(L, formattedText.c_str(), formattedText.size());
151156
lua_pushinteger(L, range.StartLine);

CodeFormatServer/src/LSP/LSPHandle.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,10 @@ std::shared_ptr<lsp::Serializable> LSPHandle::OnRangeFormatting(
231231
range.EndLine = params->range.end.line;
232232

233233
auto newText = _server->GetService<FormatService>()->RangeFormat(syntaxTree, luaStyle, range);
234+
if(newText.empty()){
235+
result->hasError = true;
236+
return result;
237+
}
234238

235239
auto &edit = result->edits.emplace_back();
236240
edit.newText = std::move(newText);

CodeService/src/Format/Analyzer/FormatDocAnalyze.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
#include "CodeService/Format/Analyzer/FormatDocAnalyze.h"
2+
#include "CodeService/Format/FormatState.h"
23
#include "LuaParser/Lexer/LuaTokenTypeDetail.h"
3-
#include "Util/StringUtil.h"
44
#include "LuaParser/Lexer/TextReader.h"
5-
#include "CodeService/Format/FormatState.h"
5+
#include "Util/StringUtil.h"
66

77
FormatDocAnalyze::FormatDocAnalyze() {
8-
98
}
109

1110
void FormatDocAnalyze::Analyze(FormatState &f, const LuaSyntaxTree &t) {
@@ -27,8 +26,7 @@ void FormatDocAnalyze::Analyze(FormatState &f, const LuaSyntaxTree &t) {
2726
void FormatDocAnalyze::ComplexAnalyze(FormatState &f, const LuaSyntaxTree &t) {
2827
}
2928

30-
void
31-
FormatDocAnalyze::Query(FormatState &f, LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t, FormatResolve &resolve) {
29+
void FormatDocAnalyze::Query(FormatState &f, LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t, FormatResolve &resolve) {
3230
auto it = _ignores.find(syntaxNode.GetIndex());
3331
if (it == _ignores.end()) {
3432
return;
@@ -102,8 +100,7 @@ void FormatDocAnalyze::AnalyzeDocFormat(LuaSyntaxNode n, FormatState &f, const L
102100
break;
103101
} else if (action == "disable-next") {
104102
auto nextNode = n.GetNextSibling(t);
105-
while (!nextNode.IsNull(t)
106-
&& !detail::multi_match::StatementMatch(nextNode.GetSyntaxKind(t))) {
103+
while (!nextNode.IsNull(t) && !detail::multi_match::StatementMatch(nextNode.GetSyntaxKind(t))) {
107104
nextNode.ToNext(t);
108105
}
109106
if (nextNode.IsNode(t)) {
@@ -124,7 +121,7 @@ void FormatDocAnalyze::AnalyzeDocFormat(LuaSyntaxNode n, FormatState &f, const L
124121

125122
return;
126123
}
127-
case ParseState::List : {
124+
case ParseState::List: {
128125
return;
129126
}
130127
}
@@ -136,3 +133,11 @@ void FormatDocAnalyze::AddIgnoreRange(const IndexRange &range, const LuaSyntaxTr
136133
auto endIndex = LuaSyntaxNode(range.EndIndex).GetLastToken(t).GetIndex();
137134
_ignores[startIndex] = IndexRange(startIndex, endIndex);
138135
}
136+
137+
std::vector<IndexRange> FormatDocAnalyze::GetIgnores() const {
138+
std::vector<IndexRange> result;
139+
for (auto p: _ignores) {
140+
result.push_back(p.second);
141+
}
142+
return result;
143+
}

CodeService/src/Format/FormatState.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
#include "CodeService/Format/FormatState.h"
2-
#include "CodeService/Format/Analyzer/SpaceAnalyzer.h"
2+
#include "CodeService/Format/Analyzer/AlignAnalyzer.h"
3+
#include "CodeService/Format/Analyzer/FormatDocAnalyze.h"
34
#include "CodeService/Format/Analyzer/IndentationAnalyzer.h"
45
#include "CodeService/Format/Analyzer/LineBreakAnalyzer.h"
5-
#include "CodeService/Format/Analyzer/AlignAnalyzer.h"
6+
#include "CodeService/Format/Analyzer/SpaceAnalyzer.h"
67
#include "CodeService/Format/Analyzer/TokenAnalyzer.h"
7-
#include "CodeService/Format/Analyzer/FormatDocAnalyze.h"
88

99
FormatState::FormatState(Mode mode)
10-
: _currentWidth(0),
11-
_mode(mode) {
10+
: _currentWidth(0),
11+
_mode(mode),
12+
_foreachContinue(true) {
1213
}
1314

1415
std::size_t &FormatState::CurrentWidth() {
1516
return _currentWidth;
1617
}
1718

1819
EndOfLine FormatState::GetEndOfLine() const {
19-
return _formatStyle.detect_end_of_line ?
20-
_fileEndOfLine : _formatStyle.end_of_line;;
20+
return _formatStyle.detect_end_of_line ? _fileEndOfLine : _formatStyle.end_of_line;
21+
;
2122
}
2223

2324
void FormatState::SetFormatStyle(LuaStyle &style) {
@@ -141,6 +142,7 @@ void FormatState::AddIgnore(IndexRange range) {
141142
void FormatState::DfsForeach(std::vector<LuaSyntaxNode> &startNodes,
142143
const LuaSyntaxTree &t,
143144
const FormatState::FormatHandle &enterHandle) {
145+
_foreachContinue = true;
144146
std::vector<Traverse> traverseStack;
145147
for (auto it = startNodes.rbegin(); it != startNodes.rend(); it++) {
146148
traverseStack.emplace_back(*it, TraverseEvent::Enter);
@@ -155,7 +157,7 @@ void FormatState::DfsForeach(std::vector<LuaSyntaxNode> &startNodes,
155157
traverseStack.back().Event = TraverseEvent::Exit;
156158
if (_ignoreRange.EndIndex != 0) {
157159
auto index = traverse.Node.GetIndex();
158-
if(index >= _ignoreRange.StartIndex && index <= _ignoreRange.EndIndex){
160+
if (index >= _ignoreRange.StartIndex && index <= _ignoreRange.EndIndex) {
159161
continue;
160162
}
161163
}
@@ -198,6 +200,9 @@ void FormatState::DfsForeach(std::vector<LuaSyntaxNode> &startNodes,
198200
}
199201

200202
enterHandle(traverse.Node, t, resolve);
203+
if (!_foreachContinue) {
204+
return;
205+
}
201206
} else {
202207
traverseStack.pop_back();
203208
if (GetCurrentIndent().SyntaxNode.GetIndex() == traverse.Node.GetIndex()) {
@@ -222,5 +227,6 @@ void FormatState::AddAbsoluteIndent(LuaSyntaxNode &syntaxNoe, std::size_t indent
222227
}
223228
}
224229

225-
226-
230+
void FormatState::StopDfsForeach() {
231+
_foreachContinue = false;
232+
}
Lines changed: 61 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,35 @@
11
#include "CodeService/RangeFormat/RangeFormatBuilder.h"
2+
#include "CodeService/Format/Analyzer/FormatDocAnalyze.h"
23
#include "LuaParser/Lexer/LuaTokenTypeDetail.h"
34

45
RangeFormatBuilder::RangeFormatBuilder(LuaStyle &style, FormatRange &range)
5-
: FormatBuilder(style), _validRange(false), _range(range) {
6-
6+
: FormatBuilder(style), _validRange(Valid::Init), _range(range) {
77
}
88

99
std::string RangeFormatBuilder::GetFormatResult(const LuaSyntaxTree &t) {
1010
_state.Analyze(t);
11-
auto root = t.GetRootNode();
12-
std::vector<LuaSyntaxNode> startNodes;
13-
for (auto child: root.GetChildren(t)) {
14-
auto childEndLine = child.GetEndLine(t);
15-
if (childEndLine >= _range.StartLine) {
16-
startNodes.push_back(child);
17-
}
18-
if (childEndLine > _range.EndLine) {
19-
break;
11+
12+
auto formatDoc = _state.GetAnalyzer<FormatDocAnalyze>();
13+
if (formatDoc) {
14+
auto ignores = formatDoc->GetIgnores();
15+
for (auto &ignore: ignores) {
16+
auto ignoreStartLine = LuaSyntaxNode(ignore.StartIndex).GetStartLine(t);
17+
auto ignoreEndLine = LuaSyntaxNode(ignore.EndIndex).GetEndLine(t);
18+
if ((_range.StartLine >= ignoreStartLine && _range.StartLine <= ignoreEndLine) || (_range.EndLine >= ignoreStartLine && _range.StartLine < ignoreStartLine)) {
19+
return "";
20+
}
2021
}
2122
}
2223

23-
LuaSyntaxNode n;
24-
_state.AddRelativeIndent(n, 0);
24+
auto root = t.GetRootNode();
25+
std::vector<LuaSyntaxNode> startNodes = {root};
2526

26-
_state.DfsForeach(startNodes, t, [this](LuaSyntaxNode &syntaxNode,
27-
const LuaSyntaxTree &t,
28-
FormatResolve &resolve) {
29-
CheckRange(syntaxNode, t);
27+
_state.DfsForeach(startNodes, t, [this](LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t, FormatResolve &resolve) {
28+
CheckRange(syntaxNode, t, resolve);
3029
DoResolve(syntaxNode, t, resolve);
3130
});
3231

33-
_validRange = true;
32+
_validRange = Valid::Process;
3433
DealEndWithNewLine(true);
3534
return _formattedText;
3635
}
@@ -40,7 +39,7 @@ FormatRange RangeFormatBuilder::GetReplaceRange() const {
4039
}
4140

4241
void RangeFormatBuilder::WriteSyntaxNode(LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t) {
43-
if (_validRange) {
42+
if (_validRange == Valid::Process) {
4443
return FormatBuilder::WriteSyntaxNode(syntaxNode, t);
4544
} else {
4645
auto text = syntaxNode.GetText(t);
@@ -60,23 +59,23 @@ void RangeFormatBuilder::WriteSyntaxNode(LuaSyntaxNode &syntaxNode, const LuaSyn
6059
}
6160

6261
void RangeFormatBuilder::WriteSpace(std::size_t space) {
63-
if (_validRange) {
62+
if (_validRange == Valid::Process) {
6463
return FormatBuilder::WriteSpace(space);
6564
} else {
6665
_state.CurrentWidth() += space;
6766
}
6867
}
6968

7069
void RangeFormatBuilder::WriteLine(std::size_t line) {
71-
if (_validRange) {
70+
if (_validRange == Valid::Process) {
7271
return FormatBuilder::WriteLine(line);
7372
} else {
7473
_state.CurrentWidth() = 0;
7574
}
7675
}
7776

7877
void RangeFormatBuilder::WriteIndent() {
79-
if (_validRange) {
78+
if (_validRange == Valid::Process) {
8079
return FormatBuilder::WriteIndent();
8180
} else {
8281
auto topLevelIndent = _state.GetCurrentIndent();
@@ -85,25 +84,23 @@ void RangeFormatBuilder::WriteIndent() {
8584
}
8685

8786
void RangeFormatBuilder::WriteChar(char ch) {
88-
if (_validRange) {
87+
if (_validRange == Valid::Process) {
8988
return FormatBuilder::WriteChar(ch);
9089
} else {
9190
_state.CurrentWidth()++;
9291
}
9392
}
9493

9594
void RangeFormatBuilder::WriteText(std::string_view text) {
96-
if (_validRange) {
95+
if (_validRange == Valid::Process) {
9796
return FormatBuilder::WriteText(text);
9897
} else {
9998
std::size_t last = 0;
10099
for (std::size_t i = 0; i != text.size(); i++) {
101100
char ch = text[i];
102101
if (ch == '\n' || ch == '\r') {
103102
_state.CurrentWidth() = 0;
104-
if (ch == '\r'
105-
&& (i + 1 < text.size())
106-
&& (text[i + 1] == '\n')) {
103+
if (ch == '\r' && (i + 1 < text.size()) && (text[i + 1] == '\n')) {
107104
i++;
108105
}
109106
last = i + 1;
@@ -116,26 +113,46 @@ void RangeFormatBuilder::WriteText(std::string_view text) {
116113
}
117114
}
118115

119-
void RangeFormatBuilder::CheckRange(LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t) {
116+
void RangeFormatBuilder::CheckRange(LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t, FormatResolve &resolve) {
120117
if (syntaxNode.IsToken(t)) {
121-
auto tokenEndLine = syntaxNode.GetEndLine(t);
122-
if (tokenEndLine < _range.StartLine) {
123-
_validRange = false;
124-
return;
125-
}
126-
auto tokenStartLine = syntaxNode.GetStartLine(t);
127-
if (tokenStartLine > _range.EndLine) {
128-
_validRange = false;
129-
return;
118+
LuaSyntaxNode startNode = syntaxNode;
119+
LuaSyntaxNode endNode = syntaxNode;
120+
if (resolve.GetTokenStrategy() == TokenStrategy::OriginRange) {
121+
auto r = resolve.GetOriginRange();
122+
startNode = LuaSyntaxNode(r.StartIndex);
123+
endNode = LuaSyntaxNode(r.EndIndex);
130124
}
131125

132-
if (tokenStartLine < _range.StartLine) {
133-
_range.StartLine = tokenStartLine;
134-
_range.StartCol = syntaxNode.GetStartCol(t);
135-
}
136-
if (tokenEndLine > _range.EndLine) {
137-
_range.EndLine = tokenEndLine;
126+
switch (_validRange) {
127+
case Valid::Init: {
128+
auto tokenEndLine = endNode.GetEndLine(t);
129+
if (tokenEndLine >= _range.StartLine) {
130+
_validRange = Valid::Process;
131+
auto tokenStartLine = startNode.GetStartLine(t);
132+
if (tokenStartLine < _range.StartLine) {
133+
_range.StartLine = tokenStartLine;
134+
_range.StartCol = startNode.GetStartCol(t);
135+
}
136+
}
137+
break;
138+
}
139+
case Valid::Process: {
140+
auto tokenStartLine = startNode.GetStartLine(t);
141+
if (tokenStartLine > _range.EndLine) {
142+
_validRange = Valid::Finish;
143+
_state.StopDfsForeach();
144+
} else {
145+
auto tokenEndLine = endNode.GetEndLine(t);
146+
if (tokenEndLine > _range.EndLine) {
147+
_range.EndLine = tokenEndLine;
148+
}
149+
}
150+
151+
break;
152+
}
153+
default: {
154+
break;
155+
}
138156
}
139-
_validRange = true;
140157
}
141158
}

CodeService/src/TypeFormat/LuaTypeFormat.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
#include "CodeService/TypeFormat/LuaTypeFormat.h"
2-
#include <algorithm>
2+
#include "CodeService/RangeFormat/RangeFormatBuilder.h"
33
#include "LuaParser/Lexer/LuaTokenTypeDetail.h"
44
#include "Util/StringUtil.h"
55
#include "Util/format.h"
6-
#include "CodeService/RangeFormat/RangeFormatBuilder.h"
6+
#include <algorithm>
77

88
std::vector<LuaTypeFormat::Result> LuaTypeFormat::GetResult() {
99
return _results;
1010
}
1111

1212
LuaTypeFormat::LuaTypeFormat(LuaTypeFormatOptions &typeOptions)
13-
: _typeOptions(typeOptions) {
14-
13+
: _typeOptions(typeOptions) {
1514
}
1615

1716
void LuaTypeFormat::Analyze(std::string_view trigger,
@@ -191,16 +190,17 @@ void LuaTypeFormat::FormatLine(std::size_t line, std::size_t character, const Lu
191190
}
192191

193192
void LuaTypeFormat::FixIndent(std::size_t line, std::size_t character, const LuaSyntaxTree &t, LuaStyle &style) {
194-
195193
}
196194

197195
void LuaTypeFormat::FixEndIndent(std::size_t line, std::size_t character) {
198-
199196
}
200197

201198
void LuaTypeFormat::FormatByRange(FormatRange range, const LuaSyntaxTree &t, LuaStyle &style) {
202199
RangeFormatBuilder f(style, range);
203200
auto newText = f.GetFormatResult(t);
201+
if (newText.empty()) {
202+
return;
203+
}
204204
range = f.GetReplaceRange();
205205
auto &result = _results.emplace_back();
206206
result.Text = newText;

include/CodeService/Format/Analyzer/FormatDocAnalyze.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class FormatDocAnalyze : public FormatAnalyzer {
2424

2525
void AddIgnoreRange(const IndexRange& range, const LuaSyntaxTree &t);
2626

27+
std::vector<IndexRange> GetIgnores() const;
2728
private:
2829

2930
void AnalyzeDocFormat(LuaSyntaxNode n, FormatState &f, const LuaSyntaxTree &t);

include/CodeService/Format/FormatState.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ class FormatState {
6767
const FormatHandle &enterHandle
6868
);
6969

70+
void StopDfsForeach();
71+
7072
private:
7173
LuaStyle _formatStyle;
7274
LuaDiagnosticStyle _diagnosticStyle;
@@ -75,5 +77,6 @@ class FormatState {
7577
std::stack<IndentState> _indentStack;
7678
Mode _mode;
7779
IndexRange _ignoreRange;
80+
bool _foreachContinue;
7881
std::array<std::unique_ptr<FormatAnalyzer>, static_cast<std::size_t>(FormatAnalyzerType::Count)> _analyzers;
7982
};

0 commit comments

Comments
 (0)