Skip to content

Commit 7c982ce

Browse files
committed
FIX #96
1 parent 5248117 commit 7c982ce

File tree

19 files changed

+212
-63
lines changed

19 files changed

+212
-63
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/FormatDocAnalyze.cpp

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,26 @@
22
#include "LuaParser/Lexer/LuaTokenTypeDetail.h"
33
#include "Util/StringUtil.h"
44
#include "LuaParser/Lexer/TextReader.h"
5+
#include "CodeService/Format/FormatState.h"
56

67
FormatDocAnalyze::FormatDocAnalyze() {
78

89
}
910

1011
void FormatDocAnalyze::Analyze(FormatState &f, const LuaSyntaxTree &t) {
11-
// for (auto syntaxNode: t.GetSyntaxNodes()) {
12-
// switch (syntaxNode.GetTokenKind(t)) {
13-
// case TK_SHORT_COMMENT: {
14-
// if (syntaxNode.GetParent(t).GetSyntaxKind(t) == LuaSyntaxNodeKind::Block) {
15-
//
16-
//
17-
// break;
18-
// }
19-
// }
20-
// default: {
21-
//
22-
// }
23-
// }
24-
// }
12+
for (auto syntaxNode: t.GetSyntaxNodes()) {
13+
switch (syntaxNode.GetTokenKind(t)) {
14+
case TK_SHORT_COMMENT: {
15+
if (syntaxNode.GetParent(t).GetSyntaxKind(t) == LuaSyntaxNodeKind::Block) {
16+
AnalyzeDocFormat(syntaxNode, f, t);
17+
break;
18+
}
19+
}
20+
default: {
21+
break;
22+
}
23+
}
24+
}
2525
}
2626

2727
void FormatDocAnalyze::ComplexAnalyze(FormatState &f, const LuaSyntaxTree &t) {
@@ -30,7 +30,13 @@ void FormatDocAnalyze::ComplexAnalyze(FormatState &f, const LuaSyntaxTree &t) {
3030

3131
void
3232
FormatDocAnalyze::Query(FormatState &f, LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t, FormatResolve &resolve) {
33+
auto it = _ignores.find(syntaxNode.GetIndex());
34+
if (it == _ignores.end()) {
35+
return;
36+
}
3337

38+
f.AddIgnore(it->second);
39+
resolve.SetOriginRange(it->second);
3440
}
3541

3642
bool IsWhiteSpaces(int c) {
@@ -91,17 +97,28 @@ void FormatDocAnalyze::AnalyzeDocFormat(LuaSyntaxNode n, FormatState &f, const L
9197
textReader.EatWhile(ActionContinue);
9298
auto action = textReader.GetSaveText();
9399
if (action == "disable") {
94-
100+
auto block = n.GetParent(t);
101+
auto lastChild = block.GetLastToken(t);
102+
AddIgnoreRange(IndexRange(n.GetIndex(), lastChild.GetIndex()), t);
103+
break;
95104
} else if (action == "disable-next") {
105+
auto nextNode = n.GetNextSibling(t);
106+
while (!nextNode.IsNull(t)
107+
&& !detail::multi_match::StatementMatch(nextNode.GetSyntaxKind(t))) {
108+
nextNode.ToNext(t);
109+
}
110+
if (nextNode.IsNode(t)) {
111+
AddIgnoreRange(IndexRange(n.GetIndex(), nextNode.GetIndex()), t);
112+
}
96113

114+
break;
97115
} else if (action == "on") {
98116
state = ParseState::List;
99117
// todo
100118
break;
101119
} else if (action == "on-next") {
102120
state = ParseState::List;
103121
// todo
104-
105122
break;
106123
}
107124
}
@@ -114,3 +131,9 @@ void FormatDocAnalyze::AnalyzeDocFormat(LuaSyntaxNode n, FormatState &f, const L
114131
}
115132
}
116133
}
134+
135+
void FormatDocAnalyze::AddIgnoreRange(const IndexRange &range, const LuaSyntaxTree &t) {
136+
auto startIndex = LuaSyntaxNode(range.StartIndex).GetFirstToken(t).GetIndex();
137+
auto endIndex = LuaSyntaxNode(range.EndIndex).GetLastToken(t).GetIndex();
138+
_ignores[startIndex] = IndexRange(startIndex, endIndex);
139+
}

CodeService/src/Format/Analyzer/FormatResolve.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ void FormatResolve::Reset() {
3434
_indentStrategy = IndentStrategy::None;
3535
_nextSpaceData = NextData();
3636
_prevSpaceData = PrevData();
37+
_range = IndexRange();
3738
_indent = 0;
3839
}
3940

@@ -82,3 +83,12 @@ void FormatResolve::SetTokenStrategy(TokenStrategy strategy) {
8283
_tokenStrategy = strategy;
8384
}
8485

86+
void FormatResolve::SetOriginRange(IndexRange range) {
87+
_tokenStrategy = TokenStrategy::OriginRange;
88+
_range = range;
89+
}
90+
91+
IndexRange FormatResolve::GetOriginRange() const {
92+
return _range;
93+
}
94+

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: 31 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;
@@ -133,6 +133,26 @@ void FormatBuilder::DoResolve(LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t,
133133
} else {
134134
WriteChar(',');
135135
}
136+
break;
137+
}
138+
case TokenStrategy::OriginRange: {
139+
auto range = resolve.GetOriginRange();
140+
LuaSyntaxNode startNode(range.StartIndex);
141+
LuaSyntaxNode endNode(range.EndIndex);
142+
auto startOffset = startNode.GetTextRange(t).StartOffset;
143+
auto endOffset = endNode.GetTextRange(t).StartOffset;
144+
auto endLength = endNode.GetTextRange(t).Length;
145+
if (endOffset >= startOffset) {
146+
TextRange textRange(startOffset, endOffset - startOffset + endLength);
147+
auto text = t.GetFile().Slice(textRange);
148+
WriteText(text);
149+
auto nextToken = endNode.GetNextToken(t);
150+
if (nextToken.IsToken(t)) {
151+
auto line = nextToken.GetStartLine(t) - endNode.GetEndLine(t);
152+
WriteLine(line);
153+
}
154+
}
155+
return;
136156
}
137157
default: {
138158
break;
@@ -219,7 +239,7 @@ void FormatBuilder::WriteSpace(std::size_t space) {
219239
auto size = _formattedText.size();
220240
_formattedText.resize(size + space, ' ');
221241
}
222-
_state.GetCurrentWidth() += space;
242+
_state.CurrentWidth() += space;
223243
}
224244

