Skip to content

Commit b09bbd3

Browse files
committed
fix: Use extra selection instead of changing current format and insert
The old method moves the window to make the squiggles visible when adding squiggles.
1 parent f3c534f commit b09bbd3

File tree

2 files changed

+12
-32
lines changed

2 files changed

+12
-32
lines changed

include/internal/QCodeEditor.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ class QCodeEditor : public QTextEdit
354354
bool m_autoRemoveParentheses;
355355
QString m_tabReplace;
356356

357-
QList<QTextEdit::ExtraSelection> extra1, extra2;
357+
QList<QTextEdit::ExtraSelection> extra1, extra2, extra_squiggles;
358358

359359
std::vector<SquiggleInformation> m_squiggler;
360360
};

src/internal/QCodeEditor.cpp

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ static QVector<QPair<QString, QString>> parentheses = {{"(", ")"}, {"{", "}"}, {
3030
QCodeEditor::QCodeEditor(QWidget *widget)
3131
: QTextEdit(widget), m_highlighter(nullptr), m_syntaxStyle(nullptr), m_lineNumberArea(new QLineNumberArea(this)),
3232
m_completer(nullptr), m_autoIndentation(true), m_autoParentheses(true), m_replaceTab(true),
33-
m_autoRemoveParentheses(true), m_tabReplace(QString(4, ' ')), extra1(), extra2()
33+
m_autoRemoveParentheses(true), m_tabReplace(QString(4, ' ')), extra1(), extra2(), extra_squiggles(), m_squiggler()
3434
{
3535
initFont();
3636
performConnections();
@@ -151,7 +151,7 @@ void QCodeEditor::updateExtraSelection1()
151151
highlightCurrentLine();
152152
highlightParenthesis();
153153

154-
setExtraSelections(extra1 + extra2);
154+
setExtraSelections(extra1 + extra2 + extra_squiggles);
155155
}
156156

157157
void QCodeEditor::updateExtraSelection2()
@@ -160,7 +160,7 @@ void QCodeEditor::updateExtraSelection2()
160160

161161
highlightOccurrences();
162162

163-
setExtraSelections(extra1 + extra2);
163+
setExtraSelections(extra1 + extra2 + extra_squiggles);
164164
}
165165

166166
void QCodeEditor::indent()
@@ -854,15 +854,13 @@ QCompleter *QCodeEditor::completer() const
854854

855855
void QCodeEditor::squiggle(SeverityLevel level, QPair<int, int> start, QPair<int, int> stop, QString tooltipMessage)
856856
{
857-
858857
if (stop < start)
859858
return;
860859

861-
SquiggleInformation info(start, stop, std::move(tooltipMessage));
860+
SquiggleInformation info(start, stop, tooltipMessage);
862861
m_squiggler.push_back(info);
863862

864-
auto originalCursor = textCursor(); // use to restore to original position
865-
auto cursor = textCursor(); // use to underline
863+
auto cursor = textCursor();
866864

867865
cursor.movePosition(QTextCursor::Start);
868866
cursor.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor, start.first - 1);
@@ -875,10 +873,7 @@ void QCodeEditor::squiggle(SeverityLevel level, QPair<int, int> start, QPair<int
875873
cursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::KeepAnchor);
876874
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, stop.second);
877875

878-
setTextCursor(cursor); // added
879-
QTextCharFormat defcharfmt = currentCharFormat();
880-
881-
QTextCharFormat newcharfmt = defcharfmt;
876+
QTextCharFormat newcharfmt = currentCharFormat();
882877
newcharfmt.setFontUnderline(true);
883878

884879
switch (level)
@@ -900,35 +895,20 @@ void QCodeEditor::squiggle(SeverityLevel level, QPair<int, int> start, QPair<int
900895
newcharfmt.setUnderlineStyle(QTextCharFormat::DotLine);
901896
}
902897

903-
setCurrentCharFormat(newcharfmt);
904-
cursor.movePosition(QTextCursor::NextCharacter);
905-
setTextCursor(cursor); // added
906-
setCurrentCharFormat(defcharfmt);
907-
setTextCursor(originalCursor);
908-
setFocus();
898+
extra_squiggles.push_back({cursor, newcharfmt});
909899

910-
if (m_highlighter != nullptr)
911-
m_highlighter->rehighlight();
900+
setExtraSelections(extra1 + extra2 + extra_squiggles);
912901
}
913902

914903
void QCodeEditor::clearSquiggle()
915904
{
916905
if (m_squiggler.empty())
917906
return;
918907

919-
auto originalCursor = textCursor();
920-
auto cursor = textCursor();
921-
922-
cursor.select(QTextCursor::SelectionType::Document);
923-
924-
setTextCursor(cursor);
925-
setCurrentCharFormat(QTextCharFormat());
926-
setTextCursor(originalCursor);
927-
928-
if (m_highlighter != nullptr)
929-
m_highlighter->rehighlight();
930-
931908
m_squiggler.clear();
909+
extra_squiggles.clear();
910+
911+
setExtraSelections(extra1 + extra2);
932912
}
933913

934914
QChar QCodeEditor::charUnderCursor(int offset) const

0 commit comments

Comments
 (0)