Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
codechunk.cpp
Go to the documentation of this file.
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
7
9
10// entries 128 and above are Other
11static const int charCategory[256] = { Other, Other, Other, Other, Other, Other, Other, Other,
15 // ! " # $ % & '
17 // ( ) * + , - . /
19 // 0 1 2 3 4 5 6 7
21 // 8 9 : ; < = > ?
23 // @ A B C D E F G
25 // H I J K L M N O
27 // P Q R S T U V W
29 // X Y Z [ \ ] ^ _
31 // ` a b c d e f g
33 // h i j k l m n o
35 // p q r s t u v w
37 // x y z { | } ~
39
40static 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
53static 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 */
80void 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(m_str.at(m_str.size() - 1));
86 int cat2 = category(lexeme[0]);
87 if (needSpace[cat1][cat2])
88 m_str += QLatin1Char(' ');
89 }
90 m_str += lexeme;
91}
92
93QT_END_NAMESPACE
static const bool needSpace[9][9]
Definition codechunk.cpp:40
static int category(QChar ch)
Definition codechunk.cpp:53
static const int charCategory[256]
Definition codechunk.cpp:11
@ RAngle
Definition codechunk.cpp:8
@ Paren
Definition codechunk.cpp:8
@ Comma
Definition codechunk.cpp:8
@ RBrace
Definition codechunk.cpp:8
@ Other
Definition codechunk.cpp:8
@ Alnum
Definition codechunk.cpp:8
@ Gizmo
Definition codechunk.cpp:8
@ LBrace
Definition codechunk.cpp:8
@ Colon
Definition codechunk.cpp:8