Skip to content

Commit af83ead

Browse files
committed
修改为增量更新
1 parent 9fb66da commit af83ead

File tree

7 files changed

+157
-89
lines changed

7 files changed

+157
-89
lines changed

CodeFormatServer/src/LanguageClient.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,20 @@ void LanguageClient::SendRequest(std::string_view method, std::shared_ptr<vscode
8686
}
8787
}
8888

89-
void LanguageClient::CacheFile(std::string_view uri, std::string&& text)
89+
void LanguageClient::UpdateFile(std::string_view uri, vscode::Range range, std::string&& text)
9090
{
9191
auto filename = url::UrlToFilePath(uri);
92-
auto virtualFile = std::make_shared<VirtualFile>(filename, std::move(text));
93-
_fileMap[filename] = virtualFile;
92+
auto it = _fileMap.find(filename);
93+
if(it == _fileMap.end())
94+
{
95+
auto virtualFile = std::make_shared<VirtualFile>(filename);
96+
virtualFile->UpdateFile(std::move(text));
97+
_fileMap[filename] = virtualFile;
98+
}
99+
else
100+
{
101+
it->second->UpdateFile(range, std::move(text));
102+
}
94103
}
95104

96105
void LanguageClient::ReleaseFile(std::string_view uri)

CodeFormatServer/src/LanguageService.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ std::shared_ptr<vscode::InitializeResult> LanguageService::OnInitialize(std::sha
6868

6969
result->capabilities.documentOnTypeFormattingProvider = typeOptions;
7070

71-
result->capabilities.textDocumentSync.change = vscode::TextDocumentSyncKind::Full;
71+
result->capabilities.textDocumentSync.change = vscode::TextDocumentSyncKind::Incremental;
7272
result->capabilities.textDocumentSync.openClose = true;
7373

7474
result->capabilities.codeActionProvider = true;
@@ -135,24 +135,29 @@ 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-
// std::string_view fileText;
139138
for (auto& content : param->contentChanges)
140139
{
141-
LanguageClient::GetInstance().ReleaseFile(param->textDocument.uri);
142-
143-
LanguageClient::GetInstance().CacheFile(param->textDocument.uri, std::move(content.text));
144-
145-
LanguageClient::GetInstance().DiagnosticFile(param->textDocument.uri);
140+
// LanguageClient::GetInstance().ReleaseFile(param->textDocument.uri);
141+
if (content.range.has_value())
142+
{
143+
LanguageClient::GetInstance().UpdateFile(param->textDocument.uri, content.range.value(),
144+
std::move(content.text));
145+
}
146+
else
147+
{
148+
LanguageClient::GetInstance().UpdateFile(param->textDocument.uri, {}, std::move(content.text));
149+
}
146150
}
147151

152+
LanguageClient::GetInstance().DiagnosticFile(param->textDocument.uri);
148153
return nullptr;
149154
}
150155

