@@ -59,8 +59,8 @@ constexpr trace::Metric LSPLatency("lsp_latency", trace::Metric::Distribution,
59
59
60
60
// LSP defines file versions as numbers that increase.
61
61
// ClangdServer treats them as opaque and therefore uses strings instead.
62
- std::string encodeVersion (int64_t LSPVersion) {
63
- return llvm::to_string (LSPVersion);
62
+ std::string encodeVersion (llvm::Optional< int64_t > LSPVersion) {
63
+ return LSPVersion ? llvm::to_string (* LSPVersion) : " " ;
64
64
}
65
65
llvm::Optional<int64_t > decodeVersion (llvm::StringRef Encoded) {
66
66
int64_t Result;
@@ -124,15 +124,15 @@ CompletionItemKindBitset defaultCompletionItemKinds() {
124
124
// Makes sure edits in \p FE are applicable to latest file contents reported by
125
125
// editor. If not generates an error message containing information about files
126
126
// that needs to be saved.
127
- llvm::Error validateEdits (const DraftStore &DraftMgr , const FileEdits &FE) {
127
+ llvm::Error validateEdits (const ClangdServer &Server , const FileEdits &FE) {
128
128
size_t InvalidFileCount = 0 ;
129
129
llvm::StringRef LastInvalidFile;
130
130
for (const auto &It : FE) {
131
- if (auto Draft = DraftMgr .getDraft (It.first ())) {
131
+ if (auto Draft = Server .getDraft (It.first ())) {
132
132
// If the file is open in user's editor, make sure the version we
133
133
// saw and current version are compatible as this is the text that
134
134
// will be replaced by editors.
135
- if (!It.second .canApplyTo (Draft-> Contents )) {
135
+ if (!It.second .canApplyTo (* Draft)) {
136
136
++InvalidFileCount;
137
137
LastInvalidFile = It.first ();
138
138
}
@@ -648,8 +648,8 @@ void ClangdLSPServer::onDocumentDidOpen(
648
648
649
649
const std::string &Contents = Params.textDocument .text ;
650
650
651
- auto Version = DraftMgr. addDraft (File, Params. textDocument . version , Contents);
652
- Server-> addDocument (File, Contents, encodeVersion (Version ),
651
+ Server-> addDocument (File, Contents,
652
+ encodeVersion (Params. textDocument . version ),
653
653
WantDiagnostics::Yes);
654
654
}
655
655
@@ -661,25 +661,28 @@ void ClangdLSPServer::onDocumentDidChange(
661
661
: WantDiagnostics::No;
662
662
663
663
PathRef File = Params.textDocument .uri .file ();
664
- llvm::Expected<DraftStore::Draft> Draft = DraftMgr.updateDraft (
665
- File, Params.textDocument .version , Params.contentChanges );
666
- if (!Draft) {
667
- // If this fails, we are most likely going to be not in sync anymore with
668
- // the client. It is better to remove the draft and let further operations
669
- // fail rather than giving wrong results.
670
- DraftMgr.removeDraft (File);
671
- Server->removeDocument (File);
672
- elog (" Failed to update {0}: {1}" , File, Draft.takeError ());
664
+ auto Code = Server->getDraft (File);
665
+ if (!Code) {
666
+ log (" Trying to incrementally change non-added document: {0}" , File);
673
667
return ;
674
668
}
675
-
676
- Server->addDocument (File, Draft->Contents , encodeVersion (Draft->Version ),
669
+ for (const auto &Change : Params.contentChanges ) {
670
+ if (auto Err = applyChange (*Code, Change)) {
671
+ // If this fails, we are most likely going to be not in sync anymore with
672
+ // the client. It is better to remove the draft and let further
673
+ // operations fail rather than giving wrong results.
674
+ Server->removeDocument (File);
675
+ elog (" Failed to update {0}: {1}" , File, std::move (Err));
676
+ return ;
677
+ }
678
+ }
679
+ Server->addDocument (File, *Code, encodeVersion (Params.textDocument .version ),
677
680
WantDiags, Params.forceRebuild );
678
681
}
679
682
680
683
void ClangdLSPServer::onDocumentDidSave (
681
684
const DidSaveTextDocumentParams &Params) {
682
- reparseOpenFilesIfNeeded ([](llvm::StringRef) { return true ; });
685
+ Server-> reparseOpenFilesIfNeeded ([](llvm::StringRef) { return true ; });
683
686
}
684
687
685
688
void ClangdLSPServer::onFileEvent (const DidChangeWatchedFilesParams &Params) {
@@ -720,13 +723,8 @@ void ClangdLSPServer::onCommandApplyEdit(const WorkspaceEdit &WE,
720
723
721
724
void ClangdLSPServer::onCommandApplyTweak (const TweakArgs &Args,
722
725
Callback<llvm::json::Value> Reply) {
723
- auto Code = DraftMgr.getDraft (Args.file .file ());
724
- if (!Code)
725
- return Reply (error (" trying to apply a code action for a non-added file" ));
726
-
727
- auto Action = [this , Reply = std::move (Reply), File = Args.file ,
728
- Code = std::move (*Code)](
729
- llvm::Expected<Tweak::Effect> R) mutable {
726
+ auto Action = [this , Reply = std::move (Reply),
727
+ File = Args.file ](llvm::Expected<Tweak::Effect> R) mutable {
730
728
if (!R)
731
729
return Reply (R.takeError ());
732
730
@@ -742,7 +740,7 @@ void ClangdLSPServer::onCommandApplyTweak(const TweakArgs &Args,
742
740
if (R->ApplyEdits .empty ())
743
741
return Reply (" Tweak applied." );
744
742
745
- if (auto Err = validateEdits (DraftMgr , R->ApplyEdits ))
743
+ if (auto Err = validateEdits (*Server , R->ApplyEdits ))
746
744
return Reply (std::move (Err));
747
745
748
746
WorkspaceEdit WE;
@@ -808,7 +806,7 @@ void ClangdLSPServer::onPrepareRename(const TextDocumentPositionParams &Params,
808
806
void ClangdLSPServer::onRename (const RenameParams &Params,
809
807
Callback<WorkspaceEdit> Reply) {
810
808
Path File = std::string (Params.textDocument .uri .file ());
811
- if (!DraftMgr. getDraft (File))
809
+ if (!Server-> getDraft (File))
812
810
return Reply (llvm::make_error<LSPError>(
813
811
" onRename called for non-added file" , ErrorCode::InvalidParams));
814
812
Server->rename (
@@ -817,7 +815,7 @@ void ClangdLSPServer::onRename(const RenameParams &Params,
817
815
this ](llvm::Expected<RenameResult> R) mutable {
818
816
if (!R)
819
817
return Reply (R.takeError ());
820
- if (auto Err = validateEdits (DraftMgr , R->GlobalChanges ))
818
+ if (auto Err = validateEdits (*Server , R->GlobalChanges ))
821
819
return Reply (std::move (Err));
822
820
WorkspaceEdit Result;
823
821
Result.changes .emplace ();
@@ -832,7 +830,6 @@ void ClangdLSPServer::onRename(const RenameParams &Params,
832
830
void ClangdLSPServer::onDocumentDidClose (
833
831
const DidCloseTextDocumentParams &Params) {
834
832
PathRef File = Params.textDocument .uri .file ();
835
- DraftMgr.removeDraft (File);
836
833
Server->removeDocument (File);
837
834
838
835
{
@@ -857,52 +854,35 @@ void ClangdLSPServer::onDocumentOnTypeFormatting(
857
854
const DocumentOnTypeFormattingParams &Params,
858
855
Callback<std::vector<TextEdit>> Reply) {
859
856
auto File = Params.textDocument .uri .file ();
860
- auto Code = DraftMgr.getDraft (File);
861
- if (!Code)
862
- return Reply (llvm::make_error<LSPError>(
863
- " onDocumentOnTypeFormatting called for non-added file" ,
864
- ErrorCode::InvalidParams));
865
-
866
- Server->formatOnType (File, Code->Contents , Params.position , Params.ch ,
867
- std::move (Reply));
857
+ Server->formatOnType (File, Params.position , Params.ch , std::move (Reply));
868
858
}
869
859
870
860
void ClangdLSPServer::onDocumentRangeFormatting (
871
861
const DocumentRangeFormattingParams &Params,
872
862
Callback<std::vector<TextEdit>> Reply) {
873
863
auto File = Params.textDocument .uri .file ();
874
- auto Code = DraftMgr.getDraft (File);
875
- if (!Code)
876
- return Reply (llvm::make_error<LSPError>(
877
- " onDocumentRangeFormatting called for non-added file" ,
878
- ErrorCode::InvalidParams));
879
-
880
- Server->formatRange (
881
- File, Code->Contents , Params.range ,
882
- [Code = Code->Contents , Reply = std::move (Reply)](
883
- llvm::Expected<tooling::Replacements> Result) mutable {
884
- if (Result)
885
- Reply (replacementsToEdits (Code, Result.get ()));
886
- else
887
- Reply (Result.takeError ());
888
- });
864
+ auto Code = Server->getDraft (File);
865
+ Server->formatFile (File, Params.range ,
866
+ [Code = std::move (Code), Reply = std::move (Reply)](
867
+ llvm::Expected<tooling::Replacements> Result) mutable {
868
+ if (Result)
869
+ Reply (replacementsToEdits (*Code, Result.get ()));
870
+ else
871
+ Reply (Result.takeError ());
872
+ });
889
873
}
890
874
891
875
void ClangdLSPServer::onDocumentFormatting (
892
876
const DocumentFormattingParams &Params,
893
877
Callback<std::vector<TextEdit>> Reply) {
894
878
auto File = Params.textDocument .uri .file ();
895
- auto Code = DraftMgr.getDraft (File);
896
- if (!Code)
897
- return Reply (llvm::make_error<LSPError>(
898
- " onDocumentFormatting called for non-added file" ,
899
- ErrorCode::InvalidParams));
900
-
901
- Server->formatFile (File, Code->Contents ,
902
- [Code = Code->Contents , Reply = std::move (Reply)](
879
+ auto Code = Server->getDraft (File);
880
+ Server->formatFile (File,
881
+ /* Rng=*/ llvm::None,
882
+ [Code = std::move (Code), Reply = std::move (Reply)](
903
883
llvm::Expected<tooling::Replacements> Result) mutable {
904
884
if (Result)
905
- Reply (replacementsToEdits (Code, Result.get ()));
885
+ Reply (replacementsToEdits (* Code, Result.get ()));
906
886
else
907
887
Reply (Result.takeError ());
908
888
});
@@ -978,11 +958,6 @@ static llvm::Optional<Command> asCommand(const CodeAction &Action) {
978
958
void ClangdLSPServer::onCodeAction (const CodeActionParams &Params,
979
959
Callback<llvm::json::Value> Reply) {
980
960
URIForFile File = Params.textDocument .uri ;
981
- auto Code = DraftMgr.getDraft (File.file ());
982
- if (!Code)
983
- return Reply (llvm::make_error<LSPError>(
984
- " onCodeAction called for non-added file" , ErrorCode::InvalidParams));
985
-
986
961
// Checks whether a particular CodeActionKind is included in the response.
987
962
auto KindAllowed = [Only (Params.context .only )](llvm::StringRef Kind) {
988
963
if (Only.empty ())
@@ -1005,8 +980,8 @@ void ClangdLSPServer::onCodeAction(const CodeActionParams &Params,
1005
980
1006
981
// Now enumerate the semantic code actions.
1007
982
auto ConsumeActions =
1008
- [Reply = std::move (Reply), File, Code = std::move (*Code) ,
1009
- Selection = Params. range , FixIts = std::move (FixIts), this ](
983
+ [Reply = std::move (Reply), File, Selection = Params. range ,
984
+ FixIts = std::move (FixIts), this ](
1010
985
llvm::Expected<std::vector<ClangdServer::TweakRef>> Tweaks) mutable {
1011
986
if (!Tweaks)
1012
987
return Reply (Tweaks.takeError ());
@@ -1246,7 +1221,7 @@ void ClangdLSPServer::applyConfiguration(
1246
1221
}
1247
1222
}
1248
1223
1249
- reparseOpenFilesIfNeeded (
1224
+ Server-> reparseOpenFilesIfNeeded (
1250
1225
[&](llvm::StringRef File) { return ModifiedFiles.count (File) != 0 ; });
1251
1226
}
1252
1227
@@ -1557,17 +1532,17 @@ bool ClangdLSPServer::shouldRunCompletion(
1557
1532
const CompletionParams &Params) const {
1558
1533
if (Params.context .triggerKind != CompletionTriggerKind::TriggerCharacter)
1559
1534
return true ;
1560
- auto Code = DraftMgr. getDraft (Params.textDocument .uri .file ());
1535
+ auto Code = Server-> getDraft (Params.textDocument .uri .file ());
1561
1536
if (!Code)
1562
1537
return true ; // completion code will log the error for untracked doc.
1563
- auto Offset = positionToOffset (Code-> Contents , Params.position ,
1538
+ auto Offset = positionToOffset (* Code, Params.position ,
1564
1539
/* AllowColumnsBeyondLineLength=*/ false );
1565
1540
if (!Offset) {
1566
1541
vlog (" could not convert position '{0}' to offset for file '{1}'" ,
1567
1542
Params.position , Params.textDocument .uri .file ());
1568
1543
return true ;
1569
1544
}
1570
- return allowImplicitCompletion (Code-> Contents , *Offset);
1545
+ return allowImplicitCompletion (* Code, *Offset);
1571
1546
}
1572
1547
1573
1548
void ClangdLSPServer::onDiagnosticsReady (PathRef File, llvm::StringRef Version,
@@ -1681,16 +1656,5 @@ void ClangdLSPServer::onFileUpdated(PathRef File, const TUStatus &Status) {
1681
1656
NotifyFileStatus (Status.render (File));
1682
1657
}
1683
1658
1684
- void ClangdLSPServer::reparseOpenFilesIfNeeded (
1685
- llvm::function_ref<bool (llvm::StringRef File)> Filter) {
1686
- // Reparse only opened files that were modified.
1687
- for (const Path &FilePath : DraftMgr.getActiveFiles ())
1688
- if (Filter (FilePath))
1689
- if (auto Draft = DraftMgr.getDraft (FilePath)) // else disappeared in race?
1690
- Server->addDocument (FilePath, std::move (Draft->Contents ),
1691
- encodeVersion (Draft->Version ),
1692
- WantDiagnostics::Auto);
1693
- }
1694
-
1695
1659
} // namespace clangd
1696
1660
} // namespace clang
0 commit comments