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

View File

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

View File

@@ -3,27 +3,10 @@
#pragma once
#include "../tools/nimlexer.h"
#include <texteditor/syntaxhighlighter.h>
namespace Nim {
class NimHighlighter : public TextEditor::SyntaxHighlighter
{
Q_OBJECT
TextEditor::SyntaxHighlighter *createNimHighlighter();
public:
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);
};
}
} // Nim

View File

@@ -15,13 +15,40 @@
namespace Nim {
NimIndenter::NimIndenter(QTextDocument *doc)
: TextEditor::TextIndenter(doc)
{}
bool NimIndenter::isElectricCharacter(const QChar &ch) const
class NimIndenter final : public TextEditor::TextIndenter
{
return NimIndenter::electricCharacters().contains(ch);
public:
explicit NimIndenter(QTextDocument *doc)
: TextEditor::TextIndenter(doc)
{}
bool isElectricCharacter(const QChar &ch) const final
{
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,
@@ -60,12 +87,6 @@ void NimIndenter::indentBlock(const QTextBlock &block,
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
{
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
if (previous.type == NimLexer::TokenType::Operator) {
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
@@ -141,14 +162,9 @@ int NimIndenter::calculateIndentationDiff(const QString &previousLine, int previ
return 0;
}
QString NimIndenter::rightTrimmed(const QString &str)
TextEditor::Indenter *createNimIndenter(QTextDocument *doc)
{
int n = str.size() - 1;
for (; n >= 0; --n) {
if (!str.at(n).isSpace())
return str.left(n + 1);
}
return QString();
return new NimIndenter(doc);
}
} // Nim

View File

@@ -7,29 +7,6 @@
namespace Nim {
class NimIndenter : public TextEditor::TextIndenter
{
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);
};
TextEditor::Indenter *createNimIndenter(QTextDocument *doc);
} // namespace Nim

View File

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