cmakeeditor.cpp 7.95 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8 9 10 11 12 13 14
** 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.
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22 23 24 25
** 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
con's avatar
con committed
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
29 30 31

#include "cmakeeditor.h"

32
#include "cmakefilecompletionassist.h"
33 34
#include "cmakehighlighter.h"
#include "cmakeprojectconstants.h"
35
#include "cmakeproject.h"
36

37
#include <coreplugin/icore.h>
38
#include <coreplugin/infobar.h>
39 40
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/actionmanager.h>
41
#include <extensionsystem/pluginmanager.h>
42 43
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/session.h>
44 45 46 47
#include <texteditor/texteditoractionhandler.h>
#include <texteditor/texteditorconstants.h>
#include <texteditor/texteditorsettings.h>

48
#include <QFileInfo>
49
#include <QSharedPointer>
50
#include <QTextBlock>
51 52 53 54 55 56 57 58

using namespace CMakeProjectManager;
using namespace CMakeProjectManager::Internal;

//
// ProFileEditorEditable
//

59
CMakeEditor::CMakeEditor(CMakeEditorWidget *editor)
60
  : BaseTextEditor(editor)
61
{
62 63
    setContext(Core::Context(CMakeProjectManager::Constants::C_CMAKEEDITOR,
              TextEditor::Constants::C_TEXTEDITOR));
64
    setDuplicateSupported(true);
65
    setCommentStyle(Utils::CommentDefinition::HashStyle);
66
    setCompletionAssistProvider(ExtensionSystem::PluginManager::getObject<CMakeFileCompletionAssistProvider>());
67
    connect(document(), SIGNAL(changed()), this, SLOT(markAsChanged()));
68 69
}

70
Core::IEditor *CMakeEditor::duplicate()
71
{
72 73
    CMakeEditorWidget *ret = new CMakeEditorWidget(
                qobject_cast<CMakeEditorWidget *>(editorWidget()));
74
    TextEditor::TextEditorSettings::initializeEditor(ret);
75
    return ret->editor();
76 77
}

78 79
void CMakeEditor::markAsChanged()
{
80
    if (!document()->isModified())
Daniel Teske's avatar
Daniel Teske committed
81
        return;
82 83 84
    Core::InfoBar *infoBar = document()->infoBar();
    Core::Id infoRunCmake("CMakeEditor.RunCMake");
    if (!infoBar->canInfoBeAdded(infoRunCmake))
Daniel Teske's avatar
Daniel Teske committed
85
        return;
86
    Core::InfoBarEntry info(infoRunCmake,
87 88
                            tr("Changes to cmake files are shown in the project tree after building."),
                            Core::InfoBarEntry::GlobalSuppressionEnabled);
89
    info.setCustomButtonInfo(tr("Build now"), this, SLOT(build()));
90
    infoBar->addInfo(info);
91 92 93 94
}

void CMakeEditor::build()
{
hjk's avatar
hjk committed
95
    foreach (ProjectExplorer::Project *p, ProjectExplorer::SessionManager::projects()) {
96 97
        CMakeProject *cmakeProject = qobject_cast<CMakeProject *>(p);
        if (cmakeProject) {
98
            if (cmakeProject->isProjectFile(document()->filePath())) {
99 100 101 102 103 104 105
                ProjectExplorer::ProjectExplorerPlugin::instance()->buildProject(cmakeProject);
                break;
            }
        }
    }
}

106 107 108
QString CMakeEditor::contextHelpId() const
{
    int pos = position();
109
    TextEditor::BaseTextDocument *doc = const_cast<CMakeEditor*>(this)->textDocument();
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147

    QChar chr;
    do {
        --pos;
        if (pos < 0)
            break;
        chr = doc->characterAt(pos);
        if (chr == QLatin1Char('('))
            return QString();
    } while (chr.unicode() != QChar::ParagraphSeparator);

    ++pos;
    chr = doc->characterAt(pos);
    while (chr.isSpace()) {
        ++pos;
        chr = doc->characterAt(pos);
    }
    int begin = pos;

    do {
        ++pos;
        chr = doc->characterAt(pos);
    } while (chr.isLetterOrNumber() || chr == QLatin1Char('_'));
    int end = pos;

    while (chr.isSpace()) {
        ++pos;
        chr = doc->characterAt(pos);
    }

    // Not a command
    if (chr != QLatin1Char('('))
        return QString();

    QString command = doc->textAt(begin, end - begin).toLower();
    return QLatin1String("command/") + command;
}

148 149 150 151
//
// CMakeEditor
//

152
CMakeEditorWidget::CMakeEditorWidget(QWidget *parent)
153
    : BaseTextEditorWidget(new CMakeDocument(), parent)
154
{}
155

156 157
CMakeEditorWidget::CMakeEditorWidget(CMakeEditorWidget *other)
    : BaseTextEditorWidget(other)
158
{}
159

160
TextEditor::BaseTextEditor *CMakeEditorWidget::createEditor()
161
{
162
    return new CMakeEditor(this);
163 164
}

165 166
void CMakeEditorWidget::contextMenuEvent(QContextMenuEvent *e)
{
167
    showDefaultContextMenu(e, Constants::M_CONTEXT);
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
}

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,
183
                                                      bool/* resolveTarget*/, bool /*inNextSplit*/)
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
{
    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

227
    QDir dir(QFileInfo(textDocument()->filePath()).absolutePath());
228 229 230 231 232
    QString fileName = dir.filePath(buffer);
    QFileInfo fi(fileName);
    if (fi.exists()) {
        if (fi.isDir()) {
            QDir subDir(fi.absoluteFilePath());
233
            QString subProject = subDir.filePath(QLatin1String("CMakeLists.txt"));
234 235 236 237 238
            if (QFileInfo(subProject).exists())
                fileName = subProject;
            else
                return link;
        }
David Schulz's avatar
David Schulz committed
239 240 241
        link.targetFileName = fileName;
        link.linkTextStart = cursor.position() - positionInBlock + beginPos + 1;
        link.linkTextEnd = cursor.position() - positionInBlock + endPos;
242 243 244 245 246
    }
    return link;
}


247
//
248
// CMakeDocument
249 250 251 252 253
//

CMakeDocument::CMakeDocument()
    : TextEditor::BaseTextDocument()
{
254
    setId(CMakeProjectManager::Constants::CMAKE_EDITOR_ID);
255
    setMimeType(QLatin1String(CMakeProjectManager::Constants::CMAKEMIMETYPE));
256
    setSyntaxHighlighter(new CMakeHighlighter);
257 258 259 260
}

QString CMakeDocument::defaultPath() const
{
261
    QFileInfo fi(filePath());
262 263 264 265 266
    return fi.absolutePath();
}

QString CMakeDocument::suggestedFileName() const
{
267
    QFileInfo fi(filePath());
268 269
    return fi.fileName();
}