diff --git a/src/plugins/clangcodemodel/clangdclient.cpp b/src/plugins/clangcodemodel/clangdclient.cpp index 7c307de4ac2..7dd6ee92cd1 100644 --- a/src/plugins/clangcodemodel/clangdclient.cpp +++ b/src/plugins/clangcodemodel/clangdclient.cpp @@ -383,7 +383,7 @@ static void addToCompilationDb(QJsonObject &cdb, const QString fileString = sourceFile.path.toUserOutput(); args.append(fileString); QJsonObject value; - value.insert("workingDirectory", workingDir.toString()); + value.insert("workingDirectory", workingDir.path()); value.insert("compilationCommand", args); cdb.insert(fileString, value); } @@ -520,13 +520,20 @@ void ClangdClient::openExtraFile(const Utils::FilePath &filePath, const QString return; } - QFile cxxFile(filePath.toString()); - if (content.isEmpty() && !cxxFile.open(QIODevice::ReadOnly)) - return; + QString text; + if (!content.isEmpty()) { + text = content; + } else { + expected_str fileContent = filePath.fileContents(); + if (!fileContent) + return; + text = QString::fromUtf8(*std::move(fileContent)); + } + TextDocumentItem item; item.setLanguageId("cpp"); item.setUri(hostPathToServerUri(filePath)); - item.setText(!content.isEmpty() ? content : QString::fromUtf8(cxxFile.readAll())); + item.setText(std::move(text)); item.setVersion(0); sendMessage(DidOpenTextDocumentNotification(DidOpenTextDocumentParams(item)), SendDocUpdates::Ignore); @@ -918,8 +925,7 @@ void ClangdClient::updateParserConfig(const Utils::FilePath &filePath, CppEditor::CompilerOptionsBuilder optionsBuilder = clangOptionsBuilder( *projectPart, warningsConfigForProject(project()), includeDir, ProjectExplorer::Macro::toMacros(config.editorDefines)); - const CppEditor::ProjectFile file(filePath, - CppEditor::ProjectFile::classify(filePath.toString())); + const CppEditor::ProjectFile file(filePath, CppEditor::ProjectFile::classify(filePath)); const QJsonArray projectPartOptions = fullProjectPartOptions( optionsBuilder, globalClangOptions()); const auto cppSettings = CppEditor::CppCodeModelSettings::settingsForProject( diff --git a/src/plugins/clangcodemodel/clangdcompletion.cpp b/src/plugins/clangcodemodel/clangdcompletion.cpp index 4ff9c748f4f..c485cef860e 100644 --- a/src/plugins/clangcodemodel/clangdcompletion.cpp +++ b/src/plugins/clangcodemodel/clangdcompletion.cpp @@ -321,7 +321,7 @@ bool ClangdCompletionAssistProvider::isContinuationChar(const QChar &c) const bool ClangdCompletionAssistProvider::isInCommentOrString(const AssistInterface *interface) const { LanguageFeatures features = LanguageFeatures::defaultFeatures(); - features.objCEnabled = ProjectFile::isObjC(interface->filePath().toString()); + features.objCEnabled = ProjectFile::isObjC(interface->filePath()); return CppEditor::isInCommentOrString(interface, features); } @@ -543,7 +543,7 @@ IAssistProposal *CustomAssistProcessor::perform() : CppCompletionAssistProcessor::preprocessorCompletions()) { completions << createItem(completion, macroIcon); } - if (ProjectFile::isObjC(interface()->filePath().toString())) + if (ProjectFile::isObjC(interface()->filePath())) completions << createItem("import", macroIcon); break; } diff --git a/src/plugins/clangcodemodel/clangdfindreferences.cpp b/src/plugins/clangcodemodel/clangdfindreferences.cpp index 13968801e7b..4d6e7da9238 100644 --- a/src/plugins/clangcodemodel/clangdfindreferences.cpp +++ b/src/plugins/clangcodemodel/clangdfindreferences.cpp @@ -191,22 +191,23 @@ ClangdFindReferences::ClangdFindReferences(ClangdClient *client, const Link &lin d->categorize = true; d->search = search; - if (!client->documentForFilePath(link.targetFilePath)) { - QFile f(link.targetFilePath.toString()); - if (!f.open(QIODevice::ReadOnly)) { + const FilePath &targetFilePath = link.targetFilePath; + if (!client->documentForFilePath(targetFilePath)) { + expected_str fileContents = targetFilePath.fileContents(); + if (!fileContents) { d->finishSearch(); return; } - const QString contents = QString::fromUtf8(f.readAll()); + const QString contents = QString::fromUtf8(*std::move(fileContents)); QTextDocument doc(contents); QTextCursor cursor(&doc); cursor.setPosition(Text::positionInText(&doc, link.targetLine, link.targetColumn + 1)); cursor.select(QTextCursor::WordUnderCursor); d->searchTerm = cursor.selectedText(); - client->openExtraFile(link.targetFilePath, contents); + client->openExtraFile(targetFilePath, contents); d->checkUnusedData->openedExtraFileForLink = true; } - const TextDocumentIdentifier documentId(client->hostPathToServerUri(link.targetFilePath)); + const TextDocumentIdentifier documentId(client->hostPathToServerUri(targetFilePath)); const Position pos(link.targetLine - 1, link.targetColumn); ReferenceParams params(TextDocumentPositionParams(documentId, pos)); params.setContext(ReferenceParams::ReferenceContext(true)); diff --git a/src/plugins/clangcodemodel/clangdiagnostictooltipwidget.cpp b/src/plugins/clangcodemodel/clangdiagnostictooltipwidget.cpp index b415eb23ef6..681294e06e7 100644 --- a/src/plugins/clangcodemodel/clangdiagnostictooltipwidget.cpp +++ b/src/plugins/clangcodemodel/clangdiagnostictooltipwidget.cpp @@ -31,11 +31,11 @@ namespace { const char LINK_ACTION_GOTO_LOCATION[] = "#gotoLocation"; const char LINK_ACTION_APPLY_FIX[] = "#applyFix"; -QString fileNamePrefix(const QString &mainFilePath, const Utils::Link &location) +QString fileNamePrefix(const Utils::FilePath &mainFilePath, const Utils::Link &location) { - const QString filePath = location.targetFilePath.toString(); + const Utils::FilePath &filePath = location.targetFilePath; if (!filePath.isEmpty() && filePath != mainFilePath) - return QFileInfo(filePath).fileName() + QLatin1Char(':'); + return filePath.fileName() + QLatin1Char(':'); return QString(); } @@ -175,8 +175,8 @@ private: QString tableRows(const ClangDiagnostic &diagnostic) { m_mainFilePath = m_displayHints.showFileNameInMainDiagnostic - ? QString() - : diagnostic.location.targetFilePath.toString(); + ? Utils::FilePath{} + : diagnostic.location.targetFilePath; const ClangDiagnostic diag = supplementedDiagnostic(diagnostic); @@ -253,8 +253,8 @@ private: return text; } - QString clickableLocation(const ClangDiagnostic &diagnostic, const QString &mainFilePath, - bool &hasContent) + QString clickableLocation( + const ClangDiagnostic &diagnostic, const Utils::FilePath &mainFilePath, bool &hasContent) { const Utils::Link &location = diagnostic.location; @@ -331,7 +331,7 @@ private: TargetIdToDiagnosticTable m_targetIdsToDiagnostics; unsigned m_targetIdCounter = 0; - QString m_mainFilePath; + Utils::FilePath m_mainFilePath; }; WidgetFromDiagnostics::DisplayHints toHints(const ClangDiagnosticWidget::Destination &destination, diff --git a/src/plugins/clangcodemodel/clangfixitoperation.cpp b/src/plugins/clangcodemodel/clangfixitoperation.cpp index 802bdd17bbb..9e989781351 100644 --- a/src/plugins/clangcodemodel/clangfixitoperation.cpp +++ b/src/plugins/clangcodemodel/clangfixitoperation.cpp @@ -13,7 +13,7 @@ namespace ClangCodeModel { namespace Internal { -using FileToFixits = QMap>; +using FileToFixits = QMap>; using RefactoringFilePtr = QSharedPointer; ClangFixItOperation::ClangFixItOperation(const QString &fixItText, const QList &fixIts) @@ -36,8 +36,8 @@ static FileToFixits fixitsPerFile(const QList &fixIts) FileToFixits mapping; for (const auto &fixItContainer : fixIts) { - const QString rangeStartFilePath = fixItContainer.range.start.targetFilePath.toString(); - const QString rangeEndFilePath = fixItContainer.range.end.targetFilePath.toString(); + const Utils::FilePath &rangeStartFilePath = fixItContainer.range.start.targetFilePath; + const Utils::FilePath &rangeEndFilePath = fixItContainer.range.end.targetFilePath; QTC_CHECK(rangeStartFilePath == rangeEndFilePath); mapping[rangeStartFilePath].append(fixItContainer); } @@ -52,11 +52,10 @@ void ClangFixItOperation::perform() const FileToFixits fileToFixIts = fixitsPerFile(fixIts); for (auto i = fileToFixIts.cbegin(), end = fileToFixIts.cend(); i != end; ++i) { - const QString filePath = i.key(); - const QList fixits = i.value(); + const Utils::FilePath &filePath = i.key(); + const QList &fixits = i.value(); - RefactoringFilePtr refactoringFile = refactoringChanges.file( - Utils::FilePath::fromString(filePath)); + RefactoringFilePtr refactoringFile = refactoringChanges.file(filePath); refactoringFiles.append(refactoringFile); applyFixitsToFile(*refactoringFile, fixits); diff --git a/src/plugins/clangcodemodel/clangmodelmanagersupport.cpp b/src/plugins/clangcodemodel/clangmodelmanagersupport.cpp index f974a52af8a..ace76461d85 100644 --- a/src/plugins/clangcodemodel/clangmodelmanagersupport.cpp +++ b/src/plugins/clangcodemodel/clangmodelmanagersupport.cpp @@ -800,7 +800,7 @@ void ClangModelManagerSupport::watchForExternalChanges() if (!LanguageClientManager::hasClients()) return; for (const FilePath &file : files) { - const ProjectFile::Kind kind = ProjectFile::classify(file.toString()); + const ProjectFile::Kind kind = ProjectFile::classify(file); if (!ProjectFile::isSource(kind) && !ProjectFile::isHeader(kind)) continue; Project * const project = ProjectManager::projectForFile(file); @@ -842,7 +842,7 @@ void ClangModelManagerSupport::watchForInternalChanges() connect(DocumentManager::instance(), &DocumentManager::filesChangedInternally, this, [this](const FilePaths &filePaths) { for (const FilePath &fp : filePaths) { - const ProjectFile::Kind kind = ProjectFile::classify(fp.toString()); + const ProjectFile::Kind kind = ProjectFile::classify(fp); if (!ProjectFile::isSource(kind) && !ProjectFile::isHeader(kind)) continue; Project * const project = ProjectManager::projectForFile(fp); diff --git a/src/plugins/cppeditor/cppprojectfile.cpp b/src/plugins/cppeditor/cppprojectfile.cpp index 580ad1762da..cf3429d0ec5 100644 --- a/src/plugins/cppeditor/cppprojectfile.cpp +++ b/src/plugins/cppeditor/cppprojectfile.cpp @@ -11,6 +11,16 @@ namespace CppEditor { +template +static ProjectFile::Kind classifyImpl(T &&file) +{ + if (ProjectFile::isAmbiguousHeader(file)) + return ProjectFile::Kind::AmbiguousHeader; + + const Utils::MimeType mimeType = Utils::mimeTypeForFile(file); + return ProjectFile::classifyByMimeType(mimeType.name()); +} + ProjectFile::ProjectFile(const Utils::FilePath &filePath, Kind kind, bool active) : path(filePath) , kind(kind) @@ -53,16 +63,22 @@ ProjectFile::Kind ProjectFile::classifyByMimeType(const QString &mt) ProjectFile::Kind ProjectFile::classify(const QString &filePath) { - if (isAmbiguousHeader(filePath)) - return AmbiguousHeader; - - const Utils::MimeType mimeType = Utils::mimeTypeForFile(filePath); - return classifyByMimeType(mimeType.name()); + return classifyImpl(filePath); } -bool ProjectFile::isAmbiguousHeader(const QString &filePath) +ProjectFile::Kind ProjectFile::classify(const Utils::FilePath &filePath) { - return filePath.endsWith(".h"); + return classifyImpl(filePath); +} + +bool ProjectFile::isAmbiguousHeader(QStringView filePath) +{ + return filePath.endsWith(u".h"); +} + +bool ProjectFile::isAmbiguousHeader(const Utils::FilePath &filePath) +{ + return isAmbiguousHeader(filePath.fileNameView()); } bool ProjectFile::isObjC(const QString &filePath) @@ -70,6 +86,11 @@ bool ProjectFile::isObjC(const QString &filePath) return isObjC(classify(filePath)); } +bool ProjectFile::isObjC(const Utils::FilePath &filePath) +{ + return isObjC(classify(filePath)); +} + bool ProjectFile::isObjC(Kind kind) { switch (kind) { @@ -131,7 +152,7 @@ bool ProjectFile::isHeader(ProjectFile::Kind kind) bool ProjectFile::isHeader(const Utils::FilePath &fp) { - return isHeader(classify(fp.toString())); + return isHeader(classify(fp)); } bool ProjectFile::isSource(ProjectFile::Kind kind) diff --git a/src/plugins/cppeditor/cppprojectfile.h b/src/plugins/cppeditor/cppprojectfile.h index 529d0bb6558..3451603d18e 100644 --- a/src/plugins/cppeditor/cppprojectfile.h +++ b/src/plugins/cppeditor/cppprojectfile.h @@ -35,6 +35,7 @@ public: static Kind classifyByMimeType(const QString &mt); static Kind classify(const QString &filePath); + static Kind classify(const Utils::FilePath &filePath); static Kind sourceForHeaderKind(Kind kind); static Kind sourceKind(Kind kind); @@ -44,8 +45,10 @@ public: static bool isHeader(const Utils::FilePath &fp); static bool isC(Kind kind); static bool isCxx(Kind kind); - static bool isAmbiguousHeader(const QString &filePath); + static bool isAmbiguousHeader(QStringView filePath); + static bool isAmbiguousHeader(const Utils::FilePath &filePath); static bool isObjC(const QString &filePath); + static bool isObjC(const Utils::FilePath &filePath); static bool isObjC(Kind kind); bool isHeader() const;