225245
void FormatBuilder::WriteLine(std::size_t line) {
@@ -272,7 +292,7 @@ void FormatBuilder::WriteLine(std::size_t line) {
272292
break;
273293
}
274294
}
275-
_state.GetCurrentWidth() = 0;
295+
_state.CurrentWidth() = 0;
276296
}
277297

278298
void FormatBuilder::WriteIndent() {
@@ -297,11 +317,11 @@ void FormatBuilder::WriteIndent() {
297317
break;
298318
}
299319
}
300-
_state.GetCurrentWidth() += topLevelIndent.SpaceSize + topLevelIndent.TabSize * _state.GetStyle().tab_width;
320+
_state.CurrentWidth() += topLevelIndent.SpaceSize + topLevelIndent.TabSize * _state.GetStyle().tab_width;
301321
}
302322

303323
void FormatBuilder::WriteChar(char ch) {
304-
_state.GetCurrentWidth()++;
324+
_state.CurrentWidth()++;
305325
_formattedText.push_back(ch);
306326
}
307327

@@ -324,7 +344,7 @@ void FormatBuilder::WriteText(std::string_view text) {
324344
}
325345

326346
if (text.size() > last) {
327-
_state.GetCurrentWidth() += text.size() - last;
347+
_state.CurrentWidth() += text.size() - last;
328348
if (last != 0) {
329349
_formattedText.append(text.substr(last));
330350
} else {

CodeService/src/Format/FormatState.cpp

Lines changed: 23 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) {
@@ -128,6 +134,10 @@ IndentState FormatState::GetCurrentIndent() const {
128134
return _indentStack.top();
129135
}
130136

137+
void FormatState::AddIgnore(IndexRange range) {
138+
_ignoreRange = range;
139+
}
140+
131141
void FormatState::DfsForeach(std::vector<LuaSyntaxNode> &startNodes,
132142
const LuaSyntaxTree &t,
133143
const FormatState::FormatHandle &enterHandle) {
@@ -143,6 +153,12 @@ void FormatState::DfsForeach(std::vector<LuaSyntaxNode> &startNodes,
143153
resolve.Reset();
144154
if (traverse.Event == TraverseEvent::Enter) {
145155
traverseStack.back().Event = TraverseEvent::Exit;
156+
if (_ignoreRange.EndIndex != 0) {
157+
auto index = traverse.Node.GetIndex();
158+
if(index >= _ignoreRange.StartIndex && index <= _ignoreRange.EndIndex){
159+
continue;
160+
}
161+
}
146162
for (auto &analyzer: _analyzers) {
147163
analyzer->Query(*this, traverse.Node, t, resolve);
148164
}
@@ -206,3 +222,5 @@ void FormatState::AddAbsoluteIndent(LuaSyntaxNode &syntaxNoe, std::size_t indent
206222
}
207223
}
208224

225+
226+

0 commit comments

Comments
 (0)