//===--- Quality.h - Ranking alternatives for ambiguous queries --*- C++-*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// /// /// Some operations such as code completion produce a set of candidates. /// Usually the user can choose between them, but we should put the best options /// at the top (they're easier to select, and more likely to be seen). /// /// This file defines building blocks for ranking candidates. /// It's used by the features directly and also in the implementation of /// indexes, as indexes also need to heuristically limit their results. /// /// The facilities here are: /// - retrieving scoring signals from e.g. indexes, AST, CodeCompletionString /// These are structured in a way that they can be debugged, and are fairly /// consistent regardless of the source. /// - compute scores from scoring signals. These are suitable for sorting. /// - sorting utilities like the TopN container. /// These could be split up further to isolate dependencies if we care. /// //===----------------------------------------------------------------------===// #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_QUALITY_H #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_QUALITY_H #include "FileDistance.h" #include "clang/Sema/CodeCompleteConsumer.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringSet.h" #include <algorithm> #include <functional> #include <optional> #include <vector> namespace llvm { class raw_ostream; } // namespace llvm namespace clang { class CodeCompletionResult; namespace clangd { struct ASTSignals; struct Symbol; class URIDistance; // Signals structs are designed to be aggregated from 0 or more sources. // A default instance has neutral signals, and sources are merged into it. // They can be dumped for debugging, and evaluate()d into a score. /// Attributes of a symbol that affect how much we like it. struct SymbolQualitySignals { … }; llvm::raw_ostream &operator<<(llvm::raw_ostream &, const SymbolQualitySignals &); /// Attributes of a symbol-query pair that affect how much we like it. struct SymbolRelevanceSignals { … }; llvm::raw_ostream &operator<<(llvm::raw_ostream &, const SymbolRelevanceSignals &); /// Combine symbol quality and relevance into a single score. float evaluateSymbolAndRelevance(float SymbolQuality, float SymbolRelevance); /// Same semantics as CodeComplete::Score. Quality score and Relevance score /// have been removed since DecisionForest cannot assign individual scores to /// Quality and Relevance signals. struct DecisionForestScores { … }; DecisionForestScores evaluateDecisionForest(const SymbolQualitySignals &Quality, const SymbolRelevanceSignals &Relevance, float Base); /// TopN<T> is a lossy container that preserves only the "best" N elements. template <typename T, typename Compare = std::greater<T>> class TopN { public: using value_type = T; TopN(size_t N, Compare Greater = Compare()) : … { … } // Adds a candidate to the set. // Returns true if a candidate was dropped to get back under N. bool push(value_type &&V) { … } // Returns candidates from best to worst. std::vector<value_type> items() && { … } private: const size_t N; std::vector<value_type> Heap; // Min-heap, comparator is Greater. Compare Greater; }; /// Returns a string that sorts in the same order as (-Score, Tiebreak), for /// LSP. (The highest score compares smallest so it sorts at the top). std::string sortText(float Score, llvm::StringRef Tiebreak = ""); struct SignatureQualitySignals { … }; llvm::raw_ostream &operator<<(llvm::raw_ostream &, const SignatureQualitySignals &); } // namespace clangd } // namespace clang #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_QUALITY_H