Skip to content

Commit

Permalink
修复bug
Browse files Browse the repository at this point in the history
  • Loading branch information
CppCXY committed Nov 25, 2021
1 parent fe08f75 commit 2edcdad
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 64 deletions.
14 changes: 5 additions & 9 deletions CodeFormatServer/src/LanguageClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ void LanguageClient::SendNotification(std::string_view method, std::shared_ptr<v
}
}

void LanguageClient::CacheFile(std::string_view uri, std::string text)
void LanguageClient::CacheFile(std::string_view uri, std::shared_ptr<LuaParser> parser)
{
_fileMap[std::string(uri)] = std::move(text);
_fileMap[std::string(uri)] = parser;
}

void LanguageClient::ReleaseFile(std::string_view uri)
Expand Down Expand Up @@ -73,17 +73,13 @@ void LanguageClient::DiagnosticFile(std::string_view uri)
return;
}

std::string text = it->second;

std::shared_ptr<LuaParser> parser = LuaParser::LoadFromBuffer(std::move(text));
std::shared_ptr<LuaParser> parser = it->second;

if (parser->HasError())
{
return;
}

parser->BuildAstWithComment();

LuaFormatter formatter(parser, *options);
formatter.BuildFormattedElement();

Expand Down Expand Up @@ -111,15 +107,15 @@ void LanguageClient::DiagnosticFile(std::string_view uri)
SendNotification("textDocument/publishDiagnostics", vscodeDiagnosis);
}

std::string LanguageClient::GetFile(std::string_view uri)
std::shared_ptr<LuaParser> LanguageClient::GetFileParser(std::string_view uri)
{
auto it = _fileMap.find(uri);
if (it != _fileMap.end())
{
return it->second;
}

return "";
return nullptr;
}

