2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2010-03-30 12:11:32 +02:00
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** Copyright (C) 2015 The Qt Company Ltd.
|
|
|
|
|
** Contact: http://www.qt.io/licensing
|
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
|
2015-01-14 18:07:15 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms and
|
|
|
|
|
** conditions see http://www.qt.io/terms-conditions. For further information
|
2014-10-01 13:21:18 +02:00
|
|
|
** use the contact form at http://www.qt.io/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
|
2014-10-01 13:21:18 +02:00
|
|
|
** General Public License version 2.1 or version 3 as published by the Free
|
|
|
|
|
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
|
|
|
|
|
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
|
|
|
|
|
** following information to ensure the GNU Lesser General Public License
|
|
|
|
|
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
|
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2012-10-02 09:12:39 +02:00
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** In addition, as a special exception, The Qt Company gives you certain additional
|
|
|
|
|
** rights. These rights are described in The Qt Company 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;
|
2014-09-03 10:34:52 +02:00
|
|
|
using namespace ProjectExplorer;
|
2014-08-20 00:07:43 +02:00
|
|
|
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]() {
|
2014-09-26 09:14:03 +02:00
|
|
|
BaseTextDocument *document = textDocument();
|
2014-08-22 16:38:38 +02:00
|
|
|
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);
|
2014-09-03 10:34:52 +02:00
|
|
|
info.setCustomButtonInfo(tr("Build now"), [document]() {
|
|
|
|
|
foreach (Project *p, SessionManager::projects()) {
|
|
|
|
|
if (CMakeProject *cmakeProject = qobject_cast<CMakeProject *>(p)) {
|
2015-02-02 00:37:38 +02:00
|
|
|
if (cmakeProject->isProjectFile(document->filePath())) {
|
2014-09-12 01:08:28 +02:00
|
|
|
ProjectExplorerPlugin::buildProject(cmakeProject);
|
2014-09-03 10:34:52 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2014-08-22 16:38:38 +02:00
|
|
|
infoBar->addInfo(info);
|
|
|
|
|
});
|
2011-03-31 17:43:48 +02:00
|
|
|
}
|
|
|
|
|
|
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-09-26 11:37:54 +02:00
|
|
|
class CMakeEditorWidget : public TextEditorWidget
|
2014-08-22 16:38:38 +02:00
|
|
|
{
|
|
|
|
|
public:
|
2014-10-15 00:36:39 +02:00
|
|
|
CMakeEditorWidget() {}
|
2014-08-22 16:38:38 +02:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
bool save(const QString &fileName = QString());
|
2015-06-01 17:55:31 +02:00
|
|
|
Link findLinkAt(const QTextCursor &cursor, bool resolveTarget = true, bool inNextSplit = false) override;
|
|
|
|
|
void contextMenuEvent(QContextMenuEvent *e) override;
|
2014-08-22 16:38:38 +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-12-21 21:54:30 +02:00
|
|
|
QDir dir(textDocument()->filePath().toFileInfo().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"));
|
2014-10-24 10:28:28 +02:00
|
|
|
if (QFileInfo::exists(subProject))
|
2012-02-19 18:58:39 +04:00
|
|
|
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-09-22 18:43:31 +02:00
|
|
|
class CMakeDocument : public TextDocument
|
2014-08-22 16:38:38 +02:00
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
CMakeDocument();
|
|
|
|
|
|
2015-06-04 15:54:10 +02:00
|
|
|
QString defaultPath() const override;
|
|
|
|
|
QString suggestedFileName() const override;
|
2014-08-22 16:38:38 +02:00
|
|
|
};
|
|
|
|
|
|
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
|
|
|
|
|
{
|
2014-12-21 21:54:30 +02:00
|
|
|
return filePath().toFileInfo().absolutePath();
|
2010-03-30 12:11:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString CMakeDocument::suggestedFileName() const
|
|
|
|
|
{
|
2015-01-10 23:40:32 +02:00
|
|
|
return filePath().fileName();
|
2010-03-30 12:11:32 +02:00
|
|
|
}
|
2014-08-20 00:07:43 +02:00
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// CMakeEditorFactory
|
|
|
|
|
//
|
|
|
|
|
|
2015-02-04 17:54:46 +01:00
|
|
|
CMakeEditorFactory::CMakeEditorFactory()
|
2014-08-20 00:07:43 +02:00
|
|
|
{
|
|
|
|
|
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; });
|
2015-02-03 09:18:57 +01:00
|
|
|
setUseGenericHighlighter(true);
|
2014-09-01 16:16:44 +02:00
|
|
|
setCommentStyle(Utils::CommentDefinition::HashStyle);
|
2014-10-15 00:36:39 +02:00
|
|
|
setCodeFoldingSupported(true);
|
2014-08-22 16:38:38 +02:00
|
|
|
|
2015-02-04 17:54:46 +01:00
|
|
|
setCompletionAssistProvider(new CMakeFileCompletionAssistProvider);
|
2014-10-01 22:39:47 +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
|