• 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
snippetcompletionitem.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) 2009 Milian Wolff <mail@milianw.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 "snippetcompletionitem.h"
24 
25 #include <KTextEditor/Document>
26 #include <KLocalizedString>
27 #include <QtGui/QTextEdit>
28 #include <QtCore/QModelIndex>
29 
30 #include <ktexteditor/templateinterface.h>
31 #include <ktexteditor/templateinterface2.h>
32 #include <ktexteditor/view.h>
33 #include <ktexteditor/codecompletioninterface.h>
34 #include <ktexteditor/codecompletionmodel.h>
35 
36 #include "snippet.h"
37 #include "snippetrepository.h"
38 
39 
40 SnippetCompletionItem::SnippetCompletionItem( Snippet* snippet, SnippetRepository* repo )
41  : m_name(snippet->text()), m_snippet(snippet->snippet()), m_prefix(snippet->prefix()),
42  m_arguments(snippet->arguments()), m_postfix(snippet->postfix()), m_repo(repo)
43 {
44  Q_ASSERT(m_repo);
45  m_name.prepend( repo->completionNamespace() );
46 }
47 
48 SnippetCompletionItem::~SnippetCompletionItem()
49 {
50 }
51 
52 QVariant SnippetCompletionItem::data( const QModelIndex& index, int role, const KTextEditor::CodeCompletionModel2* model ) const
53 {
54  // as long as the snippet completion model is not a kdevelop code completion model,
55  // model will usually be 0. hence don't use it.
56  Q_UNUSED(model);
57 
58  switch ( role ) {
59  case Qt::DisplayRole:
60  switch ( index.column() ) {
61  case KTextEditor::CodeCompletionModel::Name:
62  return m_name;
63  case KTextEditor::CodeCompletionModel::Prefix:
64  return m_prefix;
65  case KTextEditor::CodeCompletionModel::Postfix:
66  return m_postfix;
67  case KTextEditor::CodeCompletionModel::Arguments:
68  return m_arguments;
69  }
70  break;
71  case KTextEditor::CodeCompletionModel::IsExpandable:
72  return QVariant(true);
73  case KTextEditor::CodeCompletionModel::ExpandingWidget:
74  {
75  QTextEdit *textEdit = new QTextEdit();
77  // don't make it too large, only show a few lines
78  textEdit->resize(textEdit->width(), 100);
79  textEdit->setPlainText(m_snippet);
80  textEdit->setReadOnly(true);
81  textEdit->setLineWrapMode(QTextEdit::NoWrap);
82 
83  QVariant v;
84  v.setValue<QWidget*>(textEdit);
85  return v;
86  }
87  }
88 
89  return QVariant();
90 }
91 
92 void SnippetCompletionItem::execute( KTextEditor::View* view, const KTextEditor::Range& word )
93 {
94  QMap< QString, QString > values = QMap<QString, QString>();
95  KTextEditor::TemplateInterface2* templateIface2 = qobject_cast<KTextEditor::TemplateInterface2*>(view);
96  if (templateIface2)
97  {
98  // Replace matched text...
99  view->document()->removeText(word);
100  // ... with snippet content
101  templateIface2->insertTemplateText(word.start(), m_snippet, values, m_repo->registeredScript());
102  }
103 }
QModelIndex
QWidget
QString::prepend
QString & prepend(QChar ch)
QMap< QString, QString >
SnippetCompletionItem::SnippetCompletionItem
SnippetCompletionItem(Snippet *snippet, SnippetRepository *repo)
Definition: snippetcompletionitem.cpp:40
QTextEdit::setPlainText
void setPlainText(const QString &text)
SnippetCompletionItem::data
QVariant data(const QModelIndex &index, int role, const KTextEditor::CodeCompletionModel2 *model) const
Definition: snippetcompletionitem.cpp:52
QWidget::width
width
QWidget::resize
void resize(int w, int h)
SnippetCompletionItem::execute
void execute(KTextEditor::View *view, const KTextEditor::Range &word)
Definition: snippetcompletionitem.cpp:92
SnippetRepository::completionNamespace
QString completionNamespace() const
The namespace associated with this repository.
Definition: snippetrepository.cpp:127
SnippetRepository
Each object of this type represents a repository of snippets.
Definition: snippetrepository.h:52
Snippet
One object of this class represents a single snippet.
Definition: snippet.h:42
QVariant::setValue
void setValue(const T &value)
QTextEdit::setLineWrapMode
void setLineWrapMode(LineWrapMode mode)
QModelIndex::column
int column() const
QTextEdit::setReadOnly
void setReadOnly(bool ro)
snippetcompletionitem.h
QTextEdit
SnippetRepository::registeredScript
KTextEditor::TemplateScript * registeredScript() const
The token identifying the script in this repository.
Definition: snippetrepository.cpp:142
SnippetCompletionItem::~SnippetCompletionItem
~SnippetCompletionItem()
Definition: snippetcompletionitem.cpp:48
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