File indexing completed on 2024-04-21 15:24:06
0001 /* 0002 SPDX-FileCopyrightText: 2009 Milian Wolff <mail@milianw.de> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "includefileitem.h" 0008 0009 #include <KTextEditor/Document> 0010 #include <KTextEditor/View> 0011 0012 using namespace KTextEditor; 0013 0014 namespace Php { 0015 0016 void IncludeFileItem::execute(View* view, const Range& _word) 0017 { 0018 KTextEditor::Document *document = view->document(); 0019 Range word(_word); 0020 0021 QString newText; 0022 0023 if ( includeItem.isDirectory ) { 0024 newText = includeItem.name + '/'; 0025 } else { 0026 newText = includeItem.name; 0027 } 0028 0029 // Add suffix 0030 QChar closeChar; 0031 { 0032 const QString textBefore = document->text(Range(Cursor(0, 0), _word.start())); 0033 QRegExp regexp("(?:include|require)(?:_once)?(\\s*)(\\(?)(\\s*)(?:dirname\\s*\\(\\s*__FILE__\\s*\\)\\s*\\.\\s*)?([\"'])", Qt::CaseInsensitive); 0034 0035 if ( regexp.lastIndexIn(textBefore) != -1 ) { 0036 closeChar = regexp.cap(4).at(0); 0037 0038 newText.append(closeChar); 0039 if ( !regexp.cap(2).isEmpty() ) { 0040 newText.append(regexp.cap(3)); 0041 newText.append(')'); 0042 } 0043 newText.append(';'); 0044 } 0045 } 0046 0047 // Adapt range and replace existing stuff 0048 { 0049 const QString textAfter = document->text(Range(_word.end(), document->documentEnd())); 0050 if ( !textAfter.isEmpty() ) { 0051 int pos = 0; 0052 for (; pos < textAfter.length(); ++pos ) { 0053 if ( textAfter[pos].isSpace() ) { 0054 break; 0055 } else if ( textAfter[pos] == closeChar ) { 0056 // remove close char 0057 ++pos; 0058 // find semicolon (if existing) 0059 for (int i = pos; i < textAfter.length(); ++i ) { 0060 if ( textAfter[i] == ';' ) { 0061 // remove semicolon 0062 pos = i + 1; 0063 break; 0064 } else if ( !textAfter[i].isSpace() && textAfter[i] != ')' ) { 0065 break; 0066 } 0067 } 0068 break; 0069 } 0070 } 0071 if ( pos > 0 ) { 0072 word.setEnd(word.end() + Cursor(0, pos)); 0073 } 0074 } 0075 } 0076 0077 document->replaceText(word, newText); 0078 0079 // when we complete a directory, move the cursor behind it so we can continue with auto-completion 0080 if ( includeItem.isDirectory ) { 0081 if (view) { 0082 view->setCursorPosition(Cursor(_word.start().line(), _word.start().column() + includeItem.name.size() + 1)); 0083 } 0084 } 0085 } 0086 0087 }