KTextEditor.CodeCompletionModel

KDE 4.9 PyKDE API Reference

- KDE's Python API

# CodeCompletionModel Class Reference

`from PyKDE4.ktexteditor import *`

Inherits: QAbstractItemModel → QObject
Namespace: KTextEditor

## Detailed Description

An item model for providing code completion, and meta information for enhanced presentation.

Introduction

The CodeCompletionModel is the actual workhorse to provide code completions in a KTextEditor.View. It is meant to be used in conjunction with the CodeCompletionInterface. The CodeCompletionModel is not meant to be used as is. Rather you need to implement a subclass of CodeCompletionModel to actually generate completions appropriate for your type of Document.

Implementing a CodeCompletionModel

The CodeCompletionModel is a QAbstractItemModel, and can be subclassed in the same way. It provides default implementations of several members, however, so in most cases (if your completions are essentially a non-hierarchical, flat list of matches) you will only need to overload few virtual functions.

Implementing a CodeCompletionModel for a flat list

For the simple case of a flat list of completions, you will need to: - Implement completionInvoked() to actually generate/update the list of completion matches - implement itemData() (or QAbstractItemModel.data()) to return the information that should be displayed for each match. - use setRowCount() to reflect the number of matches.

Columns and roles

To do: document the meaning and usage of the columns and roles used by the CodeCompletionInterface

Using the new CodeCompletionModel

To start using your CodeCompletionModel, refer to CodeCompletionInterface.

ControllerInterface to get more control

To have more control over code completion implement CodeCompletionModelControllerInterface in your CodeCompletionModel.

See also:
  CodeCompletionInterface, CodeCompletionModelControllerInterface

Author:
  Hamish Rodda <rodda@kde.org>

## Enumerations
Columns | { Prefix, Icon, Scope, Name, Arguments, Postfix }
CompletionProperty | { NoProperty, FirstProperty, Public, Protected, Private, Static, Const, Namespace, Class, Struct, Union, Function, Variable, Enum, Template, TypeAlias, Virtual, Override, Inline, Friend, Signal, Slot, LocalScope, NamespaceScope, GlobalScope, LastProperty }
ExtraItemDataRoles | { CompletionRole, ScopeIndex, MatchQuality, SetMatchContext, HighlightingMethod, CustomHighlight, InheritanceDepth, IsExpandable, ExpandingWidget, ItemSelected, ArgumentHintDepth, BestMatchesCount, AccessibilityNext, AccessibilityPrevious, AccessibilityAccept, GroupRole }
HighlightMethod | { NoHighlighting, InternalHighlighting, CustomHighlighting }
InvocationType | { AutomaticInvocation, UserInvocation, ManualInvocation }
## Attributes
int | ColumnCount
int | LastItemDataRole
## Signals
| hasGroupsChanged (KTextEditor.CodeCompletionModel model, bool hasGroups)
| waitForReset ()
## Methods
| __init__ (self, QObject parent)
int | columnCount (self, QModelIndex parent=QModelIndex())
| completionInvoked (self, KTextEditor.View view, KTextEditor.Range range, KTextEditor.CodeCompletionModel.InvocationType invocationType)
| executeCompletionItem (self, KTextEditor.Document document, KTextEditor.Range word, int row)
bool | hasGroups (self)
QModelIndex | index (self, int row, int column, QModelIndex parent=QModelIndex())
{int:QVariant} | itemData (self, QModelIndex index)
QModelIndex | parent (self, QModelIndex index)
int | rowCount (self, QModelIndex parent=QModelIndex())
| setHasGroups (self, bool hasGroups)
| setRowCount (self, int rowCount)

## Signal Documentation

hasGroupsChanged | ( | KTextEditor.CodeCompletionModel | model,
|  | bool | hasGroups
| ) |  |  |

Internal

Signal syntax:
  `QObject.connect(source, SIGNAL("hasGroupsChanged(KTextEditor::CodeCompletionModel*, bool)"), target_slot)`

waitForReset | ( |  | ) |

Emit this if the code-completion for this model was invoked, some time is needed in order to get the data, and the model is reset once the data is available.

This only has an effect if emitted from within completionInvoked(..).

This prevents the code-completion list from showing until this model is reset, so there is no annoying flashing in the user-interface resulting from other models supplying their data earlier.

Note:
  The implementation may choose to show the completion-list anyway after some timeout

Warning:
  If you emit this, you _must_ also reset the model at some point, else the code-completion will be completely broken to the user. Consider that there may always be additional completion-models apart from yours.

Since:
  KDE 4.3

Signal syntax:
  `QObject.connect(source, SIGNAL("waitForReset()"), target_slot)`

## Method Documentation

__init__ | ( | self, |
|  | QObject | parent
| ) |  |  |

