@@ -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+
50110void VirtualFile::UpdateFile (std::string&& text)
51111{
52112 _luaFile = std::make_shared<LuaFile>(std::filesystem::path (url::UrlToFilePath (_fileUri)).filename ().string (),
0 commit comments