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: Daniel Teske <daniel.teske@nokia.com>
This commit is contained in:
Konstantin Tokarev
2012-02-19 18:58:39 +04:00
committed by Daniel Teske
parent bbe0d967a7
commit b67d761462
6 changed files with 182 additions and 11 deletions

View File

@@ -37,7 +37,10 @@
#include "cmakeprojectconstants.h" #include "cmakeprojectconstants.h"
#include "cmakeproject.h" #include "cmakeproject.h"
#include <coreplugin/icore.h>
#include <coreplugin/infobar.h> #include <coreplugin/infobar.h>
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <projectexplorer/projectexplorer.h> #include <projectexplorer/projectexplorer.h>
#include <projectexplorer/session.h> #include <projectexplorer/session.h>
#include <texteditor/fontsettings.h> #include <texteditor/fontsettings.h>
@@ -72,6 +75,11 @@ Core::IEditor *CMakeEditor::duplicate(QWidget *parent)
return ret->editor(); return ret->editor();
} }
Core::Context CMakeEditor::context() const
{
return Core::Context(Constants::C_CMAKEEDITOR);
}
Core::Id CMakeEditor::id() const Core::Id CMakeEditor::id() const
{ {
return CMakeProjectManager::Constants::CMAKE_EDITOR_ID; return CMakeProjectManager::Constants::CMAKE_EDITOR_ID;
@@ -116,9 +124,12 @@ CMakeEditorWidget::CMakeEditorWidget(QWidget *parent, CMakeEditorFactory *factor
doc->setMimeType(QLatin1String(CMakeProjectManager::Constants::CMAKEMIMETYPE)); doc->setMimeType(QLatin1String(CMakeProjectManager::Constants::CMAKEMIMETYPE));
setBaseTextDocument(doc); setBaseTextDocument(doc);
ah->setupActions(this);
baseTextDocument()->setSyntaxHighlighter(new CMakeHighlighter); baseTextDocument()->setSyntaxHighlighter(new CMakeHighlighter);
m_commentDefinition.clearCommentStyles();
m_commentDefinition.setSingleLine(QLatin1String("#"));
ah->setupActions(this);
} }
TextEditor::BaseTextEditor *CMakeEditorWidget::createEditor() TextEditor::BaseTextEditor *CMakeEditorWidget::createEditor()
@@ -126,6 +137,28 @@ TextEditor::BaseTextEditor *CMakeEditorWidget::createEditor()
return new CMakeEditor(this); 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) void CMakeEditorWidget::setFontSettings(const TextEditor::FontSettings &fs)
{ {
TextEditor::BaseTextEditorWidget::setFontSettings(fs); TextEditor::BaseTextEditorWidget::setFontSettings(fs);
@@ -147,8 +180,90 @@ void CMakeEditorWidget::setFontSettings(const TextEditor::FontSettings &fs)
highlighter->rehighlight(); 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() CMakeDocument::CMakeDocument()

View File

@@ -37,6 +37,7 @@
#include <texteditor/basetextdocument.h> #include <texteditor/basetextdocument.h>
#include <texteditor/basetexteditor.h> #include <texteditor/basetexteditor.h>
#include <utils/uncommentselection.h>
namespace TextEditor { namespace TextEditor {
@@ -59,6 +60,7 @@ public:
bool duplicateSupported() const { return true; } bool duplicateSupported() const { return true; }
Core::IEditor *duplicate(QWidget *parent); Core::IEditor *duplicate(QWidget *parent);
Core::Context context() const;
Core::Id id() const; Core::Id id() const;
bool isTemporary() const { return false; } bool isTemporary() const { return false; }
@@ -81,15 +83,23 @@ public:
CMakeEditorFactory *factory() { return m_factory; } CMakeEditorFactory *factory() { return m_factory; }
TextEditor::TextEditorActionHandler *actionHandler() const { return m_ah; } TextEditor::TextEditorActionHandler *actionHandler() const { return m_ah; }
void jumpToFile();
Link findLinkAt(const QTextCursor &cursor,
bool resolveTarget = true);
protected: protected:
TextEditor::BaseTextEditor *createEditor(); TextEditor::BaseTextEditor *createEditor();
void contextMenuEvent(QContextMenuEvent *e);
public slots: public slots:
virtual void setFontSettings(const TextEditor::FontSettings &); void unCommentSelection();
void setFontSettings(const TextEditor::FontSettings &);
private: private:
CMakeEditorFactory *m_factory; CMakeEditorFactory *m_factory;
TextEditor::TextEditorActionHandler *m_ah; TextEditor::TextEditorActionHandler *m_ah;
Utils::CommentDefinition m_commentDefinition;
}; };
class CMakeDocument : public TextEditor::BaseTextDocument class CMakeDocument : public TextEditor::BaseTextDocument

View File

@@ -34,18 +34,48 @@
#include "cmakeprojectconstants.h" #include "cmakeprojectconstants.h"
#include "cmakeeditor.h" #include "cmakeeditor.h"
#include <coreplugin/icore.h>
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/editormanager/editormanager.h> #include <coreplugin/editormanager/editormanager.h>
#include <texteditor/texteditoractionhandler.h>
#include <texteditor/texteditorconstants.h>
#include <texteditor/texteditorsettings.h> #include <texteditor/texteditorsettings.h>
using namespace CMakeProjectManager; using namespace CMakeProjectManager;
using namespace CMakeProjectManager::Internal; using namespace CMakeProjectManager::Internal;
CMakeEditorFactory::CMakeEditorFactory(CMakeManager *manager, TextEditor::TextEditorActionHandler *handler) CMakeEditorFactory::CMakeEditorFactory(CMakeManager *manager)
: m_mimeTypes(QStringList() << QLatin1String(CMakeProjectManager::Constants::CMAKEMIMETYPE)), : m_mimeTypes(QStringList() << QLatin1String(CMakeProjectManager::Constants::CMAKEMIMETYPE)),
m_manager(manager), m_manager(manager)
m_actionHandler(handler)
{ {
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 Core::Id CMakeEditorFactory::id() const
@@ -71,6 +101,14 @@ Core::IEditor *CMakeEditorFactory::createEditor(QWidget *parent)
return rc->editor(); 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 QStringList CMakeEditorFactory::mimeTypes() const
{ {
return m_mimeTypes; return m_mimeTypes;

View File

@@ -51,7 +51,7 @@ class CMakeEditorFactory : public Core::IEditorFactory
Q_OBJECT Q_OBJECT
public: public:
CMakeEditorFactory(CMakeManager *parent, TextEditor::TextEditorActionHandler *handler); CMakeEditorFactory(CMakeManager *parent);
// IEditorFactory // IEditorFactory
QStringList mimeTypes() const; QStringList mimeTypes() const;
@@ -60,6 +60,9 @@ public:
Core::IDocument *open(const QString &fileName); Core::IDocument *open(const QString &fileName);
Core::IEditor *createEditor(QWidget *parent); Core::IEditor *createEditor(QWidget *parent);
public slots:
void jumpToFile();
private: private:
const QStringList m_mimeTypes; const QStringList m_mimeTypes;
CMakeManager *m_manager; CMakeManager *m_manager;

View File

@@ -47,6 +47,13 @@ const char RUNCMAKECONTEXTMENU[] = "CMakeProject.RunCMakeContextMenu";
// Project // Project
const char CMAKEPROJECT_ID[] = "CMakeProjectManager.CMakeProject"; 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 Constants
} // namespace CMakeProjectManager } // namespace CMakeProjectManager

View File

@@ -67,10 +67,8 @@ bool CMakeProjectPlugin::initialize(const QStringList & /*arguments*/, QString *
addAutoReleasedObject(manager); addAutoReleasedObject(manager);
addAutoReleasedObject(new MakeStepFactory); addAutoReleasedObject(new MakeStepFactory);
addAutoReleasedObject(new CMakeRunConfigurationFactory); 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 CMakeTargetFactory);
addAutoReleasedObject(new CMakeLocatorFilter); addAutoReleasedObject(new CMakeLocatorFilter);