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
csshighlighter.cpp
Go to the documentation of this file.
1
// Copyright (C) 2016 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
"csshighlighter_p.h"
5
6
QT_BEGIN_NAMESPACE
7
8
using
namespace
Qt::StringLiterals;
9
10
namespace
qdesigner_internal
{
11
12
CssHighlighter
::
CssHighlighter
(
const
CssHighlightColors
&
colors
,
13
QTextDocument
*
document
)
14
:
QSyntaxHighlighter
(
document
),
m_colors
(
colors
)
15
{
16
}
17
18
void
CssHighlighter
::
highlightBlock
(
const
QString
&
text
)
19
{
20
enum
Token
{
ALNUM
,
LBRACE
,
RBRACE
,
COLON
,
SEMICOLON
,
COMMA
,
QUOTE
,
SLASH
,
STAR
};
21
static
const
int
transitions
[10][9] = {
22
{
Selector
,
Property
,
Selector
,
Pseudo
,
Property
,
Selector
,
Quote
,
MaybeComment
,
Selector
},
// Selector
23
{
Property
,
Property
,
Selector
,
Value
,
Property
,
Property
,
Quote
,
MaybeComment
,
Property
},
// Property
24
{
Value
,
Property
,
Selector
,
Value
,
Property
,
Value
,
Quote
,
MaybeComment
,
Value
},
// Value
25
{
Pseudo1
,
Property
,
Selector
,
Pseudo2
,
Selector
,
Selector
,
Quote
,
MaybeComment
,
Pseudo
},
// Pseudo
26
{
Pseudo1
,
Property
,
Selector
,
Pseudo
,
Selector
,
Selector
,
Quote
,
MaybeComment
,
Pseudo1
},
// Pseudo1
27
{
Pseudo2
,
Property
,
Selector
,
Pseudo
,
Selector
,
Selector
,
Quote
,
MaybeComment
,
Pseudo2
},
// Pseudo2
28
{
Quote
,
Quote
,
Quote
,
Quote
,
Quote
,
Quote
, -1,
Quote
,
Quote
},
// Quote
29
{ -1, -1, -1, -1, -1, -1, -1, -1,
Comment
},
// MaybeComment
30
{
Comment
,
Comment
,
Comment
,
Comment
,
Comment
,
Comment
,
Comment
,
Comment
,
MaybeCommentEnd
},
// Comment
31
{
Comment
,
Comment
,
Comment
,
Comment
,
Comment
,
Comment
,
Comment
, -1,
MaybeCommentEnd
}
// MaybeCommentEnd
32
};
33
34
qsizetype
lastIndex
= 0;
35
bool
lastWasSlash
=
false
;
36
int
state
=
previousBlockState
(),
save_state
;
37
if
(
state
== -1) {
38
// As long as the text is empty, leave the state undetermined
39
if
(
text
.
isEmpty
()) {
40
setCurrentBlockState
(-1);
41
return
;
42
}
43
// The initial state is based on the precense of a : and the absense of a {.
44
// This is because Qt style sheets support both a full stylesheet as well as
45
// an inline form with just properties.
46
state
=
save_state
= (
text
.
indexOf
(u':') > -1 &&
47
text
.
indexOf
(u'{') == -1) ?
Property
:
Selector
;
48
}
else
{
49
save_state
=
state
>>16;
50
state
&= 0x00ff;
51
}
52
53
if
(
state
==
MaybeCommentEnd
) {
54
state
=
Comment
;
55
}
else
if
(
state
==
MaybeComment
) {
56
state
=
save_state
;
57
}
58
59
for
(
qsizetype
i
= 0;
i
<
text
.
size
(); ++
i
) {
60
int
token
=
ALNUM
;
61
const
QChar
c
=
text
.
at
(
i
);
62
const
char
a
=
c
.
toLatin1
();
63
64
if
(
state
==
Quote
) {
65
if
(
a
==
'\\'
) {
66
lastWasSlash
=
true
;
67
}
else
{
68
if
(
a
==
'\"'
&& !
lastWasSlash
) {
69
token
=
QUOTE
;
70
}
71
lastWasSlash
=
false
;
72
}
73
}
else
{
74
switch
(
a
) {
75
case
'{'
:
token
=
LBRACE
;
break
;
76
case
'}'
:
token
=
RBRACE
;
break
;
77
case
':'
:
token
=
COLON
;
break
;
78
case
';'
:
token
=
SEMICOLON
;
break
;
79
case
','
:
token
=
COMMA
;
break
;
80
case
'\"'
:
token
=
QUOTE
;
break
;
81
case
'/'
:
token
=
SLASH
;
break
;
82
case
'*'
:
token
=
STAR
;
break
;
83
default
:
break
;
84
}
85
}
86
87
int
new_state
=
transitions
[
state
][
token
];
88
89
if
(
new_state
!=
state
) {
90
bool
include_token
=
new_state
==
MaybeCommentEnd
|| (
state
==
MaybeCommentEnd
&&
new_state
!=
Comment
)
91
||
state
==
Quote
;
92
highlight
(
text
,
lastIndex
,
i
-
lastIndex
+
include_token
,
state
);
93
94
if
(
new_state
==
Comment
) {
95
lastIndex
=
i
-1;
// include the slash and star
96
}
else
{
97
lastIndex
=
i
+ ((
token
==
ALNUM
||
new_state
==
Quote
) ? 0 : 1);
98
}
99
}
100
101
if
(
new_state
== -1) {
102
state
=
save_state
;
103
}
else
if
(
state
<=
Pseudo2
) {
104
save_state
=
state
;
105
state
=
new_state
;
106
}
else
{
107
state
=
new_state
;
108
}
109
}
110
111
highlight
(
text
,
lastIndex
,
text
.
size
() -
lastIndex
,
state
);
112
setCurrentBlockState
(
state
+ (
save_state
<<16));
113
}
114
115
void
CssHighlighter
::
highlight
(
const
QString
&
text
,
int
start
,
int
length
,
int
state
)
116
{
117
if
(
start
>=
text
.
size
() ||
length
<= 0)
118
return
;
119
120
QTextCharFormat
format
;
121
122
switch
(
state
) {
123
case
Selector
:
124
setFormat
(
start
,
length
,
m_colors
.
selector
);
125
break
;
126
case
Property
:
127
setFormat
(
start
,
length
,
m_colors
.
property
);
128
break
;
129
case
Value
:
130
setFormat
(
start
,
length
,
m_colors
.
value
);
131
break
;
132
case
Pseudo1
:
133
setFormat
(
start
,
length
,
m_colors
.
pseudo1
);
134
break
;
135
case
Pseudo2
:
136
setFormat
(
start
,
length
,
m_colors
.
pseudo2
);
137
break
;
138
case
Quote
:
139
setFormat
(
start
,
length
,
m_colors
.
quote
);
140
break
;
141
case
Comment
:
142
case
MaybeCommentEnd
:
143
format
.
setForeground
(
m_colors
.
comment
);
144
setFormat
(
start
,
length
,
format
);
145
break
;
146
default
:
147
break
;
148
}
149
}
150
151
}
// namespace qdesigner_internal
152
153
QT_END_NAMESPACE
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qstandardpaths_haiku.cpp:21
qdesigner_internal
Auxiliary methods to store/retrieve settings.
Definition
buddyeditor.cpp:66
qttools
src
designer
src
lib
shared
csshighlighter.cpp
Generated on Sat Sep 21 2024 00:50:31 for Qt by
1.12.0