summaryrefslogtreecommitdiff
path: root/src/plugins/cmakeprojectmanager/cmakeformatter.cpp
blob: d9aa8b2c84a8b05ef70fd29a7f29b80d3e624333 (plain)
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
// 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 "cmakeformattersettings.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/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/formattexteditor.h>
#include <texteditor/texteditor.h>

#include <QAction>
#include <QVersionNumber>

using namespace TextEditor;

namespace CMakeProjectManager {
namespace Internal {

void CMakeFormatter::updateActions(Core::IEditor *editor)
{
    const bool enabled = editor && CMakeFormatterSettings::instance()->isApplicable(editor->document());
    m_formatFile->setEnabled(enabled);
}

void CMakeFormatter::formatFile()
{
    formatCurrentFile(command());
}

Command CMakeFormatter::command() const
{
    Command command;
    command.setExecutable(CMakeFormatterSettings::instance()->command());
    command.setProcessing(Command::FileProcessing);
    command.addOption("--in-place");
    command.addOption("%file");
    return command;
}

bool CMakeFormatter::isApplicable(const Core::IDocument *document) const
{
    return CMakeFormatterSettings::instance()->isApplicable(document);
}

void CMakeFormatter::initialize()
{
    m_formatFile = new QAction(Tr::tr("Format &Current File"), this);
    Core::Command *cmd = Core::ActionManager::registerAction(m_formatFile, Constants::CMAKEFORMATTER_ACTION_ID);
    connect(m_formatFile, &QAction::triggered, this, &CMakeFormatter::formatFile);

    Core::ActionManager::actionContainer(Constants::CMAKEFORMATTER_MENU_ID)->addAction(cmd);

    connect(CMakeFormatterSettings::instance(), &CMakeFormatterSettings::supportedMimeTypesChanged,
            [this] { updateActions(Core::EditorManager::currentEditor()); });
}

} // namespace Internal
} // namespace CMakeProjectManager