2018-08-14 16:03:32 +00:00
|
|
|
//===--- Quality.cpp ---------------------------------------------*- C++-*-===//
|
[clangd] Extract scoring/ranking logic, and shave yaks.
Summary:
Code completion scoring was embedded in CodeComplete.cpp, which is bad:
- awkward to test. The mechanisms (extracting info from index/sema) can be
unit-tested well, the policy (scoring) should be quantitatively measured.
Neither was easily possible, and debugging was hard.
The intermediate signal struct makes this easier.
- hard to reuse. This is a bug in workspaceSymbols: it just presents the
results in the index order, which is not sorted in practice, it needs to rank
them!
Also, index implementations care about scoring (both query-dependent and
independent) in order to truncate result lists appropriately.
The main yak shaved here is the build() function that had 3 variants across
unit tests is unified in TestTU.h (rather than adding a 4th variant).
Reviewers: ilya-biryukov
Subscribers: klimek, mgorny, ioeric, MaskRay, jkorous, mgrang, cfe-commits
Differential Revision: https://reviews.llvm.org/D46524
llvm-svn: 332378
2018-05-15 17:43:27 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
2018-08-14 16:03:32 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
[clangd] Extract scoring/ranking logic, and shave yaks.
Summary:
Code completion scoring was embedded in CodeComplete.cpp, which is bad:
- awkward to test. The mechanisms (extracting info from index/sema) can be
unit-tested well, the policy (scoring) should be quantitatively measured.
Neither was easily possible, and debugging was hard.
The intermediate signal struct makes this easier.
- hard to reuse. This is a bug in workspaceSymbols: it just presents the
results in the index order, which is not sorted in practice, it needs to rank
them!
Also, index implementations care about scoring (both query-dependent and
independent) in order to truncate result lists appropriately.
The main yak shaved here is the build() function that had 3 variants across
unit tests is unified in TestTU.h (rather than adding a 4th variant).
Reviewers: ilya-biryukov
Subscribers: klimek, mgorny, ioeric, MaskRay, jkorous, mgrang, cfe-commits
Differential Revision: https://reviews.llvm.org/D46524
llvm-svn: 332378
2018-05-15 17:43:27 +00:00
|
|
|
#include "Quality.h"
|
2018-10-18 12:23:05 +00:00
|
|
|
#include "AST.h"
|
2018-07-03 08:09:29 +00:00
|
|
|
#include "FileDistance.h"
|
2018-06-15 08:58:12 +00:00
|
|
|
#include "URI.h"
|
[clangd] Extract scoring/ranking logic, and shave yaks.
Summary:
Code completion scoring was embedded in CodeComplete.cpp, which is bad:
- awkward to test. The mechanisms (extracting info from index/sema) can be
unit-tested well, the policy (scoring) should be quantitatively measured.
Neither was easily possible, and debugging was hard.
The intermediate signal struct makes this easier.
- hard to reuse. This is a bug in workspaceSymbols: it just presents the
results in the index order, which is not sorted in practice, it needs to rank
them!
Also, index implementations care about scoring (both query-dependent and
independent) in order to truncate result lists appropriately.
The main yak shaved here is the build() function that had 3 variants across
unit tests is unified in TestTU.h (rather than adding a 4th variant).
Reviewers: ilya-biryukov
Subscribers: klimek, mgorny, ioeric, MaskRay, jkorous, mgrang, cfe-commits
Differential Revision: https://reviews.llvm.org/D46524
llvm-svn: 332378
2018-05-15 17:43:27 +00:00
|
|
|
#include "index/Index.h"
|
2018-06-04 14:50:59 +00:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2018-07-23 10:56:37 +00:00
|
|
|
#include "clang/AST/Decl.h"
|
2018-07-05 08:14:04 +00:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
2018-07-23 10:56:37 +00:00
|
|
|
#include "clang/AST/DeclTemplate.h"
|
2018-06-06 08:53:36 +00:00
|
|
|
#include "clang/AST/DeclVisitor.h"
|
2018-07-03 08:09:29 +00:00
|
|
|
#include "clang/Basic/CharInfo.h"
|
2018-06-04 14:50:59 +00:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
[clangd] Extract scoring/ranking logic, and shave yaks.
Summary:
Code completion scoring was embedded in CodeComplete.cpp, which is bad:
- awkward to test. The mechanisms (extracting info from index/sema) can be
unit-tested well, the policy (scoring) should be quantitatively measured.
Neither was easily possible, and debugging was hard.
The intermediate signal struct makes this easier.
- hard to reuse. This is a bug in workspaceSymbols: it just presents the
results in the index order, which is not sorted in practice, it needs to rank
them!
Also, index implementations care about scoring (both query-dependent and
independent) in order to truncate result lists appropriately.
The main yak shaved here is the build() function that had 3 variants across
unit tests is unified in TestTU.h (rather than adding a 4th variant).
Reviewers: ilya-biryukov
Subscribers: klimek, mgorny, ioeric, MaskRay, jkorous, mgrang, cfe-commits
Differential Revision: https://reviews.llvm.org/D46524
llvm-svn: 332378
2018-05-15 17:43:27 +00:00
|
|
|
#include "clang/Sema/CodeCompleteConsumer.h"
|
2018-10-17 11:19:02 +00:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
2018-07-05 08:14:04 +00:00
|
|
|
#include "llvm/Support/Casting.h"
|
[clangd] Extract scoring/ranking logic, and shave yaks.
Summary:
Code completion scoring was embedded in CodeComplete.cpp, which is bad:
- awkward to test. The mechanisms (extracting info from index/sema) can be
unit-tested well, the policy (scoring) should be quantitatively measured.
Neither was easily possible, and debugging was hard.
The intermediate signal struct makes this easier.
- hard to reuse. This is a bug in workspaceSymbols: it just presents the
results in the index order, which is not sorted in practice, it needs to rank
them!
Also, index implementations care about scoring (both query-dependent and
independent) in order to truncate result lists appropriately.
The main yak shaved here is the build() function that had 3 variants across
unit tests is unified in TestTU.h (rather than adding a 4th variant).
Reviewers: ilya-biryukov
Subscribers: klimek, mgorny, ioeric, MaskRay, jkorous, mgrang, cfe-commits
Differential Revision: https://reviews.llvm.org/D46524
llvm-svn: 332378
2018-05-15 17:43:27 +00:00
|
|
|
#include "llvm/Support/FormatVariadic.h"
|
|
|
|
#include "llvm/Support/MathExtras.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2018-10-17 11:19:02 +00:00
|
|
|
#include <algorithm>
|
2018-07-03 08:09:29 +00:00
|
|
|
#include <cmath>
|
[clangd] Extract scoring/ranking logic, and shave yaks.
Summary:
Code completion scoring was embedded in CodeComplete.cpp, which is bad:
- awkward to test. The mechanisms (extracting info from index/sema) can be
unit-tested well, the policy (scoring) should be quantitatively measured.
Neither was easily possible, and debugging was hard.
The intermediate signal struct makes this easier.
- hard to reuse. This is a bug in workspaceSymbols: it just presents the
results in the index order, which is not sorted in practice, it needs to rank
them!
Also, index implementations care about scoring (both query-dependent and
independent) in order to truncate result lists appropriately.
The main yak shaved here is the build() function that had 3 variants across
unit tests is unified in TestTU.h (rather than adding a 4th variant).
Reviewers: ilya-biryukov
Subscribers: klimek, mgorny, ioeric, MaskRay, jkorous, mgrang, cfe-commits
Differential Revision: https://reviews.llvm.org/D46524
llvm-svn: 332378
2018-05-15 17:43:27 +00:00
|
|
|
|
2018-10-20 15:30:37 +00:00
|
|
|
using namespace llvm;
|
[clangd] Extract scoring/ranking logic, and shave yaks.
Summary:
Code completion scoring was embedded in CodeComplete.cpp, which is bad:
- awkward to test. The mechanisms (extracting info from index/sema) can be
unit-tested well, the policy (scoring) should be quantitatively measured.
Neither was easily possible, and debugging was hard.
The intermediate signal struct makes this easier.
- hard to reuse. This is a bug in workspaceSymbols: it just presents the
results in the index order, which is not sorted in practice, it needs to rank
them!
Also, index implementations care about scoring (both query-dependent and
independent) in order to truncate result lists appropriately.
The main yak shaved here is the build() function that had 3 variants across
unit tests is unified in TestTU.h (rather than adding a 4th variant).
Reviewers: ilya-biryukov
Subscribers: klimek, mgorny, ioeric, MaskRay, jkorous, mgrang, cfe-commits
Differential Revision: https://reviews.llvm.org/D46524
llvm-svn: 332378
2018-05-15 17:43:27 +00:00
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
2018-07-26 12:05:31 +00:00
|
|
|
static bool isReserved(StringRef Name) {
|
2018-06-08 09:36:34 +00:00
|
|
|
// FIXME: Should we exclude _Bool and others recognized by the standard?
|
|
|
|
return Name.size() >= 2 && Name[0] == '_' &&
|
|
|
|
(isUppercase(Name[1]) || Name[1] == '_');
|
|
|
|
}
|
[clangd] Extract scoring/ranking logic, and shave yaks.
Summary:
Code completion scoring was embedded in CodeComplete.cpp, which is bad:
- awkward to test. The mechanisms (extracting info from index/sema) can be
unit-tested well, the policy (scoring) should be quantitatively measured.
Neither was easily possible, and debugging was hard.
The intermediate signal struct makes this easier.
- hard to reuse. This is a bug in workspaceSymbols: it just presents the
results in the index order, which is not sorted in practice, it needs to rank
them!
Also, index implementations care about scoring (both query-dependent and
independent) in order to truncate result lists appropriately.
The main yak shaved here is the build() function that had 3 variants across
unit tests is unified in TestTU.h (rather than adding a 4th variant).
Reviewers: ilya-biryukov
Subscribers: klimek, mgorny, ioeric, MaskRay, jkorous, mgrang, cfe-commits
Differential Revision: https://reviews.llvm.org/D46524
llvm-svn: 332378
2018-05-15 17:43:27 +00:00
|
|
|
|
2018-06-04 14:50:59 +00:00
|
|
|
static bool hasDeclInMainFile(const Decl &D) {
|
|
|
|
auto &SourceMgr = D.getASTContext().getSourceManager();
|
|
|
|
for (auto *Redecl : D.redecls()) {
|
|
|
|
auto Loc = SourceMgr.getSpellingLoc(Redecl->getLocation());
|
|
|
|
if (SourceMgr.isWrittenInMainFile(Loc))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-11 14:49:49 +00:00
|
|
|
static bool hasUsingDeclInMainFile(const CodeCompletionResult &R) {
|
|
|
|
const auto &Context = R.Declaration->getASTContext();
|
|
|
|
const auto &SourceMgr = Context.getSourceManager();
|
|
|
|
if (R.ShadowDecl) {
|
|
|
|
const auto Loc = SourceMgr.getExpansionLoc(R.ShadowDecl->getLocation());
|
|
|
|
if (SourceMgr.isWrittenInMainFile(Loc))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-06 08:53:36 +00:00
|
|
|
static SymbolQualitySignals::SymbolCategory categorize(const NamedDecl &ND) {
|
2018-11-30 11:17:15 +00:00
|
|
|
if (const auto *FD = dyn_cast<FunctionDecl>(&ND)) {
|
|
|
|
if (FD->isOverloadedOperator())
|
|
|
|
return SymbolQualitySignals::Operator;
|
|
|
|
}
|
2018-06-06 08:53:36 +00:00
|
|
|
class Switch
|
|
|
|
: public ConstDeclVisitor<Switch, SymbolQualitySignals::SymbolCategory> {
|
|
|
|
public:
|
|
|
|
#define MAP(DeclType, Category) \
|
|
|
|
SymbolQualitySignals::SymbolCategory Visit##DeclType(const DeclType *) { \
|
|
|
|
return SymbolQualitySignals::Category; \
|
|
|
|
}
|
|
|
|
MAP(NamespaceDecl, Namespace);
|
|
|
|
MAP(NamespaceAliasDecl, Namespace);
|
|
|
|
MAP(TypeDecl, Type);
|
|
|
|
MAP(TypeAliasTemplateDecl, Type);
|
|
|
|
MAP(ClassTemplateDecl, Type);
|
2018-07-24 08:51:52 +00:00
|
|
|
MAP(CXXConstructorDecl, Constructor);
|
2018-11-30 11:17:15 +00:00
|
|
|
MAP(CXXDestructorDecl, Destructor);
|
2018-06-06 08:53:36 +00:00
|
|
|
MAP(ValueDecl, Variable);
|
|
|
|
MAP(VarTemplateDecl, Variable);
|
|
|
|
MAP(FunctionDecl, Function);
|
|
|
|
MAP(FunctionTemplateDecl, Function);
|
|
|
|
MAP(Decl, Unknown);
|
|
|
|
#undef MAP
|
|
|
|
};
|
|
|
|
return Switch().Visit(&ND);
|
|
|
|
}
|
|
|
|
|
2018-07-05 09:37:26 +00:00
|
|
|
static SymbolQualitySignals::SymbolCategory
|
|
|
|
categorize(const CodeCompletionResult &R) {
|
2018-06-14 13:42:21 +00:00
|
|
|
if (R.Declaration)
|
|
|
|
return categorize(*R.Declaration);
|
|
|
|
if (R.Kind == CodeCompletionResult::RK_Macro)
|
|
|
|
return SymbolQualitySignals::Macro;
|
|
|
|
// Everything else is a keyword or a pattern. Patterns are mostly keywords
|
|
|
|
// too, except a few which we recognize by cursor kind.
|
|
|
|
switch (R.CursorKind) {
|
2018-07-05 09:37:26 +00:00
|
|
|
case CXCursor_CXXMethod:
|
|
|
|
return SymbolQualitySignals::Function;
|
|
|
|
case CXCursor_ModuleImportDecl:
|
|
|
|
return SymbolQualitySignals::Namespace;
|
|
|
|
case CXCursor_MacroDefinition:
|
|
|
|
return SymbolQualitySignals::Macro;
|
|
|
|
case CXCursor_TypeRef:
|
|
|
|
return SymbolQualitySignals::Type;
|
|
|
|
case CXCursor_MemberRef:
|
|
|
|
return SymbolQualitySignals::Variable;
|
2018-07-24 08:51:52 +00:00
|
|
|
case CXCursor_Constructor:
|
|
|
|
return SymbolQualitySignals::Constructor;
|
2018-07-05 09:37:26 +00:00
|
|
|
default:
|
|
|
|
return SymbolQualitySignals::Keyword;
|
2018-06-14 13:42:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-06 08:53:36 +00:00
|
|
|
static SymbolQualitySignals::SymbolCategory
|
|
|
|
categorize(const index::SymbolInfo &D) {
|
|
|
|
switch (D.Kind) {
|
2018-07-05 09:37:26 +00:00
|
|
|
case index::SymbolKind::Namespace:
|
|
|
|
case index::SymbolKind::NamespaceAlias:
|
|
|
|
return SymbolQualitySignals::Namespace;
|
|
|
|
case index::SymbolKind::Macro:
|
|
|
|
return SymbolQualitySignals::Macro;
|
|
|
|
case index::SymbolKind::Enum:
|
|
|
|
case index::SymbolKind::Struct:
|
|
|
|
case index::SymbolKind::Class:
|
|
|
|
case index::SymbolKind::Protocol:
|
|
|
|
case index::SymbolKind::Extension:
|
|
|
|
case index::SymbolKind::Union:
|
|
|
|
case index::SymbolKind::TypeAlias:
|
|
|
|
return SymbolQualitySignals::Type;
|
|
|
|
case index::SymbolKind::Function:
|
|
|
|
case index::SymbolKind::ClassMethod:
|
|
|
|
case index::SymbolKind::InstanceMethod:
|
|
|
|
case index::SymbolKind::StaticMethod:
|
|
|
|
case index::SymbolKind::InstanceProperty:
|
|
|
|
case index::SymbolKind::ClassProperty:
|
|
|
|
case index::SymbolKind::StaticProperty:
|
|
|
|
case index::SymbolKind::ConversionFunction:
|
|
|
|
return SymbolQualitySignals::Function;
|
2018-11-30 11:17:15 +00:00
|
|
|
case index::SymbolKind::Destructor:
|
|
|
|
return SymbolQualitySignals::Destructor;
|
2018-07-24 08:51:52 +00:00
|
|
|
case index::SymbolKind::Constructor:
|
|
|
|
return SymbolQualitySignals::Constructor;
|
2018-07-05 09:37:26 +00:00
|
|
|
case index::SymbolKind::Variable:
|
|
|
|
case index::SymbolKind::Field:
|
|
|
|
case index::SymbolKind::EnumConstant:
|
|
|
|
case index::SymbolKind::Parameter:
|
|
|
|
return SymbolQualitySignals::Variable;
|
|
|
|
case index::SymbolKind::Using:
|
|
|
|
case index::SymbolKind::Module:
|
|
|
|
case index::SymbolKind::Unknown:
|
|
|
|
return SymbolQualitySignals::Unknown;
|
2018-06-06 08:53:36 +00:00
|
|
|
}
|
2018-06-06 13:28:49 +00:00
|
|
|
llvm_unreachable("Unknown index::SymbolKind");
|
2018-06-06 08:53:36 +00:00
|
|
|
}
|
|
|
|
|
2018-07-23 10:56:37 +00:00
|
|
|
static bool isInstanceMember(const NamedDecl *ND) {
|
|
|
|
if (!ND)
|
|
|
|
return false;
|
|
|
|
if (const auto *TP = dyn_cast<FunctionTemplateDecl>(ND))
|
|
|
|
ND = TP->TemplateDecl::getTemplatedDecl();
|
|
|
|
if (const auto *CM = dyn_cast<CXXMethodDecl>(ND))
|
|
|
|
return !CM->isStatic();
|
|
|
|
return isa<FieldDecl>(ND); // Note that static fields are VarDecl.
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isInstanceMember(const index::SymbolInfo &D) {
|
|
|
|
switch (D.Kind) {
|
|
|
|
case index::SymbolKind::InstanceMethod:
|
|
|
|
case index::SymbolKind::InstanceProperty:
|
|
|
|
case index::SymbolKind::Field:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
[clangd] Extract scoring/ranking logic, and shave yaks.
Summary:
Code completion scoring was embedded in CodeComplete.cpp, which is bad:
- awkward to test. The mechanisms (extracting info from index/sema) can be
unit-tested well, the policy (scoring) should be quantitatively measured.
Neither was easily possible, and debugging was hard.
The intermediate signal struct makes this easier.
- hard to reuse. This is a bug in workspaceSymbols: it just presents the
results in the index order, which is not sorted in practice, it needs to rank
them!
Also, index implementations care about scoring (both query-dependent and
independent) in order to truncate result lists appropriately.
The main yak shaved here is the build() function that had 3 variants across
unit tests is unified in TestTU.h (rather than adding a 4th variant).
Reviewers: ilya-biryukov
Subscribers: klimek, mgorny, ioeric, MaskRay, jkorous, mgrang, cfe-commits
Differential Revision: https://reviews.llvm.org/D46524
llvm-svn: 332378
2018-05-15 17:43:27 +00:00
|
|
|
void SymbolQualitySignals::merge(const CodeCompletionResult &SemaCCResult) {
|
2018-09-06 18:52:26 +00:00
|
|
|
Deprecated |= (SemaCCResult.Availability == CXAvailability_Deprecated);
|
2018-06-14 13:42:21 +00:00
|
|
|
Category = categorize(SemaCCResult);
|
2018-06-08 09:36:34 +00:00
|
|
|
|
|
|
|
if (SemaCCResult.Declaration) {
|
2018-10-18 12:23:05 +00:00
|
|
|
ImplementationDetail |= isImplementationDetail(SemaCCResult.Declaration);
|
2018-06-08 09:36:34 +00:00
|
|
|
if (auto *ID = SemaCCResult.Declaration->getIdentifier())
|
2018-07-26 12:05:31 +00:00
|
|
|
ReservedName = ReservedName || isReserved(ID->getName());
|
2018-06-08 09:36:34 +00:00
|
|
|
} else if (SemaCCResult.Kind == CodeCompletionResult::RK_Macro)
|
2018-07-26 12:05:31 +00:00
|
|
|
ReservedName = ReservedName || isReserved(SemaCCResult.Macro->getName());
|
[clangd] Extract scoring/ranking logic, and shave yaks.
Summary:
Code completion scoring was embedded in CodeComplete.cpp, which is bad:
- awkward to test. The mechanisms (extracting info from index/sema) can be
unit-tested well, the policy (scoring) should be quantitatively measured.
Neither was easily possible, and debugging was hard.
The intermediate signal struct makes this easier.
- hard to reuse. This is a bug in workspaceSymbols: it just presents the
results in the index order, which is not sorted in practice, it needs to rank
them!
Also, index implementations care about scoring (both query-dependent and
independent) in order to truncate result lists appropriately.
The main yak shaved here is the build() function that had 3 variants across
unit tests is unified in TestTU.h (rather than adding a 4th variant).
Reviewers: ilya-biryukov
Subscribers: klimek, mgorny, ioeric, MaskRay, jkorous, mgrang, cfe-commits
Differential Revision: https://reviews.llvm.org/D46524
llvm-svn: 332378
2018-05-15 17:43:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SymbolQualitySignals::merge(const Symbol &IndexResult) {
|
2018-09-06 18:52:26 +00:00
|
|
|
Deprecated |= (IndexResult.Flags & Symbol::Deprecated);
|
2018-10-18 12:23:05 +00:00
|
|
|
ImplementationDetail |= (IndexResult.Flags & Symbol::ImplementationDetail);
|
[clangd] Extract scoring/ranking logic, and shave yaks.
Summary:
Code completion scoring was embedded in CodeComplete.cpp, which is bad:
- awkward to test. The mechanisms (extracting info from index/sema) can be
unit-tested well, the policy (scoring) should be quantitatively measured.
Neither was easily possible, and debugging was hard.
The intermediate signal struct makes this easier.
- hard to reuse. This is a bug in workspaceSymbols: it just presents the
results in the index order, which is not sorted in practice, it needs to rank
them!
Also, index implementations care about scoring (both query-dependent and
independent) in order to truncate result lists appropriately.
The main yak shaved here is the build() function that had 3 variants across
unit tests is unified in TestTU.h (rather than adding a 4th variant).
Reviewers: ilya-biryukov
Subscribers: klimek, mgorny, ioeric, MaskRay, jkorous, mgrang, cfe-commits
Differential Revision: https://reviews.llvm.org/D46524
llvm-svn: 332378
2018-05-15 17:43:27 +00:00
|
|
|
References = std::max(IndexResult.References, References);
|
2018-06-06 08:53:36 +00:00
|
|
|
Category = categorize(IndexResult.SymInfo);
|
2018-07-26 12:05:31 +00:00
|
|
|
ReservedName = ReservedName || isReserved(IndexResult.Name);
|
[clangd] Extract scoring/ranking logic, and shave yaks.
Summary:
Code completion scoring was embedded in CodeComplete.cpp, which is bad:
- awkward to test. The mechanisms (extracting info from index/sema) can be
unit-tested well, the policy (scoring) should be quantitatively measured.
Neither was easily possible, and debugging was hard.
The intermediate signal struct makes this easier.
- hard to reuse. This is a bug in workspaceSymbols: it just presents the
results in the index order, which is not sorted in practice, it needs to rank
them!
Also, index implementations care about scoring (both query-dependent and
independent) in order to truncate result lists appropriately.
The main yak shaved here is the build() function that had 3 variants across
unit tests is unified in TestTU.h (rather than adding a 4th variant).
Reviewers: ilya-biryukov
Subscribers: klimek, mgorny, ioeric, MaskRay, jkorous, mgrang, cfe-commits
Differential Revision: https://reviews.llvm.org/D46524
llvm-svn: 332378
2018-05-15 17:43:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float SymbolQualitySignals::evaluate() const {
|
|
|
|
float Score = 1;
|
|
|
|
|
|
|
|
// This avoids a sharp gradient for tail symbols, and also neatly avoids the
|
|
|
|
// question of whether 0 references means a bad symbol or missing data.
|
2018-07-25 11:26:35 +00:00
|
|
|
if (References >= 10) {
|
|
|
|
// Use a sigmoid style boosting function, which flats out nicely for large
|
|
|
|
// numbers (e.g. 2.58 for 1M refererences).
|
|
|
|
// The following boosting function is equivalent to:
|
|
|
|
// m = 0.06
|
|
|
|
// f = 12.0
|
|
|
|
// boost = f * sigmoid(m * std::log(References)) - 0.5 * f + 0.59
|
|
|
|
// Sample data points: (10, 1.00), (100, 1.41), (1000, 1.82),
|
|
|
|
// (10K, 2.21), (100K, 2.58), (1M, 2.94)
|
2018-07-26 12:05:31 +00:00
|
|
|
float S = std::pow(References, -0.06);
|
|
|
|
Score *= 6.0 * (1 - S) / (1 + S) + 0.59;
|
2018-07-25 11:26:35 +00:00
|
|
|
}
|
[clangd] Extract scoring/ranking logic, and shave yaks.
Summary:
Code completion scoring was embedded in CodeComplete.cpp, which is bad:
- awkward to test. The mechanisms (extracting info from index/sema) can be
unit-tested well, the policy (scoring) should be quantitatively measured.
Neither was easily possible, and debugging was hard.
The intermediate signal struct makes this easier.
- hard to reuse. This is a bug in workspaceSymbols: it just presents the
results in the index order, which is not sorted in practice, it needs to rank
them!
Also, index implementations care about scoring (both query-dependent and
independent) in order to truncate result lists appropriately.
The main yak shaved here is the build() function that had 3 variants across
unit tests is unified in TestTU.h (rather than adding a 4th variant).
Reviewers: ilya-biryukov
Subscribers: klimek, mgorny, ioeric, MaskRay, jkorous, mgrang, cfe-commits
Differential Revision: https://reviews.llvm.org/D46524
llvm-svn: 332378
2018-05-15 17:43:27 +00:00
|
|
|
|
|
|
|
if (Deprecated)
|
2018-05-18 13:18:41 +00:00
|
|
|
Score *= 0.1f;
|
2018-06-08 09:36:34 +00:00
|
|
|
if (ReservedName)
|
|
|
|
Score *= 0.1f;
|
2018-10-18 12:23:05 +00:00
|
|
|
if (ImplementationDetail)
|
|
|
|
Score *= 0.2f;
|
[clangd] Extract scoring/ranking logic, and shave yaks.
Summary:
Code completion scoring was embedded in CodeComplete.cpp, which is bad:
- awkward to test. The mechanisms (extracting info from index/sema) can be
unit-tested well, the policy (scoring) should be quantitatively measured.
Neither was easily possible, and debugging was hard.
The intermediate signal struct makes this easier.
- hard to reuse. This is a bug in workspaceSymbols: it just presents the
results in the index order, which is not sorted in practice, it needs to rank
them!
Also, index implementations care about scoring (both query-dependent and
independent) in order to truncate result lists appropriately.
The main yak shaved here is the build() function that had 3 variants across
unit tests is unified in TestTU.h (rather than adding a 4th variant).
Reviewers: ilya-biryukov
Subscribers: klimek, mgorny, ioeric, MaskRay, jkorous, mgrang, cfe-commits
Differential Revision: https://reviews.llvm.org/D46524
llvm-svn: 332378
2018-05-15 17:43:27 +00:00
|
|
|
|
2018-06-06 08:53:36 +00:00
|
|
|
switch (Category) {
|
2018-07-05 09:37:26 +00:00
|
|
|
case Keyword: // Often relevant, but misses most signals.
|
|
|
|
Score *= 4; // FIXME: important keywords should have specific boosts.
|
|
|
|
break;
|
|
|
|
case Type:
|
|
|
|
case Function:
|
|
|
|
case Variable:
|
|
|
|
Score *= 1.1f;
|
|
|
|
break;
|
|
|
|
case Namespace:
|
|
|
|
Score *= 0.8f;
|
|
|
|
break;
|
|
|
|
case Macro:
|
2018-11-30 11:17:15 +00:00
|
|
|
case Destructor:
|
|
|
|
case Operator:
|
2018-09-05 07:40:38 +00:00
|
|
|
Score *= 0.5f;
|
2018-07-05 09:37:26 +00:00
|
|
|
break;
|
2018-07-24 08:51:52 +00:00
|
|
|
case Constructor: // No boost constructors so they are after class types.
|
2018-11-30 11:17:15 +00:00
|
|
|
case Unknown:
|
2018-07-05 09:37:26 +00:00
|
|
|
break;
|
2018-06-06 08:53:36 +00:00
|
|
|
}
|
|
|
|
|
[clangd] Extract scoring/ranking logic, and shave yaks.
Summary:
Code completion scoring was embedded in CodeComplete.cpp, which is bad:
- awkward to test. The mechanisms (extracting info from index/sema) can be
unit-tested well, the policy (scoring) should be quantitatively measured.
Neither was easily possible, and debugging was hard.
The intermediate signal struct makes this easier.
- hard to reuse. This is a bug in workspaceSymbols: it just presents the
results in the index order, which is not sorted in practice, it needs to rank
them!
Also, index implementations care about scoring (both query-dependent and
independent) in order to truncate result lists appropriately.
The main yak shaved here is the build() function that had 3 variants across
unit tests is unified in TestTU.h (rather than adding a 4th variant).
Reviewers: ilya-biryukov
Subscribers: klimek, mgorny, ioeric, MaskRay, jkorous, mgrang, cfe-commits
Differential Revision: https://reviews.llvm.org/D46524
llvm-svn: 332378
2018-05-15 17:43:27 +00:00
|
|
|
return Score;
|
|
|
|
}
|
|
|
|
|
|
|
|
raw_ostream &operator<<(raw_ostream &OS, const SymbolQualitySignals &S) {
|
|
|
|
OS << formatv("=== Symbol quality: {0}\n", S.evaluate());
|
|
|
|
OS << formatv("\tReferences: {0}\n", S.References);
|
|
|
|
OS << formatv("\tDeprecated: {0}\n", S.Deprecated);
|
2018-06-08 09:36:34 +00:00
|
|
|
OS << formatv("\tReserved name: {0}\n", S.ReservedName);
|
2018-06-06 08:53:36 +00:00
|
|
|
OS << formatv("\tCategory: {0}\n", static_cast<int>(S.Category));
|
[clangd] Extract scoring/ranking logic, and shave yaks.
Summary:
Code completion scoring was embedded in CodeComplete.cpp, which is bad:
- awkward to test. The mechanisms (extracting info from index/sema) can be
unit-tested well, the policy (scoring) should be quantitatively measured.
Neither was easily possible, and debugging was hard.
The intermediate signal struct makes this easier.
- hard to reuse. This is a bug in workspaceSymbols: it just presents the
results in the index order, which is not sorted in practice, it needs to rank
them!
Also, index implementations care about scoring (both query-dependent and
independent) in order to truncate result lists appropriately.
The main yak shaved here is the build() function that had 3 variants across
unit tests is unified in TestTU.h (rather than adding a 4th variant).
Reviewers: ilya-biryukov
Subscribers: klimek, mgorny, ioeric, MaskRay, jkorous, mgrang, cfe-commits
Differential Revision: https://reviews.llvm.org/D46524
llvm-svn: 332378
2018-05-15 17:43:27 +00:00
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
2018-06-05 16:30:25 +00:00
|
|
|
static SymbolRelevanceSignals::AccessibleScope
|
2018-07-26 12:05:31 +00:00
|
|
|
computeScope(const NamedDecl *D) {
|
2018-06-27 11:43:54 +00:00
|
|
|
// Injected "Foo" within the class "Foo" has file scope, not class scope.
|
|
|
|
const DeclContext *DC = D->getDeclContext();
|
|
|
|
if (auto *R = dyn_cast_or_null<RecordDecl>(D))
|
|
|
|
if (R->isInjectedClassName())
|
|
|
|
DC = DC->getParent();
|
2018-07-05 08:14:04 +00:00
|
|
|
// Class constructor should have the same scope as the class.
|
2018-07-05 09:35:12 +00:00
|
|
|
if (isa<CXXConstructorDecl>(D))
|
2018-07-05 08:14:04 +00:00
|
|
|
DC = DC->getParent();
|
2018-06-05 18:00:48 +00:00
|
|
|
bool InClass = false;
|
2018-06-27 11:43:54 +00:00
|
|
|
for (; !DC->isFileContext(); DC = DC->getParent()) {
|
2018-06-05 16:30:25 +00:00
|
|
|
if (DC->isFunctionOrMethod())
|
|
|
|
return SymbolRelevanceSignals::FunctionScope;
|
|
|
|
InClass = InClass || DC->isRecord();
|
|
|
|
}
|
|
|
|
if (InClass)
|
|
|
|
return SymbolRelevanceSignals::ClassScope;
|
|
|
|
// This threshold could be tweaked, e.g. to treat module-visible as global.
|
2018-06-27 11:43:54 +00:00
|
|
|
if (D->getLinkageInternal() < ExternalLinkage)
|
2018-06-05 16:30:25 +00:00
|
|
|
return SymbolRelevanceSignals::FileScope;
|
|
|
|
return SymbolRelevanceSignals::GlobalScope;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SymbolRelevanceSignals::merge(const Symbol &IndexResult) {
|
|
|
|
// FIXME: Index results always assumed to be at global scope. If Scope becomes
|
|
|
|
// relevant to non-completion requests, we should recognize class members etc.
|
2018-06-15 08:58:12 +00:00
|
|
|
|
|
|
|
SymbolURI = IndexResult.CanonicalDeclaration.FileURI;
|
2018-10-17 11:19:02 +00:00
|
|
|
SymbolScope = IndexResult.Scope;
|
2018-07-23 10:56:37 +00:00
|
|
|
IsInstanceMember |= isInstanceMember(IndexResult.SymInfo);
|
2018-06-05 16:30:25 +00:00
|
|
|
}
|
|
|
|
|
[clangd] Extract scoring/ranking logic, and shave yaks.
Summary:
Code completion scoring was embedded in CodeComplete.cpp, which is bad:
- awkward to test. The mechanisms (extracting info from index/sema) can be
unit-tested well, the policy (scoring) should be quantitatively measured.
Neither was easily possible, and debugging was hard.
The intermediate signal struct makes this easier.
- hard to reuse. This is a bug in workspaceSymbols: it just presents the
results in the index order, which is not sorted in practice, it needs to rank
them!
Also, index implementations care about scoring (both query-dependent and
independent) in order to truncate result lists appropriately.
The main yak shaved here is the build() function that had 3 variants across
unit tests is unified in TestTU.h (rather than adding a 4th variant).
Reviewers: ilya-biryukov
Subscribers: klimek, mgorny, ioeric, MaskRay, jkorous, mgrang, cfe-commits
Differential Revision: https://reviews.llvm.org/D46524
llvm-svn: 332378
2018-05-15 17:43:27 +00:00
|
|
|
void SymbolRelevanceSignals::merge(const CodeCompletionResult &SemaCCResult) {
|
|
|
|
if (SemaCCResult.Availability == CXAvailability_NotAvailable ||
|
|
|
|
SemaCCResult.Availability == CXAvailability_NotAccessible)
|
|
|
|
Forbidden = true;
|
2018-06-04 14:50:59 +00:00
|
|
|
|
|
|
|
if (SemaCCResult.Declaration) {
|
2018-10-17 11:19:02 +00:00
|
|
|
SemaSaysInScope = true;
|
2018-06-15 08:58:12 +00:00
|
|
|
// We boost things that have decls in the main file. We give a fixed score
|
|
|
|
// for all other declarations in sema as they are already included in the
|
|
|
|
// translation unit.
|
2018-07-11 14:49:49 +00:00
|
|
|
float DeclProximity = (hasDeclInMainFile(*SemaCCResult.Declaration) ||
|
|
|
|
hasUsingDeclInMainFile(SemaCCResult))
|
|
|
|
? 1.0
|
|
|
|
: 0.6;
|
2018-10-17 11:19:02 +00:00
|
|
|
SemaFileProximityScore = std::max(DeclProximity, SemaFileProximityScore);
|
2018-07-23 10:56:37 +00:00
|
|
|
IsInstanceMember |= isInstanceMember(SemaCCResult.Declaration);
|
2018-10-24 13:45:17 +00:00
|
|
|
InBaseClass |= SemaCCResult.InBaseClass;
|
2018-06-04 14:50:59 +00:00
|
|
|
}
|
2018-06-05 16:30:25 +00:00
|
|
|
|
|
|
|
// Declarations are scoped, others (like macros) are assumed global.
|
2018-06-05 17:58:12 +00:00
|
|
|
if (SemaCCResult.Declaration)
|
2018-07-26 12:05:31 +00:00
|
|
|
Scope = std::min(Scope, computeScope(SemaCCResult.Declaration));
|
2018-08-08 08:59:29 +00:00
|
|
|
|
|
|
|
NeedsFixIts = !SemaCCResult.FixIts.empty();
|
[clangd] Extract scoring/ranking logic, and shave yaks.
Summary:
Code completion scoring was embedded in CodeComplete.cpp, which is bad:
- awkward to test. The mechanisms (extracting info from index/sema) can be
unit-tested well, the policy (scoring) should be quantitatively measured.
Neither was easily possible, and debugging was hard.
The intermediate signal struct makes this easier.
- hard to reuse. This is a bug in workspaceSymbols: it just presents the
results in the index order, which is not sorted in practice, it needs to rank
them!
Also, index implementations care about scoring (both query-dependent and
independent) in order to truncate result lists appropriately.
The main yak shaved here is the build() function that had 3 variants across
unit tests is unified in TestTU.h (rather than adding a 4th variant).
Reviewers: ilya-biryukov
Subscribers: klimek, mgorny, ioeric, MaskRay, jkorous, mgrang, cfe-commits
Differential Revision: https://reviews.llvm.org/D46524
llvm-svn: 332378
2018-05-15 17:43:27 +00:00
|
|
|
}
|
|
|
|
|
2018-10-20 15:30:37 +00:00
|
|
|
static std::pair<float, unsigned> uriProximity(StringRef SymbolURI,
|
2018-10-17 11:19:02 +00:00
|
|
|
URIDistance *D) {
|
2018-07-03 08:09:29 +00:00
|
|
|
if (!D || SymbolURI.empty())
|
|
|
|
return {0.f, 0u};
|
|
|
|
unsigned Distance = D->distance(SymbolURI);
|
|
|
|
// Assume approximately default options are used for sensible scoring.
|
|
|
|
return {std::exp(Distance * -0.4f / FileDistanceOptions().UpCost), Distance};
|
|
|
|
}
|
|
|
|
|
2018-10-17 11:19:02 +00:00
|
|
|
static float scopeBoost(ScopeDistance &Distance,
|
2018-10-20 15:30:37 +00:00
|
|
|
Optional<StringRef> SymbolScope) {
|
2018-10-17 11:19:02 +00:00
|
|
|
if (!SymbolScope)
|
|
|
|
return 1;
|
|
|
|
auto D = Distance.distance(*SymbolScope);
|
|
|
|
if (D == FileDistance::Unreachable)
|
2018-11-28 13:45:25 +00:00
|
|
|
return 0.6f;
|
2018-11-28 14:00:09 +00:00
|
|
|
return std::max(0.65, 2.0 * std::pow(0.6, D / 2.0));
|
2018-10-17 11:19:02 +00:00
|
|
|
}
|
|
|
|
|
[clangd] Extract scoring/ranking logic, and shave yaks.
Summary:
Code completion scoring was embedded in CodeComplete.cpp, which is bad:
- awkward to test. The mechanisms (extracting info from index/sema) can be
unit-tested well, the policy (scoring) should be quantitatively measured.
Neither was easily possible, and debugging was hard.
The intermediate signal struct makes this easier.
- hard to reuse. This is a bug in workspaceSymbols: it just presents the
results in the index order, which is not sorted in practice, it needs to rank
them!
Also, index implementations care about scoring (both query-dependent and
independent) in order to truncate result lists appropriately.
The main yak shaved here is the build() function that had 3 variants across
unit tests is unified in TestTU.h (rather than adding a 4th variant).
Reviewers: ilya-biryukov
Subscribers: klimek, mgorny, ioeric, MaskRay, jkorous, mgrang, cfe-commits
Differential Revision: https://reviews.llvm.org/D46524
llvm-svn: 332378
2018-05-15 17:43:27 +00:00
|
|
|
float SymbolRelevanceSignals::evaluate() const {
|
2018-06-05 16:30:25 +00:00
|
|
|
float Score = 1;
|
|
|
|
|
[clangd] Extract scoring/ranking logic, and shave yaks.
Summary:
Code completion scoring was embedded in CodeComplete.cpp, which is bad:
- awkward to test. The mechanisms (extracting info from index/sema) can be
unit-tested well, the policy (scoring) should be quantitatively measured.
Neither was easily possible, and debugging was hard.
The intermediate signal struct makes this easier.
- hard to reuse. This is a bug in workspaceSymbols: it just presents the
results in the index order, which is not sorted in practice, it needs to rank
them!
Also, index implementations care about scoring (both query-dependent and
independent) in order to truncate result lists appropriately.
The main yak shaved here is the build() function that had 3 variants across
unit tests is unified in TestTU.h (rather than adding a 4th variant).
Reviewers: ilya-biryukov
Subscribers: klimek, mgorny, ioeric, MaskRay, jkorous, mgrang, cfe-commits
Differential Revision: https://reviews.llvm.org/D46524
llvm-svn: 332378
2018-05-15 17:43:27 +00:00
|
|
|
if (Forbidden)
|
|
|
|
return 0;
|
2018-06-04 14:50:59 +00:00
|
|
|
|
2018-06-05 16:30:25 +00:00
|
|
|
Score *= NameMatch;
|
|
|
|
|
2018-10-17 11:19:02 +00:00
|
|
|
// File proximity scores are [0,1] and we translate them into a multiplier in
|
|
|
|
// the range from 1 to 3.
|
|
|
|
Score *= 1 + 2 * std::max(uriProximity(SymbolURI, FileProximityMatch).first,
|
|
|
|
SemaFileProximityScore);
|
|
|
|
|
|
|
|
if (ScopeProximityMatch)
|
|
|
|
// Use a constant scope boost for sema results, as scopes of sema results
|
|
|
|
// can be tricky (e.g. class/function scope). Set to the max boost as we
|
|
|
|
// don't load top-level symbols from the preamble and sema results are
|
|
|
|
// always in the accessible scope.
|
|
|
|
Score *=
|
|
|
|
SemaSaysInScope ? 2.0 : scopeBoost(*ScopeProximityMatch, SymbolScope);
|
2018-06-05 16:30:25 +00:00
|
|
|
|
|
|
|
// Symbols like local variables may only be referenced within their scope.
|
|
|
|
// Conversely if we're in that scope, it's likely we'll reference them.
|
|
|
|
if (Query == CodeComplete) {
|
|
|
|
// The narrower the scope where a symbol is visible, the more likely it is
|
|
|
|
// to be relevant when it is available.
|
|
|
|
switch (Scope) {
|
|
|
|
case GlobalScope:
|
|
|
|
break;
|
|
|
|
case FileScope:
|
|
|
|
Score *= 1.5;
|
2018-06-07 08:16:36 +00:00
|
|
|
break;
|
2018-06-05 16:30:25 +00:00
|
|
|
case ClassScope:
|
|
|
|
Score *= 2;
|
2018-06-07 08:16:36 +00:00
|
|
|
break;
|
2018-06-05 16:30:25 +00:00
|
|
|
case FunctionScope:
|
|
|
|
Score *= 4;
|
2018-06-07 08:16:36 +00:00
|
|
|
break;
|
2018-06-05 16:30:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-26 15:38:01 +00:00
|
|
|
if (TypeMatchesPreferred)
|
|
|
|
Score *= 5.0;
|
|
|
|
|
2018-07-23 10:56:37 +00:00
|
|
|
// Penalize non-instance members when they are accessed via a class instance.
|
|
|
|
if (!IsInstanceMember &&
|
|
|
|
(Context == CodeCompletionContext::CCC_DotMemberAccess ||
|
|
|
|
Context == CodeCompletionContext::CCC_ArrowMemberAccess)) {
|
2018-10-24 19:31:24 +00:00
|
|
|
Score *= 0.2f;
|
2018-07-23 10:56:37 +00:00
|
|
|
}
|
|
|
|
|
2018-10-24 13:45:17 +00:00
|
|
|
if (InBaseClass)
|
2018-10-24 19:31:24 +00:00
|
|
|
Score *= 0.5f;
|
2018-10-24 13:45:17 +00:00
|
|
|
|
2018-08-08 08:59:29 +00:00
|
|
|
// Penalize for FixIts.
|
|
|
|
if (NeedsFixIts)
|
2018-10-24 19:31:24 +00:00
|
|
|
Score *= 0.5f;
|
2018-08-08 08:59:29 +00:00
|
|
|
|
2018-06-04 14:50:59 +00:00
|
|
|
return Score;
|
[clangd] Extract scoring/ranking logic, and shave yaks.
Summary:
Code completion scoring was embedded in CodeComplete.cpp, which is bad:
- awkward to test. The mechanisms (extracting info from index/sema) can be
unit-tested well, the policy (scoring) should be quantitatively measured.
Neither was easily possible, and debugging was hard.
The intermediate signal struct makes this easier.
- hard to reuse. This is a bug in workspaceSymbols: it just presents the
results in the index order, which is not sorted in practice, it needs to rank
them!
Also, index implementations care about scoring (both query-dependent and
independent) in order to truncate result lists appropriately.
The main yak shaved here is the build() function that had 3 variants across
unit tests is unified in TestTU.h (rather than adding a 4th variant).
Reviewers: ilya-biryukov
Subscribers: klimek, mgorny, ioeric, MaskRay, jkorous, mgrang, cfe-commits
Differential Revision: https://reviews.llvm.org/D46524
llvm-svn: 332378
2018-05-15 17:43:27 +00:00
|
|
|
}
|
2018-06-15 08:58:12 +00:00
|
|
|
|
[clangd] Extract scoring/ranking logic, and shave yaks.
Summary:
Code completion scoring was embedded in CodeComplete.cpp, which is bad:
- awkward to test. The mechanisms (extracting info from index/sema) can be
unit-tested well, the policy (scoring) should be quantitatively measured.
Neither was easily possible, and debugging was hard.
The intermediate signal struct makes this easier.
- hard to reuse. This is a bug in workspaceSymbols: it just presents the
results in the index order, which is not sorted in practice, it needs to rank
them!
Also, index implementations care about scoring (both query-dependent and
independent) in order to truncate result lists appropriately.
The main yak shaved here is the build() function that had 3 variants across
unit tests is unified in TestTU.h (rather than adding a 4th variant).
Reviewers: ilya-biryukov
Subscribers: klimek, mgorny, ioeric, MaskRay, jkorous, mgrang, cfe-commits
Differential Revision: https://reviews.llvm.org/D46524
llvm-svn: 332378
2018-05-15 17:43:27 +00:00
|
|
|
raw_ostream &operator<<(raw_ostream &OS, const SymbolRelevanceSignals &S) {
|
|
|
|
OS << formatv("=== Symbol relevance: {0}\n", S.evaluate());
|
|
|
|
OS << formatv("\tName match: {0}\n", S.NameMatch);
|
|
|
|
OS << formatv("\tForbidden: {0}\n", S.Forbidden);
|
2018-08-08 08:59:29 +00:00
|
|
|
OS << formatv("\tNeedsFixIts: {0}\n", S.NeedsFixIts);
|
2018-07-23 10:56:37 +00:00
|
|
|
OS << formatv("\tIsInstanceMember: {0}\n", S.IsInstanceMember);
|
|
|
|
OS << formatv("\tContext: {0}\n", getCompletionKindString(S.Context));
|
2018-10-17 11:19:02 +00:00
|
|
|
OS << formatv("\tQuery type: {0}\n", static_cast<int>(S.Query));
|
|
|
|
OS << formatv("\tScope: {0}\n", static_cast<int>(S.Scope));
|
|
|
|
|
2018-06-15 08:58:12 +00:00
|
|
|
OS << formatv("\tSymbol URI: {0}\n", S.SymbolURI);
|
2018-10-17 11:19:02 +00:00
|
|
|
OS << formatv("\tSymbol scope: {0}\n",
|
|
|
|
S.SymbolScope ? *S.SymbolScope : "<None>");
|
|
|
|
|
2018-06-15 08:58:12 +00:00
|
|
|
if (S.FileProximityMatch) {
|
2018-10-17 11:19:02 +00:00
|
|
|
auto Score = uriProximity(S.SymbolURI, S.FileProximityMatch);
|
|
|
|
OS << formatv("\tIndex URI proximity: {0} (distance={1})\n", Score.first,
|
2018-07-03 08:09:29 +00:00
|
|
|
Score.second);
|
2018-06-15 08:58:12 +00:00
|
|
|
}
|
2018-10-17 11:19:02 +00:00
|
|
|
OS << formatv("\tSema file proximity: {0}\n", S.SemaFileProximityScore);
|
|
|
|
|
|
|
|
OS << formatv("\tSema says in scope: {0}\n", S.SemaSaysInScope);
|
|
|
|
if (S.ScopeProximityMatch)
|
|
|
|
OS << formatv("\tIndex scope boost: {0}\n",
|
|
|
|
scopeBoost(*S.ScopeProximityMatch, S.SymbolScope));
|
|
|
|
|
2018-11-26 15:38:01 +00:00
|
|
|
OS << formatv(
|
|
|
|
"\tType matched preferred: {0} (Context type: {1}, Symbol type: {2}\n",
|
|
|
|
S.TypeMatchesPreferred, S.HadContextType, S.HadSymbolType);
|
|
|
|
|
[clangd] Extract scoring/ranking logic, and shave yaks.
Summary:
Code completion scoring was embedded in CodeComplete.cpp, which is bad:
- awkward to test. The mechanisms (extracting info from index/sema) can be
unit-tested well, the policy (scoring) should be quantitatively measured.
Neither was easily possible, and debugging was hard.
The intermediate signal struct makes this easier.
- hard to reuse. This is a bug in workspaceSymbols: it just presents the
results in the index order, which is not sorted in practice, it needs to rank
them!
Also, index implementations care about scoring (both query-dependent and
independent) in order to truncate result lists appropriately.
The main yak shaved here is the build() function that had 3 variants across
unit tests is unified in TestTU.h (rather than adding a 4th variant).
Reviewers: ilya-biryukov
Subscribers: klimek, mgorny, ioeric, MaskRay, jkorous, mgrang, cfe-commits
Differential Revision: https://reviews.llvm.org/D46524
llvm-svn: 332378
2018-05-15 17:43:27 +00:00
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
|
|
|
float evaluateSymbolAndRelevance(float SymbolQuality, float SymbolRelevance) {
|
|
|
|
return SymbolQuality * SymbolRelevance;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Produces an integer that sorts in the same order as F.
|
|
|
|
// That is: a < b <==> encodeFloat(a) < encodeFloat(b).
|
|
|
|
static uint32_t encodeFloat(float F) {
|
|
|
|
static_assert(std::numeric_limits<float>::is_iec559, "");
|
|
|
|
constexpr uint32_t TopBit = ~(~uint32_t{0} >> 1);
|
|
|
|
|
|
|
|
// Get the bits of the float. Endianness is the same as for integers.
|
|
|
|
uint32_t U = FloatToBits(F);
|
|
|
|
// IEEE 754 floats compare like sign-magnitude integers.
|
|
|
|
if (U & TopBit) // Negative float.
|
|
|
|
return 0 - U; // Map onto the low half of integers, order reversed.
|
|
|
|
return U + TopBit; // Positive floats map onto the high half of integers.
|
|
|
|
}
|
|
|
|
|
2018-10-20 15:30:37 +00:00
|
|
|
std::string sortText(float Score, StringRef Name) {
|
[clangd] Extract scoring/ranking logic, and shave yaks.
Summary:
Code completion scoring was embedded in CodeComplete.cpp, which is bad:
- awkward to test. The mechanisms (extracting info from index/sema) can be
unit-tested well, the policy (scoring) should be quantitatively measured.
Neither was easily possible, and debugging was hard.
The intermediate signal struct makes this easier.
- hard to reuse. This is a bug in workspaceSymbols: it just presents the
results in the index order, which is not sorted in practice, it needs to rank
them!
Also, index implementations care about scoring (both query-dependent and
independent) in order to truncate result lists appropriately.
The main yak shaved here is the build() function that had 3 variants across
unit tests is unified in TestTU.h (rather than adding a 4th variant).
Reviewers: ilya-biryukov
Subscribers: klimek, mgorny, ioeric, MaskRay, jkorous, mgrang, cfe-commits
Differential Revision: https://reviews.llvm.org/D46524
llvm-svn: 332378
2018-05-15 17:43:27 +00:00
|
|
|
// We convert -Score to an integer, and hex-encode for readability.
|
|
|
|
// Example: [0.5, "foo"] -> "41000000foo"
|
|
|
|
std::string S;
|
2018-10-20 15:30:37 +00:00
|
|
|
raw_string_ostream OS(S);
|
|
|
|
write_hex(OS, encodeFloat(-Score), HexPrintStyle::Lower,
|
[clangd] Extract scoring/ranking logic, and shave yaks.
Summary:
Code completion scoring was embedded in CodeComplete.cpp, which is bad:
- awkward to test. The mechanisms (extracting info from index/sema) can be
unit-tested well, the policy (scoring) should be quantitatively measured.
Neither was easily possible, and debugging was hard.
The intermediate signal struct makes this easier.
- hard to reuse. This is a bug in workspaceSymbols: it just presents the
results in the index order, which is not sorted in practice, it needs to rank
them!
Also, index implementations care about scoring (both query-dependent and
independent) in order to truncate result lists appropriately.
The main yak shaved here is the build() function that had 3 variants across
unit tests is unified in TestTU.h (rather than adding a 4th variant).
Reviewers: ilya-biryukov
Subscribers: klimek, mgorny, ioeric, MaskRay, jkorous, mgrang, cfe-commits
Differential Revision: https://reviews.llvm.org/D46524
llvm-svn: 332378
2018-05-15 17:43:27 +00:00
|
|
|
/*Width=*/2 * sizeof(Score));
|
|
|
|
OS << Name;
|
|
|
|
OS.flush();
|
|
|
|
return S;
|
|
|
|
}
|
|
|
|
|
2018-10-20 15:30:37 +00:00
|
|
|
raw_ostream &operator<<(raw_ostream &OS, const SignatureQualitySignals &S) {
|
2018-08-13 08:40:05 +00:00
|
|
|
OS << formatv("=== Signature Quality:\n");
|
|
|
|
OS << formatv("\tNumber of parameters: {0}\n", S.NumberOfParameters);
|
|
|
|
OS << formatv("\tNumber of optional parameters: {0}\n",
|
|
|
|
S.NumberOfOptionalParameters);
|
|
|
|
OS << formatv("\tContains active parameter: {0}\n",
|
|
|
|
S.ContainsActiveParameter);
|
|
|
|
OS << formatv("\tKind: {0}\n", S.Kind);
|
|
|
|
return OS;
|
|
|
|
}
|
|
|
|
|
[clangd] Extract scoring/ranking logic, and shave yaks.
Summary:
Code completion scoring was embedded in CodeComplete.cpp, which is bad:
- awkward to test. The mechanisms (extracting info from index/sema) can be
unit-tested well, the policy (scoring) should be quantitatively measured.
Neither was easily possible, and debugging was hard.
The intermediate signal struct makes this easier.
- hard to reuse. This is a bug in workspaceSymbols: it just presents the
results in the index order, which is not sorted in practice, it needs to rank
them!
Also, index implementations care about scoring (both query-dependent and
independent) in order to truncate result lists appropriately.
The main yak shaved here is the build() function that had 3 variants across
unit tests is unified in TestTU.h (rather than adding a 4th variant).
Reviewers: ilya-biryukov
Subscribers: klimek, mgorny, ioeric, MaskRay, jkorous, mgrang, cfe-commits
Differential Revision: https://reviews.llvm.org/D46524
llvm-svn: 332378
2018-05-15 17:43:27 +00:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|