LanguageClient: close and reopen renamed documents

That is the way accoarding to https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_didRename

Change-Id: Ibed1873ac85eb2e9074481537a34f959a55a6f71
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
David Schulz
2023-01-23 11:27:38 +01:00
parent ea1d729669
commit 9938fffe06
2 changed files with 18 additions and 3 deletions

View File

@@ -273,9 +273,11 @@ public:
~OpenedDocument() ~OpenedDocument()
{ {
QObject::disconnect(contentsChangedConnection); QObject::disconnect(contentsChangedConnection);
QObject::disconnect(filePathChangedConnection);
delete document; delete document;
} }
QMetaObject::Connection contentsChangedConnection; QMetaObject::Connection contentsChangedConnection;
QMetaObject::Connection filePathChangedConnection;
QTextDocument *document = nullptr; QTextDocument *document = nullptr;
}; };
QMap<TextEditor::TextDocument *, OpenedDocument> m_openedDocument; QMap<TextEditor::TextDocument *, OpenedDocument> m_openedDocument;
@@ -623,6 +625,17 @@ void Client::openDocument(TextEditor::TextDocument *document)
[this, document](int position, int charsRemoved, int charsAdded) { [this, document](int position, int charsRemoved, int charsAdded) {
documentContentsChanged(document, position, charsRemoved, charsAdded); documentContentsChanged(document, position, charsRemoved, charsAdded);
}); });
d->m_openedDocument[document].filePathChangedConnection
= connect(document,
&TextDocument::filePathChanged,
this,
[this, document](const FilePath &oldPath, const FilePath &newPath) {
if (oldPath == newPath)
return;
closeDocument(document, oldPath);
if (isSupportedDocument(document))
openDocument(document);
});
if (!d->m_documentVersions.contains(filePath)) if (!d->m_documentVersions.contains(filePath))
d->m_documentVersions[filePath] = 0; d->m_documentVersions[filePath] = 0;
d->sendOpenNotification(filePath, document->mimeType(), document->plainText(), d->sendOpenNotification(filePath, document->mimeType(), document->plainText(),
@@ -661,7 +674,8 @@ void Client::cancelRequest(const MessageId &id)
sendMessage(CancelRequest(CancelParameter(id)), SendDocUpdates::Ignore); sendMessage(CancelRequest(CancelParameter(id)), SendDocUpdates::Ignore);
} }
void Client::closeDocument(TextEditor::TextDocument *document) void Client::closeDocument(TextEditor::TextDocument *document,
const std::optional<FilePath> &overwriteFilePath)
{ {
deactivateDocument(document); deactivateDocument(document);
d->m_postponedDocuments.remove(document); d->m_postponedDocuments.remove(document);
@@ -669,7 +683,7 @@ void Client::closeDocument(TextEditor::TextDocument *document)
if (d->m_openedDocument.remove(document) != 0) { if (d->m_openedDocument.remove(document) != 0) {
handleDocumentClosed(document); handleDocumentClosed(document);
if (d->m_state == Initialized) if (d->m_state == Initialized)
d->sendCloseNotification(document->filePath()); d->sendCloseNotification(overwriteFilePath.value_or(document->filePath()));
} }
if (d->m_state != Initialized) if (d->m_state != Initialized)

View File

@@ -110,7 +110,8 @@ public:
bool isSupportedFile(const Utils::FilePath &filePath, const QString &mimeType) const; bool isSupportedFile(const Utils::FilePath &filePath, const QString &mimeType) const;
bool isSupportedUri(const LanguageServerProtocol::DocumentUri &uri) const; bool isSupportedUri(const LanguageServerProtocol::DocumentUri &uri) const;
virtual void openDocument(TextEditor::TextDocument *document); virtual void openDocument(TextEditor::TextDocument *document);
void closeDocument(TextEditor::TextDocument *document); void closeDocument(TextEditor::TextDocument *document,
const std::optional<Utils::FilePath> &overwriteFilePath = {});
void activateDocument(TextEditor::TextDocument *document); void activateDocument(TextEditor::TextDocument *document);
void activateEditor(Core::IEditor *editor); void activateEditor(Core::IEditor *editor);
void deactivateDocument(TextEditor::TextDocument *document); void deactivateDocument(TextEditor::TextDocument *document);