cmakecodecompletionmodel.cpp 10.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* KDevelop CMake Support
 *
 * Copyright 2008 Aleix Pol <aleixpol@gmail.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

#include "cmakecodecompletionmodel.h"
#include <QVariant>
#include <QModelIndex>
24
#include <QMimeDatabase>
Milian Wolff's avatar
Milian Wolff committed
25
#include <QUrl>
26
27
28
29
#include <language/duchain/duchain.h>
#include <language/duchain/duchainlock.h>
#include <language/duchain/ducontext.h>
#include <language/duchain/declaration.h>
30
31
#include <language/duchain/types/functiontype.h>
#include <language/duchain/types/delayedtype.h>
32

33
#include <cmakeduchaintypes.h>
34
35
#include "cmakeutils.h"
#include "icmakedocumentation.h"
36

37
38
39
40
41
#include <KTextEditor/Document>
#include <KTextEditor/View>
#include <KIO/Global>
#include <KLocalizedString>

42
using namespace KTextEditor;
43
using namespace KDevelop;
44

45
QVector<QString> CMakeCodeCompletionModel::s_commands;
46

47
48
CMakeCodeCompletionModel::CMakeCodeCompletionModel(QObject* parent)
    : CodeCompletionModel(parent)
49
    , m_inside(false)
50
{}
51

52
53
54
55
56
bool isFunction(const Declaration* decl)
{
    return decl->abstractType().cast<FunctionType>();
}

57
bool isPathChar(QChar c)
58
{
59
    return c.isLetterOrNumber() || c == QLatin1Char('/') || c == QLatin1Char('.');
60
61
}

62
63
QString escapePath(QString path)
{
64
65
66
67
    // see https://cmake.org/Wiki/CMake/Language_Syntax#Escapes
    static const QString toBeEscaped = QStringLiteral("\"()#$^");

    for(const QChar &ch : toBeEscaped) {
68
        path.replace(ch, QLatin1Char('\\') + ch);
69
70
71
72
    }
    return path;
}

73
74
void CMakeCodeCompletionModel::completionInvoked(View* view, const Range& range, InvocationType invocationType)
{
75
    beginResetModel();
76
77
78
79
80
81
    if(s_commands.isEmpty()) {
        ICMakeDocumentation* cmakedoc=CMake::cmakeDocumentation();
        
        if(cmakedoc)
            s_commands=cmakedoc->names(ICMakeDocumentation::Command);
    }
82
    
Stefan Martin Valouch's avatar
Stefan Martin Valouch committed
83
    Q_UNUSED(invocationType);
84
85
    m_declarations.clear();
    DUChainReadLocker lock(DUChain::lock());
86
87
88
    KTextEditor::Document* d=view->document();
    TopDUContext* ctx = DUChain::self()->chainForDocument( d->url() );
    
89
90
    QString line=d->line(range.end().line());
//     m_inside=line.lastIndexOf('(', range.end().column())>=0;
91
    m_inside=line.lastIndexOf(QLatin1Char('('), range.end().column()-line.size()-1)>=0;
92
93
94
95
    
    for(int l=range.end().line(); l>=0 && !m_inside; --l)
    {
        QString cline=d->line(l);
96
        const QStringRef line = cline.leftRef(cline.indexOf(QLatin1Char('#')));
97
        
98
        int close=line.lastIndexOf(QLatin1Char(')')), open=line.indexOf(QLatin1Char('('));
99
100
101
102
103
104
105
106
107
108
109
        
        if(close>=0 && open>=0) {
            m_inside=open>close;
            break;
        } else if(open>=0) {
            m_inside=true;
            break;
        } else if(close>=0) {
            m_inside=false;
            break;
        }
110
    }
111
    
112
    int numRows = 0;
113
    if(m_inside) {
114
        Cursor start=range.start();
115
        for(; isPathChar(d->characterAt(start)); start-=Cursor(0,1))
116
117
118
119
120
        {}
        start+=Cursor(0,1);
        
        QString tocomplete=d->text(Range(start, range.end()-Cursor(0,1)));
        
121
122
        int lastdir=tocomplete.lastIndexOf(QLatin1Char('/'));
        QString path = KIO::upUrl(QUrl(d->url())).adjusted(QUrl::StripTrailingSlash).toLocalFile()+QLatin1Char('/');
123
        if(lastdir>=0) {
124
125
            const QStringRef basePath = tocomplete.midRef(0, lastdir);
            path += basePath;
126
127
128
        }
        QDir dir(path);
        
129
        const QFileInfoList paths = dir.entryInfoList(QStringList(tocomplete.midRef(lastdir+1)+QLatin1Char('*')),
130
131
                                              QDir::AllEntries | QDir::NoDotAndDotDot);
        m_paths.clear();
132
        m_paths.reserve(paths.size());
133
        for (const QFileInfo& f : paths) {
134
135
            QString currentPath = f.fileName();
            if(f.isDir())
136
                currentPath += QLatin1Char('/');
137
138
            m_paths += currentPath;
        }
139
140
141
        
        numRows += m_paths.count();
    } else
142
143
        numRows += s_commands.count();
    
144
145
    if(ctx)
    {
146
147
148
        const auto list = ctx->allDeclarations( ctx->transformToLocalRevision(KTextEditor::Cursor(range.start())), ctx );

        for (const auto& pair : list) {
149
            bool func=isFunction(pair.first);
150
            if((func && !m_inside) || (!func && m_inside))
151
                m_declarations.append(pair.first);
152
153
        }
        
154
155
156
        numRows+=m_declarations.count();
    }
    setRowCount(numRows);
157
    endResetModel();
158
159
160
}

