Skip to content

Commit f45acc1

Browse files
SemicolonAnalyzer: don't reformat if there's nothing to do
1 parent 1f15e10 commit f45acc1

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

CodeFormatCore/include/CodeFormatCore/Format/Analyzer/SemicolonAnalyzer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class SemicolonAnalyzer : public FormatAnalyzer {
2222
bool IsFirstStmtOfLine(LuaSyntaxNode n, const LuaSyntaxTree &t);
2323
bool IsLastStmtOfLine(LuaSyntaxNode n, const LuaSyntaxTree &t);
2424
bool EndsWithSemicolon(LuaSyntaxNode n, const LuaSyntaxTree &t);
25+
bool ContainsSemicolon(LuaSyntaxNode n, const LuaSyntaxTree& t);
2526
LuaSyntaxNode GetLastNonCommentToken(LuaSyntaxNode n, const LuaSyntaxTree &t);
2627

2728
std::unordered_map<std::size_t, SemicolonStrategy> _semicolon;

CodeFormatCore/src/Format/Analyzer/SemicolonAnalyzer.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "CodeFormatCore/Config/LuaStyleEnum.h"
33
#include "CodeFormatCore/Format/FormatState.h"
44
#include "LuaParser/Lexer/LuaTokenTypeDetail.h"
5+
#include <iostream>
56

67
SemicolonAnalyzer::SemicolonAnalyzer() {
78
}
@@ -25,12 +26,15 @@ void SemicolonAnalyzer::Analyze(FormatState &f, const LuaSyntaxTree &t) {
2526
break;
2627
}
2728
case EndStmtWithSemicolon::Never: {
28-
// is on same line as other statement -> needs to go on new line
29-
if (!IsFirstStmtOfLine(syntaxNode, t)) {
30-
InsertNewLineBeforeNode(syntaxNode, t);
31-
}
32-
if (EndsWithSemicolon(syntaxNode, t)) {
33-
RemoveSemicolon(syntaxNode, t);
29+
// no action needed when there's no semicolons at all!
30+
if (ContainsSemicolon(syntaxNode, t)) {
31+
// is on same line as other statement -> needs to go on new line
32+
if (!IsFirstStmtOfLine(syntaxNode, t)) {
33+
InsertNewLineBeforeNode(syntaxNode, t);
34+
}
35+
if (EndsWithSemicolon(syntaxNode, t)) {
36+
RemoveSemicolon(syntaxNode, t);
37+
}
3438
}
3539
break;
3640
}
@@ -83,6 +87,8 @@ void SemicolonAnalyzer::AddSemicolon(LuaSyntaxNode n, const LuaSyntaxTree &t) {
8387
}
8488

8589
void SemicolonAnalyzer::InsertNewLineBeforeNode(LuaSyntaxNode n, const LuaSyntaxTree &t) {
90+
std::cout << "Called InsertNewLineBeforeNode" << std::endl;
91+
std::cout << n.GetText(t) << std::endl;
8692
auto token = n.GetFirstToken(t); // line breaks are put in front of the statement itself by non-first statements
8793
if (token.IsToken(t)) {
8894
_semicolon[token.GetIndex()] = SemicolonStrategy::InsertNewLine;
@@ -123,6 +129,10 @@ bool SemicolonAnalyzer::EndsWithSemicolon(LuaSyntaxNode n, const LuaSyntaxTree &
123129
return token.GetTokenKind(t) == ';';
124130
}
125131

132+
bool SemicolonAnalyzer::ContainsSemicolon(LuaSyntaxNode n, const LuaSyntaxTree& t) {
133+
return n.GetChildTokens(';', t).size() > 0;
134+
}
135+
126136
LuaSyntaxNode SemicolonAnalyzer::GetLastNonCommentToken(LuaSyntaxNode n, const LuaSyntaxTree &t) {
127137
auto token = n.GetLastToken(t);
128138
switch (token.GetTokenKind(t)) {

0 commit comments

Comments
 (0)