File indexing completed on 2024-05-05 05:52:18

0001 /*  This file is part of the Kate project.
0002  *  Based on the snippet plugin from KDevelop 4.
0003  *
0004  *  SPDX-FileCopyrightText: 2007 Robert Gruber <rgruber@users.sourceforge.net>
0005  *  SPDX-FileCopyrightText: 2012 Christoph Cullmann <cullmann@kde.org>
0006  *
0007  *  SPDX-License-Identifier: LGPL-2.0-or-later
0008  */
0009 #include "katesnippetglobal.h"
0010 
0011 #include "editsnippet.h"
0012 #include "snippet.h"
0013 #include "snippetcompletionitem.h"
0014 #include "snippetcompletionmodel.h"
0015 #include "snippetrepository.h"
0016 #include "snippetstore.h"
0017 
0018 #include <KAboutData>
0019 #include <KLocalizedString>
0020 #include <KPluginFactory>
0021 
0022 #include <ktexteditor/application.h>
0023 #include <ktexteditor/document.h>
0024 #include <ktexteditor/editor.h>
0025 #include <ktexteditor/mainwindow.h>
0026 #include <ktexteditor/view.h>
0027 
0028 #include <QDialogButtonBox>
0029 #include <QMenu>
0030 
0031 KateSnippetGlobal *KateSnippetGlobal::s_self = nullptr;
0032 
0033 KateSnippetGlobal::KateSnippetGlobal(QObject *parent, const QVariantList &)
0034     : QObject(parent)
0035 {
0036     s_self = this;
0037 
0038     SnippetStore::init();
0039 }
0040 
0041 KateSnippetGlobal::~KateSnippetGlobal()
0042 {
0043     delete SnippetStore::self();
0044     s_self = nullptr;
0045 }
0046 
0047 void KateSnippetGlobal::insertSnippet(Snippet *snippet)
0048 {
0049     // query active view, always prefer that!
0050     KTextEditor::View *view = KTextEditor::Editor::instance()->application()->activeMainWindow()->activeView();
0051 
0052     // fallback to stuff set for dialog
0053     if (!view) {
0054         view = m_activeViewForDialog;
0055     }
0056 
0057     // no view => nothing to do
0058     if (!view) {
0059         return;
0060     }
0061 
0062     // try to insert snippet
0063     SnippetCompletionItem item(snippet, static_cast<SnippetRepository *>(snippet->parent()));
0064     item.execute(view, KTextEditor::Range(view->cursorPosition(), view->cursorPosition()));
0065 
0066     // set focus to view
0067     view->setFocus();
0068 }
0069 
0070 void KateSnippetGlobal::insertSnippetFromActionData()
0071 {
0072     QAction *action = qobject_cast<QAction *>(sender());
0073     Q_ASSERT(action);
0074     Snippet *snippet = action->data().value<Snippet *>();
0075     Q_ASSERT(snippet);
0076     insertSnippet(snippet);
0077 }
0078 
0079 void KateSnippetGlobal::createSnippet(KTextEditor::View *view)
0080 {
0081     // no active view, bad
0082     if (!view) {
0083         return;
0084     }
0085 
0086     // get mode
0087     QString mode = view->document()->highlightingModeAt(view->selectionRange().isValid() ? view->selectionRange().start() : view->cursorPosition());
0088     if (mode.isEmpty()) {
0089         mode = view->document()->mode();
0090     }
0091 
0092     // try to look for a fitting repo
0093     SnippetRepository *match = nullptr;
0094     for (int i = 0; i < SnippetStore::self()->rowCount(); ++i) {
0095         SnippetRepository *repo = SnippetRepository::fromItem(SnippetStore::self()->item(i));
0096         if (repo && repo->fileTypes().count() == 1 && repo->fileTypes().first() == mode) {
0097             match = repo;
0098             break;
0099         }
0100     }
0101     bool created = !match;
0102     if (created) {
0103         match = SnippetRepository::createRepoFromName(i18nc("Autogenerated repository name for a programming language", "%1 snippets", mode));
0104         match->setFileTypes(QStringList() << mode);
0105     }
0106 
0107     EditSnippet dlg(match, nullptr, view);
0108     dlg.setSnippetText(view->selectionText());
0109     int status = dlg.exec();
0110     if (created && status != QDialog::Accepted) {
0111         // cleanup
0112         match->remove();
0113     }
0114 }
0115 
0116 #include "moc_katesnippetglobal.cpp"