aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/cplusplus/ExpressionUnderCursor.cpp
blob: 3ce453002f067c72de25214b31021b86ee141e83 (plain)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "ExpressionUnderCursor.h"

#include "SimpleLexer.h"
#include "BackwardsScanner.h"

#include <cplusplus/Token.h>

#include <QTextCursor>
#include <QTextBlock>

using namespace CPlusPlus;

ExpressionUnderCursor::ExpressionUnderCursor(const LanguageFeatures &languageFeatures)
    : _jumpedComma(false)
    , _languageFeatures(languageFeatures)
{ }

int ExpressionUnderCursor::startOfExpression(BackwardsScanner &tk, int index)
{
    if (tk[index - 1].is(T_GREATER)) {
        const int matchingBraceIndex = tk.startOfMatchingBrace(index);

        if (tk[matchingBraceIndex - 1].is(T_IDENTIFIER))
            index = matchingBraceIndex - 1;
    }

    index = startOfExpression_helper(tk, index);

    if (_jumpedComma) {
        const Token &tok = tk[index - 1];

        switch (tok.kind()) {
        case T_COMMA:
        case T_LPAREN:
        case T_LBRACKET:
        case T_LBRACE:
        case T_SEMICOLON:
        case T_COLON:
        case T_QUESTION:
            break;

        default:
            if (tok.isPunctuationOrOperator())
                return startOfExpression(tk, index - 1);

            break;
        }
    }

    return index;
}

