aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/android/javaeditor.cpp
diff options
context:
space:
mode:
authorJarek Kobus <jaroslaw.kobus@qt.io>2024-11-13 10:01:47 +0100
committerJarek Kobus <jaroslaw.kobus@qt.io>2024-11-13 11:14:04 +0000
commitd4209b10874b212b7fc87fc0541d49f260058853 (patch)
tree68fa69eedef765dbc5d11623a98e1d9c15763d20 /src/plugins/android/javaeditor.cpp
parent3ec3af549839fb32c403d35422fb635521ec0109 (diff)
Android: Move JavaIndenter into javaeditor.cpp
Remove createJavaIndenter() indirection. Change-Id: I872a61aff98c863547d112d47dda48fcb8b69889 Reviewed-by: hjk <hjk@qt.io>
Diffstat (limited to 'src/plugins/android/javaeditor.cpp')
-rw-r--r--src/plugins/android/javaeditor.cpp59
1 files changed, 57 insertions, 2 deletions
diff --git a/src/plugins/android/javaeditor.cpp b/src/plugins/android/javaeditor.cpp
index 8fa69e6e8b2..1023331c0c1 100644
--- a/src/plugins/android/javaeditor.cpp
+++ b/src/plugins/android/javaeditor.cpp
@@ -2,7 +2,6 @@
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "javaeditor.h"
-#include "javaindenter.h"
#include "androidconstants.h"
#include <coreplugin/coreplugintr.h>
@@ -12,18 +11,74 @@
#include <texteditor/textdocument.h>
#include <texteditor/texteditorconstants.h>
#include <texteditor/texteditor.h>
+#include <texteditor/textindenter.h>
#include <utils/mimeconstants.h>
#include <utils/uncommentselection.h>
namespace Android::Internal {
+class JavaIndenter final : public TextEditor::TextIndenter
+{
+public:
+ explicit JavaIndenter(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 &tabSettings,
+ int cursorPositionInEditor = -1) final;
+
+ int indentFor(const QTextBlock &block,
+ const TextEditor::TabSettings &tabSettings,
+ int cursorPositionInEditor = -1) final;
+};
+
+void JavaIndenter::indentBlock(const QTextBlock &block,
+ const QChar &typedChar,
+ const TextEditor::TabSettings &tabSettings,
+ int /*cursorPositionInEditor*/)
+{
+ int indent = indentFor(block, tabSettings);
+ if (typedChar == QLatin1Char('}'))
+ indent -= tabSettings.m_indentSize;
+ tabSettings.indentLine(block, qMax(0, indent));
+}
+
+int JavaIndenter::indentFor(const QTextBlock &block,
+ const TextEditor::TabSettings &tabSettings,
+ int /*cursorPositionInEditor*/)
+{
+ QTextBlock previous = block.previous();
+ if (!previous.isValid())
+ return 0;
+
+ QString previousText = previous.text();
+ while (previousText.trimmed().isEmpty()) {
+ previous = previous.previous();
+ if (!previous.isValid())
+ return 0;
+ previousText = previous.text();
+ }
+
+ int indent = tabSettings.indentationColumn(previousText);
+
+ int adjust = previousText.count(QLatin1Char('{')) - previousText.count(QLatin1Char('}'));
+ adjust *= tabSettings.m_indentSize;
+
+ return qMax(0, indent + adjust);
+}
+
static TextEditor::TextDocument *createJavaDocument()
{
auto doc = new TextEditor::TextDocument;
doc->setId(Constants::JAVA_EDITOR_ID);
doc->setMimeType(Utils::Constants::JAVA_MIMETYPE);
- doc->setIndenter(createJavaIndenter(doc->document()));
+ doc->setIndenter(new JavaIndenter(doc->document()));
return doc;
}