From 7bef09a0b9f9060c1235a104454907f55c2ab0bb Mon Sep 17 00:00:00 2001 From: rthakur-gateway Date: Sat, 31 Oct 2026 23:40:05 +0530 Subject: [PATCH] gopls: fix mouse navigation race with hover response Ctrl/Cmd-hover sends textDocument/hover and textDocument/definition together. The link was only shown if the hover reply arrived first; otherwise the definition reply cleared it, so click navigation failed intermittently. Each mouse move also cancelled the prior hover, making stale definition replies clear the link. Build the link from the definition reply alone (hover text is optional), ignore all but the latest navigation request per editor, and drop pending navigation state when an editor closes. Co-Authored-By: Claude Opus 5.5 --- liteidex/src/plugins/gopls/goplsplugin.cpp | 19 ++++++++++--------- liteidex/src/plugins/gopls/goplsplugin.h | 1 + 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/liteidex/src/plugins/gopls/goplsplugin.cpp b/liteidex/src/plugins/gopls/goplsplugin.cpp index b8ef14f8..d8c0b248 100644 --- a/liteidex/src/plugins/gopls/goplsplugin.cpp +++ b/liteidex/src/plugins/gopls/goplsplugin.cpp @@ -293,6 +293,7 @@ void GoplsPlugin::clientStopped() m_navigationStarts.clear(); m_navigationEnds.clear(); m_navigationHoverText.clear(); + m_pendingNavigations.clear(); m_metadataWarnings.clear(); setLegacyCompletionEnabled(true); if (m_restartClient) { @@ -493,6 +494,8 @@ void GoplsPlugin::editorAboutToClose(LiteApi::IEditor *editor) m_completionEditors.remove(requestId); m_completionPrefixes.remove(requestId); m_completionRoots.remove(requestId); + m_navigationEditors.remove(m_pendingNavigations.take(editor)); + m_navigationHoverText.remove(editor); int hoverId = m_pendingHovers.take(editor); if (hoverId) { m_client->notify("$/cancelRequest",QJsonObject{{"id",hoverId}}); @@ -667,14 +670,11 @@ void GoplsPlugin::handleLocationResponse(int id, const QJsonValue &result, const } QJsonObject start = range.value("start").toObject(); if (navigationEditor) { - QString hoverText = m_navigationHoverText.take(navigationEditor); - if (hoverText.isEmpty()) { - LiteApi::ILiteEditor *liteEditor = LiteApi::getLiteEditor(navigationEditor); - if (liteEditor) { - liteEditor->clearLink(); - } + if (m_pendingNavigations.value(navigationEditor) != id) { return; } + m_pendingNavigations.remove(navigationEditor); + QString hoverText = m_navigationHoverText.take(navigationEditor); LiteApi::Link link; link.linkTextStart = navigationStart; link.linkTextEnd = navigationEnd; @@ -686,8 +686,7 @@ void GoplsPlugin::handleLocationResponse(int id, const QJsonValue &result, const link.targetColumn = start.value("character").toInt(); QString location = QString("> %1:%2:%3") .arg(link.targetFileName).arg(link.targetLine + 1).arg(link.targetColumn + 1); - link.sourceInfo = hoverText + "\n\n"; - link.sourceInfo += location; + link.sourceInfo = hoverText.isEmpty() ? location : hoverText + "\n\n" + location; link.targetInfo = link.sourceInfo; LiteApi::ILiteEditor *liteEditor = LiteApi::getLiteEditor(navigationEditor); if (liteEditor) { @@ -697,7 +696,8 @@ void GoplsPlugin::handleLocationResponse(int id, const QJsonValue &result, const } LiteApi::gotoLine(m_liteApp,QUrl(uri).toLocalFile(),start.value("line").toInt(),start.value("character").toInt(),true,true); } - else if (navigationEditor) { + else if (navigationEditor && m_pendingNavigations.value(navigationEditor) == id) { + m_pendingNavigations.remove(navigationEditor); LiteApi::ILiteEditor *liteEditor = LiteApi::getLiteEditor(navigationEditor); if (liteEditor) { liteEditor->clearLink(); @@ -911,6 +911,7 @@ void GoplsPlugin::hoverRequested(const QTextCursor &cursor, const QPoint &positi m_locationRequests.insert(id,"textDocument/definition"); m_locationSearchText.insert(id,LiteApi::wordUnderCursor(requestCursor)); m_navigationEditors.insert(id,editor); + m_pendingNavigations.insert(editor,id); m_navigationPositions.insert(id,position); m_navigationStarts.insert(id,cursor.selectionStart()); m_navigationEnds.insert(id,cursor.selectionEnd()); diff --git a/liteidex/src/plugins/gopls/goplsplugin.h b/liteidex/src/plugins/gopls/goplsplugin.h index 87dbd3f1..61841678 100644 --- a/liteidex/src/plugins/gopls/goplsplugin.h +++ b/liteidex/src/plugins/gopls/goplsplugin.h @@ -94,6 +94,7 @@ private slots: QHash m_navigationStarts; QHash m_navigationEnds; QHash m_navigationHoverText; + QHash m_pendingNavigations; GoplsSearchResults *m_searchResults; QAction *m_definitionAction; QAction *m_referencesAction;