| 288 | } |
| 289 | |
| 290 | void SourceFormatterController::FileFormatter::projectOpened(const IProject& project, |
| 291 | const QVector<ISourceFormatter*>& formatters) |
| 292 | { |
| 293 | // Adapt the indentation mode if a project was just opened. Otherwise if a document |
| 294 | // is loaded before its project, it might not have the correct indentation mode set. |
| 295 | |
| 296 | if (formatters.empty()) { |
| 297 | return; |
| 298 | } |
| 299 | |
| 300 | const auto config = Config::projectConfig(project); |
| 301 | if (!Config::projectOverridesSession(config)) { |
| 302 | return; // The opened project does not specify indentation => nothing to do. |
| 303 | } |
| 304 | |
| 305 | OwningRawPointerContainer<QHash<QString, FileFormatter*>> fileFormatterCache; |
| 306 | const auto fileFormatterForUrl = [&fileFormatterCache, &config, &formatters](QUrl&& url, QMimeType&& mimeType) { |
| 307 | const auto mimeTypeName = mimeType.name(); |
| 308 | |
| 309 | auto ff = fileFormatterCache->value(mimeTypeName); |
| 310 | if (ff) { |
| 311 | ff->m_url = std::move(url); |
| 312 | Q_ASSERT_X(ff->m_mimeType == mimeType, Q_FUNC_INFO, |
| 313 | "When MIME type names are equal, the MIME types must also compare equal."); |
| 314 | // All other ff's data members already have correct values: |
| 315 | // * m_sourceFormatterConfig equals config for all elements of fileFormatterCache; |
| 316 | // * m_formatter and m_style are determined by config and m_mimeType. |
| 317 | } else { |
| 318 | const auto data = Config::readFormatterData(config, mimeTypeName, formatters); |
| 319 | if (data.isValid()) { |
| 320 | ff = new FileFormatter(std::move(url), std::move(mimeType), config, data.formatter, data.style()); |
| 321 | } |
| 322 | fileFormatterCache->insert(mimeTypeName, ff); |
| 323 | } |
| 324 | return std::as_const(ff); |
| 325 | }; |
| 326 | |
| 327 | const auto documents = ICore::self()->documentController()->openDocuments(); |
| 328 | for (const IDocument* doc : documents) { |
| 329 | auto url = doc->url(); |
| 330 | if (!project.inProject(IndexedString{url})) { |
| 331 | continue; |
| 332 | } |
| 333 | if (const auto* const ff = fileFormatterForUrl(std::move(url), doc->mimeType())) { |
| 334 | ff->adaptEditorIndentationMode(doc->textDocument()); |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | void SourceFormatterController::pluginLoaded(IPlugin* plugin) |
| 340 | { |
nothing calls this directly
no test coverage detected