• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • applications API Reference
  • KDE Home
  • Contact Us
 

Kate

  • kde-4.14
  • applications
  • kate
  • part
  • snippet
snippetcompletionmodel.cpp
Go to the documentation of this file.
1 /* This file is part of the Kate project.
2  * Based on the snippet plugin from KDevelop 4.
3  *
4  * Copyright (C) 2008 Andreas Pakulat <apaku@gmx.de>
5  * Copyright (C) 2012 Christoph Cullmann <cullmann@kde.org>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public License
18  * along with this library; see the file COPYING.LIB. If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 #include "snippetcompletionmodel.h"
24 
25 #include <KTextEditor/Document>
26 #include <KTextEditor/View>
27 
28 #include <KTextEditor/HighlightInterface>
29 
30 #include "snippetstore.h"
31 #include "snippetrepository.h"
32 #include "snippet.h"
33 #include "snippetcompletionitem.h"
34 
35 #include <KLocalizedString>
36 
37 SnippetCompletionModel::SnippetCompletionModel()
38  : KTextEditor::CodeCompletionModel2(0)
39 {
40  setHasGroups(false);
41 }
42 
43 SnippetCompletionModel::~SnippetCompletionModel()
44 {
45  qDeleteAll(m_snippets);
46  m_snippets.clear();
47 }
48 
49 QVariant SnippetCompletionModel::data( const QModelIndex& idx, int role ) const
50 {
51  if (role == KTextEditor::CodeCompletionModel::InheritanceDepth)
52  return 11000;
53 
54  // grouping of snippets
55  if (!idx.parent().isValid()) {
56  if (role == Qt::DisplayRole) {
57  return i18n("Snippets");
58  }
59  if (role == KTextEditor::CodeCompletionModel::GroupRole) {
60  return Qt::DisplayRole;
61  }
62  return QVariant();
63  }
64  // snippets
65  if( !idx.isValid() || idx.row() < 0 || idx.row() >= m_snippets.count() ) {
66  return QVariant();
67  } else {
68  return m_snippets.at( idx.row() )->data(idx, role, 0);
69  }
70 }
71 
72 void SnippetCompletionModel::executeCompletionItem2(KTextEditor::Document* /*document*/,
73  const KTextEditor::Range& word,
74  const QModelIndex& index) const
75 {
76  if ( index.parent().isValid() && m_viewToUse ) {
77  m_snippets[index.row()]->execute(m_viewToUse, word);
78  }
79 }
80 
81 void SnippetCompletionModel::completionInvoked(KTextEditor::View *view, const KTextEditor::Range &range, InvocationType invocationType)
82 {
83  Q_UNUSED( range );
84  Q_UNUSED( invocationType );
85  initData(view);
86 }
87 
88 void SnippetCompletionModel::initData(KTextEditor::View* view)
89 {
90  m_viewToUse = view;
91 
92  QString mode;
93  if ( KTextEditor::HighlightInterface* iface = qobject_cast<KTextEditor::HighlightInterface*>(view->document()) ) {
94  mode = iface->highlightingModeAt(view->cursorPosition());
95  }
96 
97  if ( mode.isEmpty() ) {
98  mode = view->document()->highlightingMode();
99  }
100 
101  qDeleteAll(m_snippets);
102  m_snippets.clear();
103  SnippetStore* store = SnippetStore::self();
104  for(int i = 0; i < store->rowCount(); i++ )
105  {
106  if ( store->item(i, 0)->checkState() != Qt::Checked ) {
107  continue;
108  }
109  SnippetRepository* repo = dynamic_cast<SnippetRepository*>( store->item( i, 0 ) );
110  if( repo && (repo->fileTypes().isEmpty() || repo->fileTypes().contains(mode)) )
111  {
112  for ( int j = 0; j < repo->rowCount(); ++j ) {
113  if ( Snippet* snippet = dynamic_cast<Snippet*>(repo->child(j)) ) {
114  m_snippets << new SnippetCompletionItem(snippet, repo);
115  }
116  }
117  }
118  }
119  reset();
120 }
121 
122 QModelIndex SnippetCompletionModel::parent(const QModelIndex& index) const {
123  if (index.internalId()) {
124  return createIndex(0, 0, 0);
125  } else {
126  return QModelIndex();
127  }
128 }
129 
130 QModelIndex SnippetCompletionModel::index(int row, int column, const QModelIndex& parent) const {
131  if (!parent.isValid()) {
132  if (row == 0) {
133  return createIndex(row, column, 0); //header index
134  } else {
135  return QModelIndex();
136  }
137  } else if (parent.parent().isValid()) { //we only have header and children, no subheaders
138  return QModelIndex();
139  }
140 
141  if (row < 0 || row >= m_snippets.count() || column < 0 || column >= ColumnCount ) {
142  return QModelIndex();
143  }
144 
145  return createIndex(row, column, 1); // normal item index
146 }
147 
148 int SnippetCompletionModel::rowCount (const QModelIndex & parent) const {
149  if (!parent.isValid() && !m_snippets.isEmpty()) {
150  return 1; //one toplevel node (group header)
151  } else if(parent.parent().isValid()) {
152  return 0; //we don't have sub children
153  } else {
154  return m_snippets.count(); // only the children
155  }
156 }
157 KTextEditor::Range SnippetCompletionModel::completionRange(KTextEditor::View* view, const KTextEditor::Cursor& position)
158 {
159  const QString& line = view->document()->line(position.line());
160  KTextEditor::Range range(position, position);
161  // include everything non-space before
162  for ( int i = position.column() - 1; i >= 0; --i ) {
163  if ( line.at(i).isSpace() ) {
164  break;
165  } else {
166  range.start().setColumn(i);
167  }
168  }
169  // include everything non-space after
170  for ( int i = position.column() + 1; i < line.length(); ++i ) {
171  if ( line.at(i).isSpace() ) {
172  break;
173  } else {
174  range.end().setColumn(i);
175  }
176  }
177  return range;
178 }
179 
180 bool SnippetCompletionModel::shouldAbortCompletion(KTextEditor::View* view, const KTextEditor::Range& range, const QString& currentCompletion)
181 {
182  if(view->cursorPosition() < range.start() || view->cursorPosition() > range.end()) {
183  return true; //Always abort when the completion-range has been left
184  }
185 
186  for ( int i = 0; i < currentCompletion.length(); ++i ) {
187  if ( currentCompletion.at(i).isSpace() ) {
188  return true;
189  }
190  }
191  // else it's valid
192  return false;
193 }
194 
195 
196 #include "snippetcompletionmodel.moc"
QObject::child
QObject * child(const char *objName, const char *inheritsClass, bool recursiveSearch) const
QList::clear
void clear()
QModelIndex
SnippetCompletionModel::rowCount
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
Definition: snippetcompletionmodel.cpp:148
Kate::Script::i18n
QScriptValue i18n(QScriptContext *context, QScriptEngine *engine)
i18n("text", arguments [optional])
Definition: katescripthelpers.cpp:186
SnippetCompletionModel::SnippetCompletionModel
SnippetCompletionModel()
Definition: snippetcompletionmodel.cpp:37
QList::at
const T & at(int i) const
SnippetCompletionModel::index
virtual QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
Definition: snippetcompletionmodel.cpp:130
QStringList::contains
bool contains(const QString &str, Qt::CaseSensitivity cs) const
SnippetCompletionModel::executeCompletionItem2
virtual void executeCompletionItem2(KTextEditor::Document *document, const KTextEditor::Range &word, const QModelIndex &index) const
Definition: snippetcompletionmodel.cpp:72
SnippetCompletionModel::shouldAbortCompletion
virtual bool shouldAbortCompletion(KTextEditor::View *view, const KTextEditor::Range &range, const QString &currentCompletion)
Definition: snippetcompletionmodel.cpp:180
QModelIndex::internalId
qint64 internalId() const
SnippetCompletionModel::parent
virtual QModelIndex parent(const QModelIndex &index) const
Definition: snippetcompletionmodel.cpp:122
SnippetCompletionModel::data
QVariant data(const QModelIndex &idx, int role=Qt::DisplayRole) const
Definition: snippetcompletionmodel.cpp:49
QStandardItem::checkState
Qt::CheckState checkState() const
SnippetCompletionModel::completionRange
virtual KTextEditor::Range completionRange(KTextEditor::View *view, const KTextEditor::Cursor &position)
Definition: snippetcompletionmodel.cpp:157
QModelIndex::isValid
bool isValid() const
SnippetCompletionItem
Definition: snippetcompletionitem.h:41
QList::count
int count(const T &value) const
QChar::isSpace
bool isSpace() const
QList::isEmpty
bool isEmpty() const
QString::isEmpty
bool isEmpty() const
QModelIndex::row
int row() const
SnippetCompletionModel::completionInvoked
void completionInvoked(KTextEditor::View *view, const KTextEditor::Range &range, KTextEditor::CodeCompletionModel::InvocationType invocationType)
Definition: snippetcompletionmodel.cpp:81
SnippetStore
This class is implemented as singelton.
Definition: snippetstore.h:44
QString
SnippetRepository
Each object of this type represents a repository of snippets.
Definition: snippetrepository.h:52
QModelIndex::parent
QModelIndex parent() const
Snippet
One object of this class represents a single snippet.
Definition: snippet.h:42
QStandardItemModel::item
QStandardItem * item(int row, int column) const
SnippetCompletionModel::~SnippetCompletionModel
~SnippetCompletionModel()
Definition: snippetcompletionmodel.cpp:43
snippetcompletionmodel.h
SnippetStore::self
static SnippetStore * self()
Retuns the SnippetStore.
Definition: snippetstore.cpp:67
QString::at
const QChar at(int position) const
QStandardItemModel::rowCount
virtual int rowCount(const QModelIndex &parent) const
QString::length
int length() const
QStandardItem::rowCount
int rowCount() const
snippetstore.h
snippetcompletionitem.h
SnippetRepository::fileTypes
QStringList fileTypes() const
The valid filetypes for the snippets contained in this repository.
Definition: snippetrepository.cpp:103
snippetrepository.h
snippet.h
QVariant
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 9 2020 03:56:59 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Kate

Skip menu "Kate"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

applications API Reference

Skip menu "applications API Reference"
  •   kate
  •       kate
  •   KTextEditor
  •   Kate
  • Konsole

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal