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

Method switchDefinitionDeclaration

plugins/switchtobuddy/switchtobuddyplugin.cpp:156–294  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

154}
155
156void SwitchToBuddyPlugin::switchDefinitionDeclaration()
157{
158 qCDebug(PLUGIN_SWITCHTOBUDDY) << "switching definition/declaration";
159
160 QUrl docUrl;
161 KTextEditor::Cursor cursor;
162
163 ///Step 1: Find the current top-level context of type DUContext::Other(the highest code-context).
164 ///-- If it belongs to a function-declaration or definition, it can be retrieved through owner(), and we are in a definition.
165 ///-- If no such context could be found, search for a declaration on the same line as the cursor, and switch to the according definition
166 {
167 auto view = ICore::self()->documentController()->activeTextDocumentView();
168 if (!view) {
169 qCDebug(PLUGIN_SWITCHTOBUDDY) << "No active document";
170 return;
171 }
172
173 docUrl = view->document()->url();
174 cursor = view->cursorPosition();
175 }
176
177 QString switchCandidate = findSwitchCandidate(docUrl);
178 if(!switchCandidate.isEmpty()) {
179
180 DUChainReadLocker lock;
181
182 //If the file has not been parsed yet, update it
183 TopDUContext* ctx = DUChainUtils::standardContextForUrl(docUrl);
184 //At least 'VisibleDeclarationsAndContexts' is required so we can do a switch
185 if (!ctx || (ctx->parsingEnvironmentFile() && !ctx->parsingEnvironmentFile()->featuresSatisfied(TopDUContext::AllDeclarationsContextsAndUses))) {
186 lock.unlock();
187 qCDebug(PLUGIN_SWITCHTOBUDDY) << "Parsing switch-candidate before switching" << switchCandidate;
188 ReferencedTopDUContext updatedContext = DUChain::self()->waitForUpdate(IndexedString(switchCandidate), TopDUContext::AllDeclarationsContextsAndUses);
189 if (!updatedContext) {
190 qCDebug(PLUGIN_SWITCHTOBUDDY) << "Failed to update document:" << switchCandidate;
191 return;
192 }
193 }
194 }
195
196 qCDebug(PLUGIN_SWITCHTOBUDDY) << "Document:" << docUrl;
197
198 DUChainReadLocker lock;
199
200 TopDUContext* standardCtx = DUChainUtils::standardContextForUrl(docUrl);
201
202 bool wasSignal = false;
203 if (standardCtx) {
204 Declaration* definition = nullptr;
205
206 DUContext* ctx = standardCtx->findContext(standardCtx->transformToLocalRevision(cursor));
207 if (!ctx) {
208 ctx = standardCtx;
209 }
210
211 while (ctx && ctx->parentContext() && (ctx->parentContext()->type() == DUContext::Other || ctx->parentContext()->type() == DUContext::Function)) {
212 ctx = ctx->parentContext();
213 }

Callers

nothing calls this directly

Calls 15

findSwitchCandidateFunction · 0.85
normalizeCursorFunction · 0.85
documentControllerMethod · 0.80
featuresSatisfiedMethod · 0.80
waitForUpdateMethod · 0.80
findContextMethod · 0.80
isDefinitionMethod · 0.80
isSignalMethod · 0.80

Tested by

no test coverage detected