LanguageClient: correctly disconnect documents changed signal on reset

connect calls to a lambda can not be disconnected with the
sender->disconnect(receiver);
syntax, so save the connection in a QMetaObject::Connection and use this
to disconnect the signal.

Fixes: QTCREATORBUG-27596
Change-Id: I69f5d990aab4e85d768e2101f0157a7dee3c1fa1
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
David Schulz
2022-08-18 10:16:42 +02:00
parent e4b35fa576
commit dc2cd9db94

View File

@@ -278,7 +278,17 @@ public:
QString m_displayName; QString m_displayName;
LanguageFilter m_languagFilter; LanguageFilter m_languagFilter;
QJsonObject m_initializationOptions; QJsonObject m_initializationOptions;
QMap<TextEditor::TextDocument *, QString> m_openedDocument; class OpenedDocument
{
public:
~OpenedDocument()
{
QObject::disconnect(contentsChangedConnection);
}
QMetaObject::Connection contentsChangedConnection;
QString documentContents;
};
QMap<TextEditor::TextDocument *, OpenedDocument> m_openedDocument;
// Used for build system artifacts (e.g. UI headers) that Qt Creator "live-generates" ahead of // Used for build system artifacts (e.g. UI headers) that Qt Creator "live-generates" ahead of
// the build. // the build.
@@ -610,8 +620,11 @@ void Client::openDocument(TextEditor::TextDocument *document)
} }
} }
d->m_openedDocument[document] = document->plainText(); d->m_openedDocument[document].documentContents = document->plainText();
connect(document, &TextDocument::contentsChangedWithPosition, this, d->m_openedDocument[document].contentsChangedConnection
= connect(document,
&TextDocument::contentsChangedWithPosition,
this,
[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);
}); });
@@ -1075,7 +1088,7 @@ void Client::documentContentsChanged(TextEditor::TextDocument *document,
} }
} }
if (append) { if (append) {
QTextDocument oldDoc(d->m_openedDocument[document]); QTextDocument oldDoc(d->m_openedDocument[document].documentContents);
QTextCursor cursor(&oldDoc); QTextCursor cursor(&oldDoc);
// Workaround https://bugreports.qt.io/browse/QTBUG-80662 // Workaround https://bugreports.qt.io/browse/QTBUG-80662
// The contentsChanged gives a character count that can be wrong for QTextCursor // The contentsChanged gives a character count that can be wrong for QTextCursor
@@ -1096,7 +1109,7 @@ void Client::documentContentsChanged(TextEditor::TextDocument *document,
d->m_documentsToUpdate[document] = { d->m_documentsToUpdate[document] = {
DidChangeTextDocumentParams::TextDocumentContentChangeEvent(document->plainText())}; DidChangeTextDocumentParams::TextDocumentContentChangeEvent(document->plainText())};
} }
d->m_openedDocument[document] = document->plainText(); d->m_openedDocument[document].documentContents = document->plainText();
} }
++d->m_documentVersions[document->filePath()]; ++d->m_documentVersions[document->filePath()];
@@ -1521,8 +1534,6 @@ bool ClientPrivate::reset()
m_dynamicCapabilities.reset(); m_dynamicCapabilities.reset();
if (m_diagnosticManager) if (m_diagnosticManager)
m_diagnosticManager->clearDiagnostics(); m_diagnosticManager->clearDiagnostics();
for (auto it = m_openedDocument.cbegin(); it != m_openedDocument.cend(); ++it)
it.key()->disconnect(this);
m_openedDocument.clear(); m_openedDocument.clear();
// temporary container needed since m_resetAssistProvider is changed in resetAssistProviders // temporary container needed since m_resetAssistProvider is changed in resetAssistProviders
for (TextEditor::TextDocument *document : m_resetAssistProvider.keys()) for (TextEditor::TextDocument *document : m_resetAssistProvider.keys())