@@ -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