Skip to content
GitLab
Explore
Projects
Groups
Topics
Snippets
Projects
Groups
Topics
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Register
Sign in
Toggle navigation
Menu
Осовик Полина Александровна
Lab34editor
Commits
849d6e21
Commit
849d6e21
authored
4 years ago
by
Осовик Полина Александровна
Browse files
Options
Download
Patches
Plain Diff
Upload New File
parent
c94c1ab9
master
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
highlighter.cpp
+75
-0
highlighter.cpp
with
75 additions
and
0 deletions
+75
-0
highlighter.cpp
0 → 100644
+
75
−
0
View file @
849d6e21
#include
"highlighter.h"
Highlighter
::
Highlighter
(
QFile
*
xml
,
QString
lang
,
QTextDocument
*
parent
)
:
QSyntaxHighlighter
(
parent
)
{
HighlightingRule
rule
;
xml
->
open
(
QIODevice
::
ReadOnly
);
QXmlStreamReader
sr
(
xml
);
bool
valid
=
false
;
do
{
sr
.
readNext
();
if
(
sr
.
name
()
==
"lang"
&&
sr
.
isStartElement
()
&&
!
valid
)
{
qDebug
()
<<
sr
.
attributes
().
at
(
0
).
value
().
toString
();
if
(
sr
.
attributes
().
at
(
0
).
value
().
toString
()
==
lang
)
{
qDebug
()
<<
sr
.
attributes
().
at
(
0
).
value
().
toString
();
valid
=
true
;
}
}
else
if
(
sr
.
name
()
==
"lang"
&&
sr
.
isEndElement
()
&&
valid
)
{
valid
=
false
;
}
else
if
(
!
valid
)
{
continue
;
}
else
if
(
sr
.
name
()
==
"regexp"
&&
sr
.
isStartElement
())
{
sr
.
readNext
();
rule
.
pattern
.
setPattern
(
sr
.
text
().
toString
());
qDebug
()
<<
"REGEXP: "
+
sr
.
text
();
}
else
if
(
sr
.
name
()
==
"color"
&&
sr
.
isStartElement
())
{
sr
.
readNext
();
rule
.
format
.
setForeground
(
QColor
(
sr
.
text
().
toString
()));
highlightingRules
.
append
(
rule
);
qDebug
()
<<
"COLOR: "
+
sr
.
text
();
}
}
while
(
!
sr
.
atEnd
());
qDebug
()
<<
highlightingRules
.
count
();
}
void
Highlighter
::
highlightBlock
(
const
QString
&
text
)
{
foreach
(
const
HighlightingRule
&
rule
,
highlightingRules
)
{
QRegularExpressionMatchIterator
matchIterator
=
rule
.
pattern
.
globalMatch
(
text
);
while
(
matchIterator
.
hasNext
())
{
QRegularExpressionMatch
match
=
matchIterator
.
next
();
setFormat
(
match
.
capturedStart
(),
match
.
capturedLength
(),
rule
.
format
);
}
}
setCurrentBlockState
(
0
);
QTextCharFormat
multiLineCommentFormat
;
multiLineCommentFormat
.
setForeground
(
Qt
::
red
);
int
startIndex
=
0
;
if
(
previousBlockState
()
!=
1
)
startIndex
=
text
.
indexOf
(
QRegularExpression
(
"/
\\
*"
));
while
(
startIndex
>=
0
)
{
QRegularExpressionMatch
match
=
QRegularExpression
(
"
\\
*/"
).
match
(
text
,
startIndex
);
int
endIndex
=
match
.
capturedStart
();
int
commentLength
=
0
;
if
(
endIndex
==
-
1
)
{
setCurrentBlockState
(
1
);
commentLength
=
text
.
length
()
-
startIndex
;
}
else
{
commentLength
=
endIndex
-
startIndex
+
match
.
capturedLength
();
}
setFormat
(
startIndex
,
commentLength
,
multiLineCommentFormat
);
startIndex
=
text
.
indexOf
(
QRegularExpression
(
"/
\\
*"
),
startIndex
+
commentLength
);
}
}
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment
Menu
Explore
Projects
Groups
Topics
Snippets