1
/* This file is part of the Kate project.
3
* Copyright (C) 2012 Christoph Cullmann <cullmann@kde.org>
4
* Copyright (C) 2003 Anders Lund <anders.lund@lund.tdcadsl.dk>
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Library General Public
8
* License as published by the Free Software Foundation; either
9
* version 2 of the License, or (at your option) any later version.
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Library General Public License for more details.
16
* You should have received a copy of the GNU Library General Public License
17
* along with this library; see the file COPYING.LIB. If not, write to
18
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
* Boston, MA 02110-1301, USA.
22
#include "kateprojectcompletion.h"
23
#include "kateprojectplugin.h"
28
KateProjectCompletion::KateProjectCompletion (KateProjectPlugin *plugin)
29
: KTextEditor::CodeCompletionModel (0)
34
KateProjectCompletion::~KateProjectCompletion ()
38
void KateProjectCompletion::saveMatches( KTextEditor::View* view, const KTextEditor::Range& range)
41
allMatches( m_matches, view, range );
44
QVariant KateProjectCompletion::data(const QModelIndex& index, int role) const
46
if( role == InheritanceDepth )
47
return 10010; //Very high value, so the word-completion group and items are shown behind any other groups/items if there is multiple
49
if( !index.parent().isValid() ) {
50
//It is the group header
54
return i18n("Project Completion");
56
return Qt::DisplayRole;
60
if( index.column() == KTextEditor::CodeCompletionModel::Name && role == Qt::DisplayRole )
61
return m_matches.item ( index.row() )->data (Qt::DisplayRole);
63
if( index.column() == KTextEditor::CodeCompletionModel::Icon && role == Qt::DecorationRole ) {
64
static QIcon icon(KIcon("insert-text").pixmap(QSize(16, 16)));
71
QModelIndex KateProjectCompletion::parent(const QModelIndex& index) const
73
if(index.internalId())
74
return createIndex(0, 0, 0);
79
QModelIndex KateProjectCompletion::index(int row, int column, const QModelIndex& parent) const
81
if( !parent.isValid()) {
83
return createIndex(row, column, 0);
87
}else if(parent.parent().isValid())
91
if (row < 0 || row >= m_matches.rowCount() || column < 0 || column >= ColumnCount )
94
return createIndex(row, column, 1);
97
int KateProjectCompletion::rowCount ( const QModelIndex & parent ) const
99
if( !parent.isValid() && !(m_matches.rowCount() == 0) )
100
return 1; //One root node to define the custom group
101
else if(parent.parent().isValid())
102
return 0; //Completion-items have no children
104
return m_matches.rowCount();
108
bool KateProjectCompletion::shouldStartCompletion(KTextEditor::View* view, const QString &insertedText, bool userInsertion, const KTextEditor::Cursor &position)
110
if (!userInsertion) return false;
111
if(insertedText.isEmpty())
114
QString text = view->document()->line(position.line()).left(position.column());
116
uint check=3;//v->config()->wordCompletionMinimalWordLength();
118
if (check<=0) return true;
119
int start=text.length();
120
int end=text.length()-check;
121
if (end<0) return false;
122
for (int i=start-1;i>=end;i--) {
124
if (! (c.isLetter() || (c.isNumber()) || c=='_') ) return false;
130
bool KateProjectCompletion::shouldAbortCompletion(KTextEditor::View* view, const KTextEditor::Range &range, const QString ¤tCompletion) {
133
if (currentCompletion.length() < 3 /*v->config()->wordCompletionMinimalWordLength()*/) return true;
136
return CodeCompletionModelControllerInterface3::shouldAbortCompletion(view,range,currentCompletion);
139
void KateProjectCompletion::completionInvoked(KTextEditor::View* view, const KTextEditor::Range& range, InvocationType it)
145
if (it==AutomaticInvocation) {
148
if (range.columnWidth() >= 3 /*v->config()->wordCompletionMinimalWordLength()*/)
149
saveMatches( view, range );
158
saveMatches( view, range );
162
// Scan throughout the entire document for possible completions,
163
// ignoring any dublets
164
void KateProjectCompletion::allMatches (QStandardItemModel &model, KTextEditor::View *view, const KTextEditor::Range &range) const
167
* get project for this document, else fail
169
KateProject *project = m_plugin->projectForDocument (view->document());
174
* let project index fill the completion for this document
176
if (project->projectIndex())
177
project->projectIndex()->findMatches (model, view->document()->text(range), KateProjectIndex::CompletionMatches);
180
KTextEditor::CodeCompletionModelControllerInterface3::MatchReaction KateProjectCompletion::matchingItem(const QModelIndex& /*matched*/)
182
return HideListIfAutomaticInvocation;
185
// Return the range containing the word left of the cursor
186
KTextEditor::Range KateProjectCompletion::completionRange(KTextEditor::View* view, const KTextEditor::Cursor &position)
188
int line = position.line();
189
int col = position.column();
191
KTextEditor::Document *doc = view->document();
194
QChar c = ( doc->character( KTextEditor::Cursor( line, col-1 ) ) );
195
if ( c.isLetterOrNumber() || c.isMark() || c == '_' )
204
return KTextEditor::Range( KTextEditor::Cursor( line, col ), position );
207
// kate: space-indent on; indent-width 2; replace-tabs on;