151156
std::shared_ptr<vscode::Serializable> LanguageService::OnDidOpen(
152157
std::shared_ptr<vscode::DidOpenTextDocumentParams> param)
153158
{
154-
LanguageClient::GetInstance().ReleaseFile(param->textDocument.uri);
155-
LanguageClient::GetInstance().CacheFile(param->textDocument.uri, std::move(param->textDocument.text));
159+
// LanguageClient::GetInstance().ReleaseFile(param->textDocument.uri);
160+
LanguageClient::GetInstance().UpdateFile(param->textDocument.uri, {}, std::move(param->textDocument.text));
156161
LanguageClient::GetInstance().DiagnosticFile(param->textDocument.uri);
157162
return nullptr;
158163
}
@@ -411,7 +416,7 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnExecuteCommand(
411416
std::string uri = param->arguments[0];
412417
std::string filePath = url::UrlToFilePath(uri);
413418
auto config = LanguageClient::GetInstance().GetService<ModuleService>()->GetIndex().GetConfig(filePath);
414-
if(!config)
419+
if (!config)
415420
{
416421
return nullptr;
417422
}

CodeFormatServer/src/VirtualFile/VirtualFile.cpp

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,57 @@
11
#include "CodeFormatServer/VirtualFile/VirtualFile.h"
2+
#include "Util/Url.h"
23

3-
VirtualFile::VirtualFile(std::string_view filename, std::string&& text)
4-
: _filename(filename),
5-
_luaFile(std::make_shared<LuaFile>(std::filesystem::path(filename).filename().string(), std::move(text)))
6-
// _isOpen(false)
4+
VirtualFile::VirtualFile(std::string_view fileUri)
5+
: _fileUri(fileUri),
6+
_luaFile(nullptr),
7+
_luaParser(nullptr)
78
{
8-
MakeParser();
99
}
1010

11-
void VirtualFile::UpdateFile(vscode::Range range, std::string& content)
11+
void VirtualFile::UpdateFile(vscode::Range range, std::string&& content)
1212
{
13-
// auto startLine = range.start.line;
14-
// if
13+
if (_luaFile == nullptr)
14+
{
15+
return UpdateFile(std::move(content));
16+
}
17+
18+
auto startOffset = _luaFile->GetOffsetFromPosition(range.start.line, range.start.character);
19+
auto endOffset = _luaFile->GetOffsetFromPosition(range.end.line, range.end.character);
20+
auto oldSize = _text.size();
21+
auto newSize = oldSize + (content.size() - (endOffset - startOffset));
22+
if (newSize > _text.capacity())
23+
{
24+
auto suitableCapacity = newSize + 4096;
25+
_text.reserve(suitableCapacity);
26+
}
27+
28+
if(newSize > oldSize)
29+
{
30+
_text.resize(newSize);
31+
std::copy_backward(_text.begin() + endOffset, _text.begin() + oldSize, _text.end());
32+
std::copy(content.begin(), content.end(), _text.begin() + startOffset);
33+
}
34+
else
35+
{
36+
std::copy(_text.begin() + endOffset, _text.end(), _text.begin() + startOffset + content.size());
37+
if(content.size() > 0)
38+
{
39+
std::copy(content.begin(), content.end(), _text.begin() + startOffset);
40+
}
41+
_text.resize(newSize);
42+
}
43+
44+
std::string fileText = _text;
45+
_luaFile = std::make_shared<LuaFile>(std::filesystem::path(url::UrlToFilePath(_fileUri)).filename().string(),
46+
std::move(fileText));
47+
MakeParser();
1548
}
1649

17-
void VirtualFile::UpdateFile(std::string& text)
50+
void VirtualFile::UpdateFile(std::string&& text)
1851
{
19-
_luaFile = std::make_shared<LuaFile>(std::filesystem::path(_filename).filename().string(), std::move(text));
52+
_text = text;
53+
_luaFile = std::make_shared<LuaFile>(std::filesystem::path(url::UrlToFilePath(_fileUri)).filename().string(),
54+
std::move(text));
2055
MakeParser();
2156
}
2257

LuaParser/src/LuaFile.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ int LuaFile::GetOffsetFromPosition(int line, int character)
6767
{
6868
if (line >= _lineOffsetVec.size() || line < 0)
6969
{
70-
return -1;
70+
return _source.size() + 1;
7171
}
7272

7373
int lineStartOffset = _lineOffsetVec[line];
@@ -81,8 +81,8 @@ int LuaFile::GetOffsetFromPosition(int line, int character)
8181
nextOffset = _lineOffsetVec[line + 1];
8282
}
8383

84-
int offset = utf8::Utf8nByteNum(_source.data() + lineStartOffset, nextOffset - lineStartOffset, character);
85-
return lineStartOffset + offset;
84+
std::size_t offset = utf8::Utf8nByteNum(_source.data() + lineStartOffset, nextOffset - lineStartOffset, character);
85+
return lineStartOffset + static_cast<int>(offset);
8686
}
8787

8888
int LuaFile::GetTotalLine()

Util/src/Utf8.cpp

Lines changed: 76 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,71 +2,90 @@
22

