pgLab/pglab/codeeditor/CodeEditor.h

77 lines
2 KiB
C++

#ifndef CODEEDITOR_H
#define CODEEDITOR_H
#include <QPlainTextEdit>
#include <set>
#include "util/Colors.h"
#include "CompletionPopup.h"
/** This class adds some capabilities to QPlainTextEdit that are useful
* in code editor scenarios.
*/
class CodeEditor : public QPlainTextEdit
{
Q_OBJECT
public:
explicit CodeEditor(QWidget *parent = nullptr);
int gutterAreaWidth();
void addErrorMarker(int position, int length);
void clearErrorMarkers();
void setFont(const QFont &);
void setTabSize(int chars);
/// Install the function that supplies completions, an empty function disables completion
void setCompletionCallback(EditorCompletionCallback callback);
/// Show completions at the cursor, also when nothing has been typed yet
void showCompletions();
protected:
void resizeEvent(QResizeEvent *event) override;
void keyPressEvent(QKeyEvent *e) override;
void focusOutEvent(QFocusEvent *e) override;
void mousePressEvent(QMouseEvent *e) override;
signals:
public slots:
private slots:
void updateGutterAreaWidth(int newBlockCount);
void highlightCurrentLine();
void updateGutterArea(const QRect &, int);
void onTextChanged();
private:
QWidget *gutterArea;
QTextEdit::ExtraSelection currentLine;
QList<QTextEdit::ExtraSelection> errorMarkers;
const ColorTheme &colorTheme;
std::set<int> errorLines;
EditorCompletionCallback m_completionCallback;
CompletionPopup *m_completionPopup = nullptr;
EditorCompletionResult m_completionResult;
bool updateCompletions(bool explicitRequest);
void hideCompletions();
void acceptCompletion(int row);
// Settings
int m_tabSize = 0; // tabSize in characters
bool m_useTab = false;
bool lineHasError(int blockNumber) const;
void updateExtraSelections();
bool indentSelection(bool indent);
void insertIndentation(QTextCursor &cursor);
void removeIndentation(QTextCursor &cursor);
void makeSelection(QTextCursor &cursor, int first_pos, int end_block);
friend class GutterPainter;
};
#endif // CODEEDITOR_H