Nim: Clean up some editor component creation

Change-Id: Ic14e7852b1cb6db47b689e59ad24b3d0eb9c4792
Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
This commit is contained in:
hjk
2023-12-21 17:22:33 +01:00
parent 941ebc84e9
commit 31702189be
6 changed files with 68 additions and 80 deletions

View File

@@ -13,7 +13,6 @@
#include <texteditor/textdocument.h> #include <texteditor/textdocument.h>
#include <texteditor/texteditoractionhandler.h> #include <texteditor/texteditoractionhandler.h>
#include <texteditor/texteditorconstants.h> #include <texteditor/texteditorconstants.h>
#include <utils/qtcassert.h>
using namespace TextEditor; using namespace TextEditor;
using namespace Utils; using namespace Utils;
@@ -37,12 +36,8 @@ NimEditorFactory::NimEditorFactory()
setDocumentCreator([]() { setDocumentCreator([]() {
return new TextDocument(Constants::C_NIMEDITOR_ID); return new TextDocument(Constants::C_NIMEDITOR_ID);
}); });
setIndenterCreator([](QTextDocument *doc) { setIndenterCreator(&createNimIndenter);
return new NimIndenter(doc); setSyntaxHighlighterCreator(&createNimHighlighter);
});
setSyntaxHighlighterCreator([]() {
return new NimHighlighter;
});
setCompletionAssistProvider(new NimCompletionAssistProvider()); setCompletionAssistProvider(new NimCompletionAssistProvider());
setCommentDefinition(CommentDefinition::HashStyle); setCommentDefinition(CommentDefinition::HashStyle);
setParenthesesMatchingEnabled(true); setParenthesesMatchingEnabled(true);
@@ -51,8 +46,8 @@ NimEditorFactory::NimEditorFactory()
void NimEditorFactory::decorateEditor(TextEditorWidget *editor) void NimEditorFactory::decorateEditor(TextEditorWidget *editor)
{ {
editor->textDocument()->resetSyntaxHighlighter([] { return new NimHighlighter();}); editor->textDocument()->resetSyntaxHighlighter(&createNimHighlighter);
editor->textDocument()->setIndenter(new NimIndenter(editor->textDocument()->document())); editor->textDocument()->setIndenter(createNimIndenter(editor->textDocument()->document()));
} }
} // Nim } // Nim

View File

