Skip to content

Commit

Permalink
quote_style option, resolve #17
Browse files Browse the repository at this point in the history
  • Loading branch information
CppCXY committed Feb 18, 2022
1 parent 0e21e3c commit 6606b51
Show file tree
Hide file tree
Showing 28 changed files with 319 additions and 49 deletions.
2 changes: 1 addition & 1 deletion CodeFormatServer/src/Protocol/ProtocolParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ std::string ProtocolParser::SerializeProtocol(std::shared_ptr<vscode::Serializab

json["result"] = result->Serialize();
json["jsonrpc"] = "2.0";
auto dumpResult = json.dump();
auto dumpResult = json.dump(-1,' ', true, nlohmann::detail::error_handler_t::ignore);
std::string message = format("Content-Length:{}\r\n\r\n", dumpResult.size());

message.append(dumpResult);
Expand Down
1 change: 1 addition & 0 deletions CodeService/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ target_sources(CodeService
${CodeService_SOURCE_DIR}/src/FormatElement/MaxSpaceElement.cpp
${CodeService_SOURCE_DIR}/src/FormatElement/AlignmentElement.cpp
${CodeService_SOURCE_DIR}/src/FormatElement/ExpressionElement.cpp
${CodeService_SOURCE_DIR}/src/FormatElement/StringLiteralElement.cpp
${CodeService_SOURCE_DIR}/src/FormatElement/FormatContext.cpp
${CodeService_SOURCE_DIR}/src/FormatElement/SerializeContext.cpp
${CodeService_SOURCE_DIR}/src/FormatElement/AlignmentLayoutElement.cpp
Expand Down
66 changes: 58 additions & 8 deletions CodeService/src/FormatElement/RangeFormatContext.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "CodeService/FormatElement/RangeFormatContext.h"
#include "CodeService/FormatElement/TextElement.h"
#include <string_view>


RangeFormatContext::RangeFormatContext(std::shared_ptr<LuaParser> parser, LuaCodeStyleOptions& options,
LuaFormatRange validRange)
Expand All @@ -12,15 +11,15 @@ RangeFormatContext::RangeFormatContext(std::shared_ptr<LuaParser> parser, LuaCod
{
}

void RangeFormatContext::Print(TextElement& textElement)
void RangeFormatContext::Print(std::string_view text, TextRange range)
{
int startOffset = textElement.GetTextRange().StartOffset;
int startOffset = range.StartOffset;
int startLine = _parser->GetLine(startOffset);
int endLine = _parser->GetLine(textElement.GetTextRange().EndOffset);
int endLine = _parser->GetLine(range.EndOffset);
if (startLine > _validRange.EndLine || endLine < _validRange.StartLine)
{
_inValidRange = false;
_characterCount += textElement.GetText().size();
_characterCount += text.size();
return;
}

Expand Down Expand Up @@ -49,8 +48,8 @@ void RangeFormatContext::Print(TextElement& textElement)
}
}
}
_buffer.append(textElement.GetText());
_characterCount += textElement.GetText().size();
_buffer.append(text);
_characterCount += text.size();

if (startLine < _formattedRange.StartLine)
{
Expand All @@ -64,6 +63,57 @@ void RangeFormatContext::Print(TextElement& textElement)
}
}

void RangeFormatContext::Print(char ch, int offset)
{
int startLine = _parser->GetLine(offset);
int endLine = _parser->GetLine(offset);
if (startLine > _validRange.EndLine || endLine < _validRange.StartLine)
{
_inValidRange = false;
_characterCount += 1;
return;
}

_inValidRange = true;
if (!_indentStack.empty())
{
auto& indentState = _indentStack.back();
if (indentState.Style == IndentStyle::Space)
{
if (_characterCount < indentState.SpaceIndent)
{
PrintIndent(indentState.SpaceIndent - _characterCount, indentState.Style);
}
}
else
{
if (_characterCount == 0)
{
PrintIndent(indentState.TabIndent, indentState.Style);
PrintIndent(indentState.SpaceIndent, IndentStyle::Space);
}
else if (_characterCount >= indentState.TabIndent && (indentState.SpaceIndent + indentState.TabIndent >
_characterCount))
{
PrintIndent(indentState.SpaceIndent - (_characterCount - indentState.TabIndent), IndentStyle::Space);
}
}
}
_buffer.push_back(ch);
_characterCount++;

if (startLine < _formattedRange.StartLine)
{
_formattedRange.StartLine = startLine;
_formattedRange.StartCharacter = _parser->GetColumn(offset);
}

if (endLine > _formattedRange.EndLine)
{
_formattedRange.EndLine = endLine;
}
}

