aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/beautifier/beautifierplugin.cpp
diff options
context:
space:
mode:
authorLorenz Haas <lorenz.haas@histomatics.de>2016-05-19 21:30:27 +0200
committerLorenz Haas <lorenz.haas@histomatics.de>2016-06-01 07:41:37 +0000
commit3af36134d1fde899c5ea424f101b4d07ec863d85 (patch)
treebf327b9cee4f71d8a68d01843c1a2d8355c3fda1 /src/plugins/beautifier/beautifierplugin.cpp
parent069714b832e2c8eb79ea0de7593bcfacc213fbf0 (diff)
Beautifier: Make all tools MIME restrict-able
The newly introduced auto save option is restricted to user definable MIME types. The underlaying tool's restriction, however, is only if the current editor is a cpp editor. This patch makes the tools also MIME types restrict-able. In addition the auto save functionality is now only applicable if the file matches the auto save MIME types as well as the MIME types of the chosen tool. Change-Id: Ic430b4a07341647e6c8e95d2b802a17db1637a36 Reviewed-by: Leena Miettinen <riitta-leena.miettinen@qt.io>
Diffstat (limited to 'src/plugins/beautifier/beautifierplugin.cpp')
-rw-r--r--src/plugins/beautifier/beautifierplugin.cpp31
1 files changed, 15 insertions, 16 deletions
diff --git a/src/plugins/beautifier/beautifierplugin.cpp b/src/plugins/beautifier/beautifierplugin.cpp
index d3f0bbacb9..5fc7c47075 100644
--- a/src/plugins/beautifier/beautifierplugin.cpp
+++ b/src/plugins/beautifier/beautifierplugin.cpp
@@ -171,20 +171,20 @@ QString sourceData(TextEditorWidget *editor, int startPos, int endPos)
: Convenience::textAt(editor->textCursor(), startPos, (endPos - startPos));
}
-bool isAutoFormatApplicable(const QString &filePath, const QList<Utils::MimeType> &allowedMimeTypes)
+bool isAutoFormatApplicable(const Core::IDocument *document,
+ const QList<Utils::MimeType> &allowedMimeTypes)
{
+ if (!document)
+ return false;
+
if (allowedMimeTypes.isEmpty())
return true;
const Utils::MimeDatabase mdb;
- const QList<Utils::MimeType> fileMimeTypes = mdb.mimeTypesForFileName(filePath);
- auto inheritedByFileMimeTypes = [&fileMimeTypes](const Utils::MimeType &mimeType){
- const QString name = mimeType.name();
- return Utils::anyOf(fileMimeTypes, [&name](const Utils::MimeType &fileMimeType){
- return fileMimeType.inherits(name);
- });
- };
- return Utils::anyOf(allowedMimeTypes, inheritedByFileMimeTypes);
+ const Utils::MimeType documentMimeType = mdb.mimeTypeForName(document->mimeType());
+ return Utils::anyOf(allowedMimeTypes, [&documentMimeType](const Utils::MimeType &mime) {
+ return documentMimeType.inherits(mime.name());
+ });
}
bool BeautifierPlugin::initialize(const QStringList &arguments, QString *errorString)
@@ -244,19 +244,16 @@ void BeautifierPlugin::autoFormatOnSave(Core::IDocument *document)
if (!m_generalSettings->autoFormatOnSave())
return;
- // Check that we are dealing with a cpp editor
- if (document->id() != CppEditor::Constants::CPPEDITOR_ID)
- return;
- const QString filePath = document->filePath().toString();
-
- if (!isAutoFormatApplicable(filePath, m_generalSettings->autoFormatMime()))
+ if (!isAutoFormatApplicable(document, m_generalSettings->autoFormatMime()))
return;
// Check if file is contained in the current project (if wished)
if (m_generalSettings->autoFormatOnlyCurrentProject()) {
const ProjectExplorer::Project *pro = ProjectExplorer::ProjectTree::currentProject();
- if (!pro || !pro->files(ProjectExplorer::Project::SourceFiles).contains(filePath))
+ if (!pro || !pro->files(ProjectExplorer::Project::SourceFiles).contains(
+ document->filePath().toString())) {
return;
+ }
}
// Find tool to use by id and format file!
@@ -264,6 +261,8 @@ void BeautifierPlugin::autoFormatOnSave(Core::IDocument *document)
auto tool = std::find_if(m_tools.constBegin(), m_tools.constEnd(),
[&id](const BeautifierAbstractTool *t){return t->id() == id;});
if (tool != m_tools.constEnd()) {
+ if (!(*tool)->isApplicable(document))
+ return;
const Command command = (*tool)->command();
if (!command.isValid())
return;