31 #include <KColorScheme>
34 #include <QApplication>
36 #define STATE_NORMAL 0
41 #define NUM_OF_RULES 5
44 : Sonnet::Highlighter(parent)
45 , tagBrush(KColorScheme::View,KColorScheme::VisitedText)
53 HighlightingRule rule;
56 tagFormat.setForeground(tagBrush.brush(QApplication::palette()));
68 rule.format.setForeground(Qt::darkMagenta);
69 rule.pattern = QRegExp(
"(&[A-Za-z_:][A-Za-z0-9_\\.:-]*;)");
70 highlightingRules.append(rule);
75 rule.format.setForeground(Qt::darkMagenta);
76 rule.pattern = QRegExp(accel);
77 highlightingRules.append(rule);
81 rule.format.setForeground(Qt::darkGreen);
82 rule.pattern = QRegExp(
"(\\\\[abfnrtv'\?\\\\])|(\\\\\\d+)|(\\\\x[\\dabcdef]+)");
83 highlightingRules.append(rule);
95 void SyntaxHighlighter::settingsChanged()
97 QRegExp re(QString(
" +$|^ +|.?")+QChar(0x0000AD)+
".?");
100 KColorScheme colorScheme(QPalette::Normal);
101 HighlightingRule rule;
102 rule.format.clearForeground();
105 rule.format.setBackground(colorScheme.background(KColorScheme::AlternateBackground));
106 rule.pattern = QRegExp(QChar(0x00a0U));
107 highlightingRules.append(rule);
110 rule.format.setBackground(colorScheme.background(KColorScheme::ActiveBackground));
112 highlightingRules.append(rule);
117 highlightingRules.resize(highlightingRules.size()-2);
137 f.setFontItalic(!m_approved);
138 setFormat(0, text.length(), f);
140 tagFormat.setFontItalic(!m_approved);
145 startIndex = text.indexOf(
'<');
147 while (startIndex >= 0)
149 int endIndex = text.indexOf(
'>', startIndex);
154 commentLength = text.length() - startIndex;
158 commentLength = endIndex - startIndex
161 setFormat(startIndex, commentLength, tagFormat);
162 startIndex = text.indexOf(
'<', startIndex + commentLength);
166 foreach (
const HighlightingRule &rule, highlightingRules)
168 QRegExp expression(rule.pattern);
169 int index = expression.indexIn(text);
172 int length = expression.matchedLength();
173 QTextCharFormat f=rule.format;
174 f.setFontItalic(!m_approved);
175 setFormat(index, length, f);
176 index = expression.indexIn(text, index + length);
180 if (spellCheckerFound())
181 Sonnet::Highlighter::highlightBlock(text);
183 setCurrentBlockState(currentBlockState);
187 void SyntaxHighlighter::setFormatRetainingUnderlines(
int start,
int count, QTextCharFormat f)
189 QVector<bool> underLines(count);
190 for (
int i=0;i<count;++i)
191 underLines[i]=format(start+i).fontUnderline();
193 setFormat(start, count, f);
195 f.setFontUnderline(
true);
197 bool isPrevUnderLined=
false;
198 for (
int i=0;i<count;++i)
200 if (!underLines.at(i) && prevStart!=-1)
201 setFormat(start+isPrevUnderLined, i-prevStart, f);
202 else if (underLines.at(i)&&!isPrevUnderLined)
205 isPrevUnderLined=underLines.at(i);
212 QString text=currentBlock().text();
213 QString word=text.mid(start,count);
214 if (m_sourceString.contains(word))
219 if (!isWordMisspelled(word.remove(accel)))
223 bool smthPreceeding=(start>0) &&
224 (accel.endsWith(text.at(start-1))
225 || text.at(start-1)==QChar(0x0000AD)
231 kWarning()<<
"ampersand is in the way. word len:"<<count;
232 int realStart=text.lastIndexOf(QRegExp(
"\\b"),start-2);
235 QString t=text.mid(realStart, count+start-realStart);
237 t.remove(QChar(0x0000AD));
238 if (!isWordMisspelled(t))
242 bool smthAfter=(start+count+1<text.size()) &&
243 (accel.startsWith(text.at(start+count))
244 || text.at(start+count)==QChar(0x0000AD)
248 kWarning()<<
"smthAfter. ampersand is in the way. word len:"<<count;
249 int realEnd=text.indexOf(QRegExp(
"\\b"),start+count+2);
252 QString t=text.mid(start, realEnd-start);
254 t.remove(QChar(0x0000AD));
255 if (!isWordMisspelled(t))
259 if (count && format(start)==tagFormat)
262 for (
int i=0;i<count;++i)
264 QTextCharFormat f(format(start+i));
265 f.setFontUnderline(
true);
266 f.setUnderlineStyle(QTextCharFormat::SpellCheckUnderline);
267 f.setUnderlineColor(Qt::red);
268 setFormat(start+i, 1, f);
274 for (
int i=0;i<count;++i)
276 QTextCharFormat f(format(start+i));
277 f.setFontUnderline(
false);
278 setFormat(start+i, 1, f);
284 #include "syntaxhighlighter.moc"
static Project * instance()
SyntaxHighlighter(QTextEdit *parent)
QString accel() const
Get Accel.
void highlightBlock(const QString &text)
static SettingsController * instance()
static bool highlightSpaces()
Get HighlightSpaces.
void setMisspelled(int start, int count)
void unsetMisspelled(int start, int count)