Files
Compiler_GUI/ui/codeeditorwidget.h

133 lines
3.6 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma once
#include <QWidget>
#include <QPlainTextEdit>
#include <QColor>
#include <QHash>
#include <QVector>
#include <QSet>
#include <QStringList>
#include <QTextCursor>
class LineNumberArea;
class CSyntaxHighlighter;
class CompletionPopup;
class FindReplaceBar;
// 每个字符的背景信息
struct CharBackgroundInfo
{
int column = 0; // 这一行中的列号(从 0 开始)
QColor color;
qreal alpha = 1.0; // 0.0 ~ 1.0
};
// 每一行的背景信息
struct LineBackgroundInfo
{
QColor color;
qreal alpha = 1.0; // 0.0 ~ 1.0
};
// 真正的代码编辑器,继承 QPlainTextEdit
class CodeEditor : public QPlainTextEdit
{
Q_OBJECT
public:
explicit CodeEditor(QWidget *parent = nullptr);
int lineNumberAreaWidth() const;
// 预留接口:设置行/字符背景(带透明度)
void setLineBackground(int line, const QColor &color, qreal alpha = 1.0);
void clearLineBackground(int line);
void clearAllLineBackgrounds();
void setCharBackground(int line, int column, const QColor &color, qreal alpha = 1.0);
void clearCharBackground(int line, int column);
void clearAllCharBackgrounds();
// 行号区域的绘制 & 点击
void lineNumberAreaPaintEvent(QPaintEvent *event);
void lineNumberAreaMousePressEvent(QMouseEvent *event);
void searchNext(const QString &text);
signals:
// 断点状态变化(暂时只发信号,不做具体逻辑)
void breakpointToggled(int line, bool enabled);
protected:
void resizeEvent(QResizeEvent *event) override;
void keyPressEvent(QKeyEvent *event) override;
private slots:
void updateLineNumberAreaWidth(int newBlockCount);
void updateLineNumberArea(const QRect &rect, int dy);
void highlightCurrentLine();
// ✅ 新增:来自联想窗口的补全插入
void insertCompletion(const QString &completion);
private:
void refreshExtraSelections();
void handleReturnKey(QKeyEvent *event);
void toggleBreakpoint(int line);
// ✅ 新增:联想相关辅助函数
void initCompletion();
void showCompletion(const QString &prefix);
void hideCompletion();
QString wordUnderCursor() const;
void rebuildSearchMatches();
void applySearchHighlights();
private:
LineNumberArea *m_lineNumberArea = nullptr;
CSyntaxHighlighter *m_highlighter = nullptr;
QSet<int> m_breakpoints;
QHash<int, LineBackgroundInfo> m_lineBackgrounds;
QHash<int, QVector<CharBackgroundInfo>> m_charBackgrounds;
// ✅ 新增:联想窗口和候选词列表
CompletionPopup *m_completionPopup = nullptr;
QStringList m_completionWords;
QString m_searchText;
QVector<QTextCursor> m_searchMatches;
int m_currentMatchIndex = -1;
};
// 对外仍然叫 CodeEditorWidget内部包一个 CodeEditor
class CodeEditorWidget : public QWidget
{
Q_OBJECT
public:
explicit CodeEditorWidget(QWidget *parent = nullptr);
void loadFromFile(const QString &filePath);
bool save();
bool saveAs(const QString &filePath);
QString currentText() const;
void clearEditor();
// 代理到内部 CodeEditor 的接口(以后你可以直接拿来做可视化用)
void setLineBackground(int line, const QColor &color, qreal alpha = 1.0);
void clearLineBackground(int line);
void setCharBackground(int line, int column, const QColor &color, qreal alpha = 1.0);
void clearCharBackground(int line, int column);
private:
CodeEditor *m_editor = nullptr;
QString m_currentFilePath;
FindReplaceBar *m_findBar = nullptr;
};