- Fehler in mehrzeiligen Kommentaren behoben
- Klammerpaare werden farblich hervorgehoben - Beim einrücken oder ausrücken eines markierten Blocks wird die Zeile, in der der Coursor steht, nicht mehr mitgerückt - Beim eintippen von Variablen wird eine Liste mit bereits deklarierten Variablen angezeigt
This commit is contained in:
@@ -29,6 +29,8 @@ set(EDITOR_SOURCES
|
||||
DeadCodeAnalyzer.h
|
||||
DeadCodeDialog.cpp
|
||||
DeadCodeDialog.h
|
||||
VariableCompleter.cpp
|
||||
VariableCompleter.h
|
||||
)
|
||||
|
||||
add_library(BareCode_Editor STATIC ${EDITOR_SOURCES})
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "ColorIndicator.h"
|
||||
#include "SignatureHelper.h"
|
||||
#include "FunctionIndex.h"
|
||||
#include "VariableCompleter.h"
|
||||
|
||||
#include "core/Settings.h"
|
||||
#include "highlighter/HighlighterFactory.h"
|
||||
@@ -27,6 +28,7 @@ CodeEditor::CodeEditor(Settings *settings, QWidget *parent)
|
||||
m_lineNumberArea = new LineNumberArea(this);
|
||||
m_colorIndicator = new ColorIndicator(this);
|
||||
m_signatureHelper = new SignatureHelper(this);
|
||||
m_varCompleter = new VariableCompleter(this);
|
||||
setupEditor();
|
||||
|
||||
connect(this, &CodeEditor::blockCountChanged,
|
||||
@@ -390,7 +392,7 @@ void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Current line highlight
|
||||
// Current line highlight + Klammerzugehörigkeit
|
||||
// ---------------------------------------------------------------------------
|
||||
void CodeEditor::highlightCurrentLine()
|
||||
{
|
||||
@@ -398,20 +400,161 @@ void CodeEditor::highlightCurrentLine()
|
||||
|
||||
if (!isReadOnly())
|
||||
{
|
||||
QTextEdit::ExtraSelection selection;
|
||||
// Aktuelle Zeile hervorheben
|
||||
QTextEdit::ExtraSelection lineSelection;
|
||||
lineSelection.format.setBackground(palette().color(QPalette::AlternateBase));
|
||||
lineSelection.format.setProperty(QTextFormat::FullWidthSelection, true);
|
||||
lineSelection.cursor = textCursor();
|
||||
lineSelection.cursor.clearSelection();
|
||||
extraSelections.append(lineSelection);
|
||||
|
||||
const QColor lineColor = palette().color(QPalette::AlternateBase);
|
||||
selection.format.setBackground(lineColor);
|
||||
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
|
||||
selection.cursor = textCursor();
|
||||
selection.cursor.clearSelection();
|
||||
|
||||
extraSelections.append(selection);
|
||||
// Klammerzugehörigkeit
|
||||
matchBrackets(extraSelections);
|
||||
}
|
||||
|
||||
setExtraSelections(extraSelections);
|
||||
}
|
||||
|
||||
void CodeEditor::matchBrackets(QList<QTextEdit::ExtraSelection> &selections)
|
||||
{
|
||||
static const QString openBrackets = "({[";
|
||||
static const QString closeBrackets = ")}]";
|
||||
|
||||
QTextCursor cursor = textCursor();
|
||||
const QString blockText = cursor.block().text();
|
||||
const int col = cursor.columnNumber();
|
||||
|
||||
// Zeichen unter oder links vom Cursor prüfen
|
||||
QChar ch;
|
||||
int charPos = -1;
|
||||
|
||||
// Zuerst Zeichen unter dem Cursor
|
||||
if (col < blockText.length())
|
||||
{
|
||||
ch = blockText[col];
|
||||
if (openBrackets.contains(ch) || closeBrackets.contains(ch))
|
||||
{
|
||||
charPos = col;
|
||||
}
|
||||
}
|
||||
|
||||
// Dann Zeichen links vom Cursor
|
||||
if (charPos == -1 && col > 0)
|
||||
{
|
||||
ch = blockText[col - 1];
|
||||
if (openBrackets.contains(ch) || closeBrackets.contains(ch))
|
||||
{
|
||||
charPos = col - 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (charPos == -1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Passende Klammer suchen
|
||||
const bool isOpen = openBrackets.contains(ch);
|
||||
const int bracketIndex = isOpen
|
||||
? openBrackets.indexOf(ch)
|
||||
: closeBrackets.indexOf(ch);
|
||||
const QChar matchChar = isOpen
|
||||
? closeBrackets[bracketIndex]
|
||||
: openBrackets[bracketIndex];
|
||||
|
||||
// Dokumentposition der gefundenen Klammer
|
||||
const int startPos = cursor.block().position() + charPos;
|
||||
|
||||
// Passende Klammer suchen — vorwärts oder rückwärts
|
||||
int depth = 0;
|
||||
int matchPos = -1;
|
||||
|
||||
if (isOpen)
|
||||
{
|
||||
// Vorwärts suchen
|
||||
QTextCursor search(document());
|
||||
search.setPosition(startPos);
|
||||
|
||||
while (!search.atEnd())
|
||||
{
|
||||
search.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor);
|
||||
const QChar c = search.selectedText()[0];
|
||||
search.clearSelection();
|
||||
|
||||
if (c == ch) { ++depth; }
|
||||
else if (c == matchChar)
|
||||
{
|
||||
--depth;
|
||||
if (depth == 0)
|
||||
{
|
||||
matchPos = search.position() - 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Rückwärts suchen
|
||||
QTextCursor search(document());
|
||||
search.setPosition(startPos + 1);
|
||||
|
||||
while (search.position() > 0)
|
||||
{
|
||||
search.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor);
|
||||
const QChar c = search.selectedText()[0];
|
||||
search.clearSelection();
|
||||
|
||||
if (c == ch) { ++depth; }
|
||||
else if (c == matchChar)
|
||||
{
|
||||
--depth;
|
||||
if (depth == 0)
|
||||
{
|
||||
matchPos = search.position();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Formatierung
|
||||
QTextCharFormat matchFormat;
|
||||
if (matchPos >= 0)
|
||||
{
|
||||
// Gefunden — beide Klammern grün hervorheben
|
||||
matchFormat.setBackground(QColor("#1a5a1a"));
|
||||
matchFormat.setForeground(QColor("#88ff88"));
|
||||
matchFormat.setFontWeight(QFont::Bold);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Kein Match — rot markieren
|
||||
matchFormat.setBackground(QColor("#5a1a1a"));
|
||||
matchFormat.setForeground(QColor("#ff8888"));
|
||||
matchFormat.setFontWeight(QFont::Bold);
|
||||
}
|
||||
|
||||
// Öffnende / schließende Klammer markieren
|
||||
QTextEdit::ExtraSelection sel1;
|
||||
sel1.cursor = QTextCursor(document());
|
||||
sel1.cursor.setPosition(startPos);
|
||||
sel1.cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor);
|
||||
sel1.format = matchFormat;
|
||||
selections.append(sel1);
|
||||
|
||||
// Passende Klammer markieren (nur wenn gefunden)
|
||||
if (matchPos >= 0)
|
||||
{
|
||||
QTextEdit::ExtraSelection sel2;
|
||||
sel2.cursor = QTextCursor(document());
|
||||
sel2.cursor.setPosition(matchPos);
|
||||
sel2.cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor);
|
||||
sel2.format = matchFormat;
|
||||
selections.append(sel2);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Key handling – Tab/Shift+Tab, Smart-Backspace, Auto-Indent
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -430,6 +573,16 @@ void CodeEditor::keyPressEvent(QKeyEvent *event)
|
||||
QTextBlock startBlock = document()->findBlock(cursor.selectionStart());
|
||||
QTextBlock endBlock = document()->findBlock(cursor.selectionEnd());
|
||||
|
||||
// Wenn die Selektion genau am Anfang des letzten Blocks endet,
|
||||
// diesen Block nicht mit einbeziehen — der Cursor steht dort nur
|
||||
// mit Position 0, der Nutzer hat die Zeile nicht markiert
|
||||
if (cursor.hasSelection() &&
|
||||
cursor.selectionEnd() == endBlock.position() &&
|
||||
endBlock != startBlock)
|
||||
{
|
||||
endBlock = endBlock.previous();
|
||||
}
|
||||
|
||||
cursor.beginEditBlock();
|
||||
for (QTextBlock b = startBlock; b != endBlock.next(); b = b.next())
|
||||
{
|
||||
@@ -438,22 +591,14 @@ void CodeEditor::keyPressEvent(QKeyEvent *event)
|
||||
|
||||
if (m_settings->useSpacesForTabs())
|
||||
{
|
||||
// Bis zu tabSize führende Leerzeichen entfernen
|
||||
for (int i = 0; i < tabSize && i < lineText.length(); ++i)
|
||||
{
|
||||
if (lineText[i] == ' ')
|
||||
{
|
||||
++toRemove;
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
if (lineText[i] == ' ') { ++toRemove; }
|
||||
else { break; }
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Einen führenden Tab entfernen
|
||||
if (!lineText.isEmpty() && lineText[0] == '\t')
|
||||
{
|
||||
toRemove = 1;
|
||||
@@ -483,10 +628,16 @@ void CodeEditor::keyPressEvent(QKeyEvent *event)
|
||||
|
||||
if (cursor.hasSelection())
|
||||
{
|
||||
// Mehrere Zeilen einrücken
|
||||
QTextBlock startBlock = document()->findBlock(cursor.selectionStart());
|
||||
QTextBlock endBlock = document()->findBlock(cursor.selectionEnd());
|
||||
|
||||
// Gleiche Korrektur: Cursor am Zeilenanfang → Zeile nicht einrücken
|
||||
if (cursor.selectionEnd() == endBlock.position() &&
|
||||
endBlock != startBlock)
|
||||
{
|
||||
endBlock = endBlock.previous();
|
||||
}
|
||||
|
||||
cursor.beginEditBlock();
|
||||
for (QTextBlock b = startBlock; b != endBlock.next(); b = b.next())
|
||||
{
|
||||
@@ -591,5 +742,8 @@ void CodeEditor::keyPressEvent(QKeyEvent *event)
|
||||
}
|
||||
|
||||
QPlainTextEdit::keyPressEvent(event);
|
||||
|
||||
// Variablen-Popup nach jedem Tastendruck aktualisieren
|
||||
m_varCompleter->handleKeyPress(event);
|
||||
}
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ class SyntaxHighlighter;
|
||||
class ColorIndicator;
|
||||
class SignatureHelper;
|
||||
class FunctionIndex;
|
||||
class VariableCompleter;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// CodeEditor – Core editing widget.
|
||||
@@ -55,8 +56,7 @@ signals:
|
||||
void fileSaved(const QString &filePath);
|
||||
void navigateToRequested(const QString &filePath, int line);
|
||||
|
||||
protected:
|
||||
void resizeEvent(QResizeEvent *event) override;
|
||||
protected: void resizeEvent(QResizeEvent *event) override;
|
||||
void keyPressEvent(QKeyEvent *event) override;
|
||||
void paintEvent(QPaintEvent *event) override;
|
||||
void mousePressEvent(QMouseEvent *event) override;
|
||||
@@ -71,6 +71,7 @@ private:
|
||||
void setupEditor();
|
||||
void installHighlighter(const QString &filePath);
|
||||
bool writeToFile(const QString &filePath);
|
||||
void matchBrackets(QList<QTextEdit::ExtraSelection> &selections);
|
||||
|
||||
Settings *m_settings = nullptr;
|
||||
LineNumberArea *m_lineNumberArea = nullptr;
|
||||
@@ -78,5 +79,6 @@ private:
|
||||
ColorIndicator *m_colorIndicator = nullptr;
|
||||
SignatureHelper *m_signatureHelper = nullptr;
|
||||
FunctionIndex *m_functionIndex = nullptr;
|
||||
VariableCompleter *m_varCompleter = nullptr;
|
||||
QString m_filePath;
|
||||
};
|
||||
|
||||
243
src/editor/VariableCompleter.cpp
Normal file
243
src/editor/VariableCompleter.cpp
Normal file
@@ -0,0 +1,243 @@
|
||||
#include "VariableCompleter.h"
|
||||
#include "CodeEditor.h"
|
||||
|
||||
#include <QTextCursor>
|
||||
#include <QTextBlock>
|
||||
#include <QKeyEvent>
|
||||
#include <QListWidgetItem>
|
||||
#include <QAbstractItemView>
|
||||
#include <QScrollBar>
|
||||
#include <QApplication>
|
||||
#include <QScreen>
|
||||
|
||||
VariableCompleter::VariableCompleter(CodeEditor *editor)
|
||||
: QObject(editor)
|
||||
, m_editor(editor)
|
||||
{
|
||||
// Popup als eigenständiges, rahmenloses Werkzeugfenster
|
||||
m_popup = new QListWidget(nullptr);
|
||||
m_popup->setWindowFlags(Qt::Tool | Qt::FramelessWindowHint |
|
||||
Qt::WindowStaysOnTopHint);
|
||||
m_popup->setAttribute(Qt::WA_ShowWithoutActivating);
|
||||
m_popup->setFocusPolicy(Qt::NoFocus);
|
||||
m_popup->setMaximumHeight(200);
|
||||
m_popup->setMinimumWidth(180);
|
||||
m_popup->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
||||
m_popup->setStyleSheet(
|
||||
"QListWidget {"
|
||||
" border: 1px solid palette(mid);"
|
||||
" background: palette(toolTipBase);"
|
||||
" color: palette(toolTipText);"
|
||||
" font-family: Monospace;"
|
||||
"}"
|
||||
"QListWidget::item:selected {"
|
||||
" background: palette(highlight);"
|
||||
" color: palette(highlightedText);"
|
||||
"}"
|
||||
);
|
||||
|
||||
connect(m_popup, &QListWidget::itemActivated,
|
||||
this, &VariableCompleter::onItemActivated);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Nach jedem Tastendruck aufrufen
|
||||
// ---------------------------------------------------------------------------
|
||||
void VariableCompleter::handleKeyPress(QKeyEvent *event)
|
||||
{
|
||||
// Popup-Navigation
|
||||
if (m_popup->isVisible())
|
||||
{
|
||||
if (event->key() == Qt::Key_Escape)
|
||||
{
|
||||
hidePopup();
|
||||
return;
|
||||
}
|
||||
if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter)
|
||||
{
|
||||
if (m_popup->currentItem())
|
||||
{
|
||||
onItemActivated(m_popup->currentItem());
|
||||
}
|
||||
hidePopup();
|
||||
return;
|
||||
}
|
||||
if (event->key() == Qt::Key_Down)
|
||||
{
|
||||
const int next = qMin(m_popup->currentRow() + 1,
|
||||
m_popup->count() - 1);
|
||||
m_popup->setCurrentRow(next);
|
||||
return;
|
||||
}
|
||||
if (event->key() == Qt::Key_Up)
|
||||
{
|
||||
const int prev = qMax(m_popup->currentRow() - 1, 0);
|
||||
m_popup->setCurrentRow(prev);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Nach dem normalen Tastendruck Popup aktualisieren
|
||||
// (der Event wurde bereits an QPlainTextEdit weitergegeben)
|
||||
updatePopup();
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Popup aktualisieren
|
||||
// ---------------------------------------------------------------------------
|
||||
void VariableCompleter::updatePopup()
|
||||
{
|
||||
const QString prefix = currentPrefix();
|
||||
|
||||
// Nur bei $ und mindestens einem weiteren Zeichen anzeigen
|
||||
if (prefix.length() < 2 || !prefix.startsWith('$'))
|
||||
{
|
||||
hidePopup();
|
||||
return;
|
||||
}
|
||||
|
||||
const QStringList allVars = collectVariables();
|
||||
const QString filter = prefix.toLower();
|
||||
|
||||
QStringList matches;
|
||||
for (const QString &var : allVars)
|
||||
{
|
||||
if (var.toLower().startsWith(filter) && var != prefix)
|
||||
{
|
||||
matches.append(var);
|
||||
}
|
||||
}
|
||||
|
||||
if (matches.isEmpty())
|
||||
{
|
||||
hidePopup();
|
||||
return;
|
||||
}
|
||||
|
||||
// Popup befüllen
|
||||
m_popup->clear();
|
||||
for (const QString &var : matches)
|
||||
{
|
||||
m_popup->addItem(var);
|
||||
}
|
||||
m_popup->setCurrentRow(0);
|
||||
|
||||
// Größe anpassen
|
||||
const int itemHeight = m_popup->sizeHintForRow(0) + 2;
|
||||
const int height = qMin(matches.size() * itemHeight + 4, 200);
|
||||
m_popup->setFixedHeight(height);
|
||||
|
||||
// Position unter dem Cursor
|
||||
const QRect cursorRect = m_editor->cursorRect();
|
||||
QPoint pos = m_editor->viewport()->mapToGlobal(
|
||||
QPoint(cursorRect.left(), cursorRect.bottom() + 2)
|
||||
);
|
||||
|
||||
// Nicht aus dem Bildschirm herausragen
|
||||
const QRect screen = QApplication::primaryScreen()->availableGeometry();
|
||||
if (pos.x() + m_popup->width() > screen.right())
|
||||
{
|
||||
pos.setX(screen.right() - m_popup->width());
|
||||
}
|
||||
if (pos.y() + height > screen.bottom())
|
||||
{
|
||||
pos.setY(cursorRect.top() - height - 2);
|
||||
}
|
||||
|
||||
m_popup->move(pos);
|
||||
m_popup->show();
|
||||
m_popup->raise();
|
||||
}
|
||||
|
||||
void VariableCompleter::hidePopup()
|
||||
{
|
||||
m_popup->hide();
|
||||
m_popup->clear();
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Variablen im aktuellen Dokument sammeln
|
||||
// ---------------------------------------------------------------------------
|
||||
QStringList VariableCompleter::collectVariables() const
|
||||
{
|
||||
static const QRegularExpression varRegex(R"(\$[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)");
|
||||
|
||||
QSet<QString> seen;
|
||||
QStringList result;
|
||||
|
||||
const QString text = m_editor->toPlainText();
|
||||
QRegularExpressionMatchIterator it = varRegex.globalMatch(text);
|
||||
|
||||
while (it.hasNext())
|
||||
{
|
||||
const QString var = it.next().captured(0);
|
||||
if (!seen.contains(var))
|
||||
{
|
||||
seen.insert(var);
|
||||
result.append(var);
|
||||
}
|
||||
}
|
||||
|
||||
result.sort(Qt::CaseInsensitive);
|
||||
return result;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Text links vom Cursor — das angefangene $variable
|
||||
// ---------------------------------------------------------------------------
|
||||
QString VariableCompleter::currentPrefix() const
|
||||
{
|
||||
const QTextCursor cursor = m_editor->textCursor();
|
||||
const QString line = cursor.block().text();
|
||||
const int col = cursor.columnNumber();
|
||||
|
||||
if (col == 0)
|
||||
{
|
||||
return QString();
|
||||
}
|
||||
|
||||
// Rückwärts gehen bis zum $ oder einem Nicht-Bezeichner-Zeichen
|
||||
int start = col - 1;
|
||||
while (start > 0)
|
||||
{
|
||||
const QChar c = line[start - 1];
|
||||
if (!c.isLetterOrNumber() && c != '_' && c != '$')
|
||||
{
|
||||
break;
|
||||
}
|
||||
--start;
|
||||
}
|
||||
|
||||
const QString token = line.mid(start, col - start);
|
||||
return token.startsWith('$') ? token : QString();
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Klick oder Enter — Variable einfügen
|
||||
// ---------------------------------------------------------------------------
|
||||
void VariableCompleter::onItemActivated(QListWidgetItem *item)
|
||||
{
|
||||
if (!item)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
const QString selected = item->text();
|
||||
const QString prefix = currentPrefix();
|
||||
|
||||
if (prefix.isEmpty())
|
||||
{
|
||||
hidePopup();
|
||||
return;
|
||||
}
|
||||
|
||||
// Prefix durch den vollständigen Variablennamen ersetzen
|
||||
QTextCursor cursor = m_editor->textCursor();
|
||||
cursor.movePosition(QTextCursor::Left,
|
||||
QTextCursor::KeepAnchor,
|
||||
prefix.length());
|
||||
cursor.insertText(selected);
|
||||
|
||||
m_editor->setTextCursor(cursor);
|
||||
hidePopup();
|
||||
}
|
||||
38
src/editor/VariableCompleter.h
Normal file
38
src/editor/VariableCompleter.h
Normal file
@@ -0,0 +1,38 @@
|
||||
#pragma once
|
||||
|
||||
#include <QObject>
|
||||
#include <QListWidget>
|
||||
#include <QStringList>
|
||||
#include <QRegularExpression>
|
||||
|
||||
class CodeEditor;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// VariableCompleter – Zeigt ein Popup mit passenden Variablennamen
|
||||
// wenn der Nutzer $ tippt und weiter eingibt.
|
||||
//
|
||||
// Kein Autocomplete — nur Anzeige. Klick oder Enter übernimmt.
|
||||
// Escape oder Weiterschreiben ohne Treffer schließt das Popup.
|
||||
// ---------------------------------------------------------------------------
|
||||
class VariableCompleter : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit VariableCompleter(CodeEditor *editor);
|
||||
|
||||
// Muss nach jedem Tastendruck aufgerufen werden
|
||||
void handleKeyPress(QKeyEvent *event);
|
||||
|
||||
private slots:
|
||||
void onItemActivated(QListWidgetItem *item);
|
||||
|
||||
private:
|
||||
void updatePopup();
|
||||
void hidePopup();
|
||||
QStringList collectVariables() const;
|
||||
QString currentPrefix() const; // "$me" etc. links vom Cursor
|
||||
|
||||
CodeEditor *m_editor = nullptr;
|
||||
QListWidget *m_popup = nullptr;
|
||||
};
|
||||
Reference in New Issue
Block a user