File indexing completed on 2024-04-14 14:47:35

0001 /*
0002     SPDX-FileCopyrightText: 2009 Milian Wolff <mail@milianw.de>
0003     Based on Cpp ImplementationHelperItem
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "keyworditem.h"
0009 
0010 #include <KTextEditor/Document>
0011 #include <KTextEditor/View>
0012 
0013 #include <KLocalizedString>
0014 
0015 #include <language/duchain/duchain.h>
0016 #include <language/duchain/duchainlock.h>
0017 #include <language/duchain/declaration.h>
0018 #include <language/duchain/types/functiontype.h>
0019 #include <language/codecompletion/codecompletionmodel.h>
0020 
0021 #include "../duchain/declarations/classmethoddeclaration.h"
0022 
0023 #include "helpers.h"
0024 
0025 using namespace KDevelop;
0026 
0027 namespace Php
0028 {
0029 
0030 QVariant KeywordItem::data(const QModelIndex& index, int role, const CodeCompletionModel* model) const
0031 {
0032     switch (role) {
0033     case CodeCompletionModel::IsExpandable:
0034         return QVariant(false);
0035     case Qt::DisplayRole:
0036         if (index.column() == KTextEditor::CodeCompletionModel::Name) {
0037             return QVariant(m_keyword);
0038         } else {
0039             return QVariant("");
0040         }
0041         break;
0042     case KTextEditor::CodeCompletionModel::ItemSelected:
0043         return QVariant("");
0044     case KTextEditor::CodeCompletionModel::InheritanceDepth:
0045         return QVariant(0);
0046     default:
0047         //pass
0048         break;
0049     }
0050 
0051     return NormalDeclarationCompletionItem::data(index, role, model);
0052 }
0053 
0054 void KeywordItem::execute(KTextEditor::View* view, const KTextEditor::Range& word)
0055 {
0056     KTextEditor::Document *document = view->document();
0057     if ( !m_replacement.isEmpty() ) {
0058         QString replacement = m_replacement;
0059         replacement = replacement.replace('\n', '\n' + getIndentation(document->line(word.start().line())));
0060         replacement = replacement.replace(QLatin1String("%INDENT%"), indentString(document));
0061 
0062         int cursorPos = replacement.indexOf(QStringLiteral("%CURSOR%"));
0063         int selectionEnd = -1;
0064         if ( cursorPos != -1 ) {
0065             replacement.remove(QStringLiteral("%CURSOR%"));
0066         } else {
0067             cursorPos = replacement.indexOf(QStringLiteral("%SELECT%"));
0068             if ( cursorPos != -1 ) {
0069                 replacement.remove(QStringLiteral("%SELECT%"));
0070                 selectionEnd = replacement.indexOf(QStringLiteral("%ENDSELECT%"), cursorPos + 1);
0071                 if ( selectionEnd == -1 ) {
0072                     selectionEnd = replacement.length();
0073                 }
0074                 replacement.remove(QStringLiteral("%ENDSELECT%"));
0075             }
0076         }
0077 
0078         document->replaceText(word, replacement);
0079 
0080         if ( cursorPos != -1 ) {
0081             if (view) {
0082                 replacement = replacement.left(cursorPos);
0083                 KTextEditor::Cursor newPos(
0084                     word.start().line() + replacement.count('\n'),
0085                     word.start().column() + replacement.length() - replacement.lastIndexOf('\n') - 1
0086                 );
0087                 view->setCursorPosition(newPos);
0088                 if ( selectionEnd != -1 ) {
0089                     ///TODO: maybe we want to support multi-line selections in the future?
0090                     view->setSelection(
0091                         KTextEditor::Range(
0092                             newPos,
0093                             KTextEditor::Cursor(
0094                                 newPos.line(),
0095                                 newPos.column() + selectionEnd - cursorPos
0096                             )
0097                         )
0098                     );
0099                 }
0100             }
0101         }
0102     } else {
0103         document->replaceText(word, m_keyword + ' ');
0104     }
0105 }
0106 
0107 }