2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2010-03-30 12:11:32 +02:00
|
|
|
**
|
2014-01-07 13:27:11 +01:00
|
|
|
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
2012-10-02 09:12:39 +02:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
2010-03-30 12:11:32 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2010-03-30 12:11:32 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
|
|
|
|
** use the contact form at http://qt.digia.com/contact-us.
|
2010-03-30 12:11:32 +02:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
2012-10-02 09:12:39 +02:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
|
|
|
** General Public License version 2.1 as published by the Free Software
|
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
|
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
|
|
|
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
|
|
|
**
|
|
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2010-03-30 12:11:32 +02:00
|
|
|
|
|
|
|
|
#include "cmakeeditor.h"
|
|
|
|
|
|
2013-08-30 12:55:06 +02:00
|
|
|
#include "cmakefilecompletionassist.h"
|
2010-03-30 12:11:32 +02:00
|
|
|
#include "cmakeprojectconstants.h"
|
2011-03-31 17:43:48 +02:00
|
|
|
#include "cmakeproject.h"
|
2010-03-30 12:11:32 +02:00
|
|
|
|
2012-02-19 18:58:39 +04:00
|
|
|
#include <coreplugin/actionmanager/actioncontainer.h>
|
|
|
|
|
#include <coreplugin/actionmanager/actionmanager.h>
|
2014-08-20 00:07:43 +02:00
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
|
|
|
|
#include <coreplugin/icore.h>
|
|
|
|
|
#include <coreplugin/infobar.h>
|
|
|
|
|
|
2013-08-30 12:55:06 +02:00
|
|
|
#include <extensionsystem/pluginmanager.h>
|
2014-08-20 00:07:43 +02:00
|
|
|
|
2011-03-31 17:43:48 +02:00
|
|
|
#include <projectexplorer/projectexplorer.h>
|
|
|
|
|
#include <projectexplorer/session.h>
|
2014-08-20 00:07:43 +02:00
|
|
|
|
|
|
|
|
#include <texteditor/highlighterutils.h>
|
2010-03-30 12:11:32 +02:00
|
|
|
#include <texteditor/texteditoractionhandler.h>
|
|
|
|
|
#include <texteditor/texteditorconstants.h>
|
2014-08-20 00:07:43 +02:00
|
|
|
|
|
|
|
|
#include <utils/qtcassert.h>
|
2010-03-30 12:11:32 +02:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QFileInfo>
|
2013-08-13 12:57:31 +02:00
|
|
|
#include <QTextBlock>
|
2010-03-30 12:11:32 +02:00
|
|
|
|
2014-08-20 00:07:43 +02:00
|
|
|
using namespace Core;
|
|
|
|
|
using namespace TextEditor;
|
|
|
|
|
|
|
|
|
|
namespace CMakeProjectManager {
|
|
|
|
|
namespace Internal {
|
2010-03-30 12:11:32 +02:00
|
|
|
|
|
|
|
|
//
|
2014-08-20 00:07:43 +02:00
|
|
|
// CMakeEditor
|
2010-03-30 12:11:32 +02:00
|
|
|
//
|
|
|
|
|
|
2014-08-19 00:54:45 +02:00
|
|
|
CMakeEditor::CMakeEditor()
|
2011-03-31 17:43:48 +02:00
|
|
|
{
|
2010-03-30 12:11:32 +02:00
|
|
|
}
|
|
|
|
|
|
2014-08-22 16:38:38 +02:00
|
|
|
void CMakeEditor::finalizeInitialization()
|
2011-03-31 17:43:48 +02:00
|
|
|
{
|
2014-08-22 16:38:38 +02:00
|
|
|
connect(document(), &IDocument::changed, [this]() {
|
|
|
|
|
BaseTextDocument *document = textDocument();
|
|
|
|
|
if (!document->isModified())
|
|
|
|
|
return;
|
|
|
|
|
InfoBar *infoBar = document->infoBar();
|
|
|
|
|
Id infoRunCmake("CMakeEditor.RunCMake");
|
|
|
|
|
if (!infoBar->canInfoBeAdded(infoRunCmake))
|
|
|
|
|
return;
|
|
|
|
|
InfoBarEntry info(infoRunCmake,
|
|
|
|
|
tr("Changes to cmake files are shown in the project tree after building."),
|
|
|
|
|
InfoBarEntry::GlobalSuppressionEnabled);
|
|
|
|
|
info.setCustomButtonInfo(tr("Build now"), this, SLOT(build()));
|
|
|
|
|
infoBar->addInfo(info);
|
|
|
|
|
});
|
2011-03-31 17:43:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CMakeEditor::build()
|
|
|
|
|
{
|
2013-09-05 11:46:07 +02:00
|
|
|
foreach (ProjectExplorer::Project *p, ProjectExplorer::SessionManager::projects()) {
|
2011-03-31 17:43:48 +02:00
|
|
|
CMakeProject *cmakeProject = qobject_cast<CMakeProject *>(p);
|
|
|
|
|
if (cmakeProject) {
|
2013-07-04 13:30:26 +02:00
|
|
|
if (cmakeProject->isProjectFile(document()->filePath())) {
|
2011-03-31 17:43:48 +02:00
|
|
|
ProjectExplorer::ProjectExplorerPlugin::instance()->buildProject(cmakeProject);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-17 11:33:58 +02:00
|
|
|
QString CMakeEditor::contextHelpId() const
|
|
|
|
|
{
|
|
|
|
|
int pos = position();
|
|
|
|
|
|
|
|
|
|
QChar chr;
|
|
|
|
|
do {
|
|
|
|
|
--pos;
|
|
|
|
|
if (pos < 0)
|
|
|
|
|
break;
|
2014-08-27 11:57:32 +02:00
|
|
|
chr = characterAt(pos);
|
2014-06-17 11:33:58 +02:00
|
|
|
if (chr == QLatin1Char('('))
|
|
|
|
|
return QString();
|
|
|
|
|
} while (chr.unicode() != QChar::ParagraphSeparator);
|
|
|
|
|
|
|
|
|
|
++pos;
|
2014-08-27 11:57:32 +02:00
|
|
|
chr = characterAt(pos);
|
2014-06-17 11:33:58 +02:00
|
|
|
while (chr.isSpace()) {
|
|
|
|
|
++pos;
|
2014-08-27 11:57:32 +02:00
|
|
|
chr = characterAt(pos);
|
2014-06-17 11:33:58 +02:00
|
|
|
}
|
|
|
|
|
int begin = pos;
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
++pos;
|
2014-08-27 11:57:32 +02:00
|
|
|
chr = characterAt(pos);
|
2014-06-17 11:33:58 +02:00
|
|
|
} while (chr.isLetterOrNumber() || chr == QLatin1Char('_'));
|
|
|
|
|
int end = pos;
|
|
|
|
|
|
|
|
|
|
while (chr.isSpace()) {
|
|
|
|
|
++pos;
|
2014-08-27 11:57:32 +02:00
|
|
|
chr = characterAt(pos);
|
2014-06-17 11:33:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Not a command
|
|
|
|
|
if (chr != QLatin1Char('('))
|
|
|
|
|
return QString();
|
|
|
|
|
|
2014-08-27 11:57:32 +02:00
|
|
|
QString command = textAt(begin, end - begin).toLower();
|
2014-06-17 11:33:58 +02:00
|
|
|
return QLatin1String("command/") + command;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-30 12:11:32 +02:00
|
|
|
//
|
2014-08-20 00:07:43 +02:00
|
|
|
// CMakeEditorWidget
|
2010-03-30 12:11:32 +02:00
|
|
|
//
|
|
|
|
|
|
2014-08-22 16:38:38 +02:00
|
|
|
class CMakeEditorWidget : public BaseTextEditorWidget
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
CMakeEditorWidget();
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
bool save(const QString &fileName = QString());
|
|
|
|
|
Link findLinkAt(const QTextCursor &cursor, bool resolveTarget = true, bool inNextSplit = false);
|
|
|
|
|
void contextMenuEvent(QContextMenuEvent *e);
|
|
|
|
|
};
|
|
|
|
|
|
2014-08-18 17:49:04 +02:00
|
|
|
CMakeEditorWidget::CMakeEditorWidget()
|
2014-08-12 12:21:09 +02:00
|
|
|
{
|
|
|
|
|
setCodeFoldingSupported(true);
|
2014-09-03 10:57:51 +02:00
|
|
|
setCompletionAssistProvider(ExtensionSystem::PluginManager::getObject<CMakeFileCompletionAssistProvider>());
|
2014-08-12 12:21:09 +02:00
|
|
|
}
|
2010-03-30 12:11:32 +02:00
|
|
|
|
2012-02-19 18:58:39 +04:00
|
|
|
void CMakeEditorWidget::contextMenuEvent(QContextMenuEvent *e)
|
|
|
|
|
{
|
2012-07-24 23:30:32 +04:00
|
|
|
showDefaultContextMenu(e, Constants::M_CONTEXT);
|
2012-02-19 18:58:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static bool isValidFileNameChar(const QChar &c)
|
|
|
|
|
{
|
2014-08-20 00:07:43 +02:00
|
|
|
return c.isLetterOrNumber()
|
2012-02-19 18:58:39 +04:00
|
|
|
|| c == QLatin1Char('.')
|
|
|
|
|
|| c == QLatin1Char('_')
|
|
|
|
|
|| c == QLatin1Char('-')
|
|
|
|
|
|| c == QLatin1Char('/')
|
2014-08-20 00:07:43 +02:00
|
|
|
|| c == QLatin1Char('\\');
|
2012-02-19 18:58:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CMakeEditorWidget::Link CMakeEditorWidget::findLinkAt(const QTextCursor &cursor,
|
2013-07-04 20:11:10 +02:00
|
|
|
bool/* resolveTarget*/, bool /*inNextSplit*/)
|
2012-02-19 18:58:39 +04:00
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
|
2014-08-01 23:31:56 +02:00
|
|
|
QDir dir(QFileInfo(textDocument()->filePath()).absolutePath());
|
2012-02-19 18:58:39 +04:00
|
|
|
QString fileName = dir.filePath(buffer);
|
|
|
|
|
QFileInfo fi(fileName);
|
|
|
|
|
if (fi.exists()) {
|
|
|
|
|
if (fi.isDir()) {
|
|
|
|
|
QDir subDir(fi.absoluteFilePath());
|
2012-11-21 23:54:06 +02:00
|
|
|
QString subProject = subDir.filePath(QLatin1String("CMakeLists.txt"));
|
2012-02-19 18:58:39 +04:00
|
|
|
if (QFileInfo(subProject).exists())
|
|
|
|
|
fileName = subProject;
|
|
|
|
|
else
|
|
|
|
|
return link;
|
|
|
|
|
}
|
2013-02-06 14:23:18 +01:00
|
|
|
link.targetFileName = fileName;
|
|
|
|
|
link.linkTextStart = cursor.position() - positionInBlock + beginPos + 1;
|
|
|
|
|
link.linkTextEnd = cursor.position() - positionInBlock + endPos;
|
2012-02-19 18:58:39 +04:00
|
|
|
}
|
|
|
|
|
return link;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-30 12:11:32 +02:00
|
|
|
//
|
2012-02-19 18:58:39 +04:00
|
|
|
// CMakeDocument
|
2010-03-30 12:11:32 +02:00
|
|
|
//
|
2014-08-20 00:07:43 +02:00
|
|
|
|
2014-08-22 16:38:38 +02:00
|
|
|
class CMakeDocument : public BaseTextDocument
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
CMakeDocument();
|
|
|
|
|
|
|
|
|
|
QString defaultPath() const;
|
|
|
|
|
QString suggestedFileName() const;
|
|
|
|
|
};
|
|
|
|
|
|
2010-03-30 12:11:32 +02:00
|
|
|
CMakeDocument::CMakeDocument()
|
|
|
|
|
{
|
2014-08-20 00:07:43 +02:00
|
|
|
setId(Constants::CMAKE_EDITOR_ID);
|
|
|
|
|
setMimeType(QLatin1String(Constants::CMAKEMIMETYPE));
|
2010-03-30 12:11:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString CMakeDocument::defaultPath() const
|
|
|
|
|
{
|
2013-07-04 13:30:26 +02:00
|
|
|
QFileInfo fi(filePath());
|
2010-03-30 12:11:32 +02:00
|
|
|
return fi.absolutePath();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString CMakeDocument::suggestedFileName() const
|
|
|
|
|
{
|
2013-07-04 13:30:26 +02:00
|
|
|
QFileInfo fi(filePath());
|
2010-03-30 12:11:32 +02:00
|
|
|
return fi.fileName();
|
|
|
|
|
}
|
2014-08-20 00:07:43 +02:00
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// CMakeEditorFactory
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
CMakeEditorFactory::CMakeEditorFactory()
|
|
|
|
|
{
|
|
|
|
|
setId(Constants::CMAKE_EDITOR_ID);
|
|
|
|
|
setDisplayName(tr(Constants::CMAKE_EDITOR_DISPLAY_NAME));
|
|
|
|
|
addMimeType(Constants::CMAKEMIMETYPE);
|
|
|
|
|
addMimeType(Constants::CMAKEPROJECTMIMETYPE);
|
|
|
|
|
|
2014-08-22 16:38:38 +02:00
|
|
|
setEditorCreator([]() { return new CMakeEditor; });
|
|
|
|
|
setEditorWidgetCreator([]() { return new CMakeEditorWidget; });
|
|
|
|
|
setDocumentCreator([]() { return new CMakeDocument; });
|
|
|
|
|
setGenericSyntaxHighlighter(QLatin1String(Constants::CMAKEMIMETYPE));
|
2014-09-01 16:16:44 +02:00
|
|
|
setCommentStyle(Utils::CommentDefinition::HashStyle);
|
2014-08-22 16:38:38 +02:00
|
|
|
|
2014-09-02 12:25:20 +02:00
|
|
|
setEditorActionHandlers(TextEditorActionHandler::UnCommentSelection
|
2014-08-20 00:07:43 +02:00
|
|
|
| TextEditorActionHandler::JumpToFileUnderCursor);
|
|
|
|
|
|
|
|
|
|
ActionContainer *contextMenu = ActionManager::createMenu(Constants::M_CONTEXT);
|
|
|
|
|
contextMenu->addAction(ActionManager::command(TextEditor::Constants::JUMP_TO_FILE_UNDER_CURSOR));
|
2014-09-02 12:25:20 +02:00
|
|
|
contextMenu->addSeparator(Context(Constants::CMAKE_EDITOR_ID));
|
2014-08-20 00:07:43 +02:00
|
|
|
contextMenu->addAction(ActionManager::command(TextEditor::Constants::UN_COMMENT_SELECTION));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace CMakeProjectManager
|