//===--- FileDistance.h - File proximity scoring -----------------*- 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 // //===----------------------------------------------------------------------===// // // This library measures the distance between file paths. // It's used for ranking symbols, e.g. in code completion. // |foo/bar.h -> foo/bar.h| = 0. // |foo/bar.h -> foo/baz.h| < |foo/bar.h -> baz.h|. // This is an edit-distance, where edits go up or down the directory tree. // It's not symmetrical, the costs of going up and down may not match. // // Dealing with multiple sources: // In practice we care about the distance from a source file, but files near // its main-header and #included files are considered "close". // So we start with a set of (anchor, cost) pairs, and call the distance to a // path the minimum of `cost + |source -> path|`. // // We allow each source to limit the number of up-traversals paths may start // with. Up-traversals may reach things that are not "semantically near". // // Symbol URI schemes: // Symbol locations may be represented by URIs rather than file paths directly. // In this case we want to perform distance computations in URI space rather // than in file-space, without performing redundant conversions. // Therefore we have a lookup structure that accepts URIs, so that intermediate // calculations for the same scheme can be reused. // // Caveats: // Assuming up and down traversals each have uniform costs is simplistic. // Often there are "semantic roots" whose children are almost unrelated. // (e.g. /usr/include/, or / in an umbrella repository). We ignore this. // //===----------------------------------------------------------------------===// #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_FILEDISTANCE_H #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_FILEDISTANCE_H #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/StringMap.h" #include "llvm/ADT/StringRef.h" #include <memory> namespace clang { namespace clangd { struct FileDistanceOptions { … }; struct SourceParams { … }; // Supports lookups to find the minimum distance to a file from any source. // This object should be reused, it memoizes intermediate computations. class FileDistance { … }; // Supports lookups like FileDistance, but the lookup keys are URIs. // We convert each of the sources to the scheme of the URI and do a FileDistance // comparison on the bodies. class URIDistance { … }; /// Support lookups like FileDistance, but the lookup keys are symbol scopes. /// For example, a scope "na::nb::" is converted to "/na/nb". class ScopeDistance { … }; } // namespace clangd } // namespace clang #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_FILEDISTANCE_H