LanguageClient: Indent code coming from the server

... if the client implementation requests it.
The server is not necessarily aware of our indentation style, so we
might have to apply it to the newly inserted code.

Change-Id: I43518575c7124568da42be3b04a28d7f352f6dc2
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Christian Kandeler
2022-02-03 10:36:40 +01:00
parent 9b7f580a14
commit 92156cc1d6
11 changed files with 100 additions and 72 deletions

View File

@@ -28,8 +28,6 @@
#include "cppqtstyleindenter.h"
#include "cppcodeformatter.h"
#include "cppeditorconstants.h"
#include "cppmodelmanager.h"
#include "cppworkingcopy.h"
#include <projectexplorer/editorconfiguration.h>
@@ -45,63 +43,15 @@ using namespace CPlusPlus;
namespace CppEditor {
class CppRefactoringChangesData : public TextEditor::RefactoringChangesData
static std::unique_ptr<TextEditor::Indenter> createIndenter(const Utils::FilePath &filePath,
QTextDocument *textDocument)
{
static std::unique_ptr<TextEditor::Indenter> createIndenter(const Utils::FilePath &filePath,
QTextDocument *textDocument)
{
TextEditor::ICodeStylePreferencesFactory *factory
= TextEditor::TextEditorSettings::codeStyleFactory(Constants::CPP_SETTINGS_ID);
std::unique_ptr<TextEditor::Indenter> indenter(factory->createIndenter(textDocument));
indenter->setFileName(filePath);
return indenter;
}
public:
explicit CppRefactoringChangesData(const Snapshot &snapshot)
: m_snapshot(snapshot)
, m_modelManager(CppModelManager::instance())
, m_workingCopy(m_modelManager->workingCopy())
{}
void indentSelection(const QTextCursor &selection,
const Utils::FilePath &filePath,
const TextEditor::TextDocument *textDocument) const override
{
if (textDocument) { // use the indenter from the textDocument if there is one, can be ClangFormat
textDocument->indenter()->indent(selection, QChar::Null, textDocument->tabSettings());
} else {
const auto &tabSettings = ProjectExplorer::actualTabSettings(filePath.toString(),
textDocument);
auto indenter = createIndenter(filePath, selection.document());
indenter->indent(selection, QChar::Null, tabSettings);
}
}
void reindentSelection(const QTextCursor &selection,
const Utils::FilePath &filePath,
const TextEditor::TextDocument *textDocument) const override
{
if (textDocument) { // use the indenter from the textDocument if there is one, can be ClangFormat
textDocument->indenter()->reindent(selection, textDocument->tabSettings());
} else {
const auto &tabSettings = ProjectExplorer::actualTabSettings(filePath.toString(),
textDocument);
auto indenter = createIndenter(filePath, selection.document());
indenter->reindent(selection, tabSettings);
}
}
void fileChanged(const Utils::FilePath &filePath) override
{
m_modelManager->updateSourceFiles({filePath.toString()});
}
Snapshot m_snapshot;
CppModelManager *m_modelManager;
WorkingCopy m_workingCopy;
};
TextEditor::ICodeStylePreferencesFactory *factory
= TextEditor::TextEditorSettings::codeStyleFactory(Constants::CPP_SETTINGS_ID);
std::unique_ptr<TextEditor::Indenter> indenter(factory->createIndenter(textDocument));
indenter->setFileName(filePath);
return indenter;
}
CppRefactoringChanges::CppRefactoringChanges(const Snapshot &snapshot)
: RefactoringChanges(new CppRefactoringChangesData(snapshot))
@@ -283,4 +233,43 @@ void CppRefactoringFile::fileChanged()
RefactoringFile::fileChanged();
}
CppRefactoringChangesData::CppRefactoringChangesData(const Snapshot &snapshot)
: m_snapshot(snapshot)
, m_modelManager(CppModelManager::instance())
, m_workingCopy(m_modelManager->workingCopy())
{}
void CppRefactoringChangesData::indentSelection(const QTextCursor &selection,
const Utils::FilePath &filePath,
const TextEditor::TextDocument *textDocument) const
{
if (textDocument) { // use the indenter from the textDocument if there is one, can be ClangFormat
textDocument->indenter()->indent(selection, QChar::Null, textDocument->tabSettings());
} else {
const auto &tabSettings = ProjectExplorer::actualTabSettings(filePath.toString(),
textDocument);
auto indenter = createIndenter(filePath, selection.document());
indenter->indent(selection, QChar::Null, tabSettings);
}
}
void CppRefactoringChangesData::reindentSelection(const QTextCursor &selection,
const Utils::FilePath &filePath,
const TextEditor::TextDocument *textDocument) const
{
if (textDocument) { // use the indenter from the textDocument if there is one, can be ClangFormat
textDocument->indenter()->reindent(selection, textDocument->tabSettings());
} else {
const auto &tabSettings = ProjectExplorer::actualTabSettings(filePath.toString(),
textDocument);
auto indenter = createIndenter(filePath, selection.document());
indenter->reindent(selection, tabSettings);
}
}
void CppRefactoringChangesData::fileChanged(const Utils::FilePath &filePath)
{
m_modelManager->updateSourceFiles({filePath.toString()});
}
} // namespace CppEditor