Skip to content

Commit eb6634c

Browse files
temp update
1 parent 0119294 commit eb6634c

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

src/SeerEditorWidgetSource.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,16 +140,20 @@ class SeerEditorWidgetSourceArea : public SeerPlainTextEdit {
140140
void showAlternateBar (bool flag);
141141
void showReloadBar (bool flag);
142142
void highlighterSettingsChanged ();
143+
void seekIdentifier (const QString& identifier);
143144

144145
public slots:
145146
void handleText (const QString& text);
146147
void handleHighlighterSettingsChanged ();
147148
void handleWatchFileModified (const QString& path);
148149
void handleBreakpointToolTip (QPoint pos, const QString& text);
150+
void handleSeekIdentifierF12 ();
149151

150152
protected:
151153
void resizeEvent (QResizeEvent* event);
152154
void contextMenuEvent (QContextMenuEvent* event);
155+
void mouseMoveEvent (QMouseEvent* event) override;
156+
void mousePressEvent (QMouseEvent* event) override;
153157
void mouseReleaseEvent (QMouseEvent* event);
154158
bool event (QEvent* event);
155159
void showExpressionTooltip ();
@@ -163,6 +167,11 @@ class SeerEditorWidgetSourceArea : public SeerPlainTextEdit {
163167
void updateBreakPointArea (const QRect& rect, int dy);
164168

165169
private:
170+
void updateCursor (const QPoint &pos);
171+
bool isOverWord (const QPoint &pos);
172+
QString wordUnderCursor (const QPoint &pos) const;
173+
bool isValidIdentifier (const QString& text);
174+
166175
QString _fullname;
167176
QString _file;
168177
QString _alternateDirectory;
@@ -194,6 +203,9 @@ class SeerEditorWidgetSourceArea : public SeerPlainTextEdit {
194203

195204
int _sourceTabSize;
196205
QString _externalEditorCommand;
206+
207+
bool _ctrlHeld = false;
208+
QString _wordUnderCursor;
197209
};
198210

199211
class SeerEditorWidgetSourceLineNumberArea : public QWidget {

src/SeerEditorWidgetSourceAreas.cpp

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2025,3 +2025,100 @@ void SeerEditorWidgetSourceBreakPointArea::mouseReleaseEvent (QMouseEvent* event
20252025
QWidget::mouseReleaseEvent(event);
20262026
}
20272027

2028+
/***********************************************************************************************************************
2029+
* Go to definition feature *
2030+
**********************************************************************************************************************/
2031+
bool SeerEditorWidgetSourceArea::isOverWord(const QPoint &pos)
2032+
{
2033+
QTextCursor cursor = cursorForPosition(pos);
2034+
cursor.select(QTextCursor::WordUnderCursor);
2035+
return !cursor.selectedText().isEmpty();
2036+
}
2037+
2038+
QString SeerEditorWidgetSourceArea::wordUnderCursor(const QPoint &pos) const
2039+
{
2040+
QTextCursor cursor = cursorForPosition(pos);
2041+
cursor.select(QTextCursor::WordUnderCursor);
2042+
return cursor.selectedText();
2043+
}
2044+
2045+
void SeerEditorWidgetSourceArea::updateCursor(const QPoint &pos)
2046+
{
2047+
_ctrlHeld = QApplication::keyboardModifiers() & Qt::ControlModifier;
2048+
if (_ctrlHeld && isOverWord(pos)) {
2049+
QApplication::setOverrideCursor(Qt::PointingHandCursor);
2050+
_wordUnderCursor = wordUnderCursor(pos);
2051+
} else {
2052+
QApplication::restoreOverrideCursor();
2053+
}
2054+
}
2055+
2056+
void SeerEditorWidgetSourceArea::mouseMoveEvent(QMouseEvent *event)
2057+
{
2058+
updateCursor(event->pos());
2059+
QPlainTextEdit::mouseMoveEvent(event);
2060+
}
2061+
2062+
// Check text and decide if that text is valid identifier (function, variable, type name)
2063+
bool SeerEditorWidgetSourceArea::isValidIdentifier(const QString& text)
2064+
{
2065+
static const QSet<QString> keywords = {
2066+
// Add your C and C++ keywords as QString literals here
2067+
"auto", "break", "case", "char", "const", "continue", "default", "do", "double",
2068+
"else", "enum", "extern", "float", "for", "goto", "if", "inline", "int", "long",
2069+
"register", "restrict", "return", "short", "signed", "sizeof", "static", "struct",
2070+
"switch", "typedef", "union", "unsigned", "void", "volatile", "while", "_Alignas",
2071+
"_Alignof", "_Atomic", "_Bool", "_Complex", "_Generic", "_Imaginary", "_Noreturn",
2072+
"_Static_assert", "_Thread_local",
2073+
2074+
"alignas", "alignof", "and", "and_eq", "asm", "bitand", "bitor", "bool", "catch",
2075+
"char16_t", "char32_t", "class", "compl", "const_cast", "constexpr", "decltype",
2076+
"delete", "dynamic_cast", "explicit", "export", "false", "friend", "mutable", "namespace",
2077+
"new", "noexcept", "not", "not_eq", "nullptr", "operator", "or", "or_eq", "private",
2078+
"protected", "public", "reinterpret_cast", "static_assert", "static_cast", "template",
2079+
"this", "thread_local", "throw", "true", "try", "typeid", "typename", "using",
2080+
"virtual", "wchar_t", "xor", "xor_eq"
2081+
};
2082+
2083+
if (text.isEmpty())
2084+
return false;
2085+
2086+
if (keywords.contains(text))
2087+
return false;
2088+
2089+
QChar firstChar = text[0];
2090+
if (!firstChar.isLetter() && firstChar != '_')
2091+
return false;
2092+
2093+
for (int i = 1; i < text.size(); ++i) {
2094+
QChar ch = text[i];
2095+
if (!ch.isLetterOrNumber() && ch != '_')
2096+
return false;
2097+
}
2098+
2099+
return true;
2100+
}
2101+
2102+
// When Ctrl is hold and left mouse is clicked, and cursor is pointing at a word, try to look for that word
2103+
void SeerEditorWidgetSourceArea::mousePressEvent(QMouseEvent *event)
2104+
{
2105+
if (event->button() == Qt::LeftButton && _wordUnderCursor != "" && _ctrlHeld) {
2106+
if (isValidIdentifier(_wordUnderCursor))
2107+
{
2108+
emit seekIdentifier(_wordUnderCursor);
2109+
}
2110+
}
2111+
QPlainTextEdit::mousePressEvent(event);
2112+
}
2113+
2114+
// When F12 is pressed, try to look for the word under cursor, if it's a valid identifier then emit seekIdentifier signal
2115+
void SeerEditorWidgetSourceArea::handleSeekIdentifierF12()
2116+
{
2117+
QTextCursor cursor = textCursor();
2118+
cursor.select(QTextCursor::WordUnderCursor);
2119+
QString wordUnderCursor = cursor.selectedText();
2120+
if (isValidIdentifier(wordUnderCursor))
2121+
{
2122+
emit seekIdentifier(wordUnderCursor);
2123+
}
2124+
}

0 commit comments

Comments
 (0)