void RangeFormatContext::PrintBlank(int blank)
{
for (int i = 0; i < blank; i++)
Expand Down
37 changes: 33 additions & 4 deletions CodeService/src/FormatElement/SerializeContext.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#include "CodeService/FormatElement/SerializeContext.h"
#include "CodeService/FormatElement/TextElement.h"

SerializeContext::SerializeContext(std::shared_ptr<LuaParser> parser, LuaCodeStyleOptions& options)
: FormatContext(parser, options)
{
}

void SerializeContext::Print(TextElement& textElement)
void SerializeContext::Print(std::string_view text, TextRange range)
{
if (!_indentStack.empty())
{
Expand All @@ -32,8 +31,38 @@ void SerializeContext::Print(TextElement& textElement)
}
}
}
_buffer.append(textElement.GetText());
_characterCount += textElement.GetText().size();
_buffer.append(text);
_characterCount += text.size();
}

void SerializeContext::Print(char ch, int Offset)
{
if (!_indentStack.empty())
{
auto& indentState = _indentStack.back();
if (indentState.Style == IndentStyle::Space)
{
if (_characterCount < indentState.SpaceIndent)
{
PrintIndent(indentState.SpaceIndent - _characterCount, indentState.Style);
}
}
else
{
if (_characterCount == 0)
{
PrintIndent(indentState.TabIndent, indentState.Style);
PrintIndent(indentState.SpaceIndent, IndentStyle::Space);
}
else if (_characterCount >= indentState.TabIndent && (indentState.SpaceIndent + indentState.TabIndent >
_characterCount))
{
PrintIndent(indentState.SpaceIndent - (_characterCount - indentState.TabIndent), IndentStyle::Space);
}
}
}
_buffer.push_back(ch);
_characterCount++;
}

void SerializeContext::PrintLine(int line)
Expand Down
69 changes: 69 additions & 0 deletions CodeService/src/FormatElement/StringLiteralElement.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include "CodeService/FormatElement/StringLiteralElement.h"

StringLiteralElement::StringLiteralElement(std::shared_ptr<LuaAstNode> node)
: FormatElement(node->GetTextRange()),
_text(node->GetText())
{
}

FormatElementType StringLiteralElement::GetType()
{
return FormatElementType::StringLiteralElement;
}

void StringLiteralElement::Serialize(SerializeContext& ctx, ChildIterator, FormatElement& parent)
{
auto quoteStyle = ctx.GetOptions().quote_style;

if (quoteStyle != QuoteStyle::None
&& !_text.empty()
&& (_text[0] == '\'' || _text[0] == '\"'))
{
char del = quoteStyle == QuoteStyle::Double ? '\"' : '\'';

if (_text.size() >= 2 && !ExistDel(del))
{
std::string_view text = _text.substr(1, _text.size() - 2);
auto range = GetTextRange();

ctx.Print(del, range.StartOffset);

ctx.Print(text, TextRange(range.StartOffset + 1, range.EndOffset - 1));

ctx.Print(del, range.EndOffset);
return;
}
}

return ctx.Print(_text, GetTextRange());
}

void StringLiteralElement::Diagnosis(DiagnosisContext& ctx, ChildIterator, FormatElement& parent)
{
//if necessary , add diagnosis
}

std::string_view StringLiteralElement::GetText() const
{
return _text;
}

bool StringLiteralElement::ExistDel(char del)
{
auto text = _text.substr(1, _text.size() - 2);
char ch = '\0';
for (std::size_t i = 0; i < text.size(); ++i)
{
ch = text[i];
if(ch == del)
{
return true;
}
else if(ch == '\\')
{
++i;
}
}

return false;
}
16 changes: 5 additions & 11 deletions CodeService/src/FormatElement/TextElement.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
#include "CodeService/FormatElement/TextElement.h"
#include "Util/format.h"

TextElement::TextElement(std::string_view text, TextDefineType textDefineType, TextRange range)
TextElement::TextElement(std::string_view text, TextRange range)
: FormatElement(range),
_text(text),
_textDefineType(textDefineType)
_text(text)
{
}

TextElement::TextElement(std::shared_ptr<LuaAstNode> node, TextDefineType textDefineType)
: TextElement(node->GetText(), textDefineType, node->GetTextRange())
TextElement::TextElement(std::shared_ptr<LuaAstNode> node)
: TextElement(node->GetText(), node->GetTextRange())
{
}

Expand All @@ -20,7 +19,7 @@ FormatElementType TextElement::GetType()

void TextElement::Serialize(SerializeContext& ctx, ChildIterator selfIt, FormatElement& parent)
{
ctx.Print(*this);
ctx.Print(_text, GetTextRange());
}

