Optionally use qmlformat for Qt Quick auto format on save

Task-number: QTCREATORBUG-28192
Task-number: QTCREATORBUG-26602
Change-Id: I22a3917e8a4de4aaf297e9d9d622bdede782c6a6
Reviewed-by: Xavier BESSON <developer@xavi-b.fr>
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
Xavier BESSON
2022-11-03 15:20:13 +03:00
parent a157de7877
commit 3694d00dce
5 changed files with 165 additions and 12 deletions

View File

@@ -30,12 +30,15 @@
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/projecttree.h>
#include <projectexplorer/taskhub.h>
#include <texteditor/command.h>
#include <texteditor/formattexteditor.h>
#include <texteditor/snippets/snippetprovider.h>
#include <texteditor/tabsettings.h>
#include <texteditor/texteditor.h>
#include <texteditor/texteditorconstants.h>
#include <utils/fsengine/fileiconprovider.h>
#include <utils/json.h>
#include <utils/macroexpander.h>
#include <utils/qtcassert.h>
#include <QTextDocument>
@@ -237,6 +240,35 @@ void QmlJSEditorPluginPrivate::renameUsages()
void QmlJSEditorPluginPrivate::reformatFile()
{
if (m_currentDocument) {
if (QmlJsEditingSettings::get().useCustomFormatCommand()) {
QString formatCommand = QmlJsEditingSettings::get().formatCommand();
if (formatCommand.isEmpty())
formatCommand = QmlJsEditingSettings::get().defaultFormatCommand();
const auto exe = FilePath::fromUserInput(globalMacroExpander()->expand(formatCommand));
const QString args = globalMacroExpander()->expand(
QmlJsEditingSettings::get().formatCommandOptions());
const CommandLine commandLine(exe, args, CommandLine::Raw);
TextEditor::Command command;
command.setExecutable(commandLine.executable().toString());
command.setProcessing(TextEditor::Command::FileProcessing);
command.addOptions(commandLine.splitArguments());
command.addOption("--inplace");
command.addOption("%file");
if (!command.isValid())
return;
const QList<Core::IEditor *> editors = Core::DocumentModel::editorsForDocument(m_currentDocument);
if (editors.isEmpty())
return;
IEditor *currentEditor = EditorManager::currentEditor();
IEditor *editor = editors.contains(currentEditor) ? currentEditor : editors.first();
if (auto widget = TextEditor::TextEditorWidget::fromEditor(editor))
TextEditor::formatEditor(widget, command);
return;
}
QmlJS::Document::Ptr document = m_currentDocument->semanticInfo().document;
QmlJS::Snapshot snapshot = QmlJS::ModelManagerInterface::instance()->snapshot();