Skip to content
Snippets Groups Projects
Commit b67d7614 authored by Konstantin Tokarev's avatar Konstantin Tokarev Committed by Daniel Teske
Browse files

Added special context menu for CMakeEditor.


This patch replaces generic context menu in CMakeEditor with
CMake-specific menu, containing action for file navigation
and line commenting.

Change-Id: I30c4ab5e517c77f801d2cc27561ded79dcf977a3
Reviewed-by: default avatarDaniel Teske <daniel.teske@nokia.com>
parent bbe0d967
No related branches found
No related tags found
No related merge requests found
......@@ -37,7 +37,10 @@
#include "cmakeprojectconstants.h"
#include "cmakeproject.h"
#include <coreplugin/icore.h>
#include <coreplugin/infobar.h>
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/session.h>
#include <texteditor/fontsettings.h>
......@@ -72,6 +75,11 @@ Core::IEditor *CMakeEditor::duplicate(QWidget *parent)
return ret->editor();
}
Core::Context CMakeEditor::context() const
{
return Core::Context(Constants::C_CMAKEEDITOR);
}
Core::Id CMakeEditor::id() const
{
return CMakeProjectManager::Constants::CMAKE_EDITOR_ID;
......@@ -116,9 +124,12 @@ CMakeEditorWidget::CMakeEditorWidget(QWidget *parent, CMakeEditorFactory *factor
doc->setMimeType(QLatin1String(CMakeProjectManager::Constants::CMAKEMIMETYPE));
setBaseTextDocument(doc);
ah->setupActions(this);
baseTextDocument()->setSyntaxHighlighter(new CMakeHighlighter);
m_commentDefinition.clearCommentStyles();
m_commentDefinition.setSingleLine(QLatin1String("#"));
ah->setupActions(this);
}
TextEditor::BaseTextEditor *CMakeEditorWidget::createEditor()
......@@ -126,6 +137,28 @@ TextEditor::BaseTextEditor *CMakeEditorWidget::createEditor()
return new CMakeEditor(this);
}
void CMakeEditorWidget::unCommentSelection()
{
Utils::unCommentSelection(this, m_commentDefinition);
}
void CMakeEditorWidget::contextMenuEvent(QContextMenuEvent *e)
{
QMenu *menu = new QMenu();
Core::ActionManager *am = Core::ICore::instance()->actionManager();
Core::ActionContainer *mcontext = am->actionContainer(Constants::M_CONTEXT);
QMenu *contextMenu = mcontext->menu();
foreach (QAction *action, contextMenu->actions())
menu->addAction(action);
appendStandardContextMenuActions(menu);
menu->exec(e->globalPos());
delete menu;
}
void CMakeEditorWidget::setFontSettings(const TextEditor::FontSettings &fs)
{
TextEditor::BaseTextEditorWidget::setFontSettings(fs);
......@@ -147,8 +180,90 @@ void CMakeEditorWidget::setFontSettings(const TextEditor::FontSettings &fs)
highlighter->rehighlight();
}
void CMakeEditorWidget::jumpToFile()
{
openLink(findLinkAt(textCursor()));
}
static bool isValidFileNameChar(const QChar &c)
{
if (c.isLetterOrNumber()
|| c == QLatin1Char('.')
|| c == QLatin1Char('_')
|| c == QLatin1Char('-')
|| c == QLatin1Char('/')
|| c == QLatin1Char('\\'))
return true;
return false;
}
CMakeEditorWidget::Link CMakeEditorWidget::findLinkAt(const QTextCursor &cursor,
bool/* resolveTarget*/)
{
Link link;
int lineNumber = 0, positionInBlock = 0;
convertPosition(cursor.position(), &lineNumber, &positionInBlock);
const QString block = cursor.block().text();
// check if the current position is commented out
const int hashPos = block.indexOf(QLatin1Char('#'));
if (hashPos >= 0 && hashPos < positionInBlock)
return link;
// find the beginning of a filename
QString buffer;
int beginPos = positionInBlock - 1;
while (beginPos >= 0) {
QChar c = block.at(beginPos);
if (isValidFileNameChar(c)) {
buffer.prepend(c);
beginPos--;
} else {
break;
}
}
// find the end of a filename
int endPos = positionInBlock;
while (endPos < block.count()) {
QChar c = block.at(endPos);
if (isValidFileNameChar(c)) {
buffer.append(c);
endPos++;
} else {
break;
}
}
if (buffer.isEmpty())
return link;
// TODO: Resolve variables
QDir dir(QFileInfo(editorDocument()->fileName()).absolutePath());
QString fileName = dir.filePath(buffer);
QFileInfo fi(fileName);
if (fi.exists()) {
if (fi.isDir()) {
QDir subDir(fi.absoluteFilePath());
QString subProject = subDir.filePath("CMakeLists.txt");
if (QFileInfo(subProject).exists())
fileName = subProject;
else
return link;
}
link.fileName = fileName;
link.begin = cursor.position() - positionInBlock + beginPos + 1;
link.end = cursor.position() - positionInBlock + endPos;
}
return link;
}
//
// ProFileDocument
// CMakeDocument
//
CMakeDocument::CMakeDocument()
......
......@@ -37,6 +37,7 @@
#include <texteditor/basetextdocument.h>
#include <texteditor/basetexteditor.h>
#include <utils/uncommentselection.h>
namespace TextEditor {
......@@ -59,6 +60,7 @@ public:
bool duplicateSupported() const { return true; }
Core::IEditor *duplicate(QWidget *parent);
Core::Context context() const;
Core::Id id() const;
bool isTemporary() const { return false; }
......@@ -81,15 +83,23 @@ public:
CMakeEditorFactory *factory() { return m_factory; }
TextEditor::TextEditorActionHandler *actionHandler() const { return m_ah; }
void jumpToFile();
Link findLinkAt(const QTextCursor &cursor,
bool resolveTarget = true);
protected:
TextEditor::BaseTextEditor *createEditor();
void contextMenuEvent(QContextMenuEvent *e);
public slots:
virtual void setFontSettings(const TextEditor::FontSettings &);
void unCommentSelection();
void setFontSettings(const TextEditor::FontSettings &);
private:
CMakeEditorFactory *m_factory;
TextEditor::TextEditorActionHandler *m_ah;
Utils::CommentDefinition m_commentDefinition;
};
class CMakeDocument : public TextEditor::BaseTextDocument
......
......@@ -34,18 +34,48 @@
#include "cmakeprojectconstants.h"
#include "cmakeeditor.h"
#include <coreplugin/icore.h>
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/editormanager/editormanager.h>
#include <texteditor/texteditoractionhandler.h>
#include <texteditor/texteditorconstants.h>
#include <texteditor/texteditorsettings.h>
using namespace CMakeProjectManager;
using namespace CMakeProjectManager::Internal;
CMakeEditorFactory::CMakeEditorFactory(CMakeManager *manager, TextEditor::TextEditorActionHandler *handler)
CMakeEditorFactory::CMakeEditorFactory(CMakeManager *manager)
: m_mimeTypes(QStringList() << QLatin1String(CMakeProjectManager::Constants::CMAKEMIMETYPE)),
m_manager(manager),
m_actionHandler(handler)
m_manager(manager)
{
using namespace Core;
using namespace TextEditor;
m_actionHandler =
new TextEditorActionHandler(Constants::C_CMAKEEDITOR,
TextEditorActionHandler::UnCommentSelection);
ICore *core = ICore::instance();
ActionManager *am = core->actionManager();
ActionContainer *contextMenu = am->createMenu(Constants::M_CONTEXT);
Command *cmd;
Context cmakeEditorContext = Context(Constants::C_CMAKEEDITOR);
QAction *jumpToFile = new QAction(tr("Jump to File Under Cursor"), this);
cmd = am->registerAction(jumpToFile,
Constants::JUMP_TO_FILE, cmakeEditorContext);
cmd->setDefaultKeySequence(QKeySequence(Qt::Key_F2));
connect(jumpToFile, SIGNAL(triggered()), this, SLOT(jumpToFile()));
contextMenu->addAction(cmd);
QAction *separator = new QAction(this);
separator->setSeparator(true);
contextMenu->addAction(am->registerAction(separator,
Id(Constants::SEPARATOR), cmakeEditorContext));
cmd = am->command(TextEditor::Constants::UN_COMMENT_SELECTION);
contextMenu->addAction(cmd);
}
Core::Id CMakeEditorFactory::id() const
......@@ -71,6 +101,14 @@ Core::IEditor *CMakeEditorFactory::createEditor(QWidget *parent)
return rc->editor();
}
void CMakeEditorFactory::jumpToFile()
{
Core::EditorManager *em = Core::EditorManager::instance();
CMakeEditorWidget *editor = qobject_cast<CMakeEditorWidget*>(em->currentEditor()->widget());
if (editor)
editor->jumpToFile();
}
QStringList CMakeEditorFactory::mimeTypes() const
{
return m_mimeTypes;
......
......@@ -51,7 +51,7 @@ class CMakeEditorFactory : public Core::IEditorFactory
Q_OBJECT
public:
CMakeEditorFactory(CMakeManager *parent, TextEditor::TextEditorActionHandler *handler);
CMakeEditorFactory(CMakeManager *parent);
// IEditorFactory
QStringList mimeTypes() const;
......@@ -60,6 +60,9 @@ public:
Core::IDocument *open(const QString &fileName);
Core::IEditor *createEditor(QWidget *parent);
public slots:
void jumpToFile();
private:
const QStringList m_mimeTypes;
CMakeManager *m_manager;
......
......@@ -47,6 +47,13 @@ const char RUNCMAKECONTEXTMENU[] = "CMakeProject.RunCMakeContextMenu";
// Project
const char CMAKEPROJECT_ID[] = "CMakeProjectManager.CMakeProject";
// Menu
const char M_CONTEXT[] = "CMakeEditor.ContextMenu";
// Actions
const char SEPARATOR[] = "CMakeEditor.Separator";
const char JUMP_TO_FILE[] = "CMakeEditor.JumpToFile";
} // namespace Constants
} // namespace CMakeProjectManager
......
......@@ -67,10 +67,8 @@ bool CMakeProjectPlugin::initialize(const QStringList & /*arguments*/, QString *
addAutoReleasedObject(manager);
addAutoReleasedObject(new MakeStepFactory);
addAutoReleasedObject(new CMakeRunConfigurationFactory);
TextEditor::TextEditorActionHandler *editorHandler
= new TextEditor::TextEditorActionHandler(CMakeProjectManager::Constants::C_CMAKEEDITOR);
addAutoReleasedObject(new CMakeEditorFactory(manager, editorHandler));
addAutoReleasedObject(new CMakeEditorFactory(manager));
addAutoReleasedObject(new CMakeTargetFactory);
addAutoReleasedObject(new CMakeLocatorFilter);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment