KTextEditor

codecompletionmodelcontrollerinterface.cpp
1/*
2 SPDX-FileCopyrightText: 2008 Niko Sams <niko.sams@gmail.com>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "codecompletionmodelcontrollerinterface.h"
8
9#include <QModelIndex>
10#include <QRegularExpression>
11
12#include <kateconfig.h>
13#include <ktexteditor/document.h>
14#include <ktexteditor/view.h>
15
16namespace KTextEditor
17{
18CodeCompletionModelControllerInterface::CodeCompletionModelControllerInterface()
19{
20}
21
22CodeCompletionModelControllerInterface::~CodeCompletionModelControllerInterface() = default;
23
24bool CodeCompletionModelControllerInterface::shouldStartCompletion(View *view, const QString &insertedText, bool userInsertion, const Cursor &position)
25{
26 Q_UNUSED(view);
27 Q_UNUSED(position);
28 if (insertedText.isEmpty()) {
29 return false;
30 }
31
32 QChar lastChar = insertedText.at(insertedText.size() - 1);
33 if ((userInsertion && (lastChar.isLetter() || lastChar.isNumber() || lastChar == QLatin1Char('_'))) || lastChar == QLatin1Char('.')
34 || insertedText.endsWith(QLatin1String("->"))) {
35 return true;
36 }
37 return false;
38}
39
41{
42 Cursor end = position;
43 const int line = end.line();
44
45 const QString lineText = view->document()->line(line);
46 QStringView text = lineText;
47
49 static const QRegularExpression findWordStart(QStringLiteral("\\b[_\\w]+$"), options);
50 static const QRegularExpression findWordEnd(QStringLiteral("^[_\\w]*\\b"), options);
51
52 Cursor start = end;
53
54 int pos = text.left(end.column()).lastIndexOf(findWordStart);
55 if (pos >= 0) {
56 start.setColumn(pos);
57 }
58
59 if (!KateViewConfig::global()->wordCompletionRemoveTail()) {
60 // We are not removing tail, range only contains the word left of the cursor
61 return Range(start, position);
62 } else {
63 // Removing tail, find the word end
65 pos = text.mid(end.column()).indexOf(findWordEnd, 0, &match);
66 if (pos >= 0) {
67 end.setColumn(end.column() + match.capturedLength());
68 }
69
70 return Range(start, end);
71 }
72}
73
75{
76 QStringList text = view->document()->textLines(range, false);
77 if (!text.isEmpty() && text.count() == 1 && text.first().trimmed().isEmpty())
78 // When inserting a newline behind an empty completion-range,, move the range forward to its end
79 {
80 return Range(range.end(), range.end());
81 }
82
83 return range;
84}
85
87{
88 return view->document()->text(KTextEditor::Range(range.start(), position));
89}
90
91bool CodeCompletionModelControllerInterface::shouldAbortCompletion(View *view, const Range &range, const QString &currentCompletion)
92{
93 if (view->cursorPosition() < range.start() || view->cursorPosition() > range.end()) {
94 return true; // Always abort when the completion-range has been left
95 }
96 // Do not abort completions when the text has been empty already before and a newline has been entered
97
98 static const QRegularExpression allowedText(QStringLiteral("^\\w*$"), QRegularExpression::UseUnicodePropertiesOption);
99 return !allowedText.match(currentCompletion).hasMatch();
100}
101
106
108{
109 Q_UNUSED(index);
110 Q_UNUSED(inserted);
111 return false;
112}
113
115{
116 Q_UNUSED(selected)
117 return HideListIfAutomaticInvocation;
118}
119
124
125}
virtual Range completionRange(View *view, const Cursor &position)
This function returns the completion range that will be used for the current completion.
virtual void aborted(View *view)
Notification that completion for this model has been aborted.
virtual MatchReaction matchingItem(const QModelIndex &matched)
Called whenever an item in the completion-list perfectly matches the current filter text.
virtual QString filterString(View *view, const Range &range, const Cursor &position)
This function returns the filter-text used for the current completion.
virtual Range updateCompletionRange(View *view, const Range &range)
This function lets the CompletionModel dynamically modify the range.
virtual bool shouldHideItemsWithEqualNames() const
When multiple completion models are used at the same time, it may happen that multiple models add ite...
virtual bool shouldAbortCompletion(View *view, const Range &range, const QString &currentCompletion)
This function decides if the completion should be aborted.
virtual bool shouldExecute(const QModelIndex &selected, QChar inserted)
When an item within this model is currently selected in the completion-list, and the user inserted th...
virtual bool shouldStartCompletion(View *view, const QString &insertedText, bool userInsertion, const Cursor &position)
This function decides if the automatic completion should be started when the user entered some text.
The Cursor represents a position in a Document.
Definition cursor.h:75
virtual QString line(int line) const =0
Get a single text line.
virtual QString text() const =0
Get the document content.
virtual QStringList textLines(Range range, bool block=false) const =0
Get the document content within the given range.
An object representing a section of text, from one Cursor to another.
constexpr Cursor end() const noexcept
Get the end position of this range.
constexpr Cursor start() const noexcept
Get the start position of this range.
A text widget with KXMLGUIClient that represents a Document.
Definition view.h:244
virtual Document * document() const =0
Get the view's document, that means the view is a view of the returned document.
virtual Cursor cursorPosition() const =0
Get the view's current cursor position.
Q_SCRIPTABLE Q_NOREPLY void start()
The KTextEditor namespace contains all the public API that is required to use the KTextEditor compone...
bool isLetter() const const
bool isNumber() const const
int count(const T &value) const const
T & first()
bool isEmpty() const const
QRegularExpressionMatch match(const QString &subject, int offset, QRegularExpression::MatchType matchType, QRegularExpression::MatchOptions matchOptions) const const
bool hasMatch() const const
const QChar at(int position) const const
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
int size() const const
qsizetype indexOf(QChar c, qsizetype from, Qt::CaseSensitivity cs) const const
qsizetype lastIndexOf(QChar c, qsizetype from, Qt::CaseSensitivity cs) const const
QStringView left(qsizetype length) const const
QStringView mid(qsizetype start) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sun Feb 25 2024 18:42:03 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.