File indexing completed on 2025-01-19 04:25:12
0001 /**************************************************************************************** 0002 * Copyright (c) 2013 Anmol Ahuja <darthcodus@gmail.com> * 0003 * * 0004 * This program is free software; you can redistribute it and/or modify it under * 0005 * the terms of the GNU General Public License as published by the Free Software * 0006 * Foundation; either version 2 of the License, or (at your option) any later * 0007 * version. * 0008 * * 0009 * This program is distributed in the hope that it will be useful, but WITHOUT ANY * 0010 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * 0011 * PARTICULAR PURPOSE. See the GNU General Public License for more details. * 0012 * * 0013 * You should have received a copy of the GNU General Public License along with * 0014 * this program. If not, see <http://www.gnu.org/licenses/>. * 0015 ****************************************************************************************/ 0016 0017 #include "CompletionModel.h" 0018 0019 #include "core/support/Debug.h" 0020 0021 #include <QFile> 0022 #include <QStandardPaths> 0023 0024 #include <KTextEditor/View> 0025 #include <KTextEditor/Document> 0026 0027 using namespace ScriptConsoleNS; 0028 0029 AmarokScriptCodeCompletionModel::AmarokScriptCodeCompletionModel( QObject *parent ) 0030 : CodeCompletionModel( parent ) 0031 { 0032 const QUrl url( QStandardPaths::locate( QStandardPaths::GenericDataLocation, QStringLiteral("amarok/scriptconsole/") ) ); 0033 QFile file( url.path() + QStringLiteral("AutoComplete.txt") ); 0034 if( file.open( QFile::ReadOnly ) ) 0035 { 0036 QTextStream in( &file ); 0037 while ( !in.atEnd() ) 0038 m_autoCompleteStrings << in.readLine(); 0039 } 0040 else 0041 debug() << "No autocomplete file found for the script console"; 0042 } 0043 0044 void 0045 AmarokScriptCodeCompletionModel::completionInvoked( KTextEditor::View *view, const KTextEditor::Range &range, KTextEditor::CodeCompletionModel::InvocationType invocationType ) 0046 { 0047 Q_UNUSED( invocationType ) 0048 0049 beginResetModel(); 0050 m_completionList.clear(); 0051 const QString ¤tText = view->document()->text( range ); 0052 foreach( const QString &completionItem, m_autoCompleteStrings ) 0053 { 0054 int index = completionItem.indexOf( currentText, Qt::CaseInsensitive ) + currentText.length(); 0055 if( index != -1 && !QStringRef( &completionItem, index, completionItem.size()-index ).contains( QLatin1Char('.') ) && completionItem != currentText ) 0056 m_completionList << completionItem; 0057 } 0058 setRowCount( m_completionList.count() ); 0059 endResetModel(); 0060 } 0061 0062 QVariant 0063 AmarokScriptCodeCompletionModel::data( const QModelIndex &index, int role ) const 0064 { 0065 if( !index.isValid() || role != Qt::DisplayRole || index.row() < 0 || index.row() >= rowCount() 0066 || index.column() != KTextEditor::CodeCompletionModel::Name ) 0067 return QVariant(); 0068 return m_completionList[ index.row() ]; 0069 } 0070 0071 KTextEditor::Range 0072 AmarokScriptCodeCompletionModel::completionRange(KTextEditor::View* view, const KTextEditor::Cursor& position) 0073 { 0074 const QString& line = view->document()->line(position.line()); 0075 KTextEditor::Range range(position, position); 0076 // include everything non-space before 0077 for( int i = position.column() - 1; i >= 0; --i ) 0078 { 0079 if( line.at( i ).isSpace() ) 0080 break; 0081 else 0082 range.start().setColumn( i ); 0083 } 0084 // include everything non-space after 0085 for( int i = position.column() + 1; i < line.length(); ++i ) 0086 { 0087 if( line.at( i ).isSpace() ) 0088 break; 0089 else 0090 range.end().setColumn( i ); 0091 } 0092 return range; 0093 } 0094 0095 bool 0096 AmarokScriptCodeCompletionModel::shouldAbortCompletion( KTextEditor::View *view, const KTextEditor::Range &range, const QString ¤tCompletion ) 0097 { 0098 if(view->cursorPosition() < range.start() || view->cursorPosition() > range.end()) 0099 return true; //Always abort when the completion-range has been left 0100 0101 for( int i = 0; i < currentCompletion.length(); ++i ) 0102 { 0103 if( currentCompletion.at( i ).isSpace() ) 0104 return true; 0105 } 0106 // else it's valid 0107 return false; 0108 } 0109 0110 void 0111 AmarokScriptCodeCompletionModel::executeCompletionItem( KTextEditor::View *view, const KTextEditor::Range &range, const QModelIndex &index ) const 0112 { 0113 view->document()->replaceText( range, m_completionList.at( index.row() ) ); 0114 } 0115 0116 AmarokScriptCodeCompletionModel::~AmarokScriptCodeCompletionModel() 0117 { 0118 DEBUG_BLOCK 0119 m_completionList.clear(); 0120 }