forked from qt-creator/qt-creator
QmlJSEditor: Add option to automatically format QML/JS file on save
Change-Id: Ide1810efdef98595705daa32c83fecc2ad367a49 Reviewed-by: Leena Miettinen <riitta-leena.miettinen@qt.io> Reviewed-by: Thomas Hartmann <Thomas.Hartmann@theqtcompany.com>
This commit is contained in:
@@ -23,18 +23,18 @@
|
||||
**
|
||||
****************************************************************************/
|
||||
|
||||
#include "qmljseditorplugin.h"
|
||||
#include "qmljshighlighter.h"
|
||||
#include "qmljseditingsettingspage.h"
|
||||
#include "qmljseditor.h"
|
||||
#include "qmljseditorconstants.h"
|
||||
#include "qmljseditordocument.h"
|
||||
#include "qmljseditorplugin.h"
|
||||
#include "qmljshighlighter.h"
|
||||
#include "qmljsoutline.h"
|
||||
#include "qmljspreviewrunner.h"
|
||||
#include "qmljsquickfixassist.h"
|
||||
#include "qmljssnippetprovider.h"
|
||||
#include "qmltaskmanager.h"
|
||||
#include "quicktoolbar.h"
|
||||
#include "quicktoolbarsettingspage.h"
|
||||
#include "qmljsquickfixassist.h"
|
||||
|
||||
#include <qmljs/qmljsicons.h>
|
||||
#include <qmljs/qmljsmodelmanagerinterface.h>
|
||||
@@ -50,6 +50,8 @@
|
||||
#include <coreplugin/actionmanager/command.h>
|
||||
#include <coreplugin/editormanager/editormanager.h>
|
||||
#include <projectexplorer/taskhub.h>
|
||||
#include <projectexplorer/project.h>
|
||||
#include <projectexplorer/projecttree.h>
|
||||
#include <projectexplorer/projectexplorerconstants.h>
|
||||
#include <texteditor/texteditorconstants.h>
|
||||
#include <utils/qtcassert.h>
|
||||
@@ -193,11 +195,14 @@ bool QmlJSEditorPlugin::initialize(const QStringList & /*arguments*/, QString *e
|
||||
addAutoReleasedObject(new QmlJSOutlineWidgetFactory);
|
||||
|
||||
addAutoReleasedObject(new QuickToolBar);
|
||||
addAutoReleasedObject(new QuickToolBarSettingsPage);
|
||||
addAutoReleasedObject(new QmlJsEditingSettingsPage);
|
||||
|
||||
connect(EditorManager::instance(), &EditorManager::currentEditorChanged,
|
||||
this, &QmlJSEditorPlugin::currentEditorChanged);
|
||||
|
||||
connect(EditorManager::instance(), &Core::EditorManager::aboutToSave,
|
||||
this, &QmlJSEditorPlugin::autoFormatOnSave);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -234,9 +239,25 @@ void QmlJSEditorPlugin::renameUsages()
|
||||
void QmlJSEditorPlugin::reformatFile()
|
||||
{
|
||||
if (m_currentDocument) {
|
||||
QTC_ASSERT(!m_currentDocument->isSemanticInfoOutdated(), return);
|
||||
QmlJS::Document::Ptr document = m_currentDocument->semanticInfo().document;
|
||||
QmlJS::Snapshot snapshot = QmlJS::ModelManagerInterface::instance()->snapshot();
|
||||
|
||||
const QString &newText = QmlJS::reformat(m_currentDocument->semanticInfo().document);
|
||||
if (m_currentDocument->isSemanticInfoOutdated()) {
|
||||
QmlJS::Document::MutablePtr latestDocument;
|
||||
|
||||
const QString fileName = m_currentDocument->filePath().toString();
|
||||
latestDocument = snapshot.documentFromSource(QString::fromUtf8(m_currentDocument->contents()),
|
||||
fileName,
|
||||
QmlJS::ModelManagerInterface::guessLanguageOfFile(fileName));
|
||||
latestDocument->parseQml();
|
||||
snapshot.insert(latestDocument);
|
||||
document = latestDocument;
|
||||
}
|
||||
|
||||
if (!document->isParsedCorrectly())
|
||||
return;
|
||||
|
||||
const QString &newText = QmlJS::reformat(document);
|
||||
QTextCursor tc(m_currentDocument->document());
|
||||
tc.movePosition(QTextCursor::Start);
|
||||
tc.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
|
||||
@@ -296,4 +317,25 @@ void QmlJSEditorPlugin::checkCurrentEditorSemanticInfoUpToDate()
|
||||
m_reformatFileAction->setEnabled(semanticInfoUpToDate);
|
||||
}
|
||||
|
||||
void QmlJSEditorPlugin::autoFormatOnSave(Core::IDocument *document)
|
||||
{
|
||||
if (!QmlJsEditingSettings::get().autoFormatOnSave())
|
||||
return;
|
||||
|
||||
// Check that we are dealing with a QML/JS editor
|
||||
if (document->id() != Constants::C_QMLJSEDITOR_ID)
|
||||
return;
|
||||
|
||||
// Check if file is contained in the current project (if wished)
|
||||
if (QmlJsEditingSettings::get().autoFormatOnlyCurrentProject()) {
|
||||
const ProjectExplorer::Project *pro = ProjectExplorer::ProjectTree::currentProject();
|
||||
if (!pro || !pro->files(ProjectExplorer::Project::SourceFiles).contains(
|
||||
document->filePath().toString())) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
reformatFile();
|
||||
}
|
||||
|
||||
} // namespace QmlJSEditor
|
||||
|
||||
Reference in New Issue
Block a user