| 1 | // Copyright (C) 2019 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 |
| 3 | |
| 4 | #include "codechunk.h" |
| 5 | |
| 6 | QT_BEGIN_NAMESPACE |
| 7 | |
| 8 | enum { Other, Alnum, Gizmo, Comma, LBrace, RBrace, RAngle, Colon, Paren }; |
| 9 | |
| 10 | // entries 128 and above are Other |
| 11 | static const int charCategory[256] = { Other, Other, Other, Other, Other, Other, Other, Other, |
| 12 | Other, Other, Other, Other, Other, Other, Other, Other, |
| 13 | Other, Other, Other, Other, Other, Other, Other, Other, |
| 14 | Other, Other, Other, Other, Other, Other, Other, Other, |
| 15 | // ! " # $ % & ' |
| 16 | Other, Other, Other, Other, Other, Gizmo, Gizmo, Other, |
| 17 | // ( ) * + , - . / |
| 18 | Paren, Paren, Gizmo, Gizmo, Comma, Other, Other, Gizmo, |
| 19 | // 0 1 2 3 4 5 6 7 |
| 20 | Alnum, Alnum, Alnum, Alnum, Alnum, Alnum, Alnum, Alnum, |
| 21 | // 8 9 : ; < = > ? |
| 22 | Alnum, Alnum, Colon, Other, Other, Gizmo, RAngle, Gizmo, |
| 23 | // @ A B C D E F G |
| 24 | Other, Alnum, Alnum, Alnum, Alnum, Alnum, Alnum, Alnum, |
| 25 | // H I J K L M N O |
| 26 | Alnum, Alnum, Alnum, Alnum, Alnum, Alnum, Alnum, Alnum, |
| 27 | // P Q R S T U V W |
| 28 | Alnum, Alnum, Alnum, Alnum, Alnum, Alnum, Alnum, Alnum, |
| 29 | // X Y Z [ \ ] ^ _ |
| 30 | Alnum, Alnum, Alnum, Other, Other, Other, Gizmo, Alnum, |
| 31 | // ` a b c d e f g |
| 32 | Other, Alnum, Alnum, Alnum, Alnum, Alnum, Alnum, Alnum, |
| 33 | // h i j k l m n o |
| 34 | Alnum, Alnum, Alnum, Alnum, Alnum, Alnum, Alnum, Alnum, |
| 35 | // p q r s t u v w |
| 36 | Alnum, Alnum, Alnum, Alnum, Alnum, Alnum, Alnum, Alnum, |
| 37 | // x y z { | } ~ |
| 38 | Alnum, Alnum, Alnum, LBrace, Gizmo, RBrace, Other, Other }; |
| 39 | |
| 40 | static const bool needSpace[9][9] = { |
| 41 | /* [ a + , { } > : ) */ |
| 42 | /* [ */ { false, false, false, false, false, true, false, false, false }, |
| 43 | /* a */ { false, true, true, false, false, true, false, false, false }, |
| 44 | /* + */ { false, true, false, false, false, true, false, true, false }, |
| 45 | /* , */ { true, true, true, true, true, true, true, true, false }, |
| 46 | /* { */ { false, false, false, false, false, false, false, false, false }, |
| 47 | /* } */ { false, false, false, false, false, false, false, false, false }, |
| 48 | /* > */ { true, true, true, false, true, true, true, false, false }, |
| 49 | /* : */ { false, false, true, true, true, true, true, false, false }, |
| 50 | /* ( */ { false, false, false, false, false, false, false, false, false }, |
| 51 | }; |
| 52 | |
| 53 | static int category(QChar ch) |
| 54 | { |
| 55 | return charCategory[static_cast<int>(ch.toLatin1())]; |
| 56 | } |
| 57 | |
| 58 | /*! |
| 59 | \class CodeChunk |
| 60 | |
| 61 | \brief The CodeChunk class represents a tiny piece of C++ code. |
| 62 | |
| 63 | The class provides conversion between a list of lexemes and a string. It adds |
| 64 | spaces in the right places for consistent style. The tiny pieces of code it |
| 65 | represents are data types, enum values, and default parameter values. |
| 66 | |
| 67 | Apart from the piece of code itself, the chunk also records a hotspot, which |
| 68 | is the place the variable name should be inserted in the case of a variable |
| 69 | or parameter declaration. The hotspot of |
| 70 | |
| 71 | char * [] |
| 72 | |
| 73 | is between '*' and '[]'. |
| 74 | */ |
| 75 | |
| 76 | /*! |
| 77 | Appends \a lexeme to the current string contents, inserting a space if |
| 78 | appropriate. |
| 79 | */ |
| 80 | void CodeChunk::append(const QString &lexeme) |
| 81 | { |
| 82 | if (!m_str.isEmpty() && !lexeme.isEmpty()) { |
| 83 | // Insert a space between the contents of the code chunk and the new |
| 84 | // lexeme if needed. |
| 85 | int cat1 = category(ch: m_str.at(i: m_str.size() - 1)); |
| 86 | int cat2 = category(ch: lexeme[0]); |
| 87 | if (needSpace[cat1][cat2]) |
| 88 | m_str += QLatin1Char(' '); |
| 89 | } |
| 90 | m_str += lexeme; |
| 91 | } |
| 92 | |
| 93 | QT_END_NAMESPACE |
| 94 | |