void TextElement::Diagnosis(DiagnosisContext& ctx, ChildIterator selfIt, FormatElement& parent)
Expand All @@ -39,8 +38,3 @@ std::string_view TextElement::GetText() const
{
return _text;
}

void TextElement::SetTextDefineType(TextDefineType textDefineType)
{
_textDefineType = textDefineType;
}
12 changes: 12 additions & 0 deletions CodeService/src/LuaEditorConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,17 @@ void LuaEditorConfig::ParseFromSection(std::shared_ptr<LuaCodeStyleOptions> opti
options->tab_width = std::stoi(configMap.at("tab_width"));
}

if (configMap.count("quote_style"))
{
if (configMap.at("quote_style") == "single") {
options->quote_style = QuoteStyle::Single;
}
else if(configMap.at("quote_style") == "double")
{
options->quote_style = QuoteStyle::Double;
}
}

if (configMap.count("continuation_indent_size")
&& isNumber(configMap.at("continuation_indent_size")))
{
Expand Down Expand Up @@ -296,6 +307,7 @@ void LuaEditorConfig::ParseFromSection(std::shared_ptr<LuaCodeStyleOptions> opti
configMap.at("long_chain_expression_allow_one_space_after_colon") == "true";
}


if (configMap.count("end_of_line"))
{
auto lineSeparatorSymbol = configMap.at("end_of_line");
Expand Down
17 changes: 14 additions & 3 deletions CodeService/src/LuaFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "CodeService/FormatElement/PlaceholderElement.h"
#include "CodeService/FormatElement/AlignIfLayoutElement.h"
#include "CodeService/FormatElement/MaxSpaceElement.h"
#include "CodeService/FormatElement/StringLiteralElement.h"
#include "Util/StringUtil.h"

bool nextMatch(LuaAstNode::ChildIterator it, LuaAstNodeType type, const LuaAstNode::ChildrenContainer& container)
Expand Down Expand Up @@ -249,10 +250,14 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatNode(std::shared_ptr<LuaAstNo
{
return FormatFunctionBody(node);
}
case LuaAstNodeType::StringLiteralExpression:
{
return FormatStringLiteralExpression(node);
}
case LuaAstNodeType::LiteralExpression:
default:
{
return std::make_shared<TextElement>(node->GetText(), TextDefineType::Normal, node->GetTextRange());
return std::make_shared<TextElement>(node->GetText(), node->GetTextRange());
}
}
}
Expand Down Expand Up @@ -1379,7 +1384,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatLocalFunctionStatement(
}
case LuaAstNodeType::Identify:
{
env->Add<TextElement>(child, TextDefineType::FunctionNameDefine);
env->Add<TextElement>(child);
break;
}
case LuaAstNodeType::FunctionBody:
Expand Down Expand Up @@ -1508,7 +1513,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatTableField(std::shared_ptr<Lu
}
case LuaAstNodeType::Identify:
{
env->Add<TextElement>(child, TextDefineType::TableFieldNameDefine);
env->Add<TextElement>(child);
break;
}
case LuaAstNodeType::Comment:
Expand Down Expand Up @@ -1556,6 +1561,12 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatTableField(std::shared_ptr<Lu
return env;
}

std::shared_ptr<FormatElement> LuaFormatter::FormatStringLiteralExpression(
std::shared_ptr<LuaAstNode> stringLiteralExpression)
{
return std::make_shared<StringLiteralElement>(stringLiteralExpression);
}

void LuaFormatter::DefaultHandle(std::shared_ptr<LuaAstNode> node, std::shared_ptr<FormatElement> envElement)
{
auto childEnv = FormatNode(node);
Expand Down
4 changes: 2 additions & 2 deletions CodeService/src/NameStyle/NameStyleRuleMatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ bool NameStyleRuleMatcher::Same(DiagnosisContext& ctx, std::shared_ptr<CheckElem
return true;
}

auto firstParamNode = checkElement->ExtraInfoNode->FindFirstOf(LuaAstNodeType::LiteralExpression);
auto firstParamNode = checkElement->ExtraInfoNode->FindFirstOf(LuaAstNodeType::StringLiteralExpression);

if (!firstParamNode)
{
Expand All @@ -370,7 +370,7 @@ bool NameStyleRuleMatcher::Same(DiagnosisContext& ctx, std::shared_ptr<CheckElem
{
return true;
}
firstParamNode = expressionNode->FindFirstOf(LuaAstNodeType::LiteralExpression);
firstParamNode = expressionNode->FindFirstOf(LuaAstNodeType::StringLiteralExpression);
if (!firstParamNode)
{
return true;
Expand Down
Loading

0 comments on commit 6606b51

Please sign in to comment.