From 8534675cefb427f33c4fa083d2751ef09acccaf5 Mon Sep 17 00:00:00 2001 From: Ilya Biryukov Date: Tue, 28 May 2019 15:33:37 +0000 Subject: [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 llvm-svn: 361841 --- clang-tools-extra/clangd/CodeCompletionStrings.cpp | 27 ++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'clang-tools-extra/clangd/CodeCompletionStrings.cpp') diff --git a/clang-tools-extra/clangd/CodeCompletionStrings.cpp b/clang-tools-extra/clangd/CodeCompletionStrings.cpp index 586be67e92c..bf3cabc2698 100644 --- a/clang-tools-extra/clangd/CodeCompletionStrings.cpp +++ b/clang-tools-extra/clangd/CodeCompletionStrings.cpp @@ -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 #include namespace clang { @@ -73,8 +75,23 @@ std::string getDeclComment(const ASTContext &Ctx, const NamedDecl &Decl) { } 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::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 CodeCompletionString &CCS, std::string *Signature, 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; -- cgit v1.2.3