1
/* This file is part of the Kate project.
3
* Copyright (C) 2012 Christoph Cullmann <cullmann@kde.org>
5
* This library is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU Library General Public
7
* License as published by the Free Software Foundation; either
8
* version 2 of the License, or (at your option) any later version.
10
* This library is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
* Library General Public License for more details.
15
* You should have received a copy of the GNU Library General Public License
16
* along with this library; see the file COPYING.LIB. If not, write to
17
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18
* Boston, MA 02110-1301, USA.
21
#include "kateprojectindex.h"
27
* include ctags reading
29
#include "ctags/readtags.c"
31
KateProjectIndex::KateProjectIndex(const QStringList &files)
32
: m_ctagsIndexFile(QDir::tempPath() + QStringLiteral("/kate.project.ctags"))
33
, m_ctagsIndexHandle(0)
41
KateProjectIndex::~KateProjectIndex()
44
* delete ctags handle if any
46
if (m_ctagsIndexHandle) {
47
tagsClose(m_ctagsIndexHandle);
48
m_ctagsIndexHandle = 0;
52
void KateProjectIndex::loadCtags(const QStringList &files)
55
* create temporary file
56
* if not possible, fail
58
if (!m_ctagsIndexFile.open()) {
63
* close file again, other process will use it
65
m_ctagsIndexFile.close();
68
* try to run ctags for all files in this project
69
* output to our ctags index file
73
args << QStringLiteral("-L") << QStringLiteral("-") << QStringLiteral("-f") << m_ctagsIndexFile.fileName() << QStringLiteral("--fields=+K+n");
74
ctags.start(QStringLiteral("ctags"), args);
75
if (!ctags.waitForStarted()) {
80
* write files list and close write channel
82
ctags.write(files.join(QStringLiteral("\n")).toLocal8Bit());
83
ctags.closeWriteChannel();
88
if (!ctags.waitForFinished()) {
93
* file not openable, bad
95
if (!m_ctagsIndexFile.open()) {
102
qint64 size = m_ctagsIndexFile.size();
107
m_ctagsIndexFile.close();
117
* try to open ctags file
120
memset(&info, 0, sizeof(tagFileInfo));
121
m_ctagsIndexHandle = tagsOpen(m_ctagsIndexFile.fileName().toLocal8Bit().constData(), &info);
124
void KateProjectIndex::findMatches(QStandardItemModel &model, const QString &searchWord, MatchType type)
127
* abort if no ctags index
129
if (!m_ctagsIndexHandle) {
137
QByteArray word = searchWord.toLocal8Bit();
138
if (word.isEmpty()) {
143
* try to search entry
147
if (tagsFind(m_ctagsIndexHandle, &entry, word.constData(), TAG_PARTIALMATCH | TAG_OBSERVECASE) != TagSuccess) {
152
* set to show words only once for completion matches
157
* loop over all found tags
158
* first one is filled by above find, others by find next
171
QString name(QString::fromLocal8Bit(entry.name));
174
* construct right items
177
case CompletionMatches:
179
* add new completion item, if new name
181
if (!guard.contains(name)) {
182
model.appendRow(new QStandardItem(name));
189
* add new find item, contains of multiple columns
191
QList<QStandardItem *> items;
192
items << new QStandardItem(name);
193
items << new QStandardItem(entry.kind ? QString::fromLocal8Bit(entry.kind) : QString());
194
items << new QStandardItem(entry.file ? QString::fromLocal8Bit(entry.file) : QString());
195
items << new QStandardItem(QString::number(entry.address.lineNumber));
196
model.appendRow(items);
199
} while (tagsFindNext(m_ctagsIndexHandle, &entry) == TagSuccess);