Index: clangd/ClangdServer.cpp =================================================================== --- clangd/ClangdServer.cpp +++ clangd/ClangdServer.cpp @@ -152,9 +152,6 @@ Opts.ClangTidyOpts = tidy::ClangTidyOptions::getDefaults(); if (ClangTidyOptProvider) Opts.ClangTidyOpts = ClangTidyOptProvider->getOptions(File); - // FIXME: cache this. - Opts.Style = - getFormatStyleForFile(File, Contents, FSProvider.getFileSystem().get()); Opts.SuggestMissingIncludes = SuggestMissingIncludes; // FIXME: some build systems like Bazel will take time to preparing // environment to build the file, it would be nice if we could emit a @@ -375,7 +372,8 @@ auto A = prepareTweak(TweakID, *Selection); if (!A) return CB(A.takeError()); - return CB((*A)->apply(*Selection, InpAST->Inputs.Opts.Style)); + // FIXME: run formatter on top of resulting replacements. + return CB((*A)->apply(*Selection)); }; WorkScheduler.runWithAST( "ApplyTweak", File, Index: clangd/ClangdUnit.cpp =================================================================== --- clangd/ClangdUnit.cpp +++ clangd/ClangdUnit.cpp @@ -309,8 +309,9 @@ llvm::Optional FixIncludes; auto BuildDir = VFS->getCurrentWorkingDirectory(); if (Opts.SuggestMissingIncludes && Index && !BuildDir.getError()) { + auto Style = getFormatStyleForFile(MainInput.getFile(), Content, VFS.get()); auto Inserter = std::make_shared( - MainInput.getFile(), Content, Opts.Style, BuildDir.get(), + MainInput.getFile(), Content, Style, BuildDir.get(), Clang->getPreprocessor().getHeaderSearchInfo()); if (Preamble) { for (const auto &Inc : Preamble->Includes.MainFileIncludes) Index: clangd/Compiler.h =================================================================== --- clangd/Compiler.h +++ clangd/Compiler.h @@ -17,7 +17,6 @@ #include "../clang-tidy/ClangTidyOptions.h" #include "index/Index.h" -#include "clang/Format/Format.h" #include "clang/Frontend/CompilerInstance.h" #include "clang/Frontend/CompilerInvocation.h" #include "clang/Frontend/PrecompiledPreamble.h" @@ -39,7 +38,6 @@ struct ParseOptions { tidy::ClangTidyOptions ClangTidyOpts; bool SuggestMissingIncludes = false; - format::FormatStyle Style; }; /// Information required to run clang, e.g. to parse AST or do code completion. Index: clangd/SourceCode.h =================================================================== --- clangd/SourceCode.h +++ clangd/SourceCode.h @@ -144,11 +144,6 @@ llvm::StringRef Content, llvm::vfs::FileSystem *FS); -// Cleanup and format the given replacements. -llvm::Expected -cleanupAndFormat(StringRef Code, const tooling::Replacements &Replaces, - const format::FormatStyle &Style); - } // namespace clangd } // namespace clang #endif Index: clangd/SourceCode.cpp =================================================================== --- clangd/SourceCode.cpp +++ clangd/SourceCode.cpp @@ -335,14 +335,5 @@ return *Style; } -llvm::Expected -cleanupAndFormat(StringRef Code, const tooling::Replacements &Replaces, - const format::FormatStyle &Style) { - auto CleanReplaces = cleanupAroundReplacements(Code, Replaces, Style); - if (!CleanReplaces) - return CleanReplaces; - return formatReplacements(Code, std::move(*CleanReplaces), Style); -} - } // namespace clangd } // namespace clang Index: clangd/refactor/Tweak.h =================================================================== --- clangd/refactor/Tweak.h +++ clangd/refactor/Tweak.h @@ -22,7 +22,6 @@ #include "ClangdUnit.h" #include "Protocol.h" #include "Selection.h" -#include "clang/Format/Format.h" #include "clang/Tooling/Core/Replacement.h" #include "llvm/ADT/Optional.h" #include "llvm/ADT/StringRef.h" @@ -48,7 +47,7 @@ ParsedAST * /// A location of the cursor in the editor. SourceLocation Cursor; - /// The AST nodes that were selected. + // The AST nodes that were selected. SelectionTree ASTSelection; // FIXME: provide a way to get sources and ASTs for other files. }; @@ -64,20 +63,13 @@ /// should be moved into 'apply'. /// Returns true iff the action is available and apply() can be called on it. virtual bool prepare(const Selection &Sel) = 0; - /// Format and apply the actual changes generated from the second stage of the - /// action. + /// Run the second stage of the action that would produce the actual changes. /// EXPECTS: prepare() was called and returned true. - Expected apply(const Selection &Sel, - const format::FormatStyle &Style); + virtual Expected apply(const Selection &Sel) = 0; /// A one-line title of the action that should be shown to the users in the /// UI. /// EXPECTS: prepare() was called and returned true. virtual std::string title() const = 0; - -protected: - /// Run the second stage of the action that would produce the actual changes. - /// EXPECTS: prepare() was called and returned true. - virtual Expected execute(const Selection &Sel) = 0; }; // All tweaks must be registered in the .cpp file next to their definition. Index: clangd/refactor/Tweak.cpp =================================================================== --- clangd/refactor/Tweak.cpp +++ clangd/refactor/Tweak.cpp @@ -46,14 +46,6 @@ Cursor = SM.getComposedLoc(SM.getMainFileID(), RangeBegin); } -Expected Tweak::apply(const Selection &Sel, - const format::FormatStyle &Style) { - auto RawReplacements = execute(Sel); - if (!RawReplacements) - return RawReplacements; - return cleanupAndFormat(Sel.Code, *RawReplacements, Style); -} - std::vector> prepareTweaks(const Tweak::Selection &S) { validateRegistry(); Index: clangd/refactor/tweaks/SwapIfBranches.cpp =================================================================== --- clangd/refactor/tweaks/SwapIfBranches.cpp +++ clangd/refactor/tweaks/SwapIfBranches.cpp @@ -37,11 +37,9 @@ const char *id() const override final; bool prepare(const Selection &Inputs) override; + Expected apply(const Selection &Inputs) override; std::string title() const override; -protected: - Expected execute(const Selection &Inputs) override; - private: const IfStmt *If = nullptr; }; @@ -62,8 +60,7 @@ dyn_cast_or_null(If->getElse()); } -Expected -SwapIfBranches::execute(const Selection &Inputs) { +Expected SwapIfBranches::apply(const Selection &Inputs) { auto &Ctx = Inputs.AST.getASTContext(); auto &SrcMgr = Ctx.getSourceManager(); Index: unittests/clangd/TweakTests.cpp =================================================================== --- unittests/clangd/TweakTests.cpp +++ unittests/clangd/TweakTests.cpp @@ -98,7 +98,7 @@ auto T = prepareTweak(ID, S); if (!T) return T.takeError(); - auto Replacements = (*T)->apply(S, clang::format::getLLVMStyle()); + auto Replacements = (*T)->apply(S); if (!Replacements) return Replacements.takeError(); return applyAllReplacements(Code.code(), *Replacements); @@ -127,40 +127,12 @@ llvm::StringLiteral Input = R"cpp( void test() { - ^if (true) { - return 100; - } else { - continue; - } + ^if (true) { return 100; } else { continue; } } )cpp"; llvm::StringLiteral Output = R"cpp( void test() { - if (true) { - continue; - } else { - return 100; - } - } - )cpp"; - checkTransform(ID, Input, Output); - - Input = R"cpp( - void test() { - ^if () { - return 100; - } else { - continue; - } - } - )cpp"; - Output = R"cpp( - void test() { - if () { - continue; - } else { - return 100; - } + if (true) { continue; } else { return 100; } } )cpp"; checkTransform(ID, Input, Output); @@ -172,11 +144,7 @@ )cpp"; Output = R"cpp( void test() { - if () { - continue; - } else { - return 100; - } + if () { continue; } else { return 100; } } )cpp"; checkTransform(ID, Input, Output);