MatchingText.cpp 9.61 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8 9 10 11 12 13 14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17 18 19 20 21 22 23 24 25
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
29

30
#include "MatchingText.h"
31

32 33
#include "BackwardsScanner.h"

34
#include <cplusplus/Token.h>
35

36 37 38
#include <QTextDocument>
#include <QTextCursor>
#include <QChar>
hjk's avatar
hjk committed
39
#include <QDebug>
40 41 42

using namespace CPlusPlus;

43
enum { MAX_NUM_LINES = 20 };
44

45
static bool shouldOverrideChar(QChar ch)
46
{
Roberto Raggi's avatar
Cleanup  
Roberto Raggi committed
47 48 49 50 51 52 53
    switch (ch.unicode()) {
    case ')': case ']': case ';': case '"': case '\'':
        return true;

    default:
        return false;
    }
54 55
}

56
static bool isCompleteStringLiteral(const BackwardsScanner &tk, int index)
57
{
58
    const QStringRef text = tk.textRef(index);
59 60 61 62 63 64 65 66 67 68

    if (text.length() < 2)
        return false;

    else if (text.at(text.length() - 1) == QLatin1Char('"'))
        return text.at(text.length() - 2) != QLatin1Char('\\'); // ### not exactly.

    return false;
}

69
static bool isCompleteCharLiteral(const BackwardsScanner &tk, int index)
70
{
71
    const QStringRef text = tk.textRef(index);
72 73 74 75 76 77 78 79 80 81

    if (text.length() < 2)
        return false;

    else if (text.at(text.length() - 1) == QLatin1Char('\''))
        return text.at(text.length() - 2) != QLatin1Char('\\'); // ### not exactly.

    return false;
}

82 83 84 85 86 87
bool MatchingText::shouldInsertMatchingText(const QTextCursor &tc)
{
    QTextDocument *doc = tc.document();
    return shouldInsertMatchingText(doc->characterAt(tc.selectionEnd()));
}

88
bool MatchingText::shouldInsertMatchingText(QChar lookAhead)
89
{
Roberto Raggi's avatar
Cleanup  
Roberto Raggi committed
90 91 92 93
    switch (lookAhead.unicode()) {
    case '{': case '}':
    case ']': case ')':
    case ';': case ',':
94 95
        return true;

Roberto Raggi's avatar
Cleanup  
Roberto Raggi committed
96 97 98 99 100 101
    default:
        if (lookAhead.isSpace())
            return true;

        return false;
    } // switch
102 103 104
}

QString MatchingText::insertMatchingBrace(const QTextCursor &cursor, const QString &textToProcess,
105
                                          QChar la, int *skippedChars)
106 107
{
    QTextCursor tc = cursor;
Roberto Raggi's avatar
Roberto Raggi committed
108
    QTextDocument *doc = tc.document();
109 110
    QString text = textToProcess;

111
    const QString blockText = tc.block().text().mid(tc.positionInBlock());
Flex Ferrum's avatar
Flex Ferrum committed
112
    const QString trimmedBlockText = blockText.trimmed();
113 114
    const int length = qMin(blockText.length(), textToProcess.length());

Roberto Raggi's avatar
Roberto Raggi committed
115 116 117
    const QChar previousChar = doc->characterAt(tc.selectionEnd() - 1);

    bool escape = false;
118

Roberto Raggi's avatar
Roberto Raggi committed
119 120 121 122 123 124 125 126 127
    if (! text.isEmpty() && (text.at(0) == QLatin1Char('"') ||
                             text.at(0) == QLatin1Char('\''))) {
        if (previousChar == QLatin1Char('\\')) {
            int escapeCount = 0;
            int index = tc.selectionEnd() - 1;
            do {
                ++escapeCount;
                --index;
            } while (doc->characterAt(index) == QLatin1Char('\\'));
128

Roberto Raggi's avatar
Roberto Raggi committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
            if ((escapeCount % 2) != 0)
                escape = true;
        }
    }

    if (! escape) {
        for (int i = 0; i < length; ++i) {
            const QChar ch1 = blockText.at(i);
            const QChar ch2 = textToProcess.at(i);

            if (ch1 != ch2)
                break;
            else if (! shouldOverrideChar(ch1))
                break;

            ++*skippedChars;
        }
146 147 148 149 150 151 152
    }

    if (*skippedChars != 0) {
        tc.movePosition(QTextCursor::NextCharacter, QTextCursor::MoveAnchor, *skippedChars);
        text = textToProcess.mid(*skippedChars);
    }

153
    if (text.isEmpty() || !shouldInsertMatchingText(la))
154 155
        return QString();

Erik Verbruggen's avatar
Erik Verbruggen committed
156
    BackwardsScanner tk(tc, MAX_NUM_LINES, textToProcess.left(*skippedChars));
157 158 159
    const int startToken = tk.startToken();
    int index = startToken;

Erik Verbruggen's avatar
Erik Verbruggen committed
160
    const Token &token = tk[index - 1];
161

162
    if (text.at(0) == QLatin1Char('"') && token.isStringLiteral()) {
163 164 165
        if (text.length() != 1)
            qWarning() << Q_FUNC_INFO << "handle event compression";

166
        if (isCompleteStringLiteral(tk, index - 1))
167 168 169
            return QLatin1String("\"");

        return QString();
170
    } else if (text.at(0) == QLatin1Char('\'') && token.isCharLiteral()) {
171 172 173
        if (text.length() != 1)
            qWarning() << Q_FUNC_INFO << "handle event compression";

174
        if (isCompleteCharLiteral(tk, index - 1))
175 176 177 178 179 180 181 182
            return QLatin1String("'");

        return QString();
    }

    QString result;

    foreach (const QChar &ch, text) {
Flex Ferrum's avatar
Flex Ferrum committed
183 184 185 186 187 188 189 190
        if      (ch == QLatin1Char('('))  result += QLatin1Char(')');
        else if (ch == QLatin1Char('['))  result += QLatin1Char(']');
        else if (ch == QLatin1Char('"'))  result += QLatin1Char('"');
        else if (ch == QLatin1Char('\'')) result += QLatin1Char('\'');
        // Handle '{' appearance within functinon call context
        else if (ch == QLatin1Char('{') && !trimmedBlockText.isEmpty() && trimmedBlockText.at(0) == QLatin1Char(')'))
            result += QLatin1Char('}');

191 192 193 194 195
    }

    return result;
}

