Commit c1e8604d authored by Thomas Schöps's avatar Thomas Schöps
Browse files

Contextbrowser: Minor improvement to tooltip showing behavior

The aim is to decide properly when the currently shown tooltip needs to be updated and when it doesn't, based on the problems and declaration that are currently being displayed in the tooltip (stored in m_currentToolTipProblems and m_currentToolTipDeclaration), and the new problems and declaration that are under the cursor now (in problems and decl).

Old line 612: The old version always keeps showing the current tooltip if the problems haven't changed and there is no declaration. But there might have been a declaration before, and in this case this declaration's info would incorrectly be continued to be shown. The new version fixes that by removing the "!decl" part and thus always also testing for existence/equality of the declaration.

Old lines 616 .. 648: In case there is no declaration (!decl), the old version does not remember this in m_currentToolTipDeclaration at the top of this block. But the problems are always remembered. In the new version, the logic is clarified: If either there are problems or there is a declaration, then the current state concerning both is remembered in m_currentToolTipProblems and m_currentToolTipDeclaration, and corresponding widgets will be created in the code below. If not, m_currentToolTipProblems and m_currentToolTipDeclaration aren't touched yet, since there is additional code below that handles additional cases.

New lines 665 .. 669: Here the check is extended to also test for equality of the declaration, i.e., for no declaration, since this case is about to show a problems-only tooltip.

New line 672: Here the code is extended to remember the current state of both the problems and the declaration in the tooltip.

New line 684: If the execution reaches this point, neither a problem nor a declaration has been found that shall be displayed. So, instead of returning nullptr and thus continuing to show whatever tooltip is currently being shown, the correct behavior should be to stop showing the tooltip if there is any.
parent 78d2319c
......@@ -590,13 +590,14 @@ QWidget* ContextBrowserPlugin::navigationWidgetForPosition(KTextEditor::View* vi
}
}
// Find problems under the cursor (first pass)
TopDUContext* topContext = DUChainUtils::standardContextForUrl(view->document()->url());
QVector<KDevelop::IProblem::Ptr> problems;
if (topContext) {
// first pass: find problems under the cursor
problems = findProblemsUnderCursor(topContext, position, itemRange);
}
// Find decl (declaration) under the cursor
const auto itemUnderCursor = DUChainUtils::itemUnderCursor(viewUrl, position);
auto declUnderCursor = itemUnderCursor.declaration;
Declaration* decl = DUChainUtils::declarationForDefinition(declUnderCursor);
......@@ -606,19 +607,14 @@ QWidget* ContextBrowserPlugin::navigationWidgetForPosition(KTextEditor::View* vi
decl = alias->aliasedDeclaration().declaration();
}
// Return nullptr if the correct contents are already shown.
// Return nullptr if the found problems / decl are already being shown in the tool tip currently.
if (m_currentToolTip &&
problems == m_currentToolTipProblems &&
(!decl || IndexedDeclaration(decl) == m_currentToolTipDeclaration)) {
IndexedDeclaration(decl) == m_currentToolTipDeclaration) {
return nullptr;
}
// Remember current state.
m_currentToolTipProblems = problems;
if (decl) {
m_currentToolTipDeclaration = IndexedDeclaration(decl);
}
// Create a widget for problems, if any have been found.
AbstractNavigationWidget* problemWidget = nullptr;
if (!problems.isEmpty()) {
problemWidget = new AbstractNavigationWidget;
......@@ -627,6 +623,8 @@ QWidget* ContextBrowserPlugin::navigationWidgetForPosition(KTextEditor::View* vi
problemWidget->setContext(NavigationContextPointer(context));
}
// Let the context create a widget for decl, if there is one.
// Note that createNavigationWidget() might also return nullptr for a valid decl however.
AbstractNavigationWidget* declWidget = nullptr;
if (decl) {
if (itemRange.isValid()) {
......@@ -637,26 +635,40 @@ QWidget* ContextBrowserPlugin::navigationWidgetForPosition(KTextEditor::View* vi
declWidget = decl->context()->createNavigationWidget(decl, DUChainUtils::standardContextForUrl(viewUrl));
}
if (problemWidget && declWidget) {
auto* combinedWidget = new QuickOpenEmbeddedWidgetCombiner;
combinedWidget->layout()->addWidget(problemWidget);
combinedWidget->layout()->addWidget(declWidget);
return combinedWidget;
} else if (problemWidget) {
return problemWidget;
} else if (declWidget) {
// If at least one widget was created for problems or decl, show it.
// If two widgets were created, combine them.
if (problemWidget || declWidget) {
// Remember current tool tip state.
m_currentToolTipProblems = problems;
m_currentToolTipDeclaration = IndexedDeclaration(decl);
if (problemWidget && declWidget) {
auto* combinedWidget = new QuickOpenEmbeddedWidgetCombiner;
combinedWidget->layout()->addWidget(problemWidget);
combinedWidget->layout()->addWidget(declWidget);
return combinedWidget;
}
if (problemWidget) {
return problemWidget;
}
return declWidget;
}
// Nothing has been found so far which created a widget.
// Thus, find the closest problem to the cursor in a second pass.
if (topContext) {
// second pass: find closest problem to the cursor
problems = findProblemsCloseToCursor(topContext, position, view, itemRange);
if (!problems.isEmpty()) {
if (problems == m_currentToolTipProblems && m_currentToolTip) {
// Return nullptr if the correct contents are already being shown in the tool tip currently.
if (m_currentToolTip &&
problems == m_currentToolTipProblems &&
!m_currentToolTipDeclaration.isValid()) {
return nullptr;
}
// Remember current tool tip state.
m_currentToolTipProblems = problems;
m_currentToolTipDeclaration = {};
auto widget = new AbstractNavigationWidget;
// since the problem is not under cursor: show location
......@@ -667,6 +679,8 @@ QWidget* ContextBrowserPlugin::navigationWidgetForPosition(KTextEditor::View* vi
}
}
// Nothing to show has been found under or next to the cursor, so hide the tool tip (if visible).
hideToolTip();
return nullptr;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment