RedPanda-CPP/RedPandaIDE/editor.h

357 lines
11 KiB
C
Raw Normal View History

2021-04-06 23:10:57 +08:00
#ifndef EDITOR_H
#define EDITOR_H
#include <QObject>
#include <utils.h>
#include <QTabWidget>
2021-05-24 00:41:00 +08:00
#include "qsynedit/SynEdit.h"
#include "colorscheme.h"
2021-06-23 22:38:02 +08:00
#include "common.h"
2021-08-23 10:16:06 +08:00
#include "parser/cppparser.h"
2021-08-29 00:48:23 +08:00
#include "widgets/codecompletionpopup.h"
#include "widgets/headercompletionpopup.h"
2021-04-06 23:10:57 +08:00
2021-09-30 21:25:48 +08:00
#define USER_CODE_IN_INSERT_POS "%INSERT%"
#define USER_CODE_IN_REPL_POS_BEGIN "%REPL_BEGIN%"
#define USER_CODE_IN_REPL_POS_END "%REPL_END%"
struct TabStop {
int x;
int endX;
int y;
};
using PTabStop = std::shared_ptr<TabStop>;
2021-04-11 21:33:08 +08:00
class SaveException: public std::exception {
public:
explicit SaveException(const QString& reason);
explicit SaveException(const QString&& reason);
// exception interface
const QString& reason() const noexcept;
public:
const char *what() const noexcept override;
private:
QString mReason;
QByteArray mReasonBuffer;
2021-04-11 21:33:08 +08:00
};
2021-05-24 00:41:00 +08:00
class Editor : public SynEdit
2021-04-06 23:10:57 +08:00
{
Q_OBJECT
public:
2021-12-02 18:29:37 +08:00
enum class LastSymbolType {
Identifier,
MemberOperator,
MatchingBracket,
BracketMatched,
MatchingParenthesis,
ParenthesisMatched,
None
};
2021-04-29 20:54:44 +08:00
enum MarginNumber {
LineNumberMargin = 0,
MarkerMargin = 1,
FoldMargin = 2,
};
enum MarkerNumber {
BreakpointMarker,
ErrorMarker,
WarningMarker
};
2021-06-22 13:24:26 +08:00
enum class QuoteStatus {
NotQuote,
SingleQuote,
SingleQuoteEscape,
DoubleQuote,
DoubleQuoteEscape,
RawString,
RawStringNoEscape
};
2021-08-25 08:48:33 +08:00
enum class WordPurpose {
wpCompletion, // walk backwards over words, array, functions, parents, no forward movement
wpEvaluation, // walk backwards over words, array, functions, parents, forwards over words, array
wpHeaderCompletion, // walk backwards over path
wpHeaderCompletionStart, // walk backwards over path, including start '<' or '"'
wpDirective, // preprocessor
wpJavadoc, //javadoc
wpInformation // walk backwards over words, array, functions, parents, forwards over words
};
2021-08-29 17:23:40 +08:00
enum class TipType {
Preprocessor, // cursor hovers above preprocessor line
Identifier, // cursor hovers above identifier
Selection, // cursor hovers above selection
None, // mouseover not allowed
Error //Cursor hovers above error line/item;
};
2021-06-23 22:38:02 +08:00
struct SyntaxIssue {
int col;
int endCol;
int startChar;
int endChar;
CompileIssueType issueType;
QString token;
QString hint;
};
using PSyntaxIssue = std::shared_ptr<SyntaxIssue>;
using SyntaxIssueList = QVector<PSyntaxIssue>;
using PSyntaxIssueList = std::shared_ptr<SyntaxIssueList>;
explicit Editor(QWidget *parent);
2021-04-09 17:48:25 +08:00
explicit Editor(QWidget *parent, const QString& filename,
const QByteArray& encoding,
2021-04-09 17:48:25 +08:00
bool inProject, bool isNew,QTabWidget* parentPageControl);
2021-04-06 23:10:57 +08:00
2021-04-07 22:44:08 +08:00
~Editor();
//tell the compiler to prohibit copy/moving editor objects ( we should only use pointers to the editor object)
Editor(const Editor&) = delete;
Editor(const Editor&&) = delete;
Editor& operator=(const Editor&) = delete;
Editor& operator=(const Editor&&) = delete;
2021-04-11 21:33:08 +08:00
const QByteArray& encodingOption() const noexcept;
void setEncodingOption(const QByteArray& encoding) noexcept;
const QByteArray& fileEncoding() const noexcept;
const QString& filename() const noexcept;
bool inProject() const noexcept;
bool isNew() const noexcept;
2021-04-06 23:10:57 +08:00
void loadFile(QString filename = "");
void saveFile(QString filename);
void convertToEncoding(const QByteArray& encoding);
2021-04-09 10:08:05 +08:00
bool save(bool force=false, bool reparse=true);
bool saveAs(const QString& name="", bool fromProject = false);
void activate();
2021-04-06 23:10:57 +08:00
2021-04-11 21:33:08 +08:00
QTabWidget* pageControl() noexcept;
void setPageControl(QTabWidget* newPageControl);
void updateCaption(const QString& newCaption=QString());
2021-06-07 11:02:03 +08:00
void applySettings();
void applyColorScheme(const QString& schemeName);
void copyToClipboard() override;
void cutToClipboard() override;
void copyAsHTML();
void setCaretPosition(int line,int aChar);
void setCaretPositionAndActivate(int line,int aChar);
2021-06-23 22:38:02 +08:00
void addSyntaxIssues(int line, int startChar, int endChar, CompileIssueType errorType, const QString& hint);
2021-06-23 22:38:02 +08:00
void clearSyntaxIssues();
void gotoNextSyntaxIssue();
void gotoPrevSyntaxIssue();
bool hasPrevSyntaxIssue() const;
bool hasNextSyntaxIssue() const;
PSyntaxIssueList getSyntaxIssuesAtLine(int line);
PSyntaxIssue getSyntaxIssueAtPosition(const BufferCoord& pos);
int gutterClickedLine() const;
void toggleBreakpoint(int line);
void clearBreakpoints();
bool hasBreakpoint(int line);
2021-10-21 17:31:25 +08:00
void addBookmark(int line,const QString& description);
void removeBookmark(int line);
bool hasBookmark(int line);
void clearBookmarks();
2021-07-26 00:22:08 +08:00
void removeBreakpointFocus();
void modifyBreakpointProperty(int line);
2021-07-26 18:22:09 +08:00
void setActiveBreakpointFocus(int Line, bool setFocus=true);
2021-08-26 11:58:29 +08:00
QString getPreviousWordAtPositionForSuggestion(const BufferCoord& p);
2021-09-02 12:14:02 +08:00
void reformat();
void checkSyntaxInBack();
2021-09-03 11:50:04 +08:00
void gotoDeclaration(const BufferCoord& pos);
void gotoDefinition(const BufferCoord& pos);
2021-09-11 18:42:49 +08:00
void reparse();
2021-10-03 17:18:43 +08:00
void reparseTodo();
2021-09-16 23:51:05 +08:00
void insertString(const QString& value, bool moveCursor);
2021-09-30 21:25:48 +08:00
void insertCodeSnippet(const QString& code);
2021-10-07 07:52:20 +08:00
void print();
void exportAsRTF(const QString& rtfFilename);
void exportAsHTML(const QString& htmlFilename);
void resetBreakpoints();
2021-11-08 14:51:20 +08:00
bool notParsed();
void insertLine();
void deleteWord();
void deleteLine();
void duplicateLine();
void deleteToEOL();
void deleteToBOL();
2021-12-01 23:41:14 +08:00
QStringList getExpressionAtPositionForCompletion(const BufferCoord& pos);
const PCppParser &parser();
2021-08-23 17:27:17 +08:00
void tab() override;
2021-09-02 12:14:02 +08:00
private slots:
2021-06-10 09:34:59 +08:00
void onStatusChanged(SynStatusChanges changes);
void onGutterClicked(Qt::MouseButton button, int x, int y, int line);
2021-08-29 22:08:43 +08:00
void onTipEvalValueReady(const QString& value);
2021-09-02 12:14:02 +08:00
void onLinesDeleted(int first,int count);
void onLinesInserted(int first,int count);
private:
bool isBraceChar(QChar ch);
2021-10-21 17:31:25 +08:00
void resetBookmarks();
2021-06-22 23:00:34 +08:00
QChar getCurrentChar();
2021-06-22 13:24:26 +08:00
bool handleSymbolCompletion(QChar key);
2021-06-22 23:00:34 +08:00
bool handleParentheseCompletion();
bool handleParentheseSkip();
bool handleBracketCompletion();
bool handleBracketSkip();
bool handleMultilineCommentCompletion();
bool handleBraceCompletion();
bool handleBraceSkip();
bool handleSingleQuoteCompletion();
bool handleDoubleQuoteCompletion();
bool handleGlobalIncludeCompletion();
bool handleGlobalIncludeSkip();
2021-08-25 08:48:33 +08:00
2021-08-26 20:18:20 +08:00
bool handleCodeCompletion(QChar key);
2021-08-23 10:16:06 +08:00
void initParser();
void undoSymbolCompletion(int pos);
2021-06-22 13:24:26 +08:00
QuoteStatus getQuoteStatus();
void showCompletion(const QString& preWord, bool autoComplete);
2021-08-25 23:53:35 +08:00
void showHeaderCompletion(bool autoComplete);
2021-08-25 08:48:33 +08:00
2021-08-26 11:58:29 +08:00
bool testInFunc(int x,int y);
2021-08-26 17:48:23 +08:00
void completionInsert(bool appendFunc=false);
void headerCompletionInsert();
2021-08-26 17:48:23 +08:00
bool onCompletionKeyPressed(QKeyEvent* event);
bool onHeaderCompletionKeyPressed(QKeyEvent* event);
bool onCompletionInputMethod(QInputMethodEvent *event);
2021-08-26 17:48:23 +08:00
2021-08-29 17:23:40 +08:00
TipType getTipType(QPoint point, BufferCoord& pos);
void cancelHint();
QString getFileHint(const QString& s);
QString getParserHint(const QString& s, int line);
2021-08-29 22:08:43 +08:00
void showDebugHint(const QString& s,int line);
QString getErrorHint(const PSyntaxIssue& issue);
2021-08-29 17:23:40 +08:00
QString getHintForFunction(const PStatement& statement, const PStatement& scope,
const QString& filename, int line);
2021-09-24 11:41:14 +08:00
void updateFunctionTip();
2021-10-01 21:16:22 +08:00
void clearUserCodeInTabStops();
2021-10-02 10:05:48 +08:00
void popUserCodeInTabStops();
void onExportedFormatToken(PSynHighlighter syntaxHighlighter, int Line, int column, const QString& token,
PSynHighlighterAttribute &attr);
2021-04-06 23:10:57 +08:00
private:
QByteArray mEncodingOption; // the encoding type set by the user
QByteArray mFileEncoding; // the real encoding of the file (auto detected)
2021-04-06 23:10:57 +08:00
QString mFilename;
QTabWidget* mParentPageControl;
bool mInProject;
bool mIsNew;
2021-06-23 22:38:02 +08:00
QMap<int,PSyntaxIssueList> mSyntaxIssues;
QColor mSyntaxErrorColor;
2021-07-26 00:22:08 +08:00
QColor mSyntaxWarningColor;
QColor mActiveBreakpointForegroundColor;
QColor mActiveBreakpointBackgroundColor;
QColor mBreakpointForegroundColor;
QColor mBreakpointBackgroundColor;
QColor mCurrentHighlighWordForeground;
QColor mCurrentHighlighWordBackground;
int mSyntaxErrorLine;
2021-06-24 20:43:09 +08:00
int mLineCount;
int mGutterClickedLine;
QSet<int> mBreakpointLines;
2021-10-21 17:31:25 +08:00
QSet<int> mBookmarkLines;
2021-07-26 00:22:08 +08:00
int mActiveBreakpointLine;
2021-08-23 10:16:06 +08:00
PCppParser mParser;
2021-08-29 00:48:23 +08:00
std::shared_ptr<CodeCompletionPopup> mCompletionPopup;
std::shared_ptr<HeaderCompletionPopup> mHeaderCompletionPopup;
2021-08-25 08:48:33 +08:00
int mLastIdCharPressed;
2021-08-25 23:53:35 +08:00
bool mUseCppSyntax;
2021-08-29 17:23:40 +08:00
QString mCurrentWord;
2021-08-29 22:08:43 +08:00
QString mCurrentDebugTipWord;
2021-08-29 17:23:40 +08:00
TipType mCurrentTipType;
QString mOldHighlightedWord;
QString mCurrentHighlightedWord;
QDateTime mHideTime;
2021-04-09 17:48:25 +08:00
bool mSaving;
bool mCurrentLineModified;
2021-09-30 21:25:48 +08:00
int mXOffsetSince;
int mTabStopBegin;
int mTabStopEnd;
int mTabStopY;
bool mCanAutoSave;
2021-09-30 21:25:48 +08:00
QString mLineBeforeTabStop;
QString mLineAfterTabStop;
QList<PTabStop> mUserCodeInTabStops;
BufferCoord mHighlightCharPos1;
BufferCoord mHighlightCharPos2;
std::shared_ptr<QHash<StatementKind, std::shared_ptr<ColorSchemeItem> > > mStatementColors;
2021-09-30 21:25:48 +08:00
2021-04-09 17:48:25 +08:00
// QWidget interface
protected:
void wheelEvent(QWheelEvent *event) override;
void focusInEvent(QFocusEvent *event) override;
void focusOutEvent(QFocusEvent *event) override;
void keyPressEvent(QKeyEvent *event) override;
// SynEdit interface
protected:
void onGutterPaint(QPainter &painter, int aLine, int X, int Y) override;
void onGetEditingAreas(int Line, SynEditingAreaList &areaList) override;
// SynEdit interface
protected:
bool onGetSpecialLineColors(int Line, QColor &foreground, QColor &backgroundColor) override;
2021-08-28 09:01:40 +08:00
// SynEdit interface
protected:
void onPreparePaintHighlightToken(int line, int aChar, const QString &token, PSynHighlighterAttribute attr, SynFontStyles &style, QColor &foreground, QColor &background) override;
2021-08-29 17:23:40 +08:00
// QObject interface
public:
bool event(QEvent *event) override;
// QWidget interface
2021-09-10 12:37:02 +08:00
void setInProject(bool newInProject);
bool useCppSyntax() const;
void setUseCppSyntax(bool newUseCppSyntax);
const std::shared_ptr<QHash<StatementKind, std::shared_ptr<ColorSchemeItem> > > &statementColors() const;
void setStatementColors(const std::shared_ptr<QHash<StatementKind, std::shared_ptr<ColorSchemeItem> > > &newStatementColors);
const QDateTime &hideTime() const;
void setHideTime(const QDateTime &newHideTime);
bool canAutoSave() const;
void setCanAutoSave(bool newCanAutoSave);
protected:
void mouseReleaseEvent(QMouseEvent *event) override;
// QWidget interface
protected:
void inputMethodEvent(QInputMethodEvent *) override;
2021-10-04 22:32:34 +08:00
// QWidget interface
protected:
void closeEvent(QCloseEvent *event) override;
2021-04-06 23:10:57 +08:00
};
QString getWordAtPosition(SynEdit* editor,
const BufferCoord& p,
BufferCoord& pWordBegin,
BufferCoord& pWordEnd,
Editor::WordPurpose purpose);
2021-04-06 23:10:57 +08:00
#endif // EDITOR_H