Skip to content

Commit

Permalink
Fix #186
Browse files Browse the repository at this point in the history
  • Loading branch information
CppCXY committed Sep 29, 2024
1 parent 81ef873 commit 4ab859f
Show file tree
Hide file tree
Showing 13 changed files with 130 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ enum class DiagnosticType {
Indent,
Semicolon,
NameStyle,
Spell
Spell,
Parentheses,
};

class LuaDiagnostic {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ class FormatResolve {

PrevSpaceStrategy GetPrevSpaceStrategy() const;

PrevTokenStrategy GetPrevTokenStrategy() const;

TokenStrategy GetTokenStrategy() const;

TokenAddStrategy GetTokenAddStrategy() const;
NextTokenStrategy GetNextTokenStrategy() const;

IndentStrategy GetIndentStrategy() const;

Expand All @@ -30,7 +32,9 @@ class FormatResolve {

void SetTokenStrategy(TokenStrategy strategy);

void SetTokenAddStrategy(TokenAddStrategy strategy);
void SetNextTokenStrategy(NextTokenStrategy strategy);

void SetPrevTokenStrategy(PrevTokenStrategy strategy);

std::size_t GetNextSpace();

Expand All @@ -49,8 +53,9 @@ class FormatResolve {
private:
NextSpaceStrategy _nextSpaceStrategy;
PrevSpaceStrategy _prevSpaceStrategy;
PrevTokenStrategy _prevTokenStrategy;
TokenStrategy _tokenStrategy;
TokenAddStrategy _tokenAddStrategy;
NextTokenStrategy _nextAddStrategy;
IndentStrategy _indentStrategy;

union NextData {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ enum class PrevSpaceStrategy {
LineBreak
};

enum class PrevTokenStrategy {
None,
LeftParentheses,

};

enum class TokenStrategy {
Origin,
Remove,
Expand All @@ -28,17 +34,18 @@ enum class TokenStrategy {
StmtEndSemicolon,
NewLineBeforeToken,

WithParentheses,
WithLeftParentheses,
WithRightParentheses,
// WithParentheses,
// WithLeftParentheses,
// WithRightParentheses,
SpaceAfterCommentDash
};

enum class TokenAddStrategy {
enum class NextTokenStrategy {
None,
TableAddColon,
TableAddComma,
StmtEndSemicolon,
RightParentheses,
};

enum class IndentStrategy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ class TokenAnalyzer : public FormatAnalyzer {

void Query(FormatState &f, LuaSyntaxNode syntaxNode, const LuaSyntaxTree &t, FormatResolve &resolve) override;

void MarkPrev(LuaSyntaxNode n, const LuaSyntaxTree &t, PrevTokenStrategy strategy);

void Mark(LuaSyntaxNode n, const LuaSyntaxTree &t, TokenStrategy strategy);

void MarkAdd(LuaSyntaxNode n, const LuaSyntaxTree &t, TokenAddStrategy strategy);
void MarkNext(LuaSyntaxNode n, const LuaSyntaxTree &t, NextTokenStrategy strategy);

bool IsRemove(LuaSyntaxNode n, const LuaSyntaxTree &t) const;

Expand All @@ -29,5 +31,6 @@ class TokenAnalyzer : public FormatAnalyzer {
void AnalyzeComment(FormatState &f, LuaSyntaxNode n, const LuaSyntaxTree &t);

std::unordered_map<std::size_t, TokenStrategy> _tokenStrategies;
std::unordered_map<std::size_t, TokenAddStrategy> _tokenAddStrategies;
std::unordered_map<std::size_t, NextTokenStrategy> _nextTokenStrategies;
std::unordered_map<std::size_t, PrevTokenStrategy> _prevTokenStrategies;
};
22 changes: 19 additions & 3 deletions CodeFormatCore/src/Diagnostic/CodeStyle/CodeStyleChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ void CodeStyleChecker::BasicResolve(LuaSyntaxNode syntaxNode, const LuaSyntaxTre
}
}

switch (resolve.GetPrevTokenStrategy()) {
case PrevTokenStrategy::LeftParentheses: {
d.PushDiagnostic(DiagnosticType::Parentheses, textRange,
LText("expected ("));
break;
}
default: {
break;
}
}

switch (resolve.GetTokenStrategy()) {
case TokenStrategy::StringSingleQuote: {
if (syntaxNode.GetTokenKind(t) == TK_STRING) {
Expand Down Expand Up @@ -117,13 +128,18 @@ void CodeStyleChecker::BasicResolve(LuaSyntaxNode syntaxNode, const LuaSyntaxTre
}
}

switch (resolve.GetTokenAddStrategy()) {
case TokenAddStrategy::StmtEndSemicolon: {
switch (resolve.GetNextTokenStrategy()) {
case NextTokenStrategy::StmtEndSemicolon: {
d.PushDiagnostic(DiagnosticType::Semicolon,
TextRange(textRange.GetEndOffset(), 1),
LText("expected ; at end of statement"));
break;
}
case NextTokenStrategy::RightParentheses: {
d.PushDiagnostic(DiagnosticType::Parentheses, textRange,
LText("expected )"));
break;
}
default: {
break;
}
Expand Down Expand Up @@ -271,7 +287,7 @@ void CodeStyleChecker::ProcessIndentDiagnostic(LuaSyntaxNode &node, const LuaSyn
// if(state.GetStyle().indent_style)
d.PushDiagnostic(DiagnosticType::Indent,
currentIndentRange,
GetIndentNote(indent,checkIndent.Space, checkIndent.Tab, state.GetStyle().indent_style));
GetIndentNote(indent, checkIndent.Space, checkIndent.Tab, state.GetStyle().indent_style));
}
}

Expand Down
24 changes: 17 additions & 7 deletions CodeFormatCore/src/Format/Analyzer/FormatResolve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ FormatResolve::FormatResolve()
: _nextSpaceStrategy(NextSpaceStrategy::None),
_prevSpaceStrategy(PrevSpaceStrategy::None),
_tokenStrategy(TokenStrategy::Origin),
_tokenAddStrategy(TokenAddStrategy::None),
_nextAddStrategy(NextTokenStrategy::None),
_indentStrategy(IndentStrategy::None),
_nextSpaceData(),
_prevSpaceData(),
Expand All @@ -19,23 +19,29 @@ PrevSpaceStrategy FormatResolve::GetPrevSpaceStrategy() const {
return _prevSpaceStrategy;
}

PrevTokenStrategy FormatResolve::GetPrevTokenStrategy() const {
return _prevTokenStrategy;
}

TokenStrategy FormatResolve::GetTokenStrategy() const {
return _tokenStrategy;
}

TokenAddStrategy FormatResolve::GetTokenAddStrategy() const {
return _tokenAddStrategy;
NextTokenStrategy FormatResolve::GetNextTokenStrategy() const {
return _nextAddStrategy;
}

IndentStrategy FormatResolve::GetIndentStrategy() const {
return _indentStrategy;
}

void FormatResolve::Reset() {
_prevSpaceStrategy = PrevSpaceStrategy::None;
_prevTokenStrategy = PrevTokenStrategy::None;
_tokenStrategy = TokenStrategy::Origin;
_tokenAddStrategy = TokenAddStrategy::None;
_nextAddStrategy = NextTokenStrategy::None;
_nextSpaceStrategy = NextSpaceStrategy::None;
_prevSpaceStrategy = PrevSpaceStrategy::None;

_indentStrategy = IndentStrategy::None;
_nextSpaceData = NextData();
_prevSpaceData = PrevData();
Expand Down Expand Up @@ -88,8 +94,12 @@ void FormatResolve::SetTokenStrategy(TokenStrategy strategy) {
_tokenStrategy = strategy;
}

void FormatResolve::SetTokenAddStrategy(TokenAddStrategy strategy) {
_tokenAddStrategy = strategy;
void FormatResolve::SetPrevTokenStrategy(PrevTokenStrategy strategy) {
_prevTokenStrategy = strategy;
}

void FormatResolve::SetNextTokenStrategy(NextTokenStrategy strategy) {
_nextAddStrategy = strategy;
}

void FormatResolve::SetOriginRange(IndexRange range) {
Expand Down
2 changes: 1 addition & 1 deletion CodeFormatCore/src/Format/Analyzer/SemicolonAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void SemicolonAnalyzer::Query(FormatState &f, LuaSyntaxNode n, const LuaSyntaxTr
auto &strategy = it->second;
switch (strategy) {
case SemicolonStrategy::Add: {
resolve.SetTokenAddStrategy(TokenAddStrategy::StmtEndSemicolon);
resolve.SetNextTokenStrategy(NextTokenStrategy::StmtEndSemicolon);
break;
}
case SemicolonStrategy::Remove: {
Expand Down
38 changes: 26 additions & 12 deletions CodeFormatCore/src/Format/Analyzer/TokenAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,33 @@ void TokenAnalyzer::Analyze(FormatState &f, const LuaSyntaxTree &t) {

void TokenAnalyzer::Query(FormatState &f, LuaSyntaxNode syntaxNode, const LuaSyntaxTree &t, FormatResolve &resolve) {
if (syntaxNode.IsToken(t)) {
auto itPrev = _prevTokenStrategies.find(syntaxNode.GetIndex());
if (itPrev != _prevTokenStrategies.end()) {
resolve.SetPrevTokenStrategy(itPrev->second);
}

auto it = _tokenStrategies.find(syntaxNode.GetIndex());
if (it != _tokenStrategies.end()) {
resolve.SetTokenStrategy(it->second);
}

auto it2 = _tokenAddStrategies.find(syntaxNode.GetIndex());
if (it2 != _tokenAddStrategies.end()) {
resolve.SetTokenAddStrategy(it2->second);
auto itNext = _nextTokenStrategies.find(syntaxNode.GetIndex());
if (itNext != _nextTokenStrategies.end()) {
resolve.SetNextTokenStrategy(itNext->second);
}
}
}

void TokenAnalyzer::MarkPrev(LuaSyntaxNode n, const LuaSyntaxTree &t, PrevTokenStrategy strategy) {
_prevTokenStrategies[n.GetIndex()] = strategy;
}

void TokenAnalyzer::Mark(LuaSyntaxNode n, const LuaSyntaxTree &t, TokenStrategy strategy) {
_tokenStrategies[n.GetIndex()] = strategy;
}

void TokenAnalyzer::MarkAdd(LuaSyntaxNode n, const LuaSyntaxTree &t, TokenAddStrategy strategy) {
_tokenAddStrategies[n.GetIndex()] = strategy;
void TokenAnalyzer::MarkNext(LuaSyntaxNode n, const LuaSyntaxTree &t, NextTokenStrategy strategy) {
_nextTokenStrategies[n.GetIndex()] = strategy;
}

bool TokenAnalyzer::IsRemove(LuaSyntaxNode n, const LuaSyntaxTree &t) const {
Expand All @@ -101,16 +110,16 @@ void TokenAnalyzer::TableFieldAddSep(FormatState &f, LuaSyntaxNode n, const LuaS
}
switch (f.GetStyle().table_separator_style) {
case TableSeparatorStyle::Semicolon: {
return MarkAdd(lastToken, t, TokenAddStrategy::TableAddColon);
return MarkNext(lastToken, t, NextTokenStrategy::TableAddColon);
}
case TableSeparatorStyle::OnlyKVColon: {
if (n.GetChildToken('=', t).IsToken(t)) {
return MarkAdd(lastToken, t, TokenAddStrategy::TableAddColon);
return MarkNext(lastToken, t, NextTokenStrategy::TableAddColon);
}
// fallthrough
}
default: {
return MarkAdd(lastToken, t, TokenAddStrategy::TableAddComma);
return MarkNext(lastToken, t, NextTokenStrategy::TableAddComma);
}
}
}
Expand Down Expand Up @@ -270,15 +279,18 @@ void TokenAnalyzer::AnalyzeCallExpression(FormatState &f, LuaSyntaxNode n, const
if (!lbrace.IsToken(t) && spaceAnalyzer) {
auto node = GetSingleArgStringOrTable(n, t);
if (node.IsToken(t)) {
Mark(node, t, TokenStrategy::WithParentheses);
MarkPrev(node, t, PrevTokenStrategy::LeftParentheses);
MarkNext(node, t, NextTokenStrategy::RightParentheses);
spaceAnalyzer->SpaceAround(node, t, 0, SpaceAnalyzer::SpacePriority::First);
} else if (node.GetSyntaxKind(t) == LuaSyntaxNodeKind::StringLiteralExpression) {
Mark(node.GetFirstToken(t), t, TokenStrategy::WithParentheses);
auto firstToken = node.GetFirstToken(t);
MarkPrev(firstToken, t, PrevTokenStrategy::LeftParentheses);
MarkNext(firstToken, t, NextTokenStrategy::RightParentheses);
spaceAnalyzer->SpaceLeft(node.GetFirstToken(t), t, 0, SpaceAnalyzer::SpacePriority::First);
} else {
Mark(node.GetFirstToken(t), t, TokenStrategy::WithLeftParentheses);
MarkPrev(node.GetFirstToken(t), t, PrevTokenStrategy::LeftParentheses);
MarkNext(node.GetLastToken(t), t, NextTokenStrategy::RightParentheses);
spaceAnalyzer->SpaceLeft(node.GetFirstToken(t), t, 0, SpaceAnalyzer::SpacePriority::First);
Mark(node.GetLastToken(t), t, TokenStrategy::WithRightParentheses);
}

return;
Expand Down Expand Up @@ -310,3 +322,5 @@ void TokenAnalyzer::AnalyzeComment(FormatState &f, LuaSyntaxNode n, const LuaSyn
}
}
}


38 changes: 18 additions & 20 deletions CodeFormatCore/src/Format/FormatBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ void FormatBuilder::DoResolve(LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t,
}
}

switch (resolve.GetPrevTokenStrategy()) {
case PrevTokenStrategy::LeftParentheses: {
WriteChar('(');
break;
}
default: {
break;
}
}

switch (resolve.GetTokenStrategy()) {
case TokenStrategy::Origin: {
WriteSyntaxNode(syntaxNode, t);
Expand Down Expand Up @@ -146,22 +156,6 @@ void FormatBuilder::DoResolve(LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t,
WriteSyntaxNode(syntaxNode, t);
break;
}
case TokenStrategy::WithParentheses: {
WriteChar('(');
WriteSyntaxNode(syntaxNode, t);
WriteChar(')');
break;
}
case TokenStrategy::WithLeftParentheses:{
WriteChar('(');
WriteSyntaxNode(syntaxNode, t);
break;
}
case TokenStrategy::WithRightParentheses: {
WriteSyntaxNode(syntaxNode, t);
WriteChar(')');
break;
}
case TokenStrategy::SpaceAfterCommentDash: {
auto text = syntaxNode.GetText(t);
std::size_t pos = 0;
Expand All @@ -178,19 +172,23 @@ void FormatBuilder::DoResolve(LuaSyntaxNode &syntaxNode, const LuaSyntaxTree &t,
}
}

switch (resolve.GetTokenAddStrategy()) {
case TokenAddStrategy::TableAddComma: {
switch (resolve.GetNextTokenStrategy()) {
case NextTokenStrategy::TableAddComma: {
WriteChar(',');
break;
}
case TokenAddStrategy::TableAddColon: {
case NextTokenStrategy::TableAddColon: {
WriteChar(';');
break;
}
case TokenAddStrategy::StmtEndSemicolon: {
case NextTokenStrategy::StmtEndSemicolon: {
WriteChar(';');
break;
}
case NextTokenStrategy::RightParentheses: {
WriteChar(')');
break;
}
default: {
break;
}
Expand Down
2 changes: 2 additions & 0 deletions CodeFormatLib/src/CodeFormatLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ std::string GetDiagnosisString(DiagnosticType type) {
return "string-quote";
case DiagnosticType::Semicolon:
return "semicolon";
case DiagnosticType::Parentheses:
return "parentheses";
default: {
break;
}
Expand Down
1 change: 1 addition & 0 deletions CodeFormatServer/src/Service/CodeActionService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ bool CodeActionService::Initialize() {
CodeProtocol(DiagnosticType::Indent, "code-indent", &CodeActionService::Reformat);
CodeProtocol(DiagnosticType::Space, "whitespace", &CodeActionService::Reformat);
CodeProtocol(DiagnosticType::StringQuote, "string-quote", &CodeActionService::Reformat);
CodeProtocol(DiagnosticType::Parentheses, "parentheses", &CodeActionService::Reformat);
CodeProtocol(DiagnosticType::EndWithNewLine, "end-with-new-line");
CodeProtocol(DiagnosticType::NameStyle, "name-style");
CodeProtocol(DiagnosticType::Semicolon, "semicolon");
Expand Down
Loading

0 comments on commit 4ab859f

Please sign in to comment.