cmakeeditor.cpp 7.26 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
    connect(document(), SIGNAL(changed()), this, SLOT(markAsChanged()));
66
67
}

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

76
77
78
79
80
TextEditor::CompletionAssistProvider *CMakeEditor::completionAssistProvider()
{
    return ExtensionSystem::PluginManager::getObject<CMakeFileCompletionAssistProvider>();
}

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

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

109
110
111
112
//
// CMakeEditor
//

113
CMakeEditorWidget::CMakeEditorWidget(QWidget *parent)
114
    : BaseTextEditorWidget(new CMakeDocument(), parent)
115
{
116
117
    ctor();
}
118

119
120
121
122
123
124
125
126
CMakeEditorWidget::CMakeEditorWidget(CMakeEditorWidget *other)
    : BaseTextEditorWidget(other)
{
    ctor();
}

void CMakeEditorWidget::ctor()
{
127
    m_commentDefinition.clearCommentStyles();
128
    m_commentDefinition.singleLine = QLatin1Char('#');
129
130
}

131
TextEditor::BaseTextEditor *CMakeEditorWidget::createEditor()
132
{
133
    return new CMakeEditor(this);
134
135
}

136
137
138
139
140
141
142
void CMakeEditorWidget::unCommentSelection()
{
    Utils::unCommentSelection(this, m_commentDefinition);
}

void CMakeEditorWidget::contextMenuEvent(QContextMenuEvent *e)
{
143
    showDefaultContextMenu(e, Constants::M_CONTEXT);
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
}

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,
159
                                                      bool/* resolveTarget*/, bool /*inNextSplit*/)
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
{
    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

203
    QDir dir(QFileInfo(baseTextDocument()->filePath()).absolutePath());
204
205
206
207
208
    QString fileName = dir.filePath(buffer);
    QFileInfo fi(fileName);
    if (fi.exists()) {
        if (fi.isDir()) {
            QDir subDir(fi.absoluteFilePath());
209
            QString subProject = subDir.filePath(QLatin1String("CMakeLists.txt"));
210
211
212
213
214
            if (QFileInfo(subProject).exists())
                fileName = subProject;
            else
                return link;
        }
David Schulz's avatar
David Schulz committed
215
216
217
        link.targetFileName = fileName;
        link.linkTextStart = cursor.position() - positionInBlock + beginPos + 1;
        link.linkTextEnd = cursor.position() - positionInBlock + endPos;
218
219
220
221
222
    }
    return link;
}


223
//
224
// CMakeDocument
225
226
227
228
229
//

CMakeDocument::CMakeDocument()
    : TextEditor::BaseTextDocument()
{
230
    setId(CMakeProjectManager::Constants::CMAKE_EDITOR_ID);
231
    setMimeType(QLatin1String(CMakeProjectManager::Constants::CMAKEMIMETYPE));
232
    setSyntaxHighlighter(new CMakeHighlighter);
233
234
235
236
}

QString CMakeDocument::defaultPath() const
{
237
    QFileInfo fi(filePath());
238
239
240
241
242
    return fi.absolutePath();
}

QString CMakeDocument::suggestedFileName() const
{
243
    QFileInfo fi(filePath());
244
245
    return fi.fileName();
}