Author: ibiryukov Date: Tue May 28 08:33:37 2019 New Revision: 361841 URL: http://llvm.org/viewvc/llvm-project?rev=361841&view=rev Log: [clangd] Place cursor better after completing patterns
Summary: By producing the $0 marker in the snippets at the last placeholder. This produces nicer results in most cases, e.g. for namespace <#name#> { <#decls#> } we now produce ${0:decls} instead of ${2:decls} and the final cursor placement is more convenient. Reviewers: hokein Reviewed By: hokein Subscribers: MaskRay, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D62389 Modified: clang-tools-extra/trunk/clangd/CodeComplete.cpp clang-tools-extra/trunk/clangd/CodeCompletionStrings.cpp clang-tools-extra/trunk/clangd/CodeCompletionStrings.h clang-tools-extra/trunk/clangd/unittests/CodeCompleteTests.cpp clang-tools-extra/trunk/clangd/unittests/CodeCompletionStringsTests.cpp Modified: clang-tools-extra/trunk/clangd/CodeComplete.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/CodeComplete.cpp?rev=361841&r1=361840&r2=361841&view=diff ============================================================================== --- clang-tools-extra/trunk/clangd/CodeComplete.cpp (original) +++ clang-tools-extra/trunk/clangd/CodeComplete.cpp Tue May 28 08:33:37 2019 @@ -394,8 +394,9 @@ struct CodeCompletionBuilder { Bundled.emplace_back(); BundledEntry &S = Bundled.back(); if (C.SemaResult) { + bool IsPattern = C.SemaResult->Kind == CodeCompletionResult::RK_Pattern; getSignature(*SemaCCS, &S.Signature, &S.SnippetSuffix, - &Completion.RequiredQualifier); + &Completion.RequiredQualifier, IsPattern); S.ReturnType = getReturnType(*SemaCCS); } else if (C.IndexResult) { S.Signature = C.IndexResult->Signature; Modified: clang-tools-extra/trunk/clangd/CodeCompletionStrings.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/CodeCompletionStrings.cpp?rev=361841&r1=361840&r2=361841&view=diff ============================================================================== --- clang-tools-extra/trunk/clangd/CodeCompletionStrings.cpp (original) +++ clang-tools-extra/trunk/clangd/CodeCompletionStrings.cpp Tue May 28 08:33:37 2019 @@ -11,6 +11,8 @@ #include "clang/AST/DeclObjC.h" #include "clang/AST/RawCommentList.h" #include "clang/Basic/SourceManager.h" +#include "clang/Sema/CodeCompleteConsumer.h" +#include <limits> #include <utility> namespace clang { @@ -73,8 +75,23 @@ std::string getDeclComment(const ASTCont } void getSignature(const CodeCompletionString &CCS, std::string *Signature, - std::string *Snippet, std::string *RequiredQualifiers) { - unsigned ArgCount = 0; + std::string *Snippet, std::string *RequiredQualifiers, + bool CompletingPattern) { + // Placeholder with this index will be ${0:â¦} to mark final cursor position. + // Usually we do not add $0, so the cursor is placed at end of completed text. + unsigned CursorSnippetArg = std::numeric_limits<unsigned>::max(); + if (CompletingPattern) { + // In patterns, it's best to place the cursor at the last placeholder, to + // handle cases like + // namespace ${1:name} { + // ${0:decls} + // } + CursorSnippetArg = + llvm::count_if(CCS, [](const CodeCompletionString::Chunk &C) { + return C.Kind == CodeCompletionString::CK_Placeholder; + }); + } + unsigned SnippetArg = 0; bool HadObjCArguments = false; for (const auto &Chunk : CCS) { // Informative qualifier chunks only clutter completion results, skip @@ -124,8 +141,10 @@ void getSignature(const CodeCompletionSt break; case CodeCompletionString::CK_Placeholder: *Signature += Chunk.Text; - ++ArgCount; - *Snippet += "${" + std::to_string(ArgCount) + ':'; + ++SnippetArg; + *Snippet += + "${" + + std::to_string(SnippetArg == CursorSnippetArg ? 0 : SnippetArg) + ':'; appendEscapeSnippet(Chunk.Text, Snippet); *Snippet += '}'; break; Modified: clang-tools-extra/trunk/clangd/CodeCompletionStrings.h URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/CodeCompletionStrings.h?rev=361841&r1=361840&r2=361841&view=diff ============================================================================== --- clang-tools-extra/trunk/clangd/CodeCompletionStrings.h (original) +++ clang-tools-extra/trunk/clangd/CodeCompletionStrings.h Tue May 28 08:33:37 2019 @@ -38,12 +38,16 @@ std::string getDeclComment(const ASTCont /// Formats the signature for an item, as a display string and snippet. /// e.g. for const_reference std::vector<T>::at(size_type) const, this returns: /// *Signature = "(size_type) const" -/// *Snippet = "(${0:size_type})" +/// *Snippet = "(${1:size_type})" /// If set, RequiredQualifiers is the text that must be typed before the name. /// e.g "Base::" when calling a base class member function that's hidden. +/// +/// When \p CompletingPattern is true, the last placeholder will be of the form +/// ${0:â¦}, indicating the cursor should stay there. void getSignature(const CodeCompletionString &CCS, std::string *Signature, std::string *Snippet, - std::string *RequiredQualifiers = nullptr); + std::string *RequiredQualifiers = nullptr, + bool CompletingPattern = false); /// Assembles formatted documentation for a completion result. This includes /// documentation comments and other relevant information like annotations. Modified: clang-tools-extra/trunk/clangd/unittests/CodeCompleteTests.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/CodeCompleteTests.cpp?rev=361841&r1=361840&r2=361841&view=diff ============================================================================== --- clang-tools-extra/trunk/clangd/unittests/CodeCompleteTests.cpp (original) +++ clang-tools-extra/trunk/clangd/unittests/CodeCompleteTests.cpp Tue May 28 08:33:37 2019 @@ -2382,6 +2382,28 @@ TEST(CompletionTest, ObjectiveCMethodTwo EXPECT_THAT(C, ElementsAre(SnippetSuffix("${1:(unsigned int)}"))); } +TEST(CompletionTest, CursorInSnippets) { + clangd::CodeCompleteOptions Options; + Options.EnableSnippets = true; + auto Results = completions( + R"cpp( + void while_foo(int a, int b); + void test() { + whil^ + })cpp", + /*IndexSymbols=*/{}, Options); + + // Last placeholder in code patterns should be $0 to put the cursor there. + EXPECT_THAT( + Results.Completions, + Contains(AllOf(Named("while"), + SnippetSuffix("(${1:condition}){${0:statements}\n}")))); + // However, snippets for functions must *not* end with $0. + EXPECT_THAT(Results.Completions, + Contains(AllOf(Named("while_foo"), + SnippetSuffix("(${1:int a}, ${2:int b})")))); +} + TEST(CompletionTest, WorksWithNullType) { auto R = completions(R"cpp( int main() { Modified: clang-tools-extra/trunk/clangd/unittests/CodeCompletionStringsTests.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clangd/unittests/CodeCompletionStringsTests.cpp?rev=361841&r1=361840&r2=361841&view=diff ============================================================================== --- clang-tools-extra/trunk/clangd/unittests/CodeCompletionStringsTests.cpp (original) +++ clang-tools-extra/trunk/clangd/unittests/CodeCompletionStringsTests.cpp Tue May 28 08:33:37 2019 @@ -22,10 +22,12 @@ public: CCTUInfo(Allocator), Builder(*Allocator, CCTUInfo) {} protected: - void computeSignature(const CodeCompletionString &CCS) { + void computeSignature(const CodeCompletionString &CCS, + bool CompletingPattern = false) { Signature.clear(); Snippet.clear(); - getSignature(CCS, &Signature, &Snippet); + getSignature(CCS, &Signature, &Snippet, /*RequiredQualifier=*/nullptr, + CompletingPattern); } std::shared_ptr<clang::GlobalCodeCompletionAllocator> Allocator; @@ -99,6 +101,25 @@ TEST_F(CompletionStringTest, EscapeSnipp EXPECT_EQ(Snippet, "(${1:\\$p\\}1\\\\})"); } +TEST_F(CompletionStringTest, SnippetsInPatterns) { + auto MakeCCS = [this]() -> const CodeCompletionString & { + CodeCompletionBuilder Builder(*Allocator, CCTUInfo); + Builder.AddTypedTextChunk("namespace"); + Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace); + Builder.AddPlaceholderChunk("name"); + Builder.AddChunk(CodeCompletionString::CK_Equal); + Builder.AddPlaceholderChunk("target"); + Builder.AddChunk(CodeCompletionString::CK_SemiColon); + return *Builder.TakeString(); + }; + computeSignature(MakeCCS(), /*CompletingPattern=*/false); + EXPECT_EQ(Snippet, " ${1:name} = ${2:target};"); + + // When completing a pattern, the last placeholder holds the cursor position. + computeSignature(MakeCCS(), /*CompletingPattern=*/true); + EXPECT_EQ(Snippet, " ${1:name} = ${0:target};"); +} + TEST_F(CompletionStringTest, IgnoreInformativeQualifier) { Builder.AddTypedTextChunk("X"); Builder.AddInformativeChunk("info ok"); _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits