1//===--- FuzzySymbolIndex.h - Lookup symbols for autocomplete ---*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_CLANG_TOOLS_EXTRA_INCLUDE_FIXER_FUZZY_SYMBOL_INDEX_H
10#define LLVM_CLANG_TOOLS_EXTRA_INCLUDE_FIXER_FUZZY_SYMBOL_INDEX_H
11
12#include "SymbolIndex.h"
13#include "find-all-symbols/SymbolInfo.h"
14#include "llvm/ADT/SmallString.h"
15#include "llvm/ADT/StringRef.h"
16#include "llvm/Support/Error.h"
17#include <string>
18#include <vector>
19
20namespace clang {
21namespace include_fixer {
22
23// A FuzzySymbolIndex retrieves top-level symbols matching a query string.
24//
25// It refines the contract of SymbolIndex::search to do fuzzy matching:
26// - symbol names are tokenized: "unique ptr", "string ref".
27// - query must match prefixes of symbol tokens: [upt]
28// - if the query has multiple tokens, splits must match: [StR], not [STr].
29// Helpers for tokenization and regex matching are provided.
30//
31// Implementations may choose to truncate results, refuse short queries, etc.
32class FuzzySymbolIndex : public SymbolIndex {
33public:
34 // Loads the specified clang-include-fixer database and returns an index serving it.
35 static llvm::Expected<std::unique_ptr<FuzzySymbolIndex>>
36 createFromYAML(llvm::StringRef File);
37
38 // Helpers for implementing indexes:
39
40 // Transforms a symbol name or query into a sequence of tokens.
41 // - URLHandlerCallback --> [url, handler, callback]
42 // - snake_case11 --> [snake, case, 11]
43 // - _WTF$ --> [wtf]
44 static std::vector<std::string> tokenize(llvm::StringRef Text);
45
46 // Transforms query tokens into an unanchored regexp to match symbol tokens.
47 // - [fe f] --> /f(\w* )?e\w* f/, matches [fee fie foe].
48 static std::string queryRegexp(const std::vector<std::string> &Tokens);
49};
50
51} // namespace include_fixer
52} // namespace clang
53
54#endif // LLVM_CLANG_TOOLS_EXTRA_INCLUDE_FIXER_FUZZY_SYMBOL_INDEX_H
55

source code of clang-tools-extra/clang-include-fixer/FuzzySymbolIndex.h