QmlJsEditor: Don't export Internal classes

Export two functions doing the actual work instead.
Centralize some repeated code.

Change-Id: I7de674ef7ae5537663d1227d36cc556c4ee3ed74
Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
This commit is contained in:
hjk
2023-12-21 16:47:22 +01:00
parent e98e5cc980
commit 63374fb34d
9 changed files with 77 additions and 87 deletions

View File

@@ -169,7 +169,7 @@ void BindingEditorFactory::decorateEditor(TextEditor::TextEditorWidget *editor)
{
editor->textDocument()->resetSyntaxHighlighter(
[] { return new QmlJSEditor::QmlJSHighlighter(); });
editor->textDocument()->setIndenter(new QmlJSEditor::Internal::Indenter(
editor->textDocument()->setIndenter(QmlJSEditor::createQmlJsIndenter(
editor->textDocument()->document()));
editor->setAutoCompleter(new QmlJSEditor::AutoCompleter);
}

View File

@@ -33,20 +33,8 @@ void BaseTextEditModifier::indentLines(int startLine, int endLine)
if (!m_textEdit)
return;
TextEditor::TextDocument *baseTextEditorDocument = m_textEdit->textDocument();
TextEditor::TabSettings tabSettings = baseTextEditorDocument->tabSettings();
QTextCursor tc(textDocument());
tc.beginEditBlock();
for (int i = startLine; i <= endLine; i++) {
QTextBlock start = textDocument()->findBlockByNumber(i);
if (start.isValid()) {
QmlJSEditor::Internal::Indenter indenter(textDocument());
indenter.indentBlock(start, QChar::Null, tabSettings);
}
}
tc.endEditBlock();
QmlJSEditor::indentQmlJs(textDocument(), startLine, endLine,
m_textEdit->textDocument()->tabSettings());
}
void BaseTextEditModifier::indent(int offset, int length)

View File

@@ -186,20 +186,6 @@ void IndentingTextEditModifier::indent(int offset, int length)
void IndentingTextEditModifier::indentLines(int startLine, int endLine)
{
if (startLine < 0)
return;
QTextCursor tc(textDocument());
tc.beginEditBlock();
for (int i = startLine; i <= endLine; i++) {
QTextBlock start = textDocument()->findBlockByNumber(i);
if (start.isValid()) {
QmlJSEditor::Internal::Indenter indenter(textDocument());
indenter.indentBlock(start, QChar::Null, m_tabSettings);
}
}
tc.endEditBlock();
QmlJSEditor::indentQmlJs(textDocument(), startLine, endLine, m_tabSettings);
}

View File

@@ -1184,7 +1184,7 @@ QmlJSEditorFactory::QmlJSEditorFactory(Utils::Id _id)
void QmlJSEditorFactory::decorateEditor(TextEditorWidget *editor)
{
editor->textDocument()->resetSyntaxHighlighter([] { return new QmlJSHighlighter(); });
editor->textDocument()->setIndenter(new Internal::Indenter(editor->textDocument()->document()));
editor->textDocument()->setIndenter(createQmlJsIndenter(editor->textDocument()->document()));
editor->setAutoCompleter(new AutoCompleter);
}

View File

@@ -822,7 +822,7 @@ QmlJSEditorDocument::QmlJSEditorDocument(Utils::Id id)
d, &Internal::QmlJSEditorDocumentPrivate::settingsChanged);
resetSyntaxHighlighter([] { return new QmlJSHighlighter(); });
setCodec(QTextCodec::codecForName("UTF-8")); // qml files are defined to be utf-8
setIndenter(new Internal::Indenter(document()));
setIndenter(createQmlJsIndenter(document()));
}
bool QmlJSEditorDocument::supportsCodec(const QTextCodec *codec) const

View File

@@ -396,17 +396,8 @@ void QuickToolBar::onEnabledChanged(bool b)
void QuickToolBar::indentLines(int startLine, int endLine)
{
if (startLine > 0) {
TextEditor::TabSettings tabSettings = m_editorWidget->textDocument()->tabSettings();
for (int i = startLine; i <= endLine; i++) {
QTextBlock start = m_editorWidget->document()->findBlockByNumber(i);
if (start.isValid()) {
QmlJSEditor::Internal::Indenter indenterMy(m_editorWidget->document());
indenterMy.indentBlock(start, QChar::Null, tabSettings);
}
}
}
QmlJSEditor::indentQmlJs(m_editorWidget->document(), startLine, endLine,
m_editorWidget->textDocument()->tabSettings());
}
ContextPaneWidget *QuickToolBar::contextWidget()

View File

@@ -64,7 +64,7 @@ TextEditor::CodeStyleEditorWidget *QmlJSCodeStylePreferencesFactory::createEdito
TextEditor::Indenter *QmlJSCodeStylePreferencesFactory::createIndenter(QTextDocument *doc) const
{
return new QmlJSEditor::Internal::Indenter(doc);
return QmlJSEditor::createQmlJsIndenter(doc);
}
QString QmlJSCodeStylePreferencesFactory::snippetProviderGroupId() const

View File

