Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Education
Cantor
Commits
ed55e2c0
Commit
ed55e2c0
authored
Aug 02, 2022
by
Alexander Semke
Browse files
Minor code cleanup.
parent
6ebec938
Pipeline
#212368
passed with stage
in 11 minutes and 52 seconds
Changes
5
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
src/backends/octave/octavehighlighter.cpp
View file @
ed55e2c0
...
...
@@ -6,32 +6,28 @@
#include
"octavehighlighter.h"
#include
"octavekeywords.h"
#include
"result.h"
#include
<QDebug>
#include
<QStringList>
#include
"octavesession.h"
#include
<session.h>
using
namespace
Cantor
;
OctaveHighlighter
::
OctaveHighlighter
(
QObject
*
parent
,
Session
*
session
)
:
DefaultHighlighter
(
parent
,
session
)
{
addKeywords
(
OctaveKeywords
::
instance
()
->
keywords
());
addFunctions
(
OctaveKeywords
::
instance
()
->
functions
());
addKeywords
(
OctaveKeywords
::
instance
()
->
keywords
());
addFunctions
(
OctaveKeywords
::
instance
()
->
functions
());
QStringList
operators
;
operators
QStringList
operators
;
operators
<<
QLatin1String
(
"+"
)
<<
QLatin1String
(
"-"
)
<<
QLatin1String
(
"*"
)
<<
QLatin1String
(
"/"
)
<<
QLatin1String
(
".+"
)
<<
QLatin1String
(
".-"
)
<<
QLatin1String
(
".*"
)
<<
QLatin1String
(
"./"
)
<<
QLatin1String
(
"="
)
<<
QLatin1String
(
"or"
)
<<
QLatin1String
(
"and"
)
<<
QLatin1String
(
"xor"
)
<<
QLatin1String
(
"not"
)
<<
QLatin1String
(
"||"
)
<<
QLatin1String
(
"&&"
)
<<
QLatin1String
(
"=="
);
addRules
(
operators
,
operatorFormat
());
addRules
(
operators
,
operatorFormat
());
addRule
(
QRegularExpression
(
QStringLiteral
(
"
\"
[^
\"
]*
\"
"
)),
stringFormat
());
addRule
(
QRegularExpression
(
QStringLiteral
(
"'[^']*'"
)),
stringFormat
());
addRule
(
QRegularExpression
(
QStringLiteral
(
"
\"
[^
\"
]*
\"
"
)),
stringFormat
());
addRule
(
QRegularExpression
(
QStringLiteral
(
"'[^']*'"
)),
stringFormat
());
addRule
(
QRegularExpression
(
QStringLiteral
(
"#[^
\n
]*"
)),
commentFormat
());
addRule
(
QRegularExpression
(
QStringLiteral
(
"%[^
\n
]*"
)),
commentFormat
());
addRule
(
QRegularExpression
(
QStringLiteral
(
"#[^
\n
]*"
)),
commentFormat
());
addRule
(
QRegularExpression
(
QStringLiteral
(
"%[^
\n
]*"
)),
commentFormat
());
rehighlight
();
rehighlight
();
}
src/backends/octave/octavehighlighter.h
View file @
ed55e2c0
...
...
@@ -8,20 +8,20 @@
#define OCTAVEHIGHLIGHTER_H
#include
"defaulthighlighter.h"
#include
<session.h>
namespace
Cantor
{
class
Expression
;
class
Session
;
}
class
OctaveHighlighter
:
public
Cantor
::
DefaultHighlighter
{
Q_OBJECT
Q_OBJECT
public:
OctaveHighlighter
(
QObject
*
parent
,
Cantor
::
Session
*
session
);
~
OctaveHighlighter
()
override
=
default
;
public:
OctaveHighlighter
(
QObject
*
parent
,
Cantor
::
Session
*
session
);
~
OctaveHighlighter
()
override
=
default
;
};
#endif // OCTAVEHIGHLIGHTER_H
src/backends/python/pythonhighlighter.cpp
View file @
ed55e2c0
...
...
@@ -7,12 +7,8 @@
#include
"pythonkeywords.h"
#include
"pythonsession.h"
#include
<QTextEdit>
#include
<QDebug>
PythonHighlighter
::
PythonHighlighter
(
QObject
*
parent
,
PythonSession
*
session
)
:
Cantor
::
DefaultHighlighter
(
parent
,
session
)
{
qDebug
()
<<
"PythonHighlighter constructor"
;
addRule
(
QRegularExpression
(
QStringLiteral
(
"
\\
b
\\
w+(?=
\\
()"
)),
functionFormat
());
//Code highlighting the different keywords
...
...
@@ -23,9 +19,8 @@ PythonHighlighter::PythonHighlighter(QObject* parent, PythonSession* session) :
void
PythonHighlighter
::
highlightBlock
(
const
QString
&
text
)
{
if
(
skipHighlighting
(
text
))
{
if
(
skipHighlighting
(
text
))
return
;
}
// Do some backend independent highlighting (brackets etc.)
DefaultHighlighter
::
highlightBlock
(
text
);
...
...
src/lib/defaulthighlighter.cpp
View file @
ed55e2c0
...
...
@@ -5,17 +5,13 @@
*/
#include
"defaulthighlighter.h"
#include
"defaultvariablemodel.h"
#include
"session.h"
#include
<QApplication>
#include
<QLocale>
#include
<QTextDocument>
#include
<QTextCursor>
#include
<QGraphicsTextItem>
#include
<KColorScheme>
#include
<QDebug>
#include
<QStack>
using
namespace
Cantor
;
...
...
@@ -39,7 +35,6 @@ struct PairOpener {
int
type
;
};
class
Cantor
::
DefaultHighlighterPrivate
{
public:
...
...
@@ -58,9 +53,10 @@ class Cantor::DefaultHighlighterPrivate
QTextCharFormat
matchingPairFormat
;
QTextCharFormat
mismatchingPairFormat
;
int
lastBlockNumber
;
int
lastPosition
;
bool
suppressRuleChangedSignal
;
int
lastBlockNumber
=
-
1
;
int
lastPosition
=
-
1
;
bool
suppressRuleChangedSignal
=
false
;
// each two consecutive items build a pair
QList
<
QChar
>
pairs
;
...
...
@@ -68,15 +64,9 @@ class Cantor::DefaultHighlighterPrivate
QHash
<
QString
,
QTextCharFormat
>
wordRules
;
};
DefaultHighlighter
::
DefaultHighlighter
(
QObject
*
parent
)
:
QSyntaxHighlighter
(
parent
),
DefaultHighlighter
::
DefaultHighlighter
(
QObject
*
parent
)
:
QSyntaxHighlighter
(
parent
),
d
(
new
DefaultHighlighterPrivate
)
{
d
->
cursor
=
QTextCursor
();
d
->
lastBlockNumber
=-
1
;
d
->
lastPosition
=-
1
;
d
->
suppressRuleChangedSignal
=
false
;
addPair
(
QLatin1Char
(
'('
),
QLatin1Char
(
')'
));
addPair
(
QLatin1Char
(
'['
),
QLatin1Char
(
']'
));
addPair
(
QLatin1Char
(
'{'
),
QLatin1Char
(
'}'
));
...
...
@@ -91,7 +81,7 @@ DefaultHighlighter::DefaultHighlighter(QObject* parent, Session* session)
{
if
(
session
)
{
DefaultVariableModel
*
model
=
session
->
variableModel
();
auto
*
model
=
session
->
variableModel
();
if
(
model
)
{
connect
(
model
,
&
DefaultVariableModel
::
variablesAdded
,
this
,
&
DefaultHighlighter
::
addVariables
);
...
...
@@ -114,12 +104,14 @@ void DefaultHighlighter::setTextItem(QGraphicsTextItem* item)
{
d
->
cursor
=
item
->
textCursor
();
setDocument
(
item
->
document
());
// make sure every item is connected only once
item
->
disconnect
(
this
,
SLOT
(
positionChanged
(
QTextCursor
)));
// QGraphicsTextItem has no signal cursorPositionChanged, but item really
// is a WorksheetTextItem
connect
(
item
,
SIGNAL
(
cursorPositionChanged
(
QTextCursor
)),
this
,
SLOT
(
positionChanged
(
QTextCursor
)));
this
,
SLOT
(
positionChanged
(
QTextCursor
)));
d
->
lastBlockNumber
=
-
1
;
d
->
lastPosition
=
-
1
;
...
...
@@ -132,10 +124,7 @@ bool DefaultHighlighter::skipHighlighting(const QString& text)
void
DefaultHighlighter
::
highlightBlock
(
const
QString
&
text
)
{
//qDebug() << text;
const
QTextCursor
&
cursor
=
d
->
cursor
;
d
->
lastBlockNumber
=
cursor
.
blockNumber
();
d
->
lastBlockNumber
=
d
->
cursor
.
blockNumber
();
if
(
skipHighlighting
(
text
))
return
;
...
...
@@ -150,11 +139,9 @@ void DefaultHighlighter::addPair(QChar openSymbol, QChar closeSymbol)
Q_ASSERT
(
!
d
->
pairs
.
contains
(
closeSymbol
));
d
->
pairs
<<
openSymbol
<<
closeSymbol
;
}
void
DefaultHighlighter
::
highlightPairs
(
const
QString
&
text
)
{
//qDebug() << text;
const
QTextCursor
&
cursor
=
d
->
cursor
;
const
auto
&
cursor
=
d
->
cursor
;
int
cursorPos
=
-
1
;
if
(
cursor
.
blockNumber
()
==
currentBlock
().
blockNumber
()
)
{
cursorPos
=
cursor
.
position
()
-
currentBlock
().
position
();
...
...
@@ -166,34 +153,33 @@ void DefaultHighlighter::highlightPairs(const QString& text)
QStack
<
PairOpener
>
opened
;
for
(
int
i
=
0
;
i
<
text
.
size
();
++
i
)
{
int
idx
=
d
->
pairs
.
indexOf
(
text
[
i
]);
if
(
idx
==
-
1
)
continue
;
if
(
idx
%
2
==
0
)
{
//opener of a pair
opened
.
push
(
PairOpener
(
i
,
idx
));
}
else
if
(
opened
.
isEmpty
())
{
//closer with no previous opener
setFormat
(
i
,
1
,
errorFormat
());
}
else
if
(
opened
.
top
().
type
==
idx
-
1
)
{
//closer with matched opener
int
openPos
=
opened
.
pop
().
position
;
if
(
cursorPos
!=
-
1
&&
(
openPos
==
cursorPos
||
openPos
==
cursorPos
-
1
||
i
==
cursorPos
||
i
==
cursorPos
-
1
))
{
setFormat
(
openPos
,
1
,
matchingPairFormat
());
setFormat
(
i
,
1
,
matchingPairFormat
());
}
}
else
{
//closer with mismatching opener
int
openPos
=
opened
.
pop
().
position
;
setFormat
(
openPos
,
1
,
mismatchingPairFormat
());
setFormat
(
i
,
1
,
mismatchingPairFormat
());
}
int
idx
=
d
->
pairs
.
indexOf
(
text
[
i
]);
if
(
idx
==
-
1
)
continue
;
if
(
idx
%
2
==
0
)
{
//opener of a pair
opened
.
push
(
PairOpener
(
i
,
idx
));
}
else
if
(
opened
.
isEmpty
())
{
//closer with no previous opener
setFormat
(
i
,
1
,
errorFormat
());
}
else
if
(
opened
.
top
().
type
==
idx
-
1
)
{
//closer with matched opener
int
openPos
=
opened
.
pop
().
position
;
if
(
cursorPos
!=
-
1
&&
(
openPos
==
cursorPos
||
openPos
==
cursorPos
-
1
||
i
==
cursorPos
||
i
==
cursorPos
-
1
))
{
setFormat
(
openPos
,
1
,
matchingPairFormat
());
setFormat
(
i
,
1
,
matchingPairFormat
());
}
}
else
{
//closer with mismatching opener
int
openPos
=
opened
.
pop
().
position
;
setFormat
(
openPos
,
1
,
mismatchingPairFormat
());
setFormat
(
i
,
1
,
mismatchingPairFormat
());
}
}
// handled unterminated pairs
while
(
!
opened
.
isEmpty
())
{
int
position
=
opened
.
pop
().
position
;
setFormat
(
position
,
1
,
errorFormat
());
int
position
=
opened
.
pop
().
position
;
setFormat
(
position
,
1
,
errorFormat
());
}
}
QStringList
Cantor
::
DefaultHighlighter
::
parseBlockTextToWords
(
const
QString
&
text
)
...
...
@@ -259,11 +245,11 @@ void DefaultHighlighter::highlightWords(const QString& text)
void
DefaultHighlighter
::
highlightRegExps
(
const
QString
&
text
)
{
for
each
(
const
HighlightingRule
&
rule
,
d
->
regExpRules
)
for
(
const
auto
&
rule
:
d
->
regExpRules
)
{
QRegularExpressionMatchIter
ato
r
iter
=
rule
.
regExp
.
globalMatch
(
text
);
a
u
to
iter
=
rule
.
regExp
.
globalMatch
(
text
);
while
(
iter
.
hasNext
())
{
QRegularExpressionMatch
match
=
iter
.
next
();
auto
match
=
iter
.
next
();
setFormat
(
match
.
capturedStart
(
0
),
match
.
capturedLength
(
0
),
rule
.
format
);
}
}
...
...
@@ -364,21 +350,21 @@ void DefaultHighlighter::updateFormats()
void
DefaultHighlighter
::
positionChanged
(
const
QTextCursor
&
cursor
)
{
if
(
!
cursor
.
isNull
()
&&
cursor
.
document
()
!=
document
())
// A new item notified us, but we did not yet change our document.
// We are waiting for that to happen.
return
;
// A new item notified us, but we did not yet change our document.
// We are waiting for that to happen.
return
;
d
->
cursor
=
cursor
;
if
(
(
cursor
.
isNull
()
||
cursor
.
blockNumber
()
!=
d
->
lastBlockNumber
)
&&
d
->
lastBlockNumber
>=
0
)
{
d
->
lastBlockNumber
>=
0
)
{
// remove highlight from last focused block
rehighlightBlock
(
document
()
->
findBlockByNumber
(
d
->
lastBlockNumber
));
}
if
(
cursor
.
isNull
())
{
d
->
lastBlockNumber
=
-
1
;
d
->
lastPosition
=
-
1
;
return
;
d
->
lastBlockNumber
=
-
1
;
d
->
lastPosition
=
-
1
;
return
;
}
d
->
lastBlockNumber
=
cursor
.
blockNumber
();
...
...
@@ -424,10 +410,9 @@ void DefaultHighlighter::removeRule(const QRegularExpression& regexp)
void
DefaultHighlighter
::
addRules
(
const
QStringList
&
conditions
,
const
QTextCharFormat
&
format
)
{
typename
QStringList
::
const_iterator
i
=
conditions
.
constBegin
();
typename
QStringList
::
const_iterator
end
=
conditions
.
constEnd
();
auto
i
=
conditions
.
constBegin
();
d
->
suppressRuleChangedSignal
=
true
;
for
(;
i
!=
end
;
++
i
)
for
(;
i
!=
conditions
.
constEnd
()
;
++
i
)
{
addRule
(
*
i
,
format
);
}
...
...
@@ -452,10 +437,9 @@ void DefaultHighlighter::addVariables(const QStringList& variables)
void
DefaultHighlighter
::
removeRules
(
const
QStringList
&
conditions
)
{
typename
QStringList
::
const_iterator
i
=
conditions
.
constBegin
();
typename
QStringList
::
const_iterator
end
=
conditions
.
constEnd
();
auto
i
=
conditions
.
constBegin
();
d
->
suppressRuleChangedSignal
=
true
;
for
(;
i
!=
end
;
++
i
)
for
(;
i
!=
conditions
.
constEnd
()
;
++
i
)
{
removeRule
(
*
i
);
}
...
...
@@ -467,5 +451,3 @@ QString DefaultHighlighter::nonSeparatingCharacters() const
{
return
QString
();
}
src/worksheet.cpp
View file @
ed55e2c0
...
...
@@ -1021,31 +1021,33 @@ void Worksheet::rehighlight()
// highlight every entry
WorksheetEntry
*
entry
;
for
(
entry
=
firstEntry
();
entry
;
entry
=
entry
->
next
())
{
WorksheetTextItem
*
item
=
entry
->
highlightItem
();
auto
*
item
=
entry
->
highlightItem
();
if
(
!
item
)
continue
;
highlightItem
(
item
);
m_highlighter
->
rehighlight
();
}
entry
=
currentEntry
();
WorksheetTextItem
*
textitem
=
entry
?
entry
->
highlightItem
()
:
nullptr
;
auto
*
textitem
=
entry
?
entry
->
highlightItem
()
:
nullptr
;
if
(
textitem
&&
textitem
->
hasFocus
())
highlightItem
(
textitem
);
}
else
}
else
{
// remove highlighting from entries
WorksheetEntry
*
entry
;
for
(
entry
=
firstEntry
();
entry
;
entry
=
entry
->
next
())
{
WorksheetTextItem
*
item
=
entry
->
highlightItem
();
auto
*
item
=
entry
->
highlightItem
();
if
(
!
item
)
continue
;
QTextCursor
cursor
(
item
->
document
());
cursor
.
beginEditBlock
();
for
(
QTextBlock
b
=
item
->
document
()
->
firstBlock
();
b
.
isValid
();
b
=
b
.
next
())
{
for
(
auto
b
=
item
->
document
()
->
firstBlock
();
b
.
isValid
();
b
=
b
.
next
())
b
.
layout
()
->
clearFormats
();
}
cursor
.
endEditBlock
();
}
update
();
...
...
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!
Cancel
Please
register
or
sign in
to comment