cmakeeditor.cpp 7.44 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3
** Copyright (C) 2013 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
35
#include "cmakehighlighter.h"
#include "cmakeeditorfactory.h"
#include "cmakeprojectconstants.h"
36
#include "cmakeproject.h"
37

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

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

using namespace CMakeProjectManager;
using namespace CMakeProjectManager::Internal;

//
// ProFileEditorEditable
//

60
CMakeEditor::CMakeEditor(CMakeEditorWidget *editor)
61
  : BaseTextEditor(editor)
62
{
63
64
    setContext(Core::Context(CMakeProjectManager::Constants::C_CMAKEEDITOR,
              TextEditor::Constants::C_TEXTEDITOR));
65
    connect(document(), SIGNAL(changed()), this, SLOT(markAsChanged()));
66
67
}

68
Core::IEditor *CMakeEditor::duplicate(QWidget *parent)
69
{
70
    CMakeEditorWidget *w = qobject_cast<CMakeEditorWidget*>(widget());
71
72
    CMakeEditorWidget *ret = new CMakeEditorWidget(parent, w->factory(), w->actionHandler());
    ret->duplicateFrom(w);
73
    TextEditor::TextEditorSettings::instance()->initializeEditor(ret);
74
    return ret->editor();
75
76
}

hjk's avatar
hjk committed
77
Core::Id CMakeEditor::id() const
78
{
Orgad Shaneh's avatar
Orgad Shaneh committed
79
    return Core::Id(CMakeProjectManager::Constants::CMAKE_EDITOR_ID);
80
81
}

82
83
84
85
86
TextEditor::CompletionAssistProvider *CMakeEditor::completionAssistProvider()
{
    return ExtensionSystem::PluginManager::getObject<CMakeFileCompletionAssistProvider>();
}

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

void CMakeEditor::build()
{
hjk's avatar
hjk committed
104
    foreach (ProjectExplorer::Project *p, ProjectExplorer::SessionManager::projects()) {
105
106
        CMakeProject *cmakeProject = qobject_cast<CMakeProject *>(p);
        if (cmakeProject) {
107
            if (cmakeProject->isProjectFile(document()->filePath())) {
108
109
110
111
112
113
114
                ProjectExplorer::ProjectExplorerPlugin::instance()->buildProject(cmakeProject);
                break;
            }
        }
    }
}

115
116
117
118
//
// CMakeEditor
//

119
120
CMakeEditorWidget::CMakeEditorWidget(QWidget *parent, CMakeEditorFactory *factory, TextEditor::TextEditorActionHandler *ah)
    : BaseTextEditorWidget(parent), m_factory(factory), m_ah(ah)
121
{
122
    QSharedPointer<CMakeDocument> doc(new CMakeDocument);
123
124
125
126
    doc->setMimeType(QLatin1String(CMakeProjectManager::Constants::CMAKEMIMETYPE));
    setBaseTextDocument(doc);

    baseTextDocument()->setSyntaxHighlighter(new CMakeHighlighter);
127
128

    m_commentDefinition.clearCommentStyles();
129
    m_commentDefinition.singleLine = QLatin1Char('#');
130
131

    ah->setupActions(this);
132
133
}

134
TextEditor::BaseTextEditor *CMakeEditorWidget::createEditor()
135
{
136
    return new CMakeEditor(this);
137
138
}

139
140
141
142
143
144
145
void CMakeEditorWidget::unCommentSelection()
{
    Utils::unCommentSelection(this, m_commentDefinition);
}

void CMakeEditorWidget::contextMenuEvent(QContextMenuEvent *e)
{
146
    showDefaultContextMenu(e, Constants::M_CONTEXT);
147
148
149
150
151
152
153
154
155
156
157
158
159
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
203
204
205
}

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,
                                                      bool/* resolveTarget*/)
{
    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

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


226
//
227
// CMakeDocument
228
229
230
231
232
233
234
235
236
//

CMakeDocument::CMakeDocument()
    : TextEditor::BaseTextDocument()
{
}

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