TextEditor: Make TabSettings accessible for AutoCompleters

Change-Id: I3591ad94ca98979c2d47585d33800a489d87eeda
Reviewed-by: Ivan Donchevskii <ivan.donchevskii@qt.io>
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Nikolai Kosjar
2017-07-17 12:41:37 +02:00
parent f77dfd64d5
commit c8a543b949
6 changed files with 23 additions and 18 deletions

View File

@@ -117,11 +117,11 @@ QString CMakeAutoCompleter::insertMatchingQuote(const QTextCursor &cursor,
return quote; return quote;
} }
int CMakeAutoCompleter::paragraphSeparatorAboutToBeInserted(QTextCursor &cursor, const TextEditor::TabSettings &tabSettings) int CMakeAutoCompleter::paragraphSeparatorAboutToBeInserted(QTextCursor &cursor)
{ {
const QString line = cursor.block().text().trimmed(); const QString line = cursor.block().text().trimmed();
if (line.contains(QRegExp(QStringLiteral("^(endfunction|endmacro|endif|endforeach|endwhile)\\w*\\(")))) if (line.contains(QRegExp(QStringLiteral("^(endfunction|endmacro|endif|endforeach|endwhile)\\w*\\("))))
tabSettings.indentLine(cursor.block(), tabSettings.indentationColumn(cursor.block().text())); tabSettings().indentLine(cursor.block(), tabSettings().indentationColumn(cursor.block().text()));
return 0; return 0;
} }

View File

@@ -43,7 +43,7 @@ public:
QChar lookAhead, bool skipChars, int *skippedChars) const override; QChar lookAhead, bool skipChars, int *skippedChars) const override;
QString insertMatchingQuote(const QTextCursor &cursor, const QString &text, QString insertMatchingQuote(const QTextCursor &cursor, const QString &text,
QChar lookAhead, bool skipChars, int *skippedChars) const override; QChar lookAhead, bool skipChars, int *skippedChars) const override;
int paragraphSeparatorAboutToBeInserted(QTextCursor &cursor, const TextEditor::TabSettings &tabSettings) override; int paragraphSeparatorAboutToBeInserted(QTextCursor &cursor) override;
bool contextAllowsAutoBrackets(const QTextCursor &cursor, const QString &textToInsert) const override; bool contextAllowsAutoBrackets(const QTextCursor &cursor, const QString &textToInsert) const override;
bool contextAllowsAutoQuotes(const QTextCursor &cursor, const QString &textToInsert) const override; bool contextAllowsAutoQuotes(const QTextCursor &cursor, const QString &textToInsert) const override;
bool contextAllowsElectricCharacters(const QTextCursor &cursor) const override; bool contextAllowsElectricCharacters(const QTextCursor &cursor) const override;

View File

@@ -431,8 +431,10 @@ void CppEditorPlugin::test_insertParagraph()
QVERIFY(!tc.isNull()); QVERIFY(!tc.isNull());
const int blockCount = CppAutoCompleter().paragraphSeparatorAboutToBeInserted( CppAutoCompleter completer = CppAutoCompleter();
tc, TextEditor::TextEditorSettings::codeStyle()->tabSettings()); completer.setTabSettings(TextEditor::TextEditorSettings::codeStyle()->tabSettings());
const int blockCount = completer.paragraphSeparatorAboutToBeInserted(tc);
QCOMPARE(blockCount, expectedBlockCount); QCOMPARE(blockCount, expectedBlockCount);
} }

View File