33
std::size_t utf8::Utf8nLen(const char* source, std::size_t byteNum)
44
{
5-
const char* t = source;
6-
std::size_t length = 0;
5+
const char* t = source;
6+
std::size_t length = 0;
77

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-
}
8+
while (static_cast<std::size_t>(source - t) < byteNum && '\0' != *source)
9+
{
10+
if (0xf0 == (0xf8 & *source))
11+
{
12+
// 4-byte utf8 code point (began with 0b11110xxx)
13+
source += 4;
14+
}
15+
else if (0xe0 == (0xf0 & *source))
16+
{
17+
// 3-byte utf8 code point (began with 0b1110xxxx)
18+
source += 3;
19+
}
20+
else if (0xc0 == (0xe0 & *source))
21+
{
22+
// 2-byte utf8 code point (began with 0b110xxxxx)
23+
source += 2;
24+
}
25+
else
26+
{
27+
// if (0x00 == (0x80 & *s)) {
28+
// 1-byte ascii (began with 0b0xxxxxxx)
29+
source += 1;
30+
}
2531

26-
// no matter the bytes we marched s forward by, it was
27-
// only 1 utf8 codepoint
28-
length++;
29-
}
32+
// no matter the bytes we marched s forward by, it was
33+
// only 1 utf8 codepoint
34+
length++;
35+
}
3036

31-
if (static_cast<size_t>(source - t) > byteNum) {
32-
length--;
33-
}
34-
return length;
37+
if (static_cast<size_t>(source - t) > byteNum)
38+
{
39+
length--;
40+
}
41+
return length;
3542
}
3643

3744
std::size_t utf8::Utf8nByteNum(const char* source, std::size_t maxByteNum, std::size_t utf8Position)
3845
{
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-
}
46+
const char* t = source;
47+
std::size_t length = 0;
48+
std::size_t byteNum = 0;
49+
while (byteNum < maxByteNum && '\0' != *source)
50+
{
51+
if (0xf0 == (0xf8 & *source))
52+
{
53+
// 4-byte utf8 code point (began with 0b11110xxx)
54+
source += 4;
55+
}
56+
else if (0xe0 == (0xf0 & *source))
57+
{
58+
// 3-byte utf8 code point (began with 0b1110xxxx)
59+
source += 3;
60+
}
61+
else if (0xc0 == (0xe0 & *source))
62+
{
63+
// 2-byte utf8 code point (began with 0b110xxxxx)
64+
source += 2;
65+
}
66+
else
67+
{
68+
// if (0x00 == (0x80 & *s)) {
69+
// 1-byte ascii (began with 0b0xxxxxxx)
70+
source += 1;
71+
}
5972

60-
// no matter the bytes we marched s forward by, it was
61-
// only 1 utf8 codepoint
62-
length++;
73+
// no matter the bytes we marched s forward by, it was
74+
// only 1 utf8 codepoint
75+
length++;
6376

64-
if(length >= utf8Position)
65-
{
66-
return byteNum;
67-
}
68-
byteNum = static_cast<std::size_t>(source - t);
69-
}
77+
if (length > utf8Position)
78+
{
79+
return byteNum;
80+
}
7081

71-
return std::string::npos;
82+
byteNum = static_cast<std::size_t>(source - t);
83+
}
84+
85+
if(byteNum > maxByteNum)
86+
{
87+
return maxByteNum;
88+
}
89+
90+
return byteNum;
7291
}

include/CodeFormatServer/LanguageClient.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class LanguageClient: public std::enable_shared_from_this<LanguageClient>
3535

3636
void SendRequest(std::string_view method, std::shared_ptr<vscode::Serializable> param);
3737

38-
void CacheFile(std::string_view uri, std::string&& text);
38+
void UpdateFile(std::string_view uri, vscode::Range range, std::string&& text);
3939

4040
void ReleaseFile(std::string_view uri);
4141

include/CodeFormatServer/VirtualFile/VirtualFile.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@
77
class VirtualFile
88
{
99
public:
10-
VirtualFile(std::string_view filename, std::string&& text);
10+
VirtualFile(std::string_view fileUri);
1111

12-
void UpdateFile(vscode::Range range, std::string& content);
12+
void UpdateFile(vscode::Range range, std::string&& content);
1313

14-
void UpdateFile(std::string& text);
14+
void UpdateFile(std::string&& text);
1515

1616
void Reset();
1717

1818
std::shared_ptr<LuaParser> GetLuaParser();
19-
2019
private:
2120
void MakeParser();
2221

23-
std::string _filename;
22+
std::string _fileUri;
23+
std::string _text;
2424
std::shared_ptr<LuaFile> _luaFile;
2525
std::shared_ptr<LuaParser> _luaParser;
2626

0 commit comments

Comments
 (0)