void LanguageClient::Run()
Expand Down
57 changes: 22 additions & 35 deletions CodeFormatServer/src/LanguageService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ std::shared_ptr<vscode::InitializeResult> LanguageService::OnInitialize(std::sha

vscode::DocumentOnTypeFormattingOptions typeOptions;

typeOptions.firstTriggerCharacter = ";";

typeOptions.moreTriggerCharacter = {"\"", "\'", ",", ":", "."};
typeOptions.firstTriggerCharacter = "\n";

result->capabilities.documentOnTypeFormattingProvider = typeOptions;

Expand All @@ -75,7 +73,10 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnDidChange(
{
for (auto& content : param->contentChanges)
{
LanguageClient::GetInstance().CacheFile(param->textDocument.uri, content.text);
auto parser = LuaParser::LoadFromBuffer(std::move(content.text));
parser->BuildAstWithComment();

LanguageClient::GetInstance().CacheFile(param->textDocument.uri, parser);

LanguageClient::GetInstance().DiagnosticFile(param->textDocument.uri);
}
Expand All @@ -85,7 +86,9 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnDidChange(
std::shared_ptr<vscode::Serializable> LanguageService::OnDidOpen(
std::shared_ptr<vscode::DidOpenTextDocumentParams> param)
{
LanguageClient::GetInstance().CacheFile(param->textDocument.uri, param->textDocument.text);
auto parser = LuaParser::LoadFromBuffer(std::move(param->textDocument.text));
parser->BuildAstWithComment();
LanguageClient::GetInstance().CacheFile(param->textDocument.uri, parser);
LanguageClient::GetInstance().DiagnosticFile(param->textDocument.uri);

return nullptr;
Expand All @@ -94,23 +97,20 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnDidOpen(
std::shared_ptr<vscode::Serializable> LanguageService::OnFormatting(
std::shared_ptr<vscode::DocumentFormattingParams> param)
{
auto text = LanguageClient::GetInstance().GetFile(param->textDocument.uri);
auto parser = LanguageClient::GetInstance().GetFileParser(param->textDocument.uri);

auto lastOffset = text.size();
auto totalLine = parser->GetTotalLine();

auto result = std::make_shared<vscode::DocumentFormattingResult>();

if (text.empty())
if (totalLine == 0)
{
result->hasError = true;
return result;
}

auto options = LanguageClient::GetInstance().GetOptions(param->textDocument.uri);

std::shared_ptr<LuaParser> parser = LuaParser::LoadFromBuffer(std::move(text));
parser->BuildAstWithComment();

if (parser->HasError())
{
result->hasError = true;
Expand All @@ -124,7 +124,7 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnFormatting(
edit.newText = formatter.GetFormattedText();
edit.range = vscode::Range(
vscode::Position(0, 0),
vscode::Position(parser->GetLine(lastOffset), parser->GetColumn(lastOffset))
vscode::Position(totalLine + 1, 0)
);
return result;
}
Expand Down Expand Up @@ -161,21 +161,12 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnEditorConfigUpdate(
std::shared_ptr<vscode::Serializable> LanguageService::OnRangeFormatting(
std::shared_ptr<vscode::DocumentRangeFormattingParams> param)
{
auto text = LanguageClient::GetInstance().GetFile(param->textDocument.uri);
auto parser = LanguageClient::GetInstance().GetFileParser(param->textDocument.uri);

auto result = std::make_shared<vscode::DocumentFormattingResult>();

if (text.empty())
{
result->hasError = true;
return result;
}

auto options = LanguageClient::GetInstance().GetOptions(param->textDocument.uri);

std::shared_ptr<LuaParser> parser = LuaParser::LoadFromBuffer(std::move(text));
parser->BuildAstWithComment();

if (parser->HasError())
{
result->hasError = true;
Expand All @@ -199,33 +190,29 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnRangeFormatting(
std::shared_ptr<vscode::Serializable> LanguageService::OnTypeFormatting(
std::shared_ptr<vscode::TextDocumentPositionParams> param)
{
auto text = LanguageClient::GetInstance().GetFile(param->textDocument.uri);
auto parser = LanguageClient::GetInstance().GetFileParser(param->textDocument.uri);
auto position = param->position;

auto result = std::make_shared<vscode::DocumentFormattingResult>();

if (text.empty())
if(parser->IsEmptyLine(position.line - 1))
{
result->hasError = true;
return result;
}

auto options = LanguageClient::GetInstance().GetOptions(param->textDocument.uri);

std::shared_ptr<LuaParser> parser = LuaParser::LoadFromBuffer(std::move(text));
parser->BuildAstWithComment();

// ºöÂÔ¼üÈë´íÎó
// if (parser->HasError())
// {
// result->hasError = true;
// return result;
// }
if (parser->HasError())
{
result->hasError = true;
return result;
}

LuaFormatter formatter(parser, *options);
formatter.BuildFormattedElement();

auto& edit = result->edits.emplace_back();
LuaFormatRange formattedRange(param->position.line, param->position.line);
LuaFormatRange formattedRange(position.line - 1, position.line - 1);

edit.newText = formatter.GetRangeFormattedText(formattedRange);
edit.range = vscode::Range(
Expand Down
35 changes: 24 additions & 11 deletions CodeService/src/FormatElement/RangeFormatContext.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#include "CodeService/FormatElement/RangeFormatContext.h"
#include "CodeService/FormatElement/TextElement.h"
#include <string_view>

RangeFormatContext::RangeFormatContext(std::shared_ptr<LuaParser> parser, LuaCodeStyleOptions& options,
LuaFormatRange validRange)
: FormatContext(parser, options),
_validRange(validRange),
_inFormattedRange(false)
_formattedLine(-1)

{
}
Expand All @@ -17,12 +18,10 @@ void RangeFormatContext::Print(TextElement& textElement)
if (offsetLine < _validRange.StartLine || offsetLine > _validRange.EndLine)
{
_characterCount += textElement.GetText().size();
_inFormattedRange = false;
_formattedLine = -1;
return;
}

_inFormattedRange = true;

auto& indentState = _indentStack.top();
if (static_cast<int>(_characterCount) < indentState.Indent)
{
Expand All @@ -31,11 +30,7 @@ void RangeFormatContext::Print(TextElement& textElement)
_os << textElement.GetText();
_characterCount += textElement.GetText().size();

int endOffsetLine = _parser->GetLine(textElement.GetTextRange().EndOffset);
if (endOffsetLine > _validRange.EndLine)
{
_validRange.EndLine = endOffsetLine;
}
_formattedLine = _parser->GetLine(textElement.GetTextRange().EndOffset);
}

void RangeFormatContext::PrintBlank(int blank)
Expand All @@ -44,7 +39,7 @@ void RangeFormatContext::PrintBlank(int blank)
{
_characterCount++;
}
if (_inFormattedRange)
if (_formattedLine >= 0)
{
for (int i = 0; i < blank; i++)
{
Expand All @@ -56,7 +51,7 @@ void RangeFormatContext::PrintBlank(int blank)
void RangeFormatContext::PrintLine(int line)
{
_characterCount = 0;
if (_inFormattedRange)
if (_formattedLine >= 0 && _formattedLine <= _validRange.EndLine)
{
for (int i = 0; i < line; i++)
{
Expand All @@ -65,6 +60,24 @@ void RangeFormatContext::PrintLine(int line)
}
}

std::string RangeFormatContext::GetText()
{
std::string formattedText = FormatContext::GetText();

for (int i = formattedText.size() - 1; i >= 0; i--)
{
char ch = formattedText[i];

if (ch != '\n' && ch != '\r')
{
formattedText = formattedText.substr(0, i + 1).append(_options.line_separator);
break;
}
}

return formattedText;
}

LuaFormatRange RangeFormatContext::GetFormattedRange()
{
return _validRange;
Expand Down
10 changes: 10 additions & 0 deletions LuaParser/src/LuaParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ int LuaParser::GetColumn(int offset) const
return _tokenParser->GetColumn(offset);
}

int LuaParser::GetTotalLine()
{
return _tokenParser->GetTotalLine();
}

bool LuaParser::IsEmptyLine(int line)
{
return _tokenParser->IsEmptyLine(line);
}

void LuaParser::BuildAstWithComment()
{
BuildAst();
Expand Down
37 changes: 35 additions & 2 deletions LuaParser/src/LuaTokenParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ LuaTokenParser::LuaTokenParser(std::string&& source)

bool LuaTokenParser::Parse()
{

while (true)
{
auto type = llex();
Expand Down Expand Up @@ -130,7 +129,7 @@ int LuaTokenParser::GetLine(int offset)

int maxLine = static_cast<int>(_lineOffsetVec.size()) - 1;
int targetLine = maxLine;
int upperLine = maxLine;
int upperLine = maxLine;
int lowestLine = 0;

while (true)
Expand Down Expand Up @@ -174,6 +173,40 @@ int LuaTokenParser::GetColumn(int offset)
return utf8Length;
}

int LuaTokenParser::GetTotalLine()
{
return _linenumber;
}

bool LuaTokenParser::IsEmptyLine(int line)
{
if (line < 0 || line >= _lineOffsetVec.size())
{
return true;
}
int lineStartOffset = _lineOffsetVec[line];
int nextLineStartOffset = 0;
if (line == _linenumber)
{
nextLineStartOffset = _source.size();
}
else
{
nextLineStartOffset = _lineOffsetVec[line + 1];
}

for (int offset = lineStartOffset; offset < nextLineStartOffset; offset++)
{
char ch = _source[offset];
if(ch != '\n' && ch != '\r' && ch != '\t' && ch != ' ')
{
return false;
}
}

return true;
}

std::string& LuaTokenParser::GetSource()
{
return _source;
Expand Down
10 changes: 5 additions & 5 deletions include/CodeFormatServer/LanguageClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <memory>
#include "Session/IOSession.h"
#include "CodeService/LuaCodeStyleOptions.h"

#include "LuaParser/LuaParser.h"
class LanguageClient
{
public:
Expand All @@ -17,13 +17,13 @@ class LanguageClient

void SendNotification(std::string_view method, std::shared_ptr<vscode::Serializable> param);

void CacheFile(std::string_view uri, std::string text);
void CacheFile(std::string_view uri, std::shared_ptr<LuaParser> parser);

void ReleaseFile(std::string_view uri);

void DiagnosticFile(const std::string_view uri);

std::string GetFile(const std::string_view uri);
std::shared_ptr<LuaParser> GetFileParser(const std::string_view uri);

void Run();

Expand All @@ -34,8 +34,8 @@ class LanguageClient
void RemoveOptions(std::string_view workspaceUri);
private:
std::shared_ptr<IOSession> _session;
// uri µ½file textµÄÓ³Éä
std::map<std::string, std::string, std::less<>> _fileMap;
// uri µ½file astµÄÓ³Éä
std::map<std::string, std::shared_ptr<LuaParser>, std::less<>> _fileMap;

std::vector<std::pair<std::string, std::shared_ptr<LuaCodeStyleOptions>>> _optionsVector;

Expand Down
2 changes: 1 addition & 1 deletion include/CodeService/FormatElement/FormatContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class FormatContext

std::size_t GetCurrentIndent() const;

std::string GetText();
virtual std::string GetText();

std::shared_ptr<LuaParser> GetParser();

Expand Down
Loading

0 comments on commit 2edcdad

Please sign in to comment.