* Takes a filepath and the include paths and determines what directive to use. */
| 329 | * Takes a filepath and the include paths and determines what directive to use. |
| 330 | */ |
| 331 | ClangFixit 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 | |
| 377 | KDevelop::Path::List includePaths( const KDevelop::Path& file ) |
| 378 | { |
no test coverage detected