Skip to content

Commit 0e60d61

Browse files
committed
Fix(clearSquiggle failing on some cases)
In some cases the clear function failed because the stored start and stop position of underline changes while not being updated in editor. Now we select all document and apply default textFormat to clear the underlines. It is much faster and better. squiggle Function has been improved too, it now handles all (Valid) ranges that LSP will send to highlight.
1 parent 8c0d19e commit 0e60d61

File tree

3 files changed

+21
-52
lines changed

3 files changed

+21
-52
lines changed

example/src/MainWindow.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,11 @@ void MainWindow::setupWidgets()
178178
m_codeEditor->setCompleter (m_completers[0].second);
179179
m_codeEditor->setHighlighter(new QCXXHighlighter);
180180

181-
m_codeEditor->squiggle(QCodeEditor::SeverityLevel::Warning, {3,2}, {13,5}, "unused variable");
182-
m_codeEditor->squiggle(QCodeEditor::SeverityLevel::Error, {3,5}, {3,7}, "Big error");
181+
// m_codeEditor->squiggle(QCodeEditor::SeverityLevel::Warning, {3,2}, {13,5}, "unused variable");
182+
m_codeEditor->squiggle(QCodeEditor::SeverityLevel::Error, {7,0}, {8,0}, "Big error");
183183

184184

185-
m_codeEditor->clearSquiggle();
185+
//m_codeEditor->clearSquiggle();
186186

187187
QStringList list;
188188
// Code samples

include/internal/QCodeEditor.hpp

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -126,21 +126,14 @@ class QCodeEditor : public QTextEdit
126126
QCompleter *completer() const;
127127

128128
/**
129-
* @brief Method to put a squiggly line (underline) for error/warning/information in code editor.
130-
* @param level The severity of the squiggle, determines the color of squiggle.
131-
* @param lineNumber The line number to squiggle.
132-
* @param startSquiggle The start position in the line where squiggly line begins.
133-
* @param stopSquiggle The stop position in the line where squiggly line stops.
134-
* @param tooltipMessage The tooltip message to show when the squiggle is under cursor.
129+
* @brief squiggle Puts a underline squiggle under text ranges in Editor
130+
* @param level defines the color of the underline depending upon the severity
131+
* @param tooltipMessage The tooltip hover message to show when over selection.
132+
* @note QPair<int, int>: first -> Line number in 1-based indexing
133+
* second -> Character number in 0-based indexing
135134
*/
136135
void squiggle(SeverityLevel level, QPair<int,int>, QPair<int, int>, QString tooltipMessage);
137136

138-
/**
139-
* @brief eraseSquiggle, Undo what was done by the function call `squiggle`.
140-
* @note This function will erase all squiggles from a line number.
141-
* @param lineNumber The line number to undo squiggle and tooltip from.
142-
*/
143-
void eraseSquiggle(int lineNumber);
144137

145138
/**
146139
* @brief clearSquiggle, Clears complete squiggle from editor
@@ -349,8 +342,6 @@ class QCodeEditor : public QTextEdit
349342
QPair<int,int> m_startPos;
350343
QPair<int,int> m_stopPos;
351344
QString m_tooltipText;
352-
QTextCharFormat originalFormat;
353-
354345
};
355346

356347
QStyleSyntaxHighlighter *m_highlighter;

src/internal/QCodeEditor.cpp

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,7 @@ QCompleter *QCodeEditor::completer() const
852852
void QCodeEditor::squiggle(SeverityLevel level, QPair<int, int> start, QPair<int, int> stop,
853853
QString tooltipMessage)
854854
{
855+
855856
if(stop < start) return;
856857

857858
SquiggleInformation info(start, stop, std::move(tooltipMessage));
@@ -864,22 +865,16 @@ void QCodeEditor::squiggle(SeverityLevel level, QPair<int, int> start, QPair<int
864865
cursor.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor, start.first - 1);
865866
cursor.movePosition(QTextCursor::StartOfBlock);
866867
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::MoveAnchor, start.second);
867-
int currentLine = start.first;
868868

869-
while(currentLine != stop.first)
870-
{
871-
cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
872-
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor);
873-
currentLine++;
874-
}
869+
if(stop.first > start.first)
870+
cursor.movePosition(QTextCursor::NextBlock, QTextCursor::KeepAnchor, stop.first - start.first);
875871

876-
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, stop.second - 1);
872+
cursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::KeepAnchor);
873+
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, stop.second);
877874

878875
setTextCursor(cursor); // added
879876
QTextCharFormat defcharfmt = currentCharFormat();
880877

881-
m_squiggler[m_squiggler.size()-1].originalFormat = defcharfmt;
882-
883878
QTextCharFormat newcharfmt = defcharfmt;
884879
newcharfmt.setFontUnderline(true);
885880

@@ -918,36 +913,19 @@ void QCodeEditor::clearSquiggle()
918913
if(m_squiggler.empty()) return;
919914

920915
auto originalCursor = textCursor();
921-
std::reverse(m_squiggler.begin(), m_squiggler.end());
922-
for(const auto& e : m_squiggler)
923-
{
924-
QPair<int, int> start;
925-
QPair<int, int> stop;
926-
start = e.m_startPos;
927-
stop = e.m_stopPos;
916+
auto cursor = textCursor();
928917

929-
auto originalCursor = textCursor(); // use to restore to original position
930-
auto cursor = textCursor(); // use to underline
918+
cursor.select(QTextCursor::SelectionType::Document);
931919

932-
cursor.movePosition(QTextCursor::Start);
933-
cursor.movePosition(QTextCursor::NextBlock, QTextCursor::MoveAnchor, start.first - 1);
934-
cursor.movePosition(QTextCursor::StartOfBlock);
935-
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::MoveAnchor, start.second);
936-
int currentLine = start.first;
920+
setTextCursor(cursor);
921+
setCurrentCharFormat(QTextCharFormat());
922+
setTextCursor(originalCursor);
937923

938-
while(currentLine != stop.first)
939-
{
940-
cursor.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor);
941-
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor);
942-
currentLine++;
943-
}
944924

945-
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, stop.second - 1);
946-
setTextCursor(cursor); // added
947-
setCurrentCharFormat(e.originalFormat); // restore original format
948-
setTextCursor(originalCursor);
949925

950-
}
926+
if (m_highlighter != nullptr)
927+
m_highlighter->rehighlight();
928+
951929
m_squiggler.clear();
952930

953931
}

0 commit comments

Comments
 (0)