ClangFormat: Add context menu item to open current config file

Add the context menu item to C++ editor which allow you to open
the currently used .clang-format configuration file.

Change-Id: If9d6bec4f4085c18b13df15d78417aee82ddb4e4
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
Reviewed-by: Leena Miettinen <riitta-leena.miettinen@qt.io>
This commit is contained in:
Ivan Donchevskii
2019-03-07 11:35:57 +01:00
parent 792fe95c00
commit 243ba8d371
4 changed files with 59 additions and 3 deletions

View File

@@ -35,5 +35,6 @@ static const char FORMAT_CODE_INSTEAD_OF_INDENT_ID[] = "ClangFormat.FormatCodeIn
static const char FORMAT_WHILE_TYPING_ID[] = "ClangFormat.FormatWhileTyping"; static const char FORMAT_WHILE_TYPING_ID[] = "ClangFormat.FormatWhileTyping";
static const char FORMAT_CODE_ON_SAVE_ID[] = "ClangFormat.FormatCodeOnSave"; static const char FORMAT_CODE_ON_SAVE_ID[] = "ClangFormat.FormatCodeOnSave";
static const char OVERRIDE_FILE_ID[] = "ClangFormat.OverrideFile"; static const char OVERRIDE_FILE_ID[] = "ClangFormat.OverrideFile";
static const char OPEN_CURRENT_CONFIG_ID[] = "ClangFormat.OpenCurrentConfig";
} // namespace Constants } // namespace Constants
} // namespace ClangFormat } // namespace ClangFormat

View File

@@ -26,16 +26,23 @@
#include "clangformatplugin.h" #include "clangformatplugin.h"
#include "clangformatconfigwidget.h" #include "clangformatconfigwidget.h"
#include "clangformatconstants.h"
#include "clangformatindenter.h" #include "clangformatindenter.h"
#include "clangformatutils.h"
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <coreplugin/icore.h> #include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/icontext.h>
#include <coreplugin/actionmanager/actionmanager.h> #include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/actionmanager/command.h> #include <coreplugin/actionmanager/command.h>
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/coreconstants.h> #include <coreplugin/coreconstants.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/icontext.h>
#include <coreplugin/icore.h>
#include <coreplugin/idocument.h>
#include <cppeditor/cppeditorconstants.h>
#include <cpptools/cppcodestylepreferencesfactory.h> #include <cpptools/cppcodestylepreferencesfactory.h>
#include <cpptools/cpptoolsconstants.h> #include <cpptools/cpptoolsconstants.h>
@@ -106,6 +113,48 @@ bool ClangFormatPlugin::initialize(const QStringList &arguments, QString *errorS
Q_UNUSED(errorString); Q_UNUSED(errorString);
#ifdef KEEP_LINE_BREAKS_FOR_NON_EMPTY_LINES_BACKPORTED #ifdef KEEP_LINE_BREAKS_FOR_NON_EMPTY_LINES_BACKPORTED
replaceCppCodeStyle(); replaceCppCodeStyle();
Core::ActionContainer *contextMenu = Core::ActionManager::actionContainer(
CppEditor::Constants::M_CONTEXT);
if (contextMenu) {
auto openClangFormatConfigAction
= new QAction(tr("Open Used .clang-format Configuration File"), this);
Core::Command *command
= Core::ActionManager::registerAction(openClangFormatConfigAction,
Constants::OPEN_CURRENT_CONFIG_ID);
contextMenu->addSeparator();
contextMenu->addAction(command);
if (Core::EditorManager::currentEditor()) {
const Core::IDocument *doc = Core::EditorManager::currentEditor()->document();
if (doc)
openClangFormatConfigAction->setData(doc->filePath().toString());
}
connect(openClangFormatConfigAction,
&QAction::triggered,
this,
[openClangFormatConfigAction]() {
const QString fileName = openClangFormatConfigAction->data().toString();
if (!fileName.isEmpty()) {
const QString clangFormatConfigPath = configForFile(
Utils::FileName::fromString(fileName));
Core::EditorManager::openEditor(clangFormatConfigPath);
}
});
connect(Core::EditorManager::instance(),
&Core::EditorManager::currentEditorChanged,
this,
[openClangFormatConfigAction](Core::IEditor *editor) {
if (!editor)
return;
const Core::IDocument *doc = editor->document();
if (doc)
openClangFormatConfigAction->setData(doc->filePath().toString());
});
}
#endif #endif
return true; return true;
} }

View File

@@ -163,6 +163,11 @@ static QString configForFile(Utils::FileName fileName, bool checkForSettings)
return findConfig(fileName); return findConfig(fileName);
} }
QString configForFile(Utils::FileName fileName)
{
return configForFile(fileName, true);
}
static clang::format::FormatStyle constructStyle(bool isGlobal, static clang::format::FormatStyle constructStyle(bool isGlobal,
const QByteArray &baseStyle = QByteArray()) const QByteArray &baseStyle = QByteArray())
{ {

View File

@@ -44,6 +44,7 @@ clang::format::FormatStyle currentProjectStyle();
clang::format::FormatStyle currentGlobalStyle(); clang::format::FormatStyle currentGlobalStyle();
// Is the style from the matching .clang-format file or global one if it's not found. // Is the style from the matching .clang-format file or global one if it's not found.
QString configForFile(Utils::FileName fileName);
clang::format::FormatStyle styleForFile(Utils::FileName fileName); clang::format::FormatStyle styleForFile(Utils::FileName fileName);
} }