File indexing completed on 2024-05-05 04:36:49

0001 /* This file is part of KDevelop
0002  *
0003  * Copyright (C) 2012-2015 Miquel Sabaté Solà <mikisabate@gmail.com>
0004  *
0005  * This program is free software: you can redistribute it and/or modify
0006  * it under the terms of the GNU General Public License as published by
0007  * the Free Software Foundation, either version 3 of the License, or
0008  * (at your option) any later version.
0009  *
0010  * This program is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013  * GNU General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU General Public License
0016  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017  */
0018 
0019 // KDE
0020 #include <KTextEditor/Document>
0021 #include <KTextEditor/View>
0022 
0023 // Ruby
0024 #include <completion/items/requirefileitem.h>
0025 
0026 using namespace KTextEditor;
0027 
0028 namespace ruby
0029 {
0030 
0031 RequireFileItem::RequireFileItem(const KDevelop::IncludeItem &include, const char closing)
0032     : BaseIncludeFileItem(include), m_closing(closing)
0033 {
0034 }
0035 
0036 void RequireFileItem::execute(View *view, const Range &word)
0037 {
0038     KTextEditor::Document *document = view->document();
0039 
0040     QString text = includeItem.name;
0041     if (includeItem.isDirectory) {
0042         text += "/";
0043     } else if (text.endsWith(".rb")) {
0044         text.chop(3); // .rb
0045     }
0046 
0047     // Close the item if needed.
0048     const QString textAfter = document->text(Range(word.end(), document->documentEnd()));
0049     bool found = false;
0050     for (int i = 0; textAfter[i] != '\n' && i < textAfter.length(); i++) {
0051         if (textAfter[i] == m_closing) {
0052             found = true;
0053             break;
0054         }
0055     }
0056     if (!found) {
0057         text += m_closing;
0058     }
0059 
0060     document->replaceText(word, text);
0061 }
0062 
0063 }