2017-12-04 21:49:59 +08:00
|
|
|
//===--- CodeComplete.h -----------------------------------------*- C++-*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Code completion provides suggestions for what the user might type next.
|
|
|
|
// After "std::string S; S." we might suggest members of std::string.
|
|
|
|
// Signature help describes the parameters of a function as you type them.
|
|
|
|
//
|
|
|
|
//===---------------------------------------------------------------------===//
|
|
|
|
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_CODECOMPLETE_H
|
|
|
|
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_CODECOMPLETE_H
|
|
|
|
|
2018-05-15 23:29:32 +08:00
|
|
|
#include "Headers.h"
|
2017-12-04 21:49:59 +08:00
|
|
|
#include "Logger.h"
|
|
|
|
#include "Path.h"
|
|
|
|
#include "Protocol.h"
|
2017-12-20 00:50:37 +08:00
|
|
|
#include "index/Index.h"
|
2017-12-04 21:49:59 +08:00
|
|
|
#include "clang/Frontend/PrecompiledPreamble.h"
|
|
|
|
#include "clang/Sema/CodeCompleteOptions.h"
|
|
|
|
#include "clang/Tooling/CompilationDatabase.h"
|
|
|
|
|
|
|
|
namespace clang {
|
[clangd] Add "member" symbols to the index
Summary:
This adds more symbols to the index:
- member variables and functions
- enum constants in scoped enums
The code completion behavior should remain intact but workspace symbols should
now provide much more useful symbols.
Other symbols should be considered such as the ones in "main files" (files not
being included) but this can be done separately as this introduces its fair
share of problems.
Signed-off-by: Marc-Andre Laperle <marc-andre.laperle@ericsson.com>
Reviewers: ioeric, sammccall
Reviewed By: ioeric, sammccall
Subscribers: hokein, sammccall, jkorous, klimek, ilya-biryukov, jkorous-apple, ioeric, MaskRay, cfe-commits
Differential Revision: https://reviews.llvm.org/D44954
llvm-svn: 334017
2018-06-05 22:01:40 +08:00
|
|
|
class NamedDecl;
|
2017-12-04 21:49:59 +08:00
|
|
|
class PCHContainerOperations;
|
|
|
|
namespace clangd {
|
|
|
|
|
|
|
|
struct CodeCompleteOptions {
|
|
|
|
/// Returns options that can be passed to clang's completion engine.
|
|
|
|
clang::CodeCompleteOptions getClangCompleteOpts() const;
|
|
|
|
|
|
|
|
/// When true, completion items will contain expandable code snippets in
|
|
|
|
/// completion (e.g. `return ${1:expression}` or `foo(${1:int a}, ${2:int
|
|
|
|
/// b})).
|
|
|
|
bool EnableSnippets = false;
|
|
|
|
|
|
|
|
/// Add code patterns to completion results.
|
|
|
|
/// If EnableSnippets is false, this options is ignored and code patterns will
|
|
|
|
/// always be omitted.
|
|
|
|
bool IncludeCodePatterns = true;
|
|
|
|
|
|
|
|
/// Add macros to code completion results.
|
|
|
|
bool IncludeMacros = true;
|
|
|
|
|
2018-05-16 20:32:44 +08:00
|
|
|
/// Add comments to code completion results, if available.
|
|
|
|
bool IncludeComments = true;
|
2017-12-04 21:49:59 +08:00
|
|
|
|
|
|
|
/// Include results that are not legal completions in the current context.
|
|
|
|
/// For example, private members are usually inaccessible.
|
|
|
|
bool IncludeIneligibleResults = false;
|
|
|
|
|
[clangd] Add option to fold overloads into a single completion item.
Summary:
Adds a CodeCompleteOption to folds together compatible function/method overloads
into a single item. This feels pretty good (for editors with signatureHelp
support), but has limitations.
This happens in the code completion merge step, so there may be inconsistencies
(e.g. if only one overload made it into the index result list, no folding).
We don't want to bundle together completions that have different side-effects
(include insertion), because we can't constructo a coherent CompletionItem.
This may be confusing for users, as the reason for non-bundling may not
be immediately obvious. (Also, the implementation seems a little fragile)
Subscribers: ilya-biryukov, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D47957
llvm-svn: 334822
2018-06-15 19:06:29 +08:00
|
|
|
/// Combine overloads into a single completion item where possible.
|
|
|
|
bool BundleOverloads = false;
|
|
|
|
|
2017-12-04 21:49:59 +08:00
|
|
|
/// Limit the number of results returned (0 means no limit).
|
|
|
|
/// If more results are available, we set CompletionList.isIncomplete.
|
|
|
|
size_t Limit = 0;
|
2017-12-20 00:50:37 +08:00
|
|
|
|
2018-06-15 21:34:18 +08:00
|
|
|
/// A visual indicator to prepend to the completion label to indicate whether
|
|
|
|
/// completion result would trigger an #include insertion or not.
|
|
|
|
struct IncludeInsertionIndicator {
|
|
|
|
std::string Insert = "•";
|
|
|
|
std::string NoInsert = " ";
|
|
|
|
} IncludeIndicator;
|
|
|
|
|
2018-07-05 14:20:41 +08:00
|
|
|
/// Expose origins of completion items in the label (for debugging).
|
|
|
|
bool ShowOrigins = false;
|
|
|
|
|
2017-12-20 00:50:37 +08:00
|
|
|
// Populated internally by clangd, do not set.
|
|
|
|
/// If `Index` is set, it is used to augment the code completion
|
|
|
|
/// results.
|
|
|
|
/// FIXME(ioeric): we might want a better way to pass the index around inside
|
|
|
|
/// clangd.
|
|
|
|
const SymbolIndex *Index = nullptr;
|
2017-12-04 21:49:59 +08:00
|
|
|
};
|
|
|
|
|
2018-06-29 22:47:57 +08:00
|
|
|
// Semi-structured representation of a code-complete suggestion for our C++ API.
|
|
|
|
// We don't use the LSP structures here (unlike most features) as we want
|
|
|
|
// to expose more data to allow for more precise testing and evaluation.
|
|
|
|
struct CodeCompletion {
|
|
|
|
// The unqualified name of the symbol or other completion item.
|
|
|
|
std::string Name;
|
|
|
|
// The scope qualifier for the symbol name. e.g. "ns1::ns2::"
|
|
|
|
// Empty for non-symbol completions. Not inserted, but may be displayed.
|
|
|
|
std::string Scope;
|
|
|
|
// Text that must be inserted before the name, and displayed (e.g. base::).
|
|
|
|
std::string RequiredQualifier;
|
|
|
|
// Details to be displayed following the name. Not inserted.
|
|
|
|
std::string Signature;
|
|
|
|
// Text to be inserted following the name, in snippet format.
|
|
|
|
std::string SnippetSuffix;
|
|
|
|
// Type to be displayed for this completion.
|
|
|
|
std::string ReturnType;
|
|
|
|
std::string Documentation;
|
|
|
|
CompletionItemKind Kind = CompletionItemKind::Missing;
|
|
|
|
// This completion item may represent several symbols that can be inserted in
|
|
|
|
// the same way, such as function overloads. In this case BundleSize > 1, and
|
|
|
|
// the following fields are summaries:
|
|
|
|
// - Signature is e.g. "(...)" for functions.
|
|
|
|
// - SnippetSuffix is similarly e.g. "(${0})".
|
|
|
|
// - ReturnType may be empty
|
|
|
|
// - Documentation may be from one symbol, or a combination of several
|
|
|
|
// Other fields should apply equally to all bundled completions.
|
2018-07-02 19:13:16 +08:00
|
|
|
unsigned BundleSize = 1;
|
2018-07-05 14:20:41 +08:00
|
|
|
SymbolOrigin Origin = SymbolOrigin::Unknown;
|
2018-06-29 22:47:57 +08:00
|
|
|
// The header through which this symbol could be included.
|
|
|
|
// Quoted string as expected by an #include directive, e.g. "<memory>".
|
|
|
|
// Empty for non-symbol completions, or when not known.
|
|
|
|
std::string Header;
|
|
|
|
// Present if Header is set and should be inserted to use this item.
|
|
|
|
llvm::Optional<TextEdit> HeaderInsertion;
|
|
|
|
|
|
|
|
// Scores are used to rank completion items.
|
|
|
|
struct Scores {
|
|
|
|
// The score that items are ranked by.
|
|
|
|
float Total = 0.f;
|
|
|
|
|
|
|
|
// The finalScore with the fuzzy name match score excluded.
|
|
|
|
// When filtering client-side, editors should calculate the new fuzzy score,
|
|
|
|
// whose scale is 0-1 (with 1 = prefix match, special case 2 = exact match),
|
|
|
|
// and recompute finalScore = fuzzyScore * symbolScore.
|
|
|
|
float ExcludingName = 0.f;
|
|
|
|
|
|
|
|
// Component scores that contributed to the final score:
|
|
|
|
|
|
|
|
// Quality describes how important we think this candidate is,
|
|
|
|
// independent of the query.
|
|
|
|
// e.g. symbols with lots of incoming references have higher quality.
|
|
|
|
float Quality = 0.f;
|
|
|
|
// Relevance describes how well this candidate matched the query.
|
|
|
|
// e.g. symbols from nearby files have higher relevance.
|
|
|
|
float Relevance = 0.f;
|
|
|
|
};
|
|
|
|
Scores Score;
|
|
|
|
|
|
|
|
// Serialize this to an LSP completion item. This is a lossy operation.
|
|
|
|
CompletionItem render(const CodeCompleteOptions &) const;
|
|
|
|
};
|
2018-07-02 19:13:16 +08:00
|
|
|
raw_ostream &operator<<(raw_ostream &, const CodeCompletion &);
|
2018-06-29 22:47:57 +08:00
|
|
|
struct CodeCompleteResult {
|
|
|
|
std::vector<CodeCompletion> Completions;
|
|
|
|
bool HasMore = false;
|
|
|
|
};
|
2018-07-02 19:13:16 +08:00
|
|
|
raw_ostream &operator<<(raw_ostream &, const CodeCompleteResult &);
|
2018-06-29 22:47:57 +08:00
|
|
|
|
2017-12-04 21:49:59 +08:00
|
|
|
/// Get code completions at a specified \p Pos in \p FileName.
|
2018-07-03 16:09:29 +08:00
|
|
|
CodeCompleteResult codeComplete(PathRef FileName,
|
|
|
|
const tooling::CompileCommand &Command,
|
|
|
|
PrecompiledPreamble const *Preamble,
|
|
|
|
const IncludeStructure &PreambleInclusions,
|
|
|
|
StringRef Contents, Position Pos,
|
|
|
|
IntrusiveRefCntPtr<vfs::FileSystem> VFS,
|
|
|
|
std::shared_ptr<PCHContainerOperations> PCHs,
|
|
|
|
CodeCompleteOptions Opts);
|
2017-12-04 21:49:59 +08:00
|
|
|
|
|
|
|
/// Get signature help at a specified \p Pos in \p FileName.
|
[clangd] Pass Context implicitly using TLS.
Summary:
Instead of passing Context explicitly around, we now have a thread-local
Context object `Context::current()` which is an implicit argument to
every function.
Most manipulation of this should use the WithContextValue helper, which
augments the current Context to add a single KV pair, and restores the
old context on destruction.
Advantages are:
- less boilerplate in functions that just propagate contexts
- reading most code doesn't require understanding context at all, and
using context as values in fewer places still
- fewer options to pass the "wrong" context when it changes within a
scope (e.g. when using Span)
- contexts pass through interfaces we can't modify, such as VFS
- propagating contexts across threads was slightly tricky (e.g.
copy vs move, no move-init in lambdas), and is now encapsulated in
the threadpool
Disadvantages are all the usual TLS stuff - hidden magic, and
potential for higher memory usage on threads that don't use the
context. (In practice, it's just one pointer)
Reviewers: ilya-biryukov
Subscribers: klimek, jkorous-apple, ioeric, cfe-commits
Differential Revision: https://reviews.llvm.org/D42517
llvm-svn: 323872
2018-01-31 21:40:48 +08:00
|
|
|
SignatureHelp signatureHelp(PathRef FileName,
|
2017-12-13 20:51:22 +08:00
|
|
|
const tooling::CompileCommand &Command,
|
|
|
|
PrecompiledPreamble const *Preamble,
|
|
|
|
StringRef Contents, Position Pos,
|
|
|
|
IntrusiveRefCntPtr<vfs::FileSystem> VFS,
|
|
|
|
std::shared_ptr<PCHContainerOperations> PCHs);
|
2017-12-04 21:49:59 +08:00
|
|
|
|
[clangd] Add "member" symbols to the index
Summary:
This adds more symbols to the index:
- member variables and functions
- enum constants in scoped enums
The code completion behavior should remain intact but workspace symbols should
now provide much more useful symbols.
Other symbols should be considered such as the ones in "main files" (files not
being included) but this can be done separately as this introduces its fair
share of problems.
Signed-off-by: Marc-Andre Laperle <marc-andre.laperle@ericsson.com>
Reviewers: ioeric, sammccall
Reviewed By: ioeric, sammccall
Subscribers: hokein, sammccall, jkorous, klimek, ilya-biryukov, jkorous-apple, ioeric, MaskRay, cfe-commits
Differential Revision: https://reviews.llvm.org/D44954
llvm-svn: 334017
2018-06-05 22:01:40 +08:00
|
|
|
// For index-based completion, we only consider:
|
|
|
|
// * symbols in namespaces or translation unit scopes (e.g. no class
|
|
|
|
// members, no locals)
|
|
|
|
// * enum constants in unscoped enum decl (e.g. "red" in "enum {red};")
|
|
|
|
// * primary templates (no specializations)
|
|
|
|
// For the other cases, we let Clang do the completion because it does not
|
|
|
|
// need any non-local information and it will be much better at following
|
|
|
|
// lookup rules. Other symbols still appear in the index for other purposes,
|
|
|
|
// like workspace/symbols or textDocument/definition, but are not used for code
|
|
|
|
// completion.
|
|
|
|
bool isIndexedForCodeCompletion(const NamedDecl &ND, ASTContext &ASTCtx);
|
2017-12-04 21:49:59 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|
|
|
|
|
|
|
|
#endif
|