~ubuntu-branches/ubuntu/saucy/kate/saucy

« back to all changes in this revision

Viewing changes to kate/plugins/project/kateprojectcompletion.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell, Rohan Garg, Jonathan Riddell
  • Date: 2013-06-21 00:48:29 UTC
  • mfrom: (1.1.28)
  • Revision ID: package-import@ubuntu.com-20130621004829-y2ui02eg0j47h94y
Tags: 4:4.10.80-0ubuntu1
[ Rohan Garg ]
* New upstream release
  - Update and sort install files
  - Drop kubuntu_pate_find_python.diff, kubuntu_kate_initial_preference.patch,
    kubuntu_find_python.diff from debian/patches , not required

[ Jonathan Riddell ]
* New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  This file is part of the Kate project.
2
 
 *
3
 
 *  Copyright (C) 2012 Christoph Cullmann <cullmann@kde.org>
4
 
 *  Copyright (C) 2003 Anders Lund <anders.lund@lund.tdcadsl.dk>
5
 
 *
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.
10
 
 *
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.
15
 
 *
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.
20
 
 */
21
 
 
22
 
#include "kateprojectcompletion.h"
23
 
#include "kateprojectplugin.h"
24
 
 
25
 
#include <klocale.h>
26
 
#include <kicon.h>
27
 
 
28
 
KateProjectCompletion::KateProjectCompletion (KateProjectPlugin *plugin)
29
 
  : KTextEditor::CodeCompletionModel (0)
30
 
  , m_plugin (plugin)
31
 
{
32
 
}
33
 
 
34
 
KateProjectCompletion::~KateProjectCompletion ()
35
 
{
36
 
}
37
 
 
38
 
void KateProjectCompletion::saveMatches( KTextEditor::View* view, const KTextEditor::Range& range)
39
 
{
40
 
  m_matches.clear ();
41
 
  allMatches( m_matches, view, range );
42
 
}
43
 
 
44
 
QVariant KateProjectCompletion::data(const QModelIndex& index, int role) const
45
 
{
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
48
 
 
49
 
  if( !index.parent().isValid() ) {
50
 
    //It is the group header
51
 
    switch ( role )
52
 
    {
53
 
      case Qt::DisplayRole:
54
 
        return i18n("Project Completion");
55
 
      case GroupRole:
56
 
        return Qt::DisplayRole;
57
 
    }
58
 
  }
59
 
 
60
 
  if( index.column() == KTextEditor::CodeCompletionModel::Name && role == Qt::DisplayRole )
61
 
    return m_matches.item ( index.row() )->data (Qt::DisplayRole);
62
 
 
63
 
  if( index.column() == KTextEditor::CodeCompletionModel::Icon && role == Qt::DecorationRole ) {
64
 
    static QIcon icon(KIcon("insert-text").pixmap(QSize(16, 16)));
65
 
    return icon;
66
 
  }
67
 
 
68
 
  return QVariant();
69
 
}
70
 
 
71
 
QModelIndex KateProjectCompletion::parent(const QModelIndex& index) const
72
 
{
73
 
  if(index.internalId())
74
 
    return createIndex(0, 0, 0);
75
 
  else
76
 
    return QModelIndex();
77
 
}
78
 
 
79
 
QModelIndex KateProjectCompletion::index(int row, int column, const QModelIndex& parent) const
80
 
{
81
 
  if( !parent.isValid()) {
82
 
    if(row == 0)
83
 
      return createIndex(row, column, 0);
84
 
    else
85
 
      return QModelIndex();
86
 
 
87
 
  }else if(parent.parent().isValid())
88
 
    return QModelIndex();
89
 
 
90
 
 
91
 
  if (row < 0 || row >= m_matches.rowCount() || column < 0 || column >= ColumnCount )
92
 
    return QModelIndex();
93
 
 
94
 
  return createIndex(row, column, 1);
95
 
}
96
 
 
97
 
