Skip to content

Commit 5526cd1

Browse files
committed
缓冲区算法优化
1 parent 6b88fdc commit 5526cd1

File tree

14 files changed

+294
-30
lines changed

14 files changed

+294
-30
lines changed

CodeFormatServer/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ target_sources(CodeFormatServer
3030
${CodeFormatServer_SOURCE_DIR}/src/CodeFormatServer.cpp
3131
${CodeFormatServer_SOURCE_DIR}/src/LanguageClient.cpp
3232
${CodeFormatServer_SOURCE_DIR}/src/LanguageService.cpp
33+
${CodeFormatServer_SOURCE_DIR}/src/FileManager.cpp
3334
${CodeFormatServer_SOURCE_DIR}/src/Session/IOSession.cpp
3435
${CodeFormatServer_SOURCE_DIR}/src/Session/SocketIOSession.cpp
3536
${CodeFormatServer_SOURCE_DIR}/src/Session/StandardIOSession.cpp
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
// #include "CodeFormatServer/FileManager.h"
2+
// #include <algorithm>
3+
// #include "Util/Utf8.h"
4+
//
5+
// FileManager::FileManager()
6+
// {
7+
// }
8+
//
9+
// FileManager::~FileManager()
10+
// {
11+
// }
12+
//
13+
// void FileManager::UpdateFile(std::string_view uri, vscode::Range range, std::string& text)
14+
// {
15+
// auto it = _fileMap.find(uri);
16+
// if (it == _fileMap.end())
17+
// {
18+
// return UpdateFile(uri, text);
19+
// }
20+
//
21+
// auto textLines = GetTextLines(text);
22+
//
23+
// auto& file = it->second;
24+
//
25+
// std::vector<std::string> newFile;
26+
//
27+
// auto startLine = range.start.line;
28+
// auto endLine = range.end.line;
29+
// auto startCharacter = range.start.character;
30+
// auto endCharacter = range.end.character;
31+
//
32+
// for (std::size_t lineNumber = 0; lineNumber < startLine; lineNumber++)
33+
// {
34+
// if (lineNumber < file.size())
35+
// {
36+
// newFile.emplace_back(std::move(file[lineNumber]));
37+
// }
38+
// else
39+
// {
40+
// newFile.emplace_back("\n");
41+
// }
42+
// }
43+
//
44+
// if (startCharacter != 0 && startLine < file.size() && !textLines.empty())
45+
// {
46+
// auto byteNumber = utf8::Utf8nByteNum(file[startLine].data(), file[startLine].size(), startCharacter + 1);
47+
// textLines.front() = file[startLine].substr(0, byteNumber) + textLines.front();
48+
// }
49+
//
50+
// if (endLine < file.size() && !textLines.empty())
51+
// {
52+
// auto byteNumber = utf8::Utf8nByteNum(file[endLine].data(), file[endLine].size(), endCharacter + 1);
53+
//
54+
// textLines.back().append(file[endLine].substr(byteNumber));
55+
// }
56+
//
57+
// for (auto& line : textLines)
58+
// {
59+
// newFile.emplace_back(std::move(line));
60+
// }
61+
//
62+
//
63+
// for (std::size_t lineNumber = endLine + 1; lineNumber < file.size(); lineNumber++)
64+
// {
65+
// newFile.emplace_back(std::move(file[lineNumber]));
66+
// }
67+
//
68+
// file = newFile;
69+
// }
70+
//
71+
// void FileManager::UpdateFile(std::string_view uri, std::string& text)
72+
// {
73+
// _fileMap[std::string(uri)] = GetTextLines(text);
74+
// }
75+
//
76+
// void FileManager::ReleaseFile(std::string_view uri)
77+
// {
78+
// auto it = _fileMap.find(uri);
79+
//
80+
// if (it != _fileMap.end())
81+
// {
82+
// _fileMap.erase(it);
83+
// }
84+
// }
85+
//
86+
// std::string FileManager::GetFileText(std::string_view uri)
87+
// {
88+
// std::string fileText;
89+
//
90+
// auto it = _fileMap.find(uri);
91+
// if (it == _fileMap.end())
92+
// {
93+
// return "";
94+
// }
95+
//
96+
// std::size_t fileSize = std::accumulate(it->second.begin(), it->second.end(), 0,
97+
// [](auto x, const std::string& line)
98+
// {
99+
// return x + line.size();
100+
// });
101+
//
102+
// fileText.reserve(fileSize);
103+
//
104+
// for (auto& line : it->second)
105+
// {
106+
// fileText.append(line);
107+
// }
108+
// return fileText;
109+
// }
110+
//
111+
//
112+
// std::vector<std::string> FileManager::GetTextLines(std::string& text)
113+
// {
114+
// std::vector<std::string> textLines;
115+
//
116+
// std::string_view source = text;
117+
// std::size_t sepLen = 1;
118+
//
119+
// while (true)
120+
// {
121+
// const std::size_t sepIndex = source.find_first_of('\n', 0);
122+
// if (sepIndex == std::string_view::npos)
123+
// {
124+
// textLines.emplace_back(source);
125+
//
126+
// break;
127+
// }
128+
// else
129+
// {
130+
// textLines.emplace_back(source.substr(0, sepIndex + sepLen));
131+
// source = source.substr(sepIndex + sepLen);
132+
// }
133+
// }
134+
// return textLines;
135+
// }

CodeFormatServer/src/LanguageClient.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,23 @@ void LanguageClient::CacheFile(std::string_view uri, std::shared_ptr<LuaParser>
4747
{
4848
std::filesystem::path path(uri);
4949
parser->SetFilename(path.filename().string());
50-
_fileMap[std::string(uri)] = parser;
50+
_parserMap[std::string(uri)] = parser;
5151
}
5252

5353
void LanguageClient::ReleaseFile(std::string_view uri)
5454
{
55-
auto it = _fileMap.find(uri);
56-
if (it != _fileMap.end())
55+
auto it = _parserMap.find(uri);
56+
if (it != _parserMap.end())
5757
{
58-
_fileMap.erase(it);
58+
_parserMap.erase(it);
5959
}
60+
6061
}
6162

6263
void LanguageClient::DiagnosticFile(std::string_view uri)
6364
{
64-
auto it = _fileMap.find(uri);
65-
if (it == _fileMap.end())
65+
auto it = _parserMap.find(uri);
66+
if (it == _parserMap.end())
6667
{
6768
return;
6869
}
@@ -112,8 +113,8 @@ void LanguageClient::DiagnosticFile(std::string_view uri)
112113

113114
std::shared_ptr<LuaParser> LanguageClient::GetFileParser(std::string_view uri)
114115
{
115-
auto it = _fileMap.find(uri);
116-
if (it != _fileMap.end())
116+
auto it = _parserMap.find(uri);
117+
if (it != _parserMap.end())
117118
{
118119
return it->second;
119120
}

CodeFormatServer/src/LanguageService.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "CodeFormatServer/LanguageService.h"
22
#include <fstream>
3+
#include <iostream>
34
#include <sstream>
45
#include "nlohmann/json.hpp"
56
#include "CodeFormatServer/VSCode.h"
@@ -59,7 +60,7 @@ std::shared_ptr<vscode::InitializeResult> LanguageService::OnInitialize(std::sha
5960
result->capabilities.documentOnTypeFormattingProvider = typeOptions;
6061

6162
result->capabilities.textDocumentSync.change = vscode::TextDocumentSyncKind::Full;
62-
result->capabilities.textDocumentSync.openClose = true;
63+
result->capabilities.textDocumentSync.openClose = true;
6364

6465
auto& configFiles = param->initializationOptions.configFiles;
6566
for (auto& configFile : configFiles)
@@ -101,6 +102,7 @@ std::shared_ptr<vscode::InitializeResult> LanguageService::OnInitialize(std::sha
101102
std::shared_ptr<vscode::Serializable> LanguageService::OnDidChange(
102103
std::shared_ptr<vscode::DidChangeTextDocumentParams> param)
103104
{
105+
std::string_view fileText;
104106
for (auto& content : param->contentChanges)
105107
{
106108
auto parser = LuaParser::LoadFromBuffer(std::move(content.text));
@@ -110,6 +112,7 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnDidChange(
110112

111113
LanguageClient::GetInstance().DiagnosticFile(param->textDocument.uri);
112114
}
115+
113116
return nullptr;
114117
}
115118

CodeFormatServer/src/Protocol/ProtocolBuffer.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,14 @@ void ProtocolBuffer::WriteBuff(std::size_t size)
2424
{
2525
if (_textProtocol.size() < _writeIndex + size)
2626
{
27-
_textProtocol.resize(_writeIndex + size);
27+
if (_bodyStartIndex != 0 && _contentLength != 0 && _writeIndex < (_bodyStartIndex + _contentLength))
28+
{
29+
_textProtocol.resize(std::max(_writeIndex + size, _bodyStartIndex + _contentLength));
30+
}
31+
else
32+
{
33+
_textProtocol.resize(_writeIndex + size);
34+
}
2835
}
2936

3037
std::copy_n(_readBuffer.begin(), size, _textProtocol.begin() + _writeIndex);

CodeFormatServer/src/VSCode.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -261,17 +261,15 @@ void vscode::InitializeResult::Deserialize(nlohmann::json json)
261261
capabilities.Deserialize(json["capabilities"]);
262262
}
263263

264-
nlohmann::json vscode::TextDocumentContentChangeEvent::Serialize()
265-
{
266-
auto object = nlohmann::json::object();
267-
268-
object["text"] = text;
269-
return object;
270-
}
271-
272264
void vscode::TextDocumentContentChangeEvent::Deserialize(nlohmann::json json)
273265
{
274266
text = json["text"];
267+
if(!json["range"].is_null())
268+
{
269+
auto textRange = Range();
270+
textRange.Deserialize(json["range"]);
271+
range = textRange;
272+
}
275273
}
276274

277275
void vscode::DidChangeTextDocumentParams::Deserialize(nlohmann::json json)

LuaParser/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ add_library(LuaParser STATIC)
66

77
target_include_directories(LuaParser PUBLIC
88
${LuaCodeStyle_SOURCE_DIR}/include
9-
${LuaCodeStyle_SOURCE_DIR}/3rd/utf8/include
109
src
1110
)
1211

LuaParser/src/LuaTokenParser.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "LuaDefine.h"
44
#include "LuaParser/LuaTokenTypeDetail.h"
55
#include "Util/format.h"
6-
#include "utf8.h"
6+
#include "Util/Utf8.h"
77

88

99
std::map<std::string, LuaTokenType, std::less<>> LuaTokenParser::LuaReserved = {
@@ -78,7 +78,7 @@ bool LuaTokenParser::Parse()
7878
break;
7979
}
8080

81-
if(!_errors.empty())
81+
if (!_errors.empty())
8282
{
8383
return false;
8484
}
@@ -176,13 +176,7 @@ int LuaTokenParser::GetColumn(int offset)
176176

177177
int bytesLength = offset - lineStartOffset;
178178

179-
#if __APPLE__
180-
// git action 编译时在macos下编译会报错,我不觉得有什么问题
181-
const char8_t* macOsBug = reinterpret_cast<const char8_t*>(_source.data() + lineStartOffset);
182-
return ::utf8nlen(macOsBug, static_cast<std::size_t>(bytesLength));
183-
#else
184-
return ::utf8nlen(_source.data() + lineStartOffset, static_cast<std::size_t>(bytesLength));
185-
#endif
179+
return static_cast<int>(utf8::Utf8nLen(_source.data() + lineStartOffset, static_cast<std::size_t>(bytesLength)));
186180
}
187181

188182
int LuaTokenParser::GetTotalLine()

Util/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ target_sources(Util
1414
PUBLIC
1515
${LuaCodeStyle_SOURCE_DIR}/include/Util/CommandLine.h
1616
${LuaCodeStyle_SOURCE_DIR}/include/Util/StringUtil.h
17+
${LuaCodeStyle_SOURCE_DIR}/include/Util/Utf8.h
1718

1819
PRIVATE
1920
${Util_SOURCE_DIR}/src/CommandLine.cpp
2021
${Util_SOURCE_DIR}/src/StringUtil.cpp
22+
${Util_SOURCE_DIR}/src/Utf8.cpp
2123
)
2224

Util/src/Utf8.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include "Util/Utf8.h"
2+
3+
std::size_t utf8::Utf8nLen(const char* source, std::size_t byteNum)
4+
{
5+
const char* t = source;
6+
std::size_t length = 0;
7+
8+
while (static_cast<std::size_t>(source - t) < byteNum && '\0' != *source) {
9+
if (0xf0 == (0xf8 & *source)) {
10+
// 4-byte utf8 code point (began with 0b11110xxx)
11+
source += 4;
12+
}
13+
else if (0xe0 == (0xf0 & *source)) {
14+
// 3-byte utf8 code point (began with 0b1110xxxx)
15+
source += 3;
16+
}
17+
else if (0xc0 == (0xe0 & *source)) {
18+
// 2-byte utf8 code point (began with 0b110xxxxx)
19+
source += 2;
20+
}
21+
else { // if (0x00 == (0x80 & *s)) {
22+
// 1-byte ascii (began with 0b0xxxxxxx)
23+
source += 1;
24+
}
25+
26+
// no matter the bytes we marched s forward by, it was
27+
// only 1 utf8 codepoint
28+
length++;
29+
}
30+
31+
if (static_cast<size_t>(source - t) > byteNum) {
32+
length--;
33+
}
34+
return length;
35+
}
36+
37+
std::size_t utf8::Utf8nByteNum(const char* source, std::size_t maxByteNum, std::size_t utf8Position)
38+
{
39+
const char* t = source;
40+
std::size_t length = 0;
41+
std::size_t byteNum = static_cast<std::size_t>(source - t);
42+
while (byteNum < maxByteNum && '\0' != *source) {
43+
if (0xf0 == (0xf8 & *source)) {
44+
// 4-byte utf8 code point (began with 0b11110xxx)
45+
source += 4;
46+
}
47+
else if (0xe0 == (0xf0 & *source)) {
48+
// 3-byte utf8 code point (began with 0b1110xxxx)
49+
source += 3;
50+
}
51+
else if (0xc0 == (0xe0 & *source)) {
52+
// 2-byte utf8 code point (began with 0b110xxxxx)
53+
source += 2;
54+
}
55+
else { // if (0x00 == (0x80 & *s)) {
56+
// 1-byte ascii (began with 0b0xxxxxxx)
57+
source += 1;
58+
}
59+
60+
// no matter the bytes we marched s forward by, it was
61+
// only 1 utf8 codepoint
62+
length++;
63+
64+
if(length >= utf8Position)
65+
{
66+
return byteNum;
67+
}
68+
byteNum = static_cast<std::size_t>(source - t);
69+
}
70+
71+
return std::string::npos;
72+
}

0 commit comments

Comments
 (0)