@@ -6,30 +6,47 @@
#include <qmljstools/qmljsqtstylecodeformatter.h>
#include <texteditor/tabsettings.h>
#include <QChar>
#include <QTextDocument>
#include <QTextBlock>
using namespace QmlJSEditor;
using namespace Internal;
using namespace TextEditor;
Indenter::Indenter(QTextDocument *doc)
: TextEditor::TextIndenter(doc)
{}
namespace QmlJSEditor {
namespace Internal {
Indenter::~Indenter() = default;
bool Indenter::isElectricCharacter(const QChar &ch) const
class QmlJsIndenter final : public TextEditor::TextIndenter
{
if (ch == QLatin1Char('{')
|| ch == QLatin1Char('}')
|| ch == QLatin1Char(']')
|| ch == QLatin1Char(':'))
return true;
return false;
public:
explicit QmlJsIndenter(QTextDocument *doc)
: TextEditor::TextIndenter(doc)
{}
bool isElectricCharacter(const QChar &ch) const final;
void indentBlock(const QTextBlock &block,
const QChar &typedChar,
const TextEditor::TabSettings &tabSettings,
int cursorPositionInEditor = -1) final;
void invalidateCache() final;
int indentFor(const QTextBlock &block,
const TextEditor::TabSettings &tabSettings,
int cursorPositionInEditor = -1) final;
int visualIndentFor(const QTextBlock &block,
const TextEditor::TabSettings &tabSettings) final;
TextEditor::IndentationForBlock indentationForBlocks(const QVector<QTextBlock> &blocks,
const TextEditor::TabSettings &tabSettings,
int cursorPositionInEditor = -1) final;
};
bool QmlJsIndenter::isElectricCharacter(const QChar &ch) const
{
return ch == QLatin1Char('{')
|| ch == QLatin1Char('}')
|| ch == QLatin1Char(']')
|| ch == QLatin1Char(':');
}
void Indenter::indentBlock(const QTextBlock &block,
void QmlJsIndenter::indentBlock(const QTextBlock &block,
const QChar &typedChar,
const TextEditor::TabSettings &tabSettings,
int /*cursorPositionInEditor*/)
@@ -52,13 +69,13 @@ void Indenter::indentBlock(const QTextBlock &block,
tabSettings.indentLine(block, depth);
}
void Indenter::invalidateCache()
void QmlJsIndenter::invalidateCache()
{
QmlJSTools::CreatorCodeFormatter codeFormatter;
codeFormatter.invalidateCache(m_doc);
}
int Indenter::indentFor(const QTextBlock &block,
int QmlJsIndenter::indentFor(const QTextBlock &block,
const TextEditor::TabSettings &tabSettings,
int /*cursorPositionInEditor*/)
{
@@ -67,12 +84,12 @@ int Indenter::indentFor(const QTextBlock &block,
return codeFormatter.indentFor(block);
}
int Indenter::visualIndentFor(const QTextBlock &block, const TextEditor::TabSettings &tabSettings)
int QmlJsIndenter::visualIndentFor(const QTextBlock &block, const TextEditor::TabSettings &tabSettings)
{
return indentFor(block, tabSettings);
}
TextEditor::IndentationForBlock Indenter::indentationForBlocks(
TextEditor::IndentationForBlock QmlJsIndenter::indentationForBlocks(
const QVector<QTextBlock> &blocks,
const TextEditor::TabSettings &tabSettings,
int /*cursorPositionInEditor*/)
@@ -86,3 +103,31 @@ TextEditor::IndentationForBlock Indenter::indentationForBlocks(
ret.insert(block.blockNumber(), codeFormatter.indentFor(block));
return ret;
}
} // Internal
TextEditor::TextIndenter *createQmlJsIndenter(QTextDocument *doc)
{
return new Internal::QmlJsIndenter(doc);
}
void indentQmlJs(QTextDocument *doc, int startLine, int endLine, const TextEditor::TabSettings &tabSettings)
{
if (startLine <= 0)
return;
QTextCursor tc(doc);
tc.beginEditBlock();
for (int i = startLine; i <= endLine; i++) {
// FIXME: block.next() should be faster.
QTextBlock block = doc->findBlockByNumber(i);
if (block.isValid()) {
Internal::QmlJsIndenter indenter(doc);
indenter.indentBlock(block, QChar::Null, tabSettings);
}
}
tc.endEditBlock();
}
} // QmlJsEditor

View File

@@ -8,30 +8,10 @@
#include <texteditor/textindenter.h>
namespace QmlJSEditor {
namespace Internal {
class QMLJSTOOLS_EXPORT Indenter : public TextEditor::TextIndenter
{
public:
explicit Indenter(QTextDocument *doc);
~Indenter() override;
QMLJSTOOLS_EXPORT TextEditor::TextIndenter *createQmlJsIndenter(QTextDocument *doc);
bool isElectricCharacter(const QChar &ch) const override;
void indentBlock(const QTextBlock &block,
const QChar &typedChar,
const TextEditor::TabSettings &tabSettings,
int cursorPositionInEditor = -1) override;
void invalidateCache() override;
QMLJSTOOLS_EXPORT void indentQmlJs(QTextDocument *doc, int startLine, int endLine,
const TextEditor::TabSettings &tabSettings);
int indentFor(const QTextBlock &block,
const TextEditor::TabSettings &tabSettings,
int cursorPositionInEditor = -1) override;
int visualIndentFor(const QTextBlock &block,
const TextEditor::TabSettings &tabSettings) override;
TextEditor::IndentationForBlock indentationForBlocks(const QVector<QTextBlock> &blocks,
const TextEditor::TabSettings &tabSettings,
int cursorPositionInEditor = -1) override;
};
} // Internal
} // QmlJSEditor