1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
// Copyright (C) 2016 Lorenz Haas
// Copyright (C) 2022 Xavier BESSON
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "cmakeformatter.h"
#include "cmakeprojectconstants.h"
#include "cmakeprojectmanagertr.h"
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/command.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/dialogs/ioptionspage.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/idocument.h>
#include <projectexplorer/project.h>
#include <projectexplorer/projectnodes.h>
#include <projectexplorer/projecttree.h>
#include <texteditor/command.h>
#include <texteditor/formattexteditor.h>
#include <texteditor/texteditor.h>
#include <utils/algorithm.h>
#include <utils/genericconstants.h>
#include <utils/layoutbuilder.h>
#include <utils/mimeutils.h>
#include <QMenu>
using namespace Core;
using namespace TextEditor;
using namespace Utils;
namespace CMakeProjectManager::Internal {
class CMakeFormatterPrivate : public PagedSettings
{
public:
CMakeFormatterPrivate()
{
setSettingsGroups(Constants::CMAKEFORMATTER_SETTINGS_GROUP,
Constants::CMAKEFORMATTER_GENERAL_GROUP);
setId(Constants::Settings::FORMATTER_ID);
setDisplayName(Tr::tr("Formatter"));
setDisplayCategory("CMake");
setCategory(Constants::Settings::CATEGORY);
registerAspect(&command);
command.setSettingsKey("autoFormatCommand");
command.setDisplayStyle(StringAspect::PathChooserDisplay);
command.setDefaultValue("cmake-format");
command.setExpectedKind(PathChooser::ExistingCommand);
registerAspect(&autoFormatOnSave);
autoFormatOnSave.setSettingsKey("autoFormatOnSave");
autoFormatOnSave.setLabelText(Tr::tr("Enable auto format on file save"));
registerAspect(&autoFormatOnlyCurrentProject);
autoFormatOnlyCurrentProject.setSettingsKey("autoFormatOnlyCurrentProject");
autoFormatOnlyCurrentProject.setDefaultValue(true);
autoFormatOnlyCurrentProject.setLabelText(Tr::tr("Restrict to files contained in the current project"));
registerAspect(&autoFormatMime);
autoFormatMime.setSettingsKey("autoFormatMime");
autoFormatMime.setDefaultValue("text/x-cmake");
autoFormatMime.setLabelText(Tr::tr("Restrict to MIME types:"));
setLayouter([this](QWidget *widget) {
using namespace Layouting;
Column {
Row { Tr::tr("CMakeFormat command:"), command },
Space(10),
Group {
title(Tr::tr("Automatic Formatting on File Save")),
autoFormatOnSave.groupChecker(),
Form {
autoFormatMime, br,
Span(2, autoFormatOnlyCurrentProject)
}
},
st
}.attachTo(widget);
});
ActionContainer *menu = ActionManager::createMenu(Constants::CMAKEFORMATTER_MENU_ID);
menu->menu()->setTitle(Tr::tr("CMakeFormatter"));
menu->setOnAllDisabledBehavior(ActionContainer::Show);
ActionManager::actionContainer(Core::Constants::M_TOOLS)->addMenu(menu);
Core::Command *cmd = ActionManager::registerAction(&formatFile, Constants::CMAKEFORMATTER_ACTION_ID);
connect(&formatFile, &QAction::triggered, this, [this] {
TextEditor::formatCurrentFile(formatCommand());
});
ActionManager::actionContainer(Constants::CMAKEFORMATTER_MENU_ID)->addAction(cmd);
auto updateActions = [this] {
auto editor = EditorManager::currentEditor();
formatFile.setEnabled(editor && isApplicable(editor->document()));
};
connect(&autoFormatMime, &Utils::StringAspect::changed,
this, updateActions);
connect(EditorManager::instance(), &EditorManager::currentEditorChanged,
this, updateActions);
connect(EditorManager::instance(), &EditorManager::aboutToSave,
this, &CMakeFormatterPrivate::applyIfNecessary);
readSettings();
}
bool isApplicable(const IDocument *document) const;
void applyIfNecessary(IDocument *document) const;
TextEditor::Command formatCommand() const
{
TextEditor::Command cmd;
cmd.setExecutable(command.filePath());
cmd.setProcessing(TextEditor::Command::FileProcessing);
cmd.addOption("--in-place");
cmd.addOption("%file");
return cmd;
}
StringAspect command;
BoolAspect autoFormatOnSave;
BoolAspect autoFormatOnlyCurrentProject;
StringAspect autoFormatMime;
QAction formatFile{Tr::tr("Format &Current File")};
};
bool CMakeFormatterPrivate::isApplicable(const IDocument *document) const
{
if (!document)
return false;
if (autoFormatMime.value().isEmpty())
return true;
const QStringList allowedMimeTypes = autoFormatMime.value().split(';');
const MimeType documentMimeType = Utils::mimeTypeForName(document->mimeType());
return anyOf(allowedMimeTypes, [&documentMimeType](const QString &mime) {
return documentMimeType.inherits(mime);
});
}
void CMakeFormatterPrivate::applyIfNecessary(IDocument *document) const
{
if (!autoFormatOnSave.value())
return;
if (!document)
return;
if (!isApplicable(document))
return;
// Check if file is contained in the current project (if wished)
if (autoFormatOnlyCurrentProject.value()) {
const ProjectExplorer::Project *pro = ProjectExplorer::ProjectTree::currentProject();
if (!pro || pro->files([document](const ProjectExplorer::Node *n) {
return ProjectExplorer::Project::SourceFiles(n)
&& n->filePath() == document->filePath();
}).isEmpty()) {
return;
}
}
TextEditor::Command command = formatCommand();
if (!command.isValid())
return;
const QList<IEditor *> editors = DocumentModel::editorsForDocument(document);
if (editors.isEmpty())
return;
IEditor *currentEditor = EditorManager::currentEditor();
IEditor *editor = editors.contains(currentEditor) ? currentEditor : editors.first();
if (auto widget = TextEditorWidget::fromEditor(editor))
TextEditor::formatEditor(widget, command);
}
// CMakeFormatter
CMakeFormatter::CMakeFormatter()
: d(new CMakeFormatterPrivate)
{}
CMakeFormatter::~CMakeFormatter()
{
delete d;
}
void CMakeFormatter::applyIfNecessary(IDocument *document) const
{
d->applyIfNecessary(document);
}
} // CMakeProjectManager::Internal
|