int columnCount | ( | self, |
|  | QModelIndex | parent=QModelIndex()
| ) |  |  |

Reimplemented from QAbstractItemModel.columnCount(). The default implementation returns ColumnCount for all indices.

completionInvoked | ( | self, |
|  | KTextEditor.View | view,
|  | KTextEditor.Range | range,
|  | KTextEditor.CodeCompletionModel.InvocationType | invocationType
| ) |  |  |

This function is responsible to generating / updating the list of current completions. The default implementation does nothing.

When implementing this function, remember to call setRowCount() (or implement rowCount()), and to generate the appropriate change notifications (for instance by calling QAbstractItemModel.reset()).

Parameters:

  | view | The view to generate completions for
  | range | The range of text to generate completions for
  | invocationType | How the code completion was triggered

executeCompletionItem | ( | self, |
|  | KTextEditor.Document | document,
|  | KTextEditor.Range | word,
|  | int | row
| ) |  |  |

Deprecated:
  This does not work if your model is hierarchical(@see GroupRole). Use CodeCompletionModel2.executeCompletionItem2 instead.

This function is responsible for inserting a selected completion into the document. The default implementation replaces the text that the completions were based on with the Qt.DisplayRole of the Name column of the given match.

Parameters:

  | document | The document to insert the completion into
  | word | The Range that the completions are based on (what the user entered so far)
  | row | The row of the completion match to insert

bool hasGroups | ( |  | self ) |

This function returns true if the model needs grouping, otherwise false in KDE 4 default value is true, in KDE 5 the default will be false TODO KDE 5

QModelIndex index | ( | self, |
|  | int | row,
|  | int | column,
|  | QModelIndex | parent=QModelIndex()
| ) |  |  |

Reimplemented from QAbstractItemModel.index(). The default implementation returns a standard QModelIndex as long as the row and column are valid.

{int:QVariant} itemData | ( | self, |
|  | QModelIndex | index
| ) |  |  |

Reimplemented from QAbstractItemModel.itemData(). The default implementation returns a map with the QAbstractItemModel.data() for all roles that are used by the CodeCompletionInterface. You will need to reimplement either this function or QAbstractItemModel.data() in your CodeCompletionModel.

QModelIndex parent | ( | self, |
|  | QModelIndex | index
| ) |  |  |

Reimplemented from QAbstractItemModel.parent(). The default implementation returns an invalid QModelIndex for all items. This is appropriate for non-hierarchical / flat lists of completions.

int rowCount | ( | self, |
|  | QModelIndex | parent=QModelIndex()
| ) |  |  |

Reimplemented from QAbstractItemModel.rowCount(). The default implementation returns the value set by setRowCount() for invalid (toplevel) indices, and 0 for all other indices. This is appropriate for non-hierarchical / flat lists of completions

setHasGroups | ( | self, |
|  | bool | hasGroups
| ) |  |  |

setRowCount | ( | self, |
|  | int | rowCount
| ) |  |  |

## Attribute Documentation

int ColumnCount

int LastItemDataRole

## Enumeration Documentation

Columns

Enumerator:

  Prefix = 0 |
  Icon |
  Scope |
  Name |
  Arguments |
  Postfix |

CompletionProperty

Enumerator:

  NoProperty = 0x0 |
  FirstProperty = 0x1 |
  Public = 0x1 |
  Protected = 0x2 |
  Private = 0x4 |
  Static = 0x8 |
  Const = 0x10 |
  Namespace = 0x20 |
  Class = 0x40 |
  Struct = 0x80 |
  Union = 0x100 |
  Function = 0x200 |
  Variable = 0x400 |
  Enum = 0x800 |
  Template = 0x1000 |
  TypeAlias = 0x2000 |
  Virtual = 0x4000 |
  Override = 0x8000 |
  Inline = 0x10000 |
  Friend = 0x20000 |
  Signal = 0x40000 |
  Slot = 0x80000 |
  LocalScope = 0x100000 |
  NamespaceScope = 0x200000 |
  GlobalScope = 0x400000 |
  LastProperty = GlobalScope |

ExtraItemDataRoles

Enumerator:

  CompletionRole = Qt::UserRole |
  ScopeIndex |
  MatchQuality |
  SetMatchContext |
  HighlightingMethod |
  CustomHighlight |
  InheritanceDepth |
  IsExpandable |
  ExpandingWidget |
  ItemSelected |
  ArgumentHintDepth |
  BestMatchesCount |
  AccessibilityNext |
  AccessibilityPrevious |
  AccessibilityAccept |
  GroupRole |

HighlightMethod

Enumerator:

  NoHighlighting = 0x0 |
  InternalHighlighting = 0x1 |
  CustomHighlighting = 0x2 |

InvocationType

Enumerator:

  AutomaticInvocation |
  UserInvocation |
  ManualInvocation |