File: includefileitem.cpp

package info (click to toggle)
umbrello 4%3A25.12.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 46,212 kB
  • sloc: cpp: 144,235; php: 2,405; sh: 855; xml: 354; cs: 309; java: 91; python: 68; makefile: 11; sql: 7
file content (89 lines) | stat: -rw-r--r-- 2,690 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
    KDevelop Php Code Completion Support

    SPDX-FileCopyrightText: 2009 Milian Wolff <mail@milianw.de>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include "includefileitem.h"

#include <KTextEditor/Document>
#include <KTextEditor/View>

using namespace KTextEditor;

namespace Php {

void IncludeFileItem::execute(View* view, const Range& _word)
{
    KTextEditor::Document *document = view->document();
    Range word(_word);

    QString newText;

    if ( includeItem.isDirectory ) {
        newText = includeItem.name + '/';
    } else {
        newText = includeItem.name;
    }

    // Add suffix
    QChar closeChar;
    {
        const QString textBefore = document->text(Range(Cursor(0, 0), _word.start()));
        QRegularExpression regexp("(?:include|require)(?:_once)?(\\s*)(\\(?)(\\s*)(?:dirname\\s*\\(\\s*__FILE__\\s*\\)\\s*\\.\\s*)?([\"'])", Qt::CaseInsensitive);

        if ( regexp.lastIndexIn(textBefore) != -1 ) {
            closeChar = regexp.cap(4).at(0);

            newText.append(closeChar);
            if ( !regexp.cap(2).isEmpty() ) {
                newText.append(regexp.cap(3));
                newText.append(')');
            }
            newText.append(';');
        }
    }

    // Adapt range and replace existing stuff
    {
        const QString textAfter = document->text(Range(_word.end(), document->documentEnd()));
        if ( !textAfter.isEmpty() ) {
            int pos = 0;
            for (; pos < textAfter.length(); ++pos ) {
                if ( textAfter[pos].isSpace() ) {
                    break;
                } else if ( textAfter[pos] == closeChar ) {
                    // remove close char
                    ++pos;
                    // find semicolon (if existing)
                    for (int i = pos; i < textAfter.length(); ++i ) {
                        if ( textAfter[i] == ';' ) {
                            // remove semicolon
                            pos = i + 1;
                            break;
                        } else if ( !textAfter[i].isSpace() && textAfter[i] != ')' ) {
                            break;
                        }
                    }
                    break;
                }
            }
            if ( pos > 0 ) {
                word.setEnd(word.end() + Cursor(0, pos));
            }
        }
    }

    document->replaceText(word, newText);

    // when we complete a directory, move the cursor behind it so we can continue with auto-completion
    if ( includeItem.isDirectory ) {
        if (view) {
            view->setCursorPosition(Cursor(_word.start().line(), _word.start().column() + includeItem.name.size() + 1));
        }
    }
}

}