Skip to content

Commit e566f95

Browse files
committed
实现增量更新,但是性能低一半,有待优化
1 parent 83a0d34 commit e566f95

File tree

10 files changed

+103
-11
lines changed

10 files changed

+103
-11
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,4 @@
2929

3030
.vs
3131
out
32-
CMakeSettings.json
3332
3rd/uriparser/src/config.h

3rd/uriparser/src/config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343

4444
#define PACKAGE_VERSION "0.9.5"
4545

46-
#define HAVE_WPRINTF
46+
/* #undef HAVE_WPRINTF */
4747
/* #undef HAVE_REALLOCARRAY */
4848

4949

CodeFormatServer/src/LanguageClient.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,26 @@ void LanguageClient::UpdateFile(std::string_view uri, vscode::Range range, std::
9696
virtualFile->UpdateFile(std::move(text));
9797
_fileMap[filename] = virtualFile;
9898
}
99+
else if(range.start.line == -1)
100+
{
101+
it->second->UpdateFile(std::move(text));
102+
}
99103
else
100104
{
101105
it->second->UpdateFile(range, std::move(text));
102106
}
103107
}
104108

109+
void LanguageClient::UpdateFile(std::string_view uri, std::vector<vscode::TextDocumentContentChangeEvent>& changeEvent)
110+
{
111+
auto filename = url::UrlToFilePath(uri);
112+
auto it = _fileMap.find(filename);
113+
if (it != _fileMap.end())
114+
{
115+
it->second->UpdateFile(changeEvent);
116+
}
117+
}
118+
105119
void LanguageClient::ParseFile(std::string_view uri)
106120
{
107121
auto filename = url::UrlToFilePath(uri);

CodeFormatServer/src/LanguageService.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,25 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnInitialized(std::shared
135135
std::shared_ptr<vscode::Serializable> LanguageService::OnDidChange(
136136
std::shared_ptr<vscode::DidChangeTextDocumentParams> param)
137137
{
138-
for (auto& content : param->contentChanges)
138+
if (param->contentChanges.size() == 1)
139139
{
140+
auto& content = param->contentChanges.front();
140141
if (content.range.has_value())
141142
{
142143
LanguageClient::GetInstance().UpdateFile(param->textDocument.uri, content.range.value(),
143144
std::move(content.text));
144145
}
145146
else
146147
{
147-
LanguageClient::GetInstance().UpdateFile(param->textDocument.uri, {}, std::move(content.text));
148+
LanguageClient::GetInstance().UpdateFile(param->textDocument.uri,
149+
{vscode::Position(-1, 0), vscode::Position(-1, 0)},
150+
std::move(content.text));
148151
}
149152
}
153+
else
154+
{
155+
LanguageClient::GetInstance().UpdateFile(param->textDocument.uri, param->contentChanges);
156+
}
150157

151158
LanguageClient::GetInstance().ParseFile(param->textDocument.uri);
152159
LanguageClient::GetInstance().DiagnosticFile(param->textDocument.uri);

CodeFormatServer/src/Session/IOSession.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
#include <iostream>
33
#include <nlohmann/json.hpp>
44
#include "CodeFormatServer/Protocol/ProtocolParser.h"
5+
#include <chrono>
6+
#include "Util/format.h"
7+
8+
namespace chrono = std::chrono;
59

610
IOSession::IOSession()
711
: _protocolBuffer(65535)
@@ -29,11 +33,15 @@ std::string IOSession::Handle(std::string_view msg)
2933

3034
if (!params.is_null())
3135
{
36+
auto start = chrono::system_clock::now();
3237
auto result = _service.Dispatch(parser.GetMethod(), params);
3338
if (result)
3439
{
3540
return parser.SerializeProtocol(result);
3641
}
42+
std::cerr << format("request {}, it cost: {}ms\n", parser.GetMethod(), chrono::duration_cast<chrono::milliseconds>(
43+
chrono::system_clock::now() - start
44+
).count());
3745
}
3846
}
3947
catch (std::exception& e)

CodeFormatServer/src/Session/SocketIOSession.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ void SocketIOSession::Run()
3939

4040
do {
4141
auto content = _protocolBuffer.ReadOneProtocol();
42-
// std::cout << content << std::endl;
4342
std::string result = Handle(content);
4443

4544
_protocolBuffer.Reset();

CodeFormatServer/src/VirtualFile/VirtualFile.cpp

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ void VirtualFile::UpdateFile(vscode::Range range, std::string&& content)
1515
return UpdateFile(std::move(content));
1616
}
1717

18-
std::string text;
19-
text.swap(_luaFile->GetSource());
18+
std::string& text = _luaFile->GetSource();
19+
2020
auto startOffset = _luaFile->GetOffsetFromPosition(range.start.line, range.start.character);
2121
auto endOffset = _luaFile->GetOffsetFromPosition(range.end.line, range.end.character);
22+
2223
auto oldSize = text.size();
2324
auto newSize = oldSize + (content.size() - (endOffset - startOffset));
2425
if (newSize > text.capacity())
@@ -27,7 +28,7 @@ void VirtualFile::UpdateFile(vscode::Range range, std::string&& content)
2728
text.reserve(suitableCapacity);
2829
}
2930

30-
if(newSize > oldSize)
31+
if (newSize > oldSize)
3132
{
3233
text.resize(newSize);
3334
std::copy_backward(text.begin() + endOffset, text.begin() + oldSize, text.end());
@@ -36,7 +37,7 @@ void VirtualFile::UpdateFile(vscode::Range range, std::string&& content)
3637
else
3738
{
3839
std::copy(text.begin() + endOffset, text.end(), text.begin() + startOffset + content.size());
39-
if(content.size() > 0)
40+
if (content.size() > 0)
4041
{
4142
std::copy(content.begin(), content.end(), text.begin() + startOffset);
4243
}
@@ -47,6 +48,65 @@ void VirtualFile::UpdateFile(vscode::Range range, std::string&& content)
4748
std::move(text));
4849
}
4950

51+
void VirtualFile::UpdateFile(std::vector<vscode::TextDocumentContentChangeEvent>& changeEvent)
52+
{
53+
if (_luaFile == nullptr)
54+
{
55+
return;
56+
}
57+
58+
std::string text;
59+
int64_t totalSize = static_cast<int64_t>(_luaFile->GetSource().size());
60+
std::vector<std::pair<TextRange, std::string>> textChanges;
61+
for (auto& change : changeEvent)
62+
{
63+
if (!change.range.has_value())
64+
{
65+
return;
66+
}
67+
auto range = change.range.value();
68+
auto& content = change.text;
69+
auto startOffset = _luaFile->GetOffsetFromPosition(range.start.line, range.start.character);
70+
auto endOffset = _luaFile->GetOffsetFromPosition(range.end.line, range.end.character);
71+
textChanges.emplace_back(TextRange{startOffset, endOffset}, std::move(content));
72+
totalSize += content.size() - (endOffset - startOffset);
73+
}
74+
75+
std::stable_sort(textChanges.begin(), textChanges.end(), [](auto& x, auto& y)->bool
76+
{
77+
return x.first.StartOffset < y.first.StartOffset;
78+
});
79+
80+
if (totalSize > 0)
81+
{
82+
auto& source = _luaFile->GetSource();
83+
text.reserve(totalSize);
84+
std::size_t start = 0;
85+
for (std::size_t index = 0; index != textChanges.size(); index++)
86+
{
87+
auto textRange = textChanges[index].first;
88+
if (start < textRange.StartOffset)
89+
{
90+
text.append(source.begin() + start, source.begin() + textRange.StartOffset);
91+
}
92+
93+
auto& content = textChanges[index].second;
94+
95+
text.append(content);
96+
97+
start = textChanges[index].first.EndOffset;
98+
}
99+
100+
if (start < source.size())
101+
{
102+
text.append(source.begin() + start, source.end());
103+
}
104+
}
105+
106+
_luaFile = std::make_shared<LuaFile>(std::filesystem::path(url::UrlToFilePath(_fileUri)).filename().string(),
107+
std::move(text));
108+
}
109+
50110
void VirtualFile::UpdateFile(std::string&& text)
51111
{
52112
_luaFile = std::make_shared<LuaFile>(std::filesystem::path(url::UrlToFilePath(_fileUri)).filename().string(),

LuaParser/src/LuaFile.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ void LuaFile::PushLine(int offset)
126126
_lineOffsetVec.push_back(offset);
127127
}
128128

129-
std::string_view LuaFile::GetSource()
129+
std::string& LuaFile::GetSource()
130130
{
131131
return _source;
132132
}

include/CodeFormatServer/LanguageClient.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ class LanguageClient: public std::enable_shared_from_this<LanguageClient>
3737

3838
void UpdateFile(std::string_view uri, vscode::Range range, std::string&& text);
3939

40-
void ReleaseFile(std::string_view uri);
40+
void UpdateFile(std::string_view uri, std::vector<vscode::TextDocumentContentChangeEvent>& changeEvent);
41+
42+
void ParseFile(std::string_view uri);
4143

4244
void DiagnosticFile(std::string_view uri);
4345

include/CodeFormatServer/VirtualFile/VirtualFile.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ class VirtualFile
1111

1212
void UpdateFile(vscode::Range range, std::string&& content);
1313

14+
void UpdateFile(std::vector<vscode::TextDocumentContentChangeEvent>& changeEvent);
15+
1416
void UpdateFile(std::string&& text);
1517

1618
void Reset();
@@ -20,6 +22,7 @@ class VirtualFile
2022
void MakeParser();
2123
private:
2224

25+
2326
std::string _fileUri;
2427
std::shared_ptr<LuaFile> _luaFile;
2528
std::shared_ptr<LuaParser> _luaParser;

0 commit comments

Comments
 (0)