int ExpressionUnderCursor::startOfExpression_helper(BackwardsScanner &tk, int index)
{
    if (tk[index - 1].isLiteral()) {
        return index - 1;
    } else if (tk[index - 1].is(T_THIS)) {
        return index - 1;
    } else if (tk[index - 1].is(T_TYPEID)) {
        return index - 1;
    } else if (tk[index - 1].is(T_SIGNAL)) {
        if (tk[index - 2].is(T_COMMA) && !_jumpedComma) {
            _jumpedComma = true;
            return startOfExpression(tk, index - 2);
        }
        return index - 1;
    } else if (tk[index - 1].is(T_SLOT)) {
        if (tk[index - 2].is(T_COMMA) && !_jumpedComma) {
            _jumpedComma = true;
            return startOfExpression(tk, index - 2);
        }
        return index - 1;
    } else if (tk[index - 1].is(T_IDENTIFIER)) {
        if (tk[index - 2].is(T_TILDE)) {
            if (tk[index - 3].is(T_COLON_COLON))
                return startOfExpression(tk, index - 3);
            else if (tk[index - 3].is(T_DOT) || tk[index - 3].is(T_ARROW))
                return startOfExpression(tk, index - 3);
            return index - 2;
        } else if (tk[index - 2].is(T_COLON_COLON)) {
            return startOfExpression(tk, index - 1);
        } else if (tk[index - 2].is(T_DOT) || tk[index - 2].is(T_ARROW)) {
            return startOfExpression(tk, index - 2);
        } else if (tk[index - 2].is(T_DOT_STAR) || tk[index - 2].is(T_ARROW_STAR)) {
            return startOfExpression(tk, index - 2);
        } else if (tk[index - 2].is(T_LBRACKET)) {
            // array subscription:
            //     array[i
            return index - 1;
        } else if (tk[index - 2].is(T_COLON)) {
            // either of:
            //     cond ? expr1 : id
            // or:
            //     [receiver messageParam:id
            // and in both cases, the id (and only the id) is what we want, so:
            return index - 1;
        } else if (tk[index - 2].is(T_IDENTIFIER) && tk[index - 3].is(T_LBRACKET)) {
            // Very common Objective-C case:
            //     [receiver message
            // which we handle immediately:
            return index - 3;
        } else {
#if 0 // see QTCREATORBUG-1501
            // See if we are handling an Objective-C messaging expression in the form of:
            //     [receiver messageParam1:expression messageParam2
            // or:
            //     [receiver messageParam1:expression messageParam2:expression messageParam3
            // ... etc
            int i = index - 1;
            while (i >= 0 && tk[i].isNot(T_EOF_SYMBOL)) {
                if (tk[i].is(T_LBRACKET))
                    break;
                if (tk[i].is(T_LBRACE) || tk[i].is(T_RBRACE))
                    break;
                else if (tk[i].is(T_RBRACKET))
                    i = tk.startOfMatchingBrace(i + 1) - 1;
                else
                    --i;
            }

            if (i >= 0) {
                int j = i;
                while (tk[j].is(T_LBRACKET))
                    ++j;
                if (tk[j].is(T_IDENTIFIER) && tk[j + 1].is(T_IDENTIFIER))
                    return i;
            }
#endif
        }
        return index - 1;
    } else if (tk[index - 1].is(T_RPAREN)) {
        int matchingBraceIndex = tk.startOfMatchingBrace(index);
        if (! matchingBraceIndex)
            return matchingBraceIndex;
        if (matchingBraceIndex != index) {
            if (tk[matchingBraceIndex - 1].is(T_GREATER)) {
                int lessIndex = tk.startOfMatchingBrace(matchingBraceIndex);
                if (lessIndex != matchingBraceIndex - 1) {
                    if (tk[lessIndex - 1].is(T_DYNAMIC_CAST)     ||
                        tk[lessIndex - 1].is(T_STATIC_CAST)      ||
                        tk[lessIndex - 1].is(T_CONST_CAST)       ||
                        tk[lessIndex - 1].is(T_REINTERPRET_CAST))
                        return lessIndex - 1;
                    else if (tk[lessIndex - 1].is(T_IDENTIFIER))
                        return startOfExpression(tk, lessIndex);
                    else if (tk[lessIndex - 1].is(T_SIGNAL))
                        return startOfExpression(tk, lessIndex);
                    else if (tk[lessIndex - 1].is(T_SLOT))
                        return startOfExpression(tk, lessIndex);
                }
            } else if (tk[matchingBraceIndex - 1].is(T_RBRACE)) {
                // lambda: [](){} ()
                int leftBraceIndex = tk.startOfMatchingBrace(matchingBraceIndex);
                if (matchingBraceIndex != leftBraceIndex) {
                    int currentIndex = leftBraceIndex;
                    while (currentIndex >= 0) {
                        if (tk[currentIndex-1].is(T_RPAREN)) {
                            int leftParenIndex = tk.startOfMatchingBrace(currentIndex);
                            if (tk[leftParenIndex-1].is(T_THROW)) {
                                currentIndex = leftParenIndex-1;
                                continue;
                            } else if (tk[leftParenIndex-1].is(T_RBRACKET)) {
                                int leftBracketIndex = tk.startOfMatchingBrace(leftParenIndex);
                                if (leftBracketIndex != leftParenIndex-1)
                                    return leftBracketIndex;
                            }
                        } else if (tk[currentIndex-1].is(T_RBRACKET)) {
                            int leftBracketIndex = tk.startOfMatchingBrace(currentIndex);
                            if (leftBracketIndex != currentIndex-1)
                                return leftBracketIndex;
                        }
                        --currentIndex;
                    }
                }
            }
            return startOfExpression(tk, matchingBraceIndex);
        }
        return index;
    } else if (tk[index - 1].is(T_RBRACKET)) {
        int rbracketIndex = tk.startOfMatchingBrace(index);
        if (rbracketIndex != index)
            return startOfExpression(tk, rbracketIndex);
        return index;
    } else if (tk[index - 1].is(T_COLON_COLON)) {
        if (tk[index - 2].is(T_GREATER)) { // ### not exactly
            int lessIndex = tk.startOfMatchingBrace(index - 1);
            if (lessIndex != index - 1)
                return startOfExpression(tk, lessIndex);
            return index - 1;
        } else if (tk[index - 2].is(T_IDENTIFIER)) {
            return startOfExpression(tk, index - 1);
        }
        return index - 1;
    } else if (tk[index - 1].is(T_DOT) || tk[index - 1].is(T_ARROW)) {
        return startOfExpression(tk, index - 1);
    } else if (tk[index - 1].is(T_DOT_STAR) || tk[index - 1].is(T_ARROW_STAR)) {
        return startOfExpression(tk, index - 1);
    }

    return index;
}

QString ExpressionUnderCursor::operator()(const QTextCursor &cursor)
{
    BackwardsScanner scanner(cursor, _languageFeatures);

    _jumpedComma = false;

    const int initialSize = scanner.startToken();
    const int i = startOfExpression(scanner, initialSize);
    if (i == initialSize)
        return QString();

    return scanner.mid(i);
}

int ExpressionUnderCursor::startOfFunctionCall(const QTextCursor &cursor) const
{
    BackwardsScanner scanner(cursor, _languageFeatures);

    int index = scanner.startToken();

    forever {
        const Token &tk = scanner[index - 1];

        if (tk.is(T_EOF_SYMBOL)) {
            break;
        } else if (tk.is(T_LPAREN) || tk.is(T_LBRACE)) {
            return scanner.startPosition() + tk.utf16charsBegin();
        } else if (tk.is(T_RPAREN) || tk.is(T_RBRACE)) {
            int matchingBrace = scanner.startOfMatchingBrace(index);

            if (matchingBrace == index) // If no matching brace found
                return -1;

            index = matchingBrace;
        } else {
            --index;
        }
    }

    return -1;
}