KTextEditor

codecompletionmodelcontrollerinterface.cpp
1 /*
2  SPDX-FileCopyrightText: 2008 Niko Sams <[email protected]>
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 
16 namespace KTextEditor
17 {
18 CodeCompletionModelControllerInterface::CodeCompletionModelControllerInterface()
19 {
20 }
21 
22 CodeCompletionModelControllerInterface::~CodeCompletionModelControllerInterface() = default;
23 
24 bool 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.count() - 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  // TODO KF6 make this a QStringView
46  const QString text = view->document()->line(line);
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 
91 bool 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 
103 {
104  Q_UNUSED(view);
105 }
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 
121 {
122  return false;
123 }
124 
125 }
virtual Document * document() const =0
Get the view's document, that means the view is a view of the returned document.
T & first()
virtual bool shouldHideItemsWithEqualNames() const
When multiple completion models are used at the same time, it may happen that multiple models add ite...
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.
virtual void aborted(View *view)
Notification that completion for this model has been aborted.
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const const
virtual QStringList textLines(const Range &range, bool block=false) const =0
Get the document content within the given range.
virtual QString filterString(View *view, const Range &range, const Cursor &position)
This function returns the filter-text used for the current completion.
int count(const T &value) const const
Q_SCRIPTABLE Q_NOREPLY void start()
An object representing a section of text, from one Cursor to another.
virtual Range updateCompletionRange(View *view, const Range &range)
This function lets the CompletionModel dynamically modify the range.
bool isLetter() const const
int lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const const
constexpr Cursor end() const Q_DECL_NOEXCEPT
Get the end position of this range.
virtual MatchReaction matchingItem(const QModelIndex &matched)
Called whenever an item in the completion-list perfectly matches the current filter text.
The Cursor represents a position in a Document.
Definition: cursor.h:71
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...
bool isEmpty() const const
virtual QString line(int line) const =0
Get a single text line.
A text widget with KXMLGUIClient that represents a Document.
Definition: view.h:146
bool isEmpty() const const
bool hasMatch() const const
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const const
QRegularExpressionMatch match(const QString &subject, int offset, QRegularExpression::MatchType matchType, QRegularExpression::MatchOptions matchOptions) const const
The KTextEditor namespace contains all the public API that is required to use the KTextEditor compone...
Definition: katetextblock.h:22
virtual bool shouldAbortCompletion(View *view, const Range &range, const QString &currentCompletion)
This function decides if the completion should be aborted.
int count() const const
constexpr Cursor start() const Q_DECL_NOEXCEPT
Get the start position of this range.
QString left(int n) const const
virtual Range completionRange(View *view, const Cursor &position)
This function returns the completion range that will be used for the current completion.
virtual QString text() const =0
Get the document content.
bool isNumber() const const
const QChar at(int position) const const
virtual Cursor cursorPosition() const =0
Get the view's current cursor position.
QString mid(int position, int n) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon May 8 2023 03:50:21 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.