CMakeCodeCompletionModel::Type CMakeCodeCompletionModel::indexType(int row) const
161
{
162
    if(m_inside)
163
    {
164
        if(row < m_declarations.count()) {
165
            KDevelop::DUChainReadLocker lock;
166
167
168
169
170
            Declaration* dec = m_declarations.at(row).declaration();
            if (dec && dec->type<TargetType>())
                return Target;
            else
                return Variable;
171
        } else
172
173
            return Path;
    }
174
    else
175
    {
176
        if(row<m_declarations.count())
177
            return Macro;
178
179
        else
            return Command;
180
    }
181
182
183
184
185
}

QVariant CMakeCodeCompletionModel::data (const QModelIndex & index, int role) const
{
    if(!index.isValid())
186
        return QVariant();
187
188
189
190
    Type type=indexType(index.row());

    if(role==Qt::DisplayRole && index.column()==CodeCompletionModel::Name)
    {
191
192
193
194
195
196
197
198
199
200
201
202
203
204
        int pos = index.row();
        switch(type) {
            case Command:
                return s_commands[pos-m_declarations.size()];
            case Path:
                return m_paths[pos-m_declarations.size()];
            case Target:
            case Variable:
            case Macro: {
                DUChainReadLocker lock(DUChain::lock());
                Declaration* dec=m_declarations[pos].data();
                
                return dec ? dec->identifier().toString() : i18n("INVALID");
            }
205
206
207
208
        }
    }
    else if(role==Qt::DisplayRole && index.column()==CodeCompletionModel::Prefix)
    {
209
        switch(type)
210
        {
211
212
213
            case Command:   return i18n("Command");
            case Variable:  return i18n("Variable");
            case Macro:     return i18n("Macro");
214
            case Path:      return i18n("Path");
215
            case Target:      return i18n("Target");
216
217
218
219
220
221
        }
    }
    else if(role==Qt::DecorationRole && index.column()==CodeCompletionModel::Icon)
    {
        switch(type)
        {
222
223
224
225
            case Command:   return QIcon::fromTheme(QStringLiteral("code-block"));
            case Variable:  return QIcon::fromTheme(QStringLiteral("code-variable"));
            case Macro:     return QIcon::fromTheme(QStringLiteral("code-function"));
            case Target:    return QIcon::fromTheme(QStringLiteral("system-run"));
226
            case Path: {
227
228
229
230
231
232
233
234
235
236
237
                QUrl url = QUrl::fromUserInput(m_paths[index.row()-m_declarations.size()]);
                QString iconName;
                if (url.isLocalFile()) {
                    // don't read contents even if it is a local file
                    iconName = QMimeDatabase().mimeTypeForFile(url.toLocalFile(), QMimeDatabase::MatchExtension).iconName();
                }
                else {
                    // remote always only looks at the extension
                    iconName = QMimeDatabase().mimeTypeForUrl(url).iconName();
                }
                return QIcon::fromTheme(iconName);
238
            }
239
240
241
242
        }
    }
    else if(role==Qt::DisplayRole && index.column()==CodeCompletionModel::Arguments)
    {
243
244
245
        switch(type) {
            case Variable:
            case Command:
246
            case Path:
247
            case Target:
248
249
                break;
            case Macro:
250
            {
251
                DUChainReadLocker lock(DUChain::lock());
252
                int pos=index.row();
253
254

                FunctionType::Ptr func;
255
256
                
                if(m_declarations[pos].data())
257
                    func = m_declarations[pos].data()->abstractType().cast<FunctionType>();
258
                
259
260
261
                if(!func)
                    return QVariant();

262
                QStringList args;
263
264
265
                const auto arguments = func->arguments();
                args.reserve(arguments.size());
                for (const AbstractType::Ptr& t : arguments) {
266
                    DelayedType::Ptr delay = t.cast<DelayedType>();
267
                    args.append(delay ? delay->identifier().toString() : i18n("wrong"));
268
                }
269
                return QString(QLatin1Char('(')+args.join(QStringLiteral(", "))+QLatin1Char(')'));
270
271
            }
        }
272
        
273
274
    }
    return QVariant();
275
276
}

