• 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
  • completion
katekeywordcompletion.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright (C) 2014 Sven Brauch <svenbrauch@gmail.com>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License version 2, or any later version,
7  as published by the Free Software Foundation.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "katekeywordcompletion.h"
21 
22 #include "katehighlight.h"
23 #include "katehighlighthelpers.h"
24 #include "katedocument.h"
25 #include "katetextline.h"
26 
27 #include <KLocalizedString>
28 
29 KateKeywordCompletionModel::KateKeywordCompletionModel(QObject* parent)
30  : CodeCompletionModel2(parent)
31 {
32  setHasGroups(false);
33 }
34 
35 void KateKeywordCompletionModel::completionInvoked(KTextEditor::View* view, const KTextEditor::Range& range,
36  KTextEditor::CodeCompletionModel::InvocationType /*invocationType*/)
37 {
38  KateDocument* doc = static_cast<KateDocument*>(view->document());
39  if ( ! doc->highlight() || doc->highlight()->noHighlighting() ) {
40  // no highlighting -- nothing to do
41  return;
42  }
43 
44  Kate::TextLine line = doc->kateTextLine(range.end().line());
45  Kate::TextLine previousLine = doc->kateTextLine(range.end().line() - 1);
46  Kate::TextLine nextLine = doc->kateTextLine(range.end().line() + 1);
47  bool contextChanged;
48  QVector<KateHighlighting::ContextChange> contextChanges;
49  // Ask the highlighting engine to re-calcualte the highlighting for the line
50  // where completion was invoked, and store all the context changes in the process.
51  doc->highlight()->doHighlight(previousLine.data(), line.data(), nextLine.data(),
52  contextChanged, 0, &contextChanges);
53 
54  // From the list of context changes, find the highlighting context which is
55  // active at the position where completion was invoked.
56  KateHlContext* context = 0;
57  foreach ( const KateHighlighting::ContextChange& change, contextChanges ) {
58  if ( change.pos == 0 || change.pos <= range.end().column() ) {
59  context = change.toContext;
60  }
61  if ( change.pos > range.end().column() ) {
62  break;
63  }
64  }
65 
66  // Find all keyword items which exist for that context,
67  // and suggest them as completion items.
68  QSet<QString> items;
69  if ( ! context ) {
70  // default context
71  context = doc->highlight()->contextNum(0);
72  }
73  foreach ( const KateHlItem* item, context->items ) {
74  if ( const KateHlKeyword* kw = dynamic_cast<const KateHlKeyword*>(item) ) {
75  items.unite(kw->allKeywords());
76  }
77  }
78  m_items = items.toList();
79  qSort(m_items);
80 }
81 
82 QModelIndex KateKeywordCompletionModel::parent(const QModelIndex& index) const
83 {
84  if ( index.internalId() )
85  return createIndex(0, 0, 0);
86  else
87  return QModelIndex();
88 }
89 
90 QModelIndex KateKeywordCompletionModel::index(int row, int column, const QModelIndex& parent) const
91 {
92  if ( !parent.isValid() ) {
93  if ( row == 0 )
94  return createIndex(row, column, 0);
95  else
96  return QModelIndex();
97  } else if ( parent.parent().isValid() ) {
98  return QModelIndex();
99  }
100 
101  if ( row < 0 || row >= m_items.count() || column < 0 || column >= ColumnCount ) {
102  return QModelIndex();
103  }
104 
105  return createIndex(row, column, 1);
106 }
107 
108 int KateKeywordCompletionModel::rowCount(const QModelIndex& parent) const
109 {
110  if( !parent.isValid() && !m_items.isEmpty() )
111  return 1; //One root node to define the custom group
112  else if(parent.parent().isValid())
113  return 0; //Completion-items have no children
114  else
115  return m_items.count();
116 }
117 
118 static bool isInWord(const KTextEditor::View* view, const KTextEditor::Cursor& position, QChar c)
119 {
120  KateDocument* document = static_cast<KateDocument*>(view->document());
121  KateHighlighting* highlight = document->highlight();
122  Kate::TextLine line = document->kateTextLine(position.line());
123  return highlight->isInWord(c, line->attribute(position.column()-1));
124 };
125 
126 KTextEditor::Range KateKeywordCompletionModel::completionRange(KTextEditor::View* view,
127  const KTextEditor::Cursor& position)
128 {
129  const QString& text = view->document()->text(KTextEditor::Range(position, KTextEditor::Cursor(position.line(), 0)));
130  int pos;
131  for ( pos = text.size() - 1; pos >= 0; pos-- ) {
132  if ( isInWord(view, position, text.at(pos)) ) {
133  // This needs to be aware of what characters are word-characters in the
134  // active language, so that languages which prefix commands with e.g. @
135  // or \ have properly working completion.
136  continue;
137  }
138  break;
139  }
140  return KTextEditor::Range(KTextEditor::Cursor(position.line(), pos + 1), position);
141 }
142 
143 bool KateKeywordCompletionModel::shouldAbortCompletion(KTextEditor::View* view, const KTextEditor::Range& range,
144  const QString& currentCompletion)
145 {
146  if ( view->cursorPosition() < range.start() || view->cursorPosition() > range.end() )
147  return true; // Always abort when the completion-range has been left
148  // Do not abort completions when the text has been empty already before and a newline has been entered
149 
150  foreach ( QChar c, currentCompletion ) {
151  if ( ! isInWord(view, range.start(), c) ) {
152  return true;
153  }
154  }
155  return false;
156 }
157 
158 bool KateKeywordCompletionModel::shouldStartCompletion(KTextEditor::View* /*view*/, const QString& insertedText,
159  bool userInsertion, const KTextEditor::Cursor& /*position*/)
160 {
161  if ( userInsertion && insertedText.size() > 3 && ! insertedText.contains(' ')
162  && insertedText.at(insertedText.size()-1).isLetter() ) {
163  return true;
164  }
165  return false;
166 }
167 
168 bool KateKeywordCompletionModel::shouldHideItemsWithEqualNames() const
169 {
170  return true;
171 }
172 
173 QVariant KateKeywordCompletionModel::data(const QModelIndex& index, int role) const
174 {
175  if ( role == UnimportantItemRole )
176  return QVariant(true);
177  if ( role == InheritanceDepth )
178  return 9000;
179 
180  if ( !index.parent().isValid() ) {
181  // group header
182  switch ( role ) {
183  case Qt::DisplayRole:
184  return i18n("Language keywords");
185  case GroupRole:
186  return Qt::DisplayRole;
187  }
188  }
189 
190  if ( index.column() == KTextEditor::CodeCompletionModel::Name && role == Qt::DisplayRole )
191  return m_items.at(index.row());
192 
193  if ( index.column() == KTextEditor::CodeCompletionModel::Icon && role == Qt::DecorationRole ) {
194  static const QIcon icon(KIcon("code-variable").pixmap(QSize(16, 16)));
195  return icon;
196  }
197 
198  return QVariant();
199 }
200 
201 KTextEditor::CodeCompletionModelControllerInterface3::MatchReaction KateKeywordCompletionModel::matchingItem(
202  const QModelIndex& /*matched*/)
203 {
204  return KTextEditor::CodeCompletionModelControllerInterface3::None;
205 }
206 
207 #include "katekeywordcompletion.moc"
208 
209 // kate: indent-width 4; replace-tabs on
QModelIndex
katehighlight.h
katetextline.h
KateKeywordCompletionModel::shouldHideItemsWithEqualNames
virtual bool shouldHideItemsWithEqualNames() const
When multiple completion models are used at the same time, it may happen that multiple models add ite...
Definition: katekeywordcompletion.cpp:168
Kate::Script::i18n
QScriptValue i18n(QScriptContext *context, QScriptEngine *engine)
i18n("text", arguments [optional])
Definition: katescripthelpers.cpp:186
KateDocument::highlight
KateHighlighting * highlight() const
Definition: katedocument.cpp:4701
QChar
KateHlItem
Definition: katehighlighthelpers.h:28
KateKeywordCompletionModel::completionInvoked
virtual void completionInvoked(KTextEditor::View *view, const KTextEditor::Range &range, InvocationType invocationType)
Definition: katekeywordcompletion.cpp:35
katekeywordcompletion.h
QList::at
const T & at(int i) const
QString::size
int size() const
katedocument.h
QModelIndex::internalId
qint64 internalId() const
KateHighlighting::ContextChange
Definition: katehighlight.h:137
QSharedPointer::data
T * data() const
KateKeywordCompletionModel::completionRange
virtual KTextEditor::Range completionRange(KTextEditor::View *view, const KTextEditor::Cursor &position)
Definition: katekeywordcompletion.cpp:126
KateHlContext::items
QVector< KateHlItem * > items
Definition: katehighlighthelpers.h:82
KateHlContext
Definition: katehighlighthelpers.h:72
KateHighlighting::noHighlighting
bool noHighlighting() const
Definition: katehighlight.h:255
QModelIndex::isValid
bool isValid() const
QList::count
int count(const T &value) const
QSharedPointer
KateKeywordCompletionModel::matchingItem
virtual MatchReaction matchingItem(const QModelIndex &matched)
Definition: katekeywordcompletion.cpp:201
QObject
KateHighlighting::ContextChange::pos
int pos
Definition: katehighlight.h:139
KateKeywordCompletionModel::rowCount
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
Definition: katekeywordcompletion.cpp:108
QList::isEmpty
bool isEmpty() const
QModelIndex::row
int row() const
KateKeywordCompletionModel::KateKeywordCompletionModel
KateKeywordCompletionModel(QObject *parent)
Definition: katekeywordcompletion.cpp:29
QSet< QString >
QString
QModelIndex::parent
QModelIndex parent() const
isInWord
static bool isInWord(const KTextEditor::View *view, const KTextEditor::Cursor &position, QChar c)
Definition: katekeywordcompletion.cpp:118
KateDocument
Definition: katedocument.h:74
QString::contains
bool contains(QChar ch, Qt::CaseSensitivity cs) const
QSize
KateDocument::kateTextLine
Kate::TextLine kateTextLine(uint i)
Definition: katedocument.cpp:4706
KateKeywordCompletionModel::parent
virtual QModelIndex parent(const QModelIndex &index) const
Definition: katekeywordcompletion.cpp:82
KateHlKeyword
Definition: katehighlighthelpers.h:184
KateKeywordCompletionModel::shouldAbortCompletion
virtual bool shouldAbortCompletion(KTextEditor::View *view, const KTextEditor::Range &range, const QString &currentCompletion)
Definition: katekeywordcompletion.cpp:143
QVector
QSet::unite
QSet< T > & unite(const QSet< T > &other)
KateHighlighting::ContextChange::toContext
KateHlContext * toContext
Definition: katehighlight.h:138
KateHighlighting
Definition: katehighlight.h:119
QString::at
const QChar at(int position) const
KateKeywordCompletionModel::index
virtual QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
Definition: katekeywordcompletion.cpp:90
QModelIndex::column
int column() const
None
Definition: kateviinsertmode.h:39
QSet::toList
QList< T > toList() const
KateKeywordCompletionModel::data
virtual QVariant data(const QModelIndex &index, int role) const
Definition: katekeywordcompletion.cpp:173
KateHighlighting::isInWord
bool isInWord(QChar c, int attrib=0) const
Definition: katehighlight.cpp:1122
katehighlighthelpers.h
KateHighlighting::doHighlight
void doHighlight(const Kate::TextLineData *prevLine, Kate::TextLineData *textLine, const Kate::TextLineData *nextLine, bool &ctxChanged, int tabWidth=0, QVector< ContextChange > *contextChanges=0)
Parse the text and fill in the context array and folding list array.
Definition: katehighlight.cpp:279
KateHighlighting::contextNum
KateHlContext * contextNum(int n)
Definition: katehighlight.h:275
QIcon
KateKeywordCompletionModel::shouldStartCompletion
virtual bool shouldStartCompletion(KTextEditor::View *view, const QString &insertedText, bool userInsertion, const KTextEditor::Cursor &position)
Definition: katekeywordcompletion.cpp:158
QVariant
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 9 2020 03:56:58 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