MCPcopy Create free account
hub / github.com/KDE/kdevelop / jumpToNearestFunction

Method jumpToNearestFunction

plugins/quickopen/quickopenplugin.cpp:728–790  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

726}
727
728void QuickOpenPlugin::jumpToNearestFunction(QuickOpenPlugin::FunctionJumpDirection direction)
729{
730 IDocument* doc = ICore::self()->documentController()->activeDocument();
731 if (!doc) {
732 qCDebug(PLUGIN_QUICKOPEN) << "No active document";
733 return;
734 }
735
736 KDevelop::DUChainReadLocker lock(DUChain::lock());
737
738 TopDUContext* context = DUChainUtils::standardContextForUrl(doc->url());
739
740 if (!context) {
741 qCDebug(PLUGIN_QUICKOPEN) << "Got no standard context";
742 return;
743 }
744
745 QVector<DUChainItem> items;
746 OutlineFilter filter(items, OutlineFilter::Functions);
747 DUChainUtils::collectItems(context, filter);
748
749 CursorInRevision cursor = context->transformToLocalRevision(KTextEditor::Cursor(doc->cursorPosition()));
750 if (!cursor.isValid()) {
751 return;
752 }
753
754 Declaration* nearestDeclBefore = nullptr;
755 int distanceBefore = INT_MIN;
756 Declaration* nearestDeclAfter = nullptr;
757 int distanceAfter = INT_MAX;
758
759 for (auto& item : std::as_const(items)) {
760 Declaration* decl = item.m_item.data();
761
762 int distance = decl->range().start.line - cursor.line;
763 if (distance < 0 && distance >= distanceBefore) {
764 distanceBefore = distance;
765 nearestDeclBefore = decl;
766 } else if (distance > 0 && distance <= distanceAfter) {
767 distanceAfter = distance;
768 nearestDeclAfter = decl;
769 }
770 }
771
772 CursorInRevision c = CursorInRevision::invalid();
773 if (direction == QuickOpenPlugin::NextFunction && nearestDeclAfter) {
774 c = nearestDeclAfter->range().start;
775 } else if (direction == QuickOpenPlugin::PreviousFunction && nearestDeclBefore) {
776 c = nearestDeclBefore->range().start;
777 }
778
779 KTextEditor::Cursor textCursor = KTextEditor::Cursor::invalid();
780 if (c.isValid()) {
781 textCursor = context->transformFromLocalRevision(c);
782 }
783
784 lock.unlock();
785 if (textCursor.isValid()) {

Callers

nothing calls this directly

Calls 13

activeDocumentMethod · 0.80
documentControllerMethod · 0.80
CursorClass · 0.70
invalidFunction · 0.50
urlMethod · 0.45
cursorPositionMethod · 0.45
isValidMethod · 0.45
dataMethod · 0.45
rangeMethod · 0.45
unlockMethod · 0.45

Tested by

no test coverage detected