int KateProjectCompletion::rowCount ( const QModelIndex & parent ) const
98
 
{
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
103
 
  else
104
 
    return m_matches.rowCount();
105
 
}
106
 
 
107
 
 
108
 
bool KateProjectCompletion::shouldStartCompletion(KTextEditor::View* view, const QString &insertedText, bool userInsertion, const KTextEditor::Cursor &position)
109
 
{
110
 
    if (!userInsertion) return false;
111
 
    if(insertedText.isEmpty())
112
 
        return false;
113
 
 
114
 
    QString text = view->document()->line(position.line()).left(position.column());
115
 
    
116
 
    uint check=3;//v->config()->wordCompletionMinimalWordLength();
117
 
    
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--) {
123
 
      QChar c=text.at(i);
124
 
      if (! (c.isLetter() || (c.isNumber()) || c=='_') ) return false;
125
 
    }
126
 
 
127
 
    return true;
128
 
}
129
 
 
130
 
bool KateProjectCompletion::shouldAbortCompletion(KTextEditor::View* view, const KTextEditor::Range &range, const QString &currentCompletion) {
131
 
 
132
 
    if (m_automatic) {
133
 
      if (currentCompletion.length() < 3 /*v->config()->wordCompletionMinimalWordLength()*/) return true;
134
 
    }
135
 
 
136
 
    return CodeCompletionModelControllerInterface3::shouldAbortCompletion(view,range,currentCompletion);
137
 
}
138
 
 
139
 
void KateProjectCompletion::completionInvoked(KTextEditor::View* view, const KTextEditor::Range& range, InvocationType it)
140
 
{
141
 
  /**
142
 
   * auto invoke...
143
 
   */
144
 
  m_automatic=false;
145
 
  if (it==AutomaticInvocation) {
146
 
      m_automatic=true;
147
 
 
148
 
      if (range.columnWidth() >= 3 /*v->config()->wordCompletionMinimalWordLength()*/)
149
 
        saveMatches( view, range );
150
 
      else
151
 
        m_matches.clear();
152
 
 
153
 
      // done here...
154
 
      return;
155
 
  }
156
 
 
157
 
  // normal case ;)
158
 
  saveMatches( view, range );
159
 
}
160
 
 
161
 
 
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
165
 
{
166
 
  /**
167
 
   * get project for this document, else fail
168
 
   */
169
 
  KateProject *project = m_plugin->projectForDocument (view->document());
170
 
  if (!project)
171
 
    return;
172
 
  
173
 
  /**
174
 
   * let project index fill the completion for this document
175
 
   */
176
 
  if (project->projectIndex())
177
 
    project->projectIndex()->findMatches (model, view->document()->text(range), KateProjectIndex::CompletionMatches);
178
 
}
179
 
 
180
 
KTextEditor::CodeCompletionModelControllerInterface3::MatchReaction KateProjectCompletion::matchingItem(const QModelIndex& /*matched*/)
181
 
{
182
 
  return HideListIfAutomaticInvocation;
183
 
}
184
 
 
185
 
// Return the range containing the word left of the cursor
186
 
KTextEditor::Range KateProjectCompletion::completionRange(KTextEditor::View* view, const KTextEditor::Cursor &position)
187
 
{
188
 
  int line = position.line();
189
 
  int col = position.column();
190
 
 
191
 
  KTextEditor::Document *doc = view->document();
192
 
  while ( col > 0 )
193
 
  {
194
 
    QChar c = ( doc->character( KTextEditor::Cursor( line, col-1 ) ) );
195
 
    if ( c.isLetterOrNumber() || c.isMark() || c == '_' )
196
 
    {
197
 
      col--;
198
 
      continue;
199
 
    }
200
 
 
201
 
    break;
202
 
  }
203
 
 
204
 
  return KTextEditor::Range( KTextEditor::Cursor( line, col ), position );
205
 
}
206
 
 
207
 
// kate: space-indent on; indent-width 2; replace-tabs on;