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

Function directiveForFile

plugins/clang/duchain/unknowndeclarationproblem.cpp:331–375  ·  view source on GitHub ↗

* Takes a filepath and the include paths and determines what directive to use. */

Source from the content-addressed store, hash-verified

329 * Takes a filepath and the include paths and determines what directive to use.
330 */
331ClangFixit directiveForFile( const QString& includefile, const KDevelop::Path::List& includepaths, const KDevelop::Path& source )
332{
333 const auto sourceFolder = source.parent();
334 const Path canonicalFile( QFileInfo( includefile ).canonicalFilePath() );
335
336 QString shortestDirective;
337 bool isRelative = false;
338
339 // we can include the file directly
340 if (sourceFolder == canonicalFile.parent()) {
341 shortestDirective = canonicalFile.lastPathSegment();
342 isRelative = true;
343 } else {
344 // find the include directive with the shortest length
345 for( const auto& includePath : includepaths ) {
346 QString relative = includePath.relativePath( canonicalFile );
347 if( relative.startsWith( QLatin1String("./") ) )
348 relative.remove(0, 2);
349
350 if( shortestDirective.isEmpty() || relative.length() < shortestDirective.length() ) {
351 shortestDirective = relative;
352 isRelative = includePath == sourceFolder;
353 }
354 }
355 }
356
357 if( shortestDirective.isEmpty() ) {
358 // Item not found in include path
359 return {};
360 }
361
362 const auto range = DocumentRange(IndexedString(source.pathOrUrl()), includeDirectivePosition(source, canonicalFile.lastPathSegment()));
363 if( !range.isValid() ) {
364 clangDebug() << "unable to determine valid position for" << includefile << "in" << source.pathOrUrl();
365 return {};
366 }
367
368 QString directive;
369 if( isRelative ) {
370 directive = QStringLiteral("#include \"%1\"").arg(shortestDirective);
371 } else {
372 directive = QStringLiteral("#include <%1>").arg(shortestDirective);
373 }
374 return ClangFixit{directive + QLatin1Char('\n'), range, i18n("Insert \'%1\'", directive), QString()};
375}
376
377KDevelop::Path::List includePaths( const KDevelop::Path& file )
378{

Callers 1

fixUnknownDeclarationFunction · 0.85

Calls 13

includeDirectivePositionFunction · 0.85
lastPathSegmentMethod · 0.80
relativePathMethod · 0.80
pathOrUrlMethod · 0.80
QFileInfoClass · 0.50
DocumentRangeClass · 0.50
IndexedStringClass · 0.50
QStringClass · 0.50
parentMethod · 0.45
removeMethod · 0.45
isEmptyMethod · 0.45
lengthMethod · 0.45

Tested by

no test coverage detected