277
void CMakeCodeCompletionModel::executeCompletionItem(View* view, const Range& word, const QModelIndex& idx) const
278
{
279
280
    Document* document = view->document();
    const int row = idx.row();
281
282
    switch(indexType(row))
    {
283
284
        case Path: {
            Range r=word;
285
            for (QChar c=document->characterAt(r.end()); c.isLetterOrNumber() || c==QLatin1Char('.'); c=document->characterAt(r.end())) {
286
                r.setEnd(KTextEditor::Cursor(r.end().line(), r.end().column()+1));
287
            }
288
289
290
            QString path = data(index(row, Name, QModelIndex())).toString();

            document->replaceText(r, escapePath(path));
291
        }   break;
292
        case Macro:
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
293
294
        case Command: {
            QString code=data(index(row, Name, QModelIndex())).toString();
295
296
            if (!document->line(word.start().line()).contains(QLatin1Char('(')))
                code.append(QLatin1Char('('));
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
297
298
            
            document->replaceText(word, code);
299
300
        }   break;
        case Variable: {
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
301
            Range r=word, prefix(word.start()-Cursor(0,2), word.start());
302
            QString bef=document->text(prefix);
303
            if(r.start().column()>=2 && bef==QLatin1String("${"))
304
                r.setStart(KTextEditor::Cursor(r.start().line(), r.start().column()-2));
305
306
307
            QString code = QLatin1String("${")+data(index(row, Name, QModelIndex())).toString();
            if(document->characterAt(word.end())!=QLatin1Char('}'))
                code += QLatin1Char('}');
Aleix Pol Gonzalez's avatar
Aleix Pol Gonzalez committed
308
309
            
            document->replaceText(r, code);
310
        }   break;
311
312
313
        case Target:
            document->replaceText(word, data(index(row, Name, QModelIndex())).toString());
            break;
314
    }
315
316
}