2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2010-04-26 14:02:09 +02:00
|
|
|
|
2016-03-18 07:55:01 +01:00
|
|
|
#pragma once
|
2010-04-26 14:02:09 +02:00
|
|
|
|
|
|
|
|
#include "texteditor_global.h"
|
|
|
|
|
|
2014-07-19 11:27:28 +02:00
|
|
|
#include "textmark.h"
|
2015-02-26 13:22:35 +01:00
|
|
|
#include "textdocument.h"
|
2010-04-26 14:02:09 +02:00
|
|
|
|
2021-02-26 10:16:26 +01:00
|
|
|
#include <utils/id.h>
|
|
|
|
|
|
2022-05-18 18:03:22 +03:00
|
|
|
#include <KSyntaxHighlighting/State>
|
2018-11-08 11:39:48 +01:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QTextBlockUserData>
|
|
|
|
|
#include <QPlainTextDocumentLayout>
|
2010-04-26 14:02:09 +02:00
|
|
|
|
|
|
|
|
namespace TextEditor {
|
|
|
|
|
|
|
|
|
|
struct TEXTEDITOR_EXPORT Parenthesis
|
|
|
|
|
{
|
2015-05-11 15:33:12 +02:00
|
|
|
enum Type : char { Opened, Closed };
|
2010-04-26 14:02:09 +02:00
|
|
|
|
2021-12-06 05:11:04 +01:00
|
|
|
Parenthesis() = default;
|
|
|
|
|
Parenthesis(Type t, QChar c, int position) : pos(position), chr(c), type(t) {}
|
|
|
|
|
|
|
|
|
|
friend TEXTEDITOR_EXPORT QDebug operator<<(QDebug debug, const Parenthesis &parenthesis);
|
|
|
|
|
|
2018-04-08 23:40:30 +03:00
|
|
|
int pos = -1;
|
2015-05-11 15:33:12 +02:00
|
|
|
QChar chr;
|
2021-02-26 10:16:26 +01:00
|
|
|
Utils::Id source;
|
2018-04-08 23:40:30 +03:00
|
|
|
Type type = Opened;
|
2021-09-24 09:59:50 +02:00
|
|
|
|
|
|
|
|
bool operator==(const Parenthesis &other) const;
|
2010-04-26 14:02:09 +02:00
|
|
|
};
|
2021-09-24 12:08:33 +02:00
|
|
|
using Parentheses = QVector<Parenthesis>;
|
|
|
|
|
TEXTEDITOR_EXPORT void insertSorted(Parentheses &list, const Parenthesis &elem);
|
2010-04-26 14:02:09 +02:00
|
|
|
|
2010-07-02 15:43:34 +02:00
|
|
|
class TEXTEDITOR_EXPORT CodeFormatterData
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
virtual ~CodeFormatterData();
|
|
|
|
|
};
|
2010-04-26 14:02:09 +02:00
|
|
|
|
2023-03-27 07:28:48 +02:00
|
|
|
class TEXTEDITOR_EXPORT TextSuggestion
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
TextSuggestion();
|
|
|
|
|
virtual bool apply() = 0;
|
|
|
|
|
virtual void reset() = 0;
|
|
|
|
|
virtual int position() = 0;
|
|
|
|
|
|
|
|
|
|
int currentPosition() const { return m_currentPosition; }
|
|
|
|
|
void setCurrentPosition(int position) { m_currentPosition = position; }
|
|
|
|
|
|
|
|
|
|
QTextDocument *document() { return &m_replacementDocument; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
QTextDocument m_replacementDocument;
|
|
|
|
|
int m_currentPosition = -1;
|
|
|
|
|
};
|
|
|
|
|
|
2010-04-26 14:02:09 +02:00
|
|
|
class TEXTEDITOR_EXPORT TextBlockUserData : public QTextBlockUserData
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
inline TextBlockUserData()
|
2015-05-20 08:38:52 +02:00
|
|
|
: m_foldingIndent(0)
|
2018-04-08 23:40:30 +03:00
|
|
|
, m_lexerState(0)
|
2015-05-20 08:38:52 +02:00
|
|
|
, m_folded(false)
|
|
|
|
|
, m_ifdefedOut(false)
|
|
|
|
|
, m_foldingStartIncluded(false)
|
|
|
|
|
, m_foldingEndIncluded(false)
|
2018-04-08 23:40:30 +03:00
|
|
|
, m_codeFormatterData(nullptr)
|
2010-07-02 15:43:34 +02:00
|
|
|
{}
|
2018-05-07 15:02:41 +02:00
|
|
|
~TextBlockUserData() override;
|
2010-04-26 14:02:09 +02:00
|
|
|
|
|
|
|
|
inline TextMarks marks() const { return m_marks; }
|
2014-07-19 11:27:28 +02:00
|
|
|
void addMark(TextMark *mark);
|
|
|
|
|
inline bool removeMark(TextMark *mark) { return m_marks.removeAll(mark); }
|
2012-02-14 16:25:52 +01:00
|
|
|
|
2012-03-30 11:40:31 +02:00
|
|
|
inline TextMarks documentClosing() {
|
2018-04-08 23:40:30 +03:00
|
|
|
const TextMarks marks = m_marks;
|
|
|
|
|
for (TextMark *mrk : marks)
|
|
|
|
|
mrk->setBaseTextDocument(nullptr);
|
2012-03-30 11:40:31 +02:00
|
|
|
m_marks.clear();
|
|
|
|
|
return marks;
|
|
|
|
|
}
|
2010-04-26 14:02:09 +02:00
|
|
|
|
2010-05-20 15:10:26 +02:00
|
|
|
inline void setFolded(bool b) { m_folded = b; }
|
|
|
|
|
inline bool folded() const { return m_folded; }
|
2010-04-26 14:02:09 +02:00
|
|
|
|
|
|
|
|
inline void setParentheses(const Parentheses &parentheses) { m_parentheses = parentheses; }
|
|
|
|
|
inline void clearParentheses() { m_parentheses.clear(); }
|
|
|
|
|
inline const Parentheses &parentheses() const { return m_parentheses; }
|
|
|
|
|
inline bool hasParentheses() const { return !m_parentheses.isEmpty(); }
|
|
|
|
|
int braceDepthDelta() const;
|
|
|
|
|
|
|
|
|
|
inline bool setIfdefedOut() { bool result = m_ifdefedOut; m_ifdefedOut = true; return !result; }
|
|
|
|
|
inline bool clearIfdefedOut() { bool result = m_ifdefedOut; m_ifdefedOut = false; return result;}
|
|
|
|
|
inline bool ifdefedOut() const { return m_ifdefedOut; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
enum MatchType { NoMatch, Match, Mismatch };
|
|
|
|
|
static MatchType checkOpenParenthesis(QTextCursor *cursor, QChar c);
|
|
|
|
|
static MatchType checkClosedParenthesis(QTextCursor *cursor, QChar c);
|
|
|
|
|
static MatchType matchCursorBackward(QTextCursor *cursor);
|
|
|
|
|
static MatchType matchCursorForward(QTextCursor *cursor);
|
2011-10-29 13:09:33 +08:00
|
|
|
static bool findPreviousOpenParenthesis(QTextCursor *cursor, bool select = false,
|
|
|
|
|
bool onlyInCurrentBlock = false);
|
2010-04-26 14:02:09 +02:00
|
|
|
static bool findNextClosingParenthesis(QTextCursor *cursor, bool select = false);
|
|
|
|
|
|
|
|
|
|
static bool findPreviousBlockOpenParenthesis(QTextCursor *cursor, bool checkStartPosition = false);
|
|
|
|
|
static bool findNextBlockClosingParenthesis(QTextCursor *cursor);
|
|
|
|
|
|
2010-11-22 10:46:55 +01:00
|
|
|
// Get the code folding level
|
2010-07-12 11:16:10 +02:00
|
|
|
inline int foldingIndent() const { return m_foldingIndent; }
|
2010-11-22 10:46:55 +01:00
|
|
|
/* Set the code folding level.
|
|
|
|
|
*
|
|
|
|
|
* A code folding marker will appear the line *before* the one where the indention
|
|
|
|
|
* level increases. The code folding reagion will end in the last line that has the same
|
|
|
|
|
* indention level (or higher).
|
|
|
|
|
*/
|
2010-07-12 11:16:10 +02:00
|
|
|
inline void setFoldingIndent(int indent) { m_foldingIndent = indent; }
|
2011-02-25 15:27:13 +01:00
|
|
|
// Set whether the first character of the folded region will show when the code is folded.
|
2010-07-12 11:16:10 +02:00
|
|
|
inline void setFoldingStartIncluded(bool included) { m_foldingStartIncluded = included; }
|
|
|
|
|
inline bool foldingStartIncluded() const { return m_foldingStartIncluded; }
|
2011-02-25 15:27:13 +01:00
|
|
|
// Set whether the last character of the folded region will show when the code is folded.
|
2010-07-12 11:16:10 +02:00
|
|
|
inline void setFoldingEndIncluded(bool included) { m_foldingEndIncluded = included; }
|
|
|
|
|
inline bool foldingEndIncluded() const { return m_foldingEndIncluded; }
|
|
|
|
|
inline int lexerState() const { return m_lexerState; }
|
2018-04-08 23:40:30 +03:00
|
|
|
inline void setLexerState(int state) { m_lexerState = state; }
|
2010-05-20 15:10:26 +02:00
|
|
|
|
2017-10-26 15:18:56 +02:00
|
|
|
inline void setAdditionalAnnotationHeight(int annotationHeight)
|
|
|
|
|
{ m_additionalAnnotationHeight = annotationHeight; }
|
|
|
|
|
inline int additionalAnnotationHeight() const { return m_additionalAnnotationHeight; }
|
2010-07-02 15:43:34 +02:00
|
|
|
|
|
|
|
|
CodeFormatterData *codeFormatterData() const { return m_codeFormatterData; }
|
|
|
|
|
void setCodeFormatterData(CodeFormatterData *data);
|
2010-04-26 14:02:09 +02:00
|
|
|
|
2018-11-08 11:39:48 +01:00
|
|
|
KSyntaxHighlighting::State syntaxState() { return m_syntaxState; }
|
|
|
|
|
void setSyntaxState(KSyntaxHighlighting::State state) { m_syntaxState = state; }
|
|
|
|
|
|
2021-12-08 13:57:24 +01:00
|
|
|
QByteArray expectedRawStringSuffix() { return m_expectedRawStringSuffix; }
|
|
|
|
|
void setExpectedRawStringSuffix(const QByteArray &suffix) { m_expectedRawStringSuffix = suffix; }
|
|
|
|
|
|
2023-03-27 07:28:48 +02:00
|
|
|
void insertSuggestion(std::unique_ptr<TextSuggestion> &&suggestion);
|
|
|
|
|
TextSuggestion *suggestion() const;
|
|
|
|
|
void clearSuggestion();
|
2023-01-26 15:19:46 +01:00
|
|
|
|
2010-04-26 14:02:09 +02:00
|
|
|
private:
|
|
|
|
|
TextMarks m_marks;
|
2015-05-20 08:38:52 +02:00
|
|
|
int m_foldingIndent : 16;
|
2018-04-08 23:40:30 +03:00
|
|
|
int m_lexerState : 8;
|
2010-05-20 15:10:26 +02:00
|
|
|
uint m_folded : 1;
|
2010-04-26 14:02:09 +02:00
|
|
|
uint m_ifdefedOut : 1;
|
2010-05-20 15:10:26 +02:00
|
|
|
uint m_foldingStartIncluded : 1;
|
|
|
|
|
uint m_foldingEndIncluded : 1;
|
2017-10-26 15:18:56 +02:00
|
|
|
int m_additionalAnnotationHeight = 0;
|
2010-04-26 14:02:09 +02:00
|
|
|
Parentheses m_parentheses;
|
2010-07-02 15:43:34 +02:00
|
|
|
CodeFormatterData *m_codeFormatterData;
|
2018-11-08 11:39:48 +01:00
|
|
|
KSyntaxHighlighting::State m_syntaxState;
|
2021-12-08 13:57:24 +01:00
|
|
|
QByteArray m_expectedRawStringSuffix; // A bit C++-specific, but let's be pragmatic.
|
2023-01-26 15:19:46 +01:00
|
|
|
std::unique_ptr<QTextDocument> m_replacement;
|
2023-03-27 07:28:48 +02:00
|
|
|
std::unique_ptr<TextSuggestion> m_suggestion;
|
2010-04-26 14:02:09 +02:00
|
|
|
};
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
class TEXTEDITOR_EXPORT TextDocumentLayout : public QPlainTextDocumentLayout
|
2010-04-26 14:02:09 +02:00
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
|
|
public:
|
2014-09-26 09:14:03 +02:00
|
|
|
TextDocumentLayout(QTextDocument *doc);
|
2018-04-08 23:40:30 +03:00
|
|
|
~TextDocumentLayout() override;
|
2010-04-26 14:02:09 +02:00
|
|
|
|
|
|
|
|
static void setParentheses(const QTextBlock &block, const Parentheses &parentheses);
|
|
|
|
|
static void clearParentheses(const QTextBlock &block) { setParentheses(block, Parentheses());}
|
|
|
|
|
static Parentheses parentheses(const QTextBlock &block);
|
|
|
|
|
static bool hasParentheses(const QTextBlock &block);
|
|
|
|
|
static bool setIfdefedOut(const QTextBlock &block);
|
|
|
|
|
static bool clearIfdefedOut(const QTextBlock &block);
|
|
|
|
|
static bool ifdefedOut(const QTextBlock &block);
|
|
|
|
|
static int braceDepthDelta(const QTextBlock &block);
|
|
|
|
|
static int braceDepth(const QTextBlock &block);
|
|
|
|
|
static void setBraceDepth(QTextBlock &block, int depth);
|
|
|
|
|
static void changeBraceDepth(QTextBlock &block, int delta);
|
2010-05-20 15:10:26 +02:00
|
|
|
static void setFoldingIndent(const QTextBlock &block, int indent);
|
|
|
|
|
static int foldingIndent(const QTextBlock &block);
|
2010-07-12 11:16:10 +02:00
|
|
|
static void setLexerState(const QTextBlock &block, int state);
|
|
|
|
|
static int lexerState(const QTextBlock &block);
|
2010-05-20 15:10:26 +02:00
|
|
|
static void changeFoldingIndent(QTextBlock &block, int delta);
|
|
|
|
|
static bool canFold(const QTextBlock &block);
|
|
|
|
|
static void doFoldOrUnfold(const QTextBlock& block, bool unfold);
|
|
|
|
|
static bool isFolded(const QTextBlock &block);
|
|
|
|
|
static void setFolded(const QTextBlock &block, bool folded);
|
2021-12-08 13:57:24 +01:00
|
|
|
static void setExpectedRawStringSuffix(const QTextBlock &block, const QByteArray &suffix);
|
|
|
|
|
static QByteArray expectedRawStringSuffix(const QTextBlock &block);
|
2023-03-27 07:28:48 +02:00
|
|
|
static TextSuggestion *suggestion(const QTextBlock &block);
|
|
|
|
|
static void updateSuggestionFormats(const QTextBlock &block,
|
2023-03-13 15:11:50 +01:00
|
|
|
const FontSettings &fontSettings);
|
2023-03-27 07:28:48 +02:00
|
|
|
static bool updateSuggestion(const QTextBlock &block,
|
|
|
|
|
int position,
|
|
|
|
|
const FontSettings &fontSettings);
|
2010-04-26 14:02:09 +02:00
|
|
|
|
2011-08-12 12:13:23 +02:00
|
|
|
class TEXTEDITOR_EXPORT FoldValidator
|
|
|
|
|
{
|
|
|
|
|
public:
|
2014-09-26 09:14:03 +02:00
|
|
|
void setup(TextDocumentLayout *layout);
|
2011-08-12 12:13:23 +02:00
|
|
|
void reset();
|
|
|
|
|
void process(QTextBlock block);
|
|
|
|
|
void finalize();
|
|
|
|
|
|
|
|
|
|
private:
|
2018-04-08 23:40:30 +03:00
|
|
|
TextDocumentLayout *m_layout = nullptr;
|
|
|
|
|
bool m_requestDocUpdate = false;
|
|
|
|
|
int m_insideFold = 0;
|
2011-08-12 12:13:23 +02:00
|
|
|
};
|
|
|
|
|
|
2019-12-04 08:19:53 +01:00
|
|
|
static TextBlockUserData *textUserData(const QTextBlock &block) {
|
2010-04-26 14:02:09 +02:00
|
|
|
return static_cast<TextBlockUserData*>(block.userData());
|
|
|
|
|
}
|
|
|
|
|
static TextBlockUserData *userData(const QTextBlock &block) {
|
2018-09-20 01:16:01 +03:00
|
|
|
auto data = static_cast<TextBlockUserData*>(block.userData());
|
2010-04-26 14:02:09 +02:00
|
|
|
if (!data && block.isValid())
|
|
|
|
|
const_cast<QTextBlock &>(block).setUserData((data = new TextBlockUserData));
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-17 14:59:14 +01:00
|
|
|
void requestExtraAreaUpdate();
|
2010-04-26 14:02:09 +02:00
|
|
|
|
|
|
|
|
void emitDocumentSizeChanged() { emit documentSizeChanged(documentSize()); }
|
2012-02-15 13:06:16 +01:00
|
|
|
|
2018-04-08 23:40:30 +03:00
|
|
|
int lastSaveRevision = 0;
|
|
|
|
|
bool hasMarks = false;
|
2022-09-06 13:01:51 +02:00
|
|
|
bool hasLocationMarker = false;
|
2018-04-08 23:40:30 +03:00
|
|
|
int m_requiredWidth = 0;
|
2012-02-15 13:06:16 +01:00
|
|
|
|
2010-07-02 13:47:14 +02:00
|
|
|
void setRequiredWidth(int width);
|
|
|
|
|
|
2017-10-26 15:18:56 +02:00
|
|
|
QSizeF documentSize() const override;
|
|
|
|
|
QRectF blockBoundingRect(const QTextBlock &block) const override;
|
2010-07-12 11:16:10 +02:00
|
|
|
|
2012-03-30 11:40:31 +02:00
|
|
|
TextMarks documentClosing();
|
2014-09-22 18:43:31 +02:00
|
|
|
void documentReloaded(TextMarks marks, TextDocument *baseextDocument);
|
2012-02-14 19:27:15 +01:00
|
|
|
void updateMarksLineNumber();
|
|
|
|
|
void updateMarksBlock(const QTextBlock &block);
|
2022-08-04 08:42:48 +02:00
|
|
|
void scheduleUpdate();
|
|
|
|
|
void requestUpdateNow();
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
bool m_updateScheduled = false;
|
2012-02-17 14:59:14 +01:00
|
|
|
|
|
|
|
|
signals:
|
|
|
|
|
void updateExtraArea();
|
2017-10-11 14:14:10 +02:00
|
|
|
void foldChanged(const int blockNumber, bool folded);
|
2021-09-24 09:59:50 +02:00
|
|
|
void parenthesesChanged(const QTextBlock block);
|
2010-04-26 14:02:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace TextEditor
|