Skip to content

Commit c95ed7b

Browse files
committed
fix #99
1 parent 5248117 commit c95ed7b

File tree

10 files changed

+61
-42
lines changed

10 files changed

+61
-42
lines changed

CodeService/src/Diagnostic/CodeStyle/CodeStyleChecker.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ void CodeStyleChecker::BasicResolve(LuaSyntaxNode syntaxNode, const LuaSyntaxTre
5757
DiagnosticBuilder &d) {
5858
if (syntaxNode.IsToken(t)) {
5959
auto textRange = syntaxNode.GetTextRange(t);
60-
auto prevToken = syntaxNode.GetPrevToken(t);
61-
if (prevToken.GetEndLine(t) != syntaxNode.GetStartLine(t) || prevToken.IsNull(t)) {
60+
if (d.GetState().IsNewLine(syntaxNode, t)) {
6261
ProcessIndentDiagnostic(syntaxNode, t, d);
6362
}
6463

CodeService/src/Diagnostic/DiagnosticBuilder.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
#include "CodeService/Diagnostic/CodeStyle/CodeStyleChecker.h"
66

77
DiagnosticBuilder::DiagnosticBuilder(LuaStyle &style, LuaDiagnosticStyle &diagnosticStyle)
8-
: _diagnosticStyle(diagnosticStyle) {
8+
: _diagnosticStyle(diagnosticStyle),
9+
_state(FormatState::Mode::Diagnostic) {
910
_state.SetFormatStyle(style);
1011
_state.SetDiagnosticStyle(diagnosticStyle);
1112
}

CodeService/src/Format/Analyzer/AlignAnalyzer.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,12 @@ AlignAnalyzer::ResolveAlignGroup(FormatState &f, std::size_t groupIndex, AlignGr
403403
break;
404404
}
405405
case AlignStrategy::AlignToFirst: {
406-
if (!f.IsNewLine()) {
407-
auto width = f.GetCurrentWidth();
406+
if(group.SyntaxGroup.empty()){
407+
return ;
408+
}
409+
LuaSyntaxNode firstNode(group.SyntaxGroup.front());
410+
if (!f.IsNewLine(firstNode, t)) {
411+
auto width = f.CurrentWidth();
408412
group.AlignPos = width;
409413
for (auto i: group.SyntaxGroup) {
410414
_resolveGroupIndex[i] = groupIndex;

CodeService/src/Format/Analyzer/IndentationAnalyzer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ IndentationAnalyzer::Query(FormatState &f, LuaSyntaxNode &n, const LuaSyntaxTree
185185
break;
186186
}
187187
case IndentType::WhenLineBreak: {
188-
if (f.IsNewLine()) {
188+
if (f.IsNewLine(n, t)) {
189189
resolve.SetIndent(indentData.Indent);
190190
}
191191
break;

CodeService/src/Format/Analyzer/LineBreakAnalyzer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ LineBreakAnalyzer::Query(FormatState &f, LuaSyntaxNode &syntaxNode, const LuaSyn
202202
break;
203203
}
204204
case LineBreakStrategy::WhenMayExceed: {
205-
auto lineWidth = f.GetCurrentWidth();
205+
auto lineWidth = f.CurrentWidth();
206206
auto &style = f.GetStyle();
207207
auto relationNode = LuaSyntaxNode(lineBreakData.Data.Index);
208208
auto guessLineWidth = lineWidth + syntaxNode.GetFirstLineWidth(t) + relationNode.GetFirstLineWidth(t);

CodeService/src/Format/FormatBuilder.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ void FormatBuilder::WriteSyntaxNode(LuaSyntaxNode &syntaxNode, const LuaSyntaxTr
3636
break;
3737
}
3838
default: {
39-
_state.GetCurrentWidth() += text.size();
39+
_state.CurrentWidth() += text.size();
4040
_formattedText.append(text);
4141
}
4242
}
4343
}
4444

4545
void FormatBuilder::DoResolve(LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t, FormatResolve &resolve) {
4646
if (syntaxNode.IsToken(t)) {
47-
if (_state.IsNewLine()) {
47+
if (_state.IsNewLine(syntaxNode, t)) {
4848
WriteIndent();
4949
auto indentAnalyzer = _state.GetAnalyzer<IndentationAnalyzer>();
5050
if (indentAnalyzer) {
@@ -55,8 +55,8 @@ void FormatBuilder::DoResolve(LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t,
5555
switch (resolve.GetPrevSpaceStrategy()) {
5656
case PrevSpaceStrategy::AlignPos: {
5757
auto pos = resolve.GetAlign();
58-
if (pos > _state.GetCurrentWidth()) {
59-
auto space = pos - _state.GetCurrentWidth();
58+
if (pos > _state.CurrentWidth()) {
59+
auto space = pos - _state.CurrentWidth();
6060
WriteSpace(space);
6161
}
6262
break;
@@ -65,8 +65,8 @@ void FormatBuilder::DoResolve(LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t,
6565
auto relativePos = resolve.GetAlign();
6666
auto indentState = _state.GetCurrentIndent();
6767
auto pos = relativePos + indentState.SpaceSize + indentState.TabSize * _state.GetStyle().tab_width;
68-
if (pos > _state.GetCurrentWidth()) {
69-
auto space = pos - _state.GetCurrentWidth();
68+
if (pos > _state.CurrentWidth()) {
69+
auto space = pos - _state.CurrentWidth();
7070
WriteSpace(space);
7171
}
7272
break;
@@ -219,7 +219,7 @@ void FormatBuilder::WriteSpace(std::size_t space) {
219219
auto size = _formattedText.size();
220220
_formattedText.resize(size + space, ' ');
221221
}
222-
_state.GetCurrentWidth() += space;
222+
_state.CurrentWidth() += space;
223223
}
224224

225225
void FormatBuilder::WriteLine(std::size_t line) {
@@ -272,7 +272,7 @@ void FormatBuilder::WriteLine(std::size_t line) {
272272
break;
273273
}
274274
}
275-
_state.GetCurrentWidth() = 0;
275+
_state.CurrentWidth() = 0;
276276
}
277277

278278
void FormatBuilder::WriteIndent() {
@@ -297,11 +297,11 @@ void FormatBuilder::WriteIndent() {
297297
break;
298298
}
299299
}
300-
_state.GetCurrentWidth() += topLevelIndent.SpaceSize + topLevelIndent.TabSize * _state.GetStyle().tab_width;
300+
_state.CurrentWidth() += topLevelIndent.SpaceSize + topLevelIndent.TabSize * _state.GetStyle().tab_width;
301301
}
302302

303303
void FormatBuilder::WriteChar(char ch) {
304-
_state.GetCurrentWidth()++;
304+
_state.CurrentWidth()++;
305305
_formattedText.push_back(ch);
306306
}
307307

@@ -324,7 +324,7 @@ void FormatBuilder::WriteText(std::string_view text) {
324324
}
325325

326326
if (text.size() > last) {
327-
_state.GetCurrentWidth() += text.size() - last;
327+
_state.CurrentWidth() += text.size() - last;
328328
if (last != 0) {
329329
_formattedText.append(text.substr(last));
330330
} else {

CodeService/src/Format/FormatState.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
#include "CodeService/Format/Analyzer/TokenAnalyzer.h"
77
#include "CodeService/Format/Analyzer/FormatDocAnalyze.h"
88

9-
FormatState::FormatState()
10-
: _currentWidth(0) {
9+
FormatState::FormatState(Mode mode)
10+
: _currentWidth(0),
11+
_mode(mode) {
1112
}
1213

13-
std::size_t &FormatState::GetCurrentWidth() {
14+
std::size_t &FormatState::CurrentWidth() {
1415
return _currentWidth;
1516
}
1617

@@ -35,8 +36,13 @@ const LuaDiagnosticStyle &FormatState::GetDiagnosticStyle() const {
3536
return _diagnosticStyle;
3637
}
3738

38-
bool FormatState::IsNewLine() const {
39-
return _currentWidth == 0;
39+
bool FormatState::IsNewLine(LuaSyntaxNode &n, const LuaSyntaxTree &t) const {
40+
if (_mode == Mode::Format) {
41+
return _currentWidth == 0;
42+
} else {
43+
auto prev = n.GetPrevToken(t);
44+
return prev.IsNull(t) || n.GetPrevToken(t).GetEndLine(t) != n.GetStartLine(t);
45+
}
4046
}
4147

4248
void FormatState::Analyze(const LuaSyntaxTree &t) {

CodeService/src/RangeFormat/RangeFormatBuilder.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void RangeFormatBuilder::WriteSyntaxNode(LuaSyntaxNode &syntaxNode, const LuaSyn
5353
break;
5454
}
5555
default: {
56-
_state.GetCurrentWidth() += text.size();
56+
_state.CurrentWidth() += text.size();
5757
}
5858
}
5959
}
@@ -63,15 +63,15 @@ void RangeFormatBuilder::WriteSpace(std::size_t space) {
6363
if (_validRange) {
6464
return FormatBuilder::WriteSpace(space);
6565
} else {
66-
_state.GetCurrentWidth() += space;
66+
_state.CurrentWidth() += space;
6767
}
6868
}
6969

7070
void RangeFormatBuilder::WriteLine(std::size_t line) {
7171
if (_validRange) {
7272
return FormatBuilder::WriteLine(line);
7373
} else {
74-
_state.GetCurrentWidth() = 0;
74+
_state.CurrentWidth() = 0;
7575
}
7676
}
7777

@@ -80,15 +80,15 @@ void RangeFormatBuilder::WriteIndent() {
8080
return FormatBuilder::WriteIndent();
8181
} else {
8282
auto topLevelIndent = _state.GetCurrentIndent();
83-
_state.GetCurrentWidth() += topLevelIndent.SpaceSize + topLevelIndent.TabSize * _state.GetStyle().tab_width;
83+
_state.CurrentWidth() += topLevelIndent.SpaceSize + topLevelIndent.TabSize * _state.GetStyle().tab_width;
8484
}
8585
}
8686

8787
void RangeFormatBuilder::WriteChar(char ch) {
8888
if (_validRange) {
8989
return FormatBuilder::WriteChar(ch);
9090
} else {
91-
_state.GetCurrentWidth()++;
91+
_state.CurrentWidth()++;
9292
}
9393
}
9494

@@ -100,7 +100,7 @@ void RangeFormatBuilder::WriteText(std::string_view text) {
100100
for (std::size_t i = 0; i != text.size(); i++) {
101101
char ch = text[i];
102102
if (ch == '\n' || ch == '\r') {
103-
_state.GetCurrentWidth() = 0;
103+
_state.CurrentWidth() = 0;
104104
if (ch == '\r'
105105
&& (i + 1 < text.size())
106106
&& (text[i + 1] == '\n')) {
@@ -111,7 +111,7 @@ void RangeFormatBuilder::WriteText(std::string_view text) {
111111
}
112112

113113
if (text.size() > last) {
114-
_state.GetCurrentWidth() += text.size() - last;
114+
_state.CurrentWidth() += text.size() - last;
115115
}
116116
}
117117
}

Test2/src/FormatTest2.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@
44
#include "LuaParser/Ast/LuaSyntaxTree.h"
55
#include "CodeService/Format/FormatBuilder.h"
66
#include "CodeService/Config/EditorconfigPattern.h"
7+
#include "CodeService/Diagnostic/DiagnosticBuilder.h"
78

89
int main() {
910
std::string buffer = R"(
10-
(
11-
aa
12-
).aa =123
13-
11+
local function f(a,
12+
b,
13+
c,
14+
d,
15+
e
16+
)
17+
return x
18+
end
1419
)";
1520

1621
auto file = std::make_shared<LuaFile>(std::move(buffer));
@@ -28,11 +33,10 @@ aa
2833
FormatBuilder b(s);
2934
auto text = b.GetFormatResult(t);
3035
std::cout<< text << std::endl;
31-
3236
// LuaDiagnosticStyle style;
33-
// StyleDiagnostic d(style);
34-
// b.Diagnostic(d, t);
35-
// for (auto &diag: d.GetDiagnosticResults()) {
37+
// DiagnosticBuilder dd(s, style);
38+
// dd.CodeStyleCheck(t);
39+
// for (auto &diag: dd.GetDiagnosticResults(t)) {
3640
// std::cout << diag.Message << std::endl;
3741
// }
3842
return 0;

include/CodeService/Format/FormatState.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@
88

99
class FormatState {
1010
public:
11+
enum class Mode {
12+
Format,
13+
Diagnostic
14+
};
15+
1116
using FormatHandle = std::function<void(LuaSyntaxNode &syntaxNode,
1217
const LuaSyntaxTree &t,
1318
FormatResolve &resolve)>;
1419

15-
FormatState();
20+
explicit FormatState(Mode mode = Mode::Format);
1621

1722
void SetFormatStyle(LuaStyle &style);
1823

@@ -24,9 +29,9 @@ class FormatState {
2429

2530
EndOfLine GetEndOfLine() const;
2631

27-
bool IsNewLine() const;
32+
bool IsNewLine(LuaSyntaxNode &n, const LuaSyntaxTree& t) const;
2833

29-
std::size_t &GetCurrentWidth();
34+
std::size_t &CurrentWidth();
3035

3136
void AddAbsoluteIndent(LuaSyntaxNode &syntaxNoe, std::size_t indent);
3237

@@ -66,6 +71,6 @@ class FormatState {
6671
EndOfLine _fileEndOfLine;
6772
std::size_t _currentWidth;
6873
std::stack<IndentState> _indentStack;
69-
74+
Mode _mode;
7075
std::array<std::unique_ptr<FormatAnalyzer>, static_cast<std::size_t>(FormatAnalyzerType::Count)> _analyzers;
7176
};

0 commit comments

Comments
 (0)