cmakeeditor.cpp 8.23 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
QString CMakeEditor::contextHelpId() const
{
    int pos = position();
112
    TextEditor::BaseTextEditorDocument* doc = const_cast<CMakeEditor*>(this)->textDocument();
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
148
149
150

    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;
}

151
152
153
154
//
// CMakeEditor
//

155
CMakeEditorWidget::CMakeEditorWidget(QWidget *parent)
156
    : BaseTextEditorWidget(new CMakeDocument(), parent)
157
{
158
159
    ctor();
}
160

161
162
163
164
165
166
167
168
CMakeEditorWidget::CMakeEditorWidget(CMakeEditorWidget *other)
    : BaseTextEditorWidget(other)
{
    ctor();
}

void CMakeEditorWidget::ctor()
{
169
    m_commentDefinition.clearCommentStyles();
170
    m_commentDefinition.singleLine = QLatin1Char('#');
171
172
}

173
TextEditor::BaseTextEditor *CMakeEditorWidget::createEditor()
174
{
175
    return new CMakeEditor(this);
176
177
}

178
179
180
181
182
183
184
void CMakeEditorWidget::unCommentSelection()
{
    Utils::unCommentSelection(this, m_commentDefinition);
}

void CMakeEditorWidget::contextMenuEvent(QContextMenuEvent *e)
{
185
    showDefaultContextMenu(e, Constants::M_CONTEXT);
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
}

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,
201
                                                      bool/* resolveTarget*/, bool /*inNextSplit*/)
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
{
    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

245
    QDir dir(QFileInfo(baseTextDocument()->filePath()).absolutePath());
246
247
248
249
250
    QString fileName = dir.filePath(buffer);
    QFileInfo fi(fileName);
    if (fi.exists()) {
        if (fi.isDir()) {
            QDir subDir(fi.absoluteFilePath());
251
            QString subProject = subDir.filePath(QLatin1String("CMakeLists.txt"));
252
253
254
255
256
            if (QFileInfo(subProject).exists())
                fileName = subProject;
            else
                return link;
        }
David Schulz's avatar
David Schulz committed
257
258
259
        link.targetFileName = fileName;
        link.linkTextStart = cursor.position() - positionInBlock + beginPos + 1;
        link.linkTextEnd = cursor.position() - positionInBlock + endPos;
260
261
262
263
264
    }
    return link;
}


265
//
266
// CMakeDocument
267
268
269
270
271
//

CMakeDocument::CMakeDocument()
    : TextEditor::BaseTextDocument()
{
272
    setId(CMakeProjectManager::Constants::CMAKE_EDITOR_ID);
273
    setMimeType(QLatin1String(CMakeProjectManager::Constants::CMAKEMIMETYPE));
274
    setSyntaxHighlighter(new CMakeHighlighter);
275
276
277
278
}

QString CMakeDocument::defaultPath() const
{
279
    QFileInfo fi(filePath());
280
281
282
283
284
    return fi.absolutePath();
}

QString CMakeDocument::suggestedFileName() const
{
285
    QFileInfo fi(filePath());
286
287
    return fi.fileName();
}