196
static bool shouldInsertNewline(const QTextCursor &tc)
197 198 199 200 201 202 203 204 205 206 207
{
    QTextDocument *doc = tc.document();
    int pos = tc.selectionEnd();

    // count the number of empty lines.
    int newlines = 0;
    for (int e = doc->characterCount(); pos != e; ++pos) {
        const QChar ch = doc->characterAt(pos);

        if (! ch.isSpace())
            break;
208
        if (ch == QChar::ParagraphSeparator)
209 210 211
            ++newlines;
    }

212
    return newlines <= 1 && doc->characterAt(pos) != QLatin1Char('}');
213 214
}

215
QString MatchingText::insertParagraphSeparator(const QTextCursor &tc)
216
{
Erik Verbruggen's avatar
Erik Verbruggen committed
217
    BackwardsScanner tk(tc, MAX_NUM_LINES);
218 219 220 221 222
    int index = tk.startToken();

    if (tk[index - 1].isNot(T_LBRACE))
        return QString(); // nothing to do.

223
    const QString textBlock = tc.block().text().mid(tc.positionInBlock()).trimmed();
224 225 226
    if (! textBlock.isEmpty())
        return QString();

227 228
    --index; // consume the `{'

Erik Verbruggen's avatar
Erik Verbruggen committed
229
    const Token &token = tk[index - 1];
230 231 232 233 234 235 236 237 238

    if (token.is(T_STRING_LITERAL) && tk[index - 2].is(T_EXTERN)) {
        // recognized extern "C"
        return QLatin1String("}");

    } else if (token.is(T_IDENTIFIER)) {
        int i = index - 1;

        forever {
Erik Verbruggen's avatar
Erik Verbruggen committed
239
            const Token &current = tk[i - 1];
240 241 242 243

            if (current.is(T_EOF_SYMBOL))
                break;

244
            if (current.is(T_CLASS) || current.is(T_STRUCT) || current.is(T_UNION) || current.is(T_ENUM)) {
245 246 247 248 249 250 251 252
                // found a class key.
                QString str = QLatin1String("};");

                if (shouldInsertNewline(tc))
                    str += QLatin1Char('\n');

                return str;
            }
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276

            else if (current.is(T_NAMESPACE))
                return QLatin1String("}"); // found a namespace declaration

            else if (current.is(T_SEMICOLON))
                break; // found the `;' sync token

            else if (current.is(T_LBRACE) || current.is(T_RBRACE))
                break; // braces are considered sync tokens

            else if (current.is(T_LPAREN) || current.is(T_RPAREN))
                break; // sync token

            else if (current.is(T_LBRACKET) || current.is(T_RBRACKET))
                break; // sync token

            --i;
        }
    }

    if (token.is(T_NAMESPACE)) {
        // anonymous namespace
        return QLatin1String("}");

277
    } else if (token.is(T_CLASS) || token.is(T_STRUCT) || token.is(T_UNION) || token.is(T_ENUM)) {
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
        if (tk[index - 2].is(T_TYPEDEF)) {
            // recognized:
            //   typedef struct {
            //
            // in this case we don't want to insert the extra semicolon+newline.
            return QLatin1String("}");
        }

        // anonymous class
        return QLatin1String("};");

    } else if (token.is(T_RPAREN)) {
        // search the matching brace.
        const int lparenIndex = tk.startOfMatchingBrace(index);

        if (lparenIndex == index) {
            // found an unmatched brace. We don't really know to do in this case.
            return QString();
        }

        // look at the token before the matched brace
Erik Verbruggen's avatar
Erik Verbruggen committed
299
        const Token &tokenBeforeBrace = tk[lparenIndex - 1];
300 301 302 303 304 305 306 307 308 309 310 311

        if (tokenBeforeBrace.is(T_IF)) {
            // recognized an if statement
            return QLatin1String("}");

        } else if (tokenBeforeBrace.is(T_FOR) || tokenBeforeBrace.is(T_WHILE)) {
            // recognized a for-like statement
            return QLatin1String("}");

        }

        // if we reached this point there is a good chance that we are parsing a function definition
312 313 314 315 316 317
        QString str = QLatin1String("}");

        if (shouldInsertNewline(tc))
            str += QLatin1Char('\n');

        return str;
318 319 320 321 322
    }

    // match the block
    return QLatin1String("}");
}