ClangFormat: Fix preview update

Fixes: QTCREATORBUG-30089
Change-Id: Id89e8b2ee0439c5235755256e8410f42ad94b432
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Artem Sokolovskii
2023-12-18 17:20:30 +01:00
parent bbf02473e0
commit a0cc2e3c3e
4 changed files with 24 additions and 3 deletions

View File

@@ -782,7 +782,8 @@ void ClangFormatBaseIndenter::autoIndent(const QTextCursor &cursor,
}
}
clang::format::FormatStyle overrideStyle(const Utils::FilePath &fileName)
clang::format::FormatStyle ClangFormatBaseIndenter::overrideStyle(
const Utils::FilePath &fileName) const
{
const ProjectExplorer::Project *projectForFile
= ProjectExplorer::ProjectManager::projectForFile(fileName);
@@ -792,6 +793,9 @@ clang::format::FormatStyle overrideStyle(const Utils::FilePath &fileName)
? projectForFile->editorConfiguration()->codeStyle("Cpp")->currentPreferences()
: TextEditor::TextEditorSettings::codeStyle("Cpp")->currentPreferences();
if (m_overriddenPreferences)
preferences = m_overriddenPreferences->currentPreferences();
Utils::FilePath filePath = filePathToCurrentSettings(preferences);
if (!filePath.exists())
@@ -854,4 +858,9 @@ const clang::format::FormatStyle &ClangFormatBaseIndenter::styleForFile() const
return m_cachedStyle.style;
}
void ClangFormatBaseIndenter::setOverriddenPreferences(TextEditor::ICodeStylePreferences *preferences)
{
m_overriddenPreferences = preferences;
}
} // namespace ClangFormat

View File

@@ -49,6 +49,8 @@ public:
const clang::format::FormatStyle &styleForFile() const;
void setOverriddenPreferences(TextEditor::ICodeStylePreferences *preferences);
protected:
virtual bool formatCodeInsteadOfIndent() const { return false; }
virtual bool formatWhileTyping() const { return false; }
@@ -84,6 +86,9 @@ private:
};
mutable CachedStyle m_cachedStyle;
clang::format::FormatStyle overrideStyle(const Utils::FilePath &fileName) const;
TextEditor::ICodeStylePreferences *m_overriddenPreferences = nullptr;
};
} // namespace ClangFormat

View File

@@ -129,7 +129,9 @@ ClangFormatConfigWidget::ClangFormatConfigWidget(TextEditor::ICodeStylePreferenc
displaySettings.m_visualizeWhitespace = true;
d->preview->setDisplaySettings(displaySettings);
d->preview->setPlainText(QLatin1String(CppEditor::Constants::DEFAULT_CODE_STYLE_SNIPPETS[0]));
d->preview->textDocument()->setIndenter(new ClangFormatIndenter(d->preview->document()));
auto *indenter = new ClangFormatIndenter(d->preview->document());
indenter->setOverriddenPreferences(codeStyle);
d->preview->textDocument()->setIndenter(indenter);
d->preview->textDocument()->setFontSettings(TextEditor::TextEditorSettings::fontSettings());
d->preview->textDocument()->setSyntaxHighlighter(new CppEditor::CppHighlighter);
d->preview->textDocument()->indenter()->setFileName(fileName);
@@ -271,7 +273,7 @@ void ClangFormatConfigWidget::updatePreview()
QTextCursor cursor(d->preview->document());
cursor.setPosition(0);
cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
d->preview->textDocument()->autoIndent(cursor);
d->preview->textDocument()->autoFormatOrIndent(cursor);
}
std::string ClangFormatConfigWidget::readFile(const QString &path)

View File

@@ -605,6 +605,11 @@ public:
m_codeStyleEditor->apply();
}
void finish() final
{
m_codeStyleEditor->finish();
}
CppCodeStylePreferences *m_pageCppCodeStylePreferences = nullptr;
CodeStyleEditorWidget *m_codeStyleEditor;
};