@@ -7,20 +7,32 @@
#include <texteditor/textdocument.h> #include <texteditor/textdocument.h>
#include <texteditor/texteditorconstants.h> #include <texteditor/texteditorconstants.h>
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
namespace Nim { namespace Nim {
NimHighlighter::NimHighlighter() class NimHighlighter final : public TextEditor::SyntaxHighlighter
{
public:
NimHighlighter()
{ {
setDefaultTextFormatCategories(); setDefaultTextFormatCategories();
} }
void NimHighlighter::highlightBlock(const QString &text) void highlightBlock(const QString &text) final
{ {
setCurrentBlockState(highlightLine(text, previousBlockState())); setCurrentBlockState(highlightLine(text, previousBlockState()));
} }
private:
TextEditor::TextStyle styleForToken(const NimLexer::Token &token, const QString &tokenValue);
TextEditor::TextStyle styleForIdentifier(const NimLexer::Token &token, const QString &tokenValue);
int highlightLine(const QString &text, int initialState);
};
TextEditor::TextStyle NimHighlighter::styleForToken(const NimLexer::Token &token, TextEditor::TextStyle NimHighlighter::styleForToken(const NimLexer::Token &token,
const QString &tokenValue) const QString &tokenValue)
{ {
@@ -92,4 +104,9 @@ int NimHighlighter::highlightLine(const QString &text, int initialState)
return lexer.state(); return lexer.state();
} }
TextEditor::SyntaxHighlighter *createNimHighlighter()
{
return new NimHighlighter;
}
} // Nim } // Nim

View File

@@ -3,27 +3,10 @@
#pragma once #pragma once
#include "../tools/nimlexer.h"
#include <texteditor/syntaxhighlighter.h> #include <texteditor/syntaxhighlighter.h>
namespace Nim { namespace Nim {
class NimHighlighter : public TextEditor::SyntaxHighlighter TextEditor::SyntaxHighlighter *createNimHighlighter();
{
Q_OBJECT
public: } // Nim
NimHighlighter();
protected:
void highlightBlock(const QString &text) override;
private:
TextEditor::TextStyle styleForToken(const NimLexer::Token &token, const QString &tokenValue);
TextEditor::TextStyle styleForIdentifier(const NimLexer::Token &token, const QString &tokenValue);
int highlightLine(const QString &text, int initialState);
};
}

View File

@@ -15,13 +15,40 @@
namespace Nim { namespace Nim {
NimIndenter::NimIndenter(QTextDocument *doc) class NimIndenter final : public TextEditor::TextIndenter
{
public:
explicit NimIndenter(QTextDocument *doc)
: TextEditor::TextIndenter(doc) : TextEditor::TextIndenter(doc)
{} {}
bool NimIndenter::isElectricCharacter(const QChar &ch) const bool isElectricCharacter(const QChar &ch) const final
{ {
return NimIndenter::electricCharacters().contains(ch); return ch == QLatin1Char(':') || ch == QLatin1Char('=');
}
void indentBlock(const QTextBlock &block,
const QChar &typedChar,
const TextEditor::TabSettings &settings,
int cursorPositionInEditor = -1) final;
private:
bool startsBlock(const QString &line, int state) const;
bool endsBlock(const QString &line, int state) const;
int calculateIndentationDiff(const QString &previousLine,
int previousState,
int indentSize) const;
};
static QString rightTrimmed(const QString &str)
{
int n = str.size() - 1;
for (; n >= 0; --n) {
if (!str.at(n).isSpace())
return str.left(n + 1);
}
return QString();
} }
void NimIndenter::indentBlock(const QTextBlock &block, void NimIndenter::indentBlock(const QTextBlock &block,
@@ -60,12 +87,6 @@ void NimIndenter::indentBlock(const QTextBlock &block,
settings.indentLine(block, std::max(0, indentation)); settings.indentLine(block, std::max(0, indentation));
} }
const QSet<QChar> &NimIndenter::electricCharacters()
{
static QSet<QChar> result{QLatin1Char(':'), QLatin1Char('=')};
return result;
}
bool NimIndenter::startsBlock(const QString &line, int state) const bool NimIndenter::startsBlock(const QString &line, int state) const
{ {
NimLexer lexer(line.constData(), line.length(), static_cast<NimLexer::State>(state)); NimLexer lexer(line.constData(), line.length(), static_cast<NimLexer::State>(state));
@@ -88,7 +109,7 @@ bool NimIndenter::startsBlock(const QString &line, int state) const
// electric characters start a new block, and are operators // electric characters start a new block, and are operators
if (previous.type == NimLexer::TokenType::Operator) { if (previous.type == NimLexer::TokenType::Operator) {
QStringView ref = QStringView(line).mid(previous.begin, previous.length); QStringView ref = QStringView(line).mid(previous.begin, previous.length);
return ref.isEmpty() ? false : electricCharacters().contains(ref.at(0)); return ref.isEmpty() ? false : isElectricCharacter(ref.at(0));
} }
// some keywords starts a new block // some keywords starts a new block
@@ -141,14 +162,9 @@ int NimIndenter::calculateIndentationDiff(const QString &previousLine, int previ
return 0; return 0;
} }
QString NimIndenter::rightTrimmed(const QString &str) TextEditor::Indenter *createNimIndenter(QTextDocument *doc)
{ {
int n = str.size() - 1; return new NimIndenter(doc);
for (; n >= 0; --n) {
if (!str.at(n).isSpace())
return str.left(n + 1);
}
return QString();
} }
} // Nim } // Nim

View File

@@ -7,29 +7,6 @@
namespace Nim { namespace Nim {
class NimIndenter : public TextEditor::TextIndenter TextEditor::Indenter *createNimIndenter(QTextDocument *doc);
{
public:
explicit NimIndenter(QTextDocument *doc);
bool isElectricCharacter(const QChar &ch) const override;
void indentBlock(const QTextBlock &block,
const QChar &typedChar,
const TextEditor::TabSettings &settings,
int cursorPositionInEditor = -1) override;
private:
static const QSet<QChar> &electricCharacters();
bool startsBlock(const QString &line, int state) const;
bool endsBlock(const QString &line, int state) const;
int calculateIndentationDiff(const QString &previousLine,
int previousState,
int indentSize) const;
static QString rightTrimmed(const QString &other);
};
} // namespace Nim } // namespace Nim

View File

@@ -49,7 +49,7 @@ CodeStyleEditorWidget *NimCodeStylePreferencesFactory::createEditor(
Indenter *NimCodeStylePreferencesFactory::createIndenter(QTextDocument *doc) const Indenter *NimCodeStylePreferencesFactory::createIndenter(QTextDocument *doc) const
{ {
return new NimIndenter(doc); return createNimIndenter(doc);
} }
QString NimCodeStylePreferencesFactory::snippetProviderGroupId() const QString NimCodeStylePreferencesFactory::snippetProviderGroupId() const