@@ -273,8 +273,7 @@ bool AutoCompleter::autoBackspace(QTextCursor &cursor)
return false; return false;
} }
int AutoCompleter::paragraphSeparatorAboutToBeInserted(QTextCursor &cursor, int AutoCompleter::paragraphSeparatorAboutToBeInserted(QTextCursor &cursor)
const TabSettings &tabSettings)
{ {
if (!m_autoInsertBrackets) if (!m_autoInsertBrackets)
return 0; return 0;
@@ -302,15 +301,15 @@ int AutoCompleter::paragraphSeparatorAboutToBeInserted(QTextCursor &cursor,
if (condition) {| if (condition) {|
statement; statement;
*/ */
int indentation = tabSettings.indentationColumn(block.text()); int indentation = m_tabSettings.indentationColumn(block.text());
if (block.next().isValid()) { // not the last block if (block.next().isValid()) { // not the last block
block = block.next(); block = block.next();
//skip all empty blocks //skip all empty blocks
while (block.isValid() && tabSettings.onlySpace(block.text())) while (block.isValid() && m_tabSettings.onlySpace(block.text()))
block = block.next(); block = block.next();
if (block.isValid() if (block.isValid()
&& tabSettings.indentationColumn(block.text()) > indentation) && m_tabSettings.indentationColumn(block.text()) > indentation)
return 0; return 0;
} }

View File

@@ -26,6 +26,7 @@
#pragma once #pragma once
#include "texteditor_global.h" #include "texteditor_global.h"
#include "tabsettings.h"
#include <QString> #include <QString>
@@ -35,8 +36,6 @@ QT_END_NAMESPACE
namespace TextEditor { namespace TextEditor {
class TabSettings;
class TEXTEDITOR_EXPORT AutoCompleter class TEXTEDITOR_EXPORT AutoCompleter
{ {
public: public:
@@ -53,6 +52,9 @@ public:
void setSurroundWithQuotesEnabled(bool b) { m_surroundWithQuotes = b; } void setSurroundWithQuotesEnabled(bool b) { m_surroundWithQuotes = b; }
bool isSurroundWithQuotesEnabled() const { return m_surroundWithQuotes; } bool isSurroundWithQuotesEnabled() const { return m_surroundWithQuotes; }
void setTabSettings(const TabSettings &tabSettings) { m_tabSettings = tabSettings; }
const TabSettings &tabSettings() const { return m_tabSettings; }
// Returns the text to complete at the cursor position, or an empty string // Returns the text to complete at the cursor position, or an empty string
virtual QString autoComplete(QTextCursor &cursor, const QString &text, bool skipChars) const; virtual QString autoComplete(QTextCursor &cursor, const QString &text, bool skipChars) const;
@@ -60,8 +62,7 @@ public:
virtual bool autoBackspace(QTextCursor &cursor); virtual bool autoBackspace(QTextCursor &cursor);
// Hook to insert special characters on enter. Returns the number of extra blocks inserted. // Hook to insert special characters on enter. Returns the number of extra blocks inserted.
virtual int paragraphSeparatorAboutToBeInserted(QTextCursor &cursor, virtual int paragraphSeparatorAboutToBeInserted(QTextCursor &cursor);
const TabSettings &tabSettings);
virtual bool contextAllowsAutoBrackets(const QTextCursor &cursor, virtual bool contextAllowsAutoBrackets(const QTextCursor &cursor,
const QString &textToInsert = QString()) const; const QString &textToInsert = QString()) const;
@@ -94,6 +95,7 @@ private:
QString replaceSelection(QTextCursor &cursor, const QString &textToInsert) const; QString replaceSelection(QTextCursor &cursor, const QString &textToInsert) const;
private: private:
TabSettings m_tabSettings;
mutable bool m_allowSkippingOfBlockEnd; mutable bool m_allowSkippingOfBlockEnd;
bool m_autoInsertBrackets; bool m_autoInsertBrackets;
bool m_surroundWithBrackets; bool m_surroundWithBrackets;

View File

@@ -2247,9 +2247,7 @@ void TextEditorWidget::keyPressEvent(QKeyEvent *e)
const TypingSettings &tps = d->m_document->typingSettings(); const TypingSettings &tps = d->m_document->typingSettings();
cursor.beginEditBlock(); cursor.beginEditBlock();
int extraBlocks = int extraBlocks = d->m_autoCompleter->paragraphSeparatorAboutToBeInserted(cursor);
d->m_autoCompleter->paragraphSeparatorAboutToBeInserted(cursor,
d->m_document->tabSettings());
QString previousIndentationString; QString previousIndentationString;
if (tps.m_autoIndent) { if (tps.m_autoIndent) {
@@ -3160,7 +3158,10 @@ void TextEditorWidgetPrivate::setupDocumentSignals()
this, &TextEditorWidgetPrivate::documentReloadFinished); this, &TextEditorWidgetPrivate::documentReloadFinished);
QObject::connect(m_document.data(), &TextDocument::tabSettingsChanged, QObject::connect(m_document.data(), &TextDocument::tabSettingsChanged,
this, &TextEditorWidgetPrivate::updateTabStops); this, [this](){
updateTabStops();
m_autoCompleter->setTabSettings(m_document->tabSettings());
});
QObject::connect(m_document.data(), &TextDocument::fontSettingsChanged, QObject::connect(m_document.data(), &TextDocument::fontSettingsChanged,
this, &TextEditorWidgetPrivate::applyFontSettingsDelayed); this, &TextEditorWidgetPrivate::applyFontSettingsDelayed);
@@ -8242,6 +8243,7 @@ BaseTextEditor *TextEditorFactoryPrivate::createEditorHelper(const TextDocumentP
widget->setAutoCompleter(m_autoCompleterCreator()); widget->setAutoCompleter(m_autoCompleterCreator());
widget->setTextDocument(document); widget->setTextDocument(document);
widget->autoCompleter()->setTabSettings(document->tabSettings());
widget->d->m_hoverHandlers = m_hoverHandlers; widget->d->m_hoverHandlers = m_hoverHandlers;
widget->d->m_codeAssistant.configure(widget); widget->d->m_codeAssistant.configure(widget);