10 #include "llvm/ADT/StringExtras.h"
11 #include "llvm/Support/Path.h"
25 while (
char c = *curPtr++) {
27 if (StringRef(
"\"\n\v\f").
contains(c))
33 if (*curPtr ==
'"' || *curPtr ==
'\\' || *curPtr ==
'n' || *curPtr ==
't')
35 else if (llvm::isHexDigit(*curPtr) && llvm::isHexDigit(curPtr[1]))
50 const char *curPtr = loc.getPointer();
59 auto isIdentifierChar = [=](
char c) {
60 return isalnum(c) || c ==
'_' || identifierChars.contains(c);
63 while (*curPtr && isIdentifierChar(*(++curPtr)))
67 return SMRange(loc, SMLoc::getFromPointer(curPtr));
70 std::optional<std::string>
77 int bufferId = sourceMgr.FindBufferContainingLoc(loc);
80 const char *bufferStart =
81 sourceMgr.getMemoryBuffer(bufferId)->getBufferStart();
82 StringRef buffer(bufferStart, loc.getPointer() - bufferStart);
85 auto popLastLine = [&]() -> std::optional<StringRef> {
86 size_t newlineOffset = buffer.find_last_of(
'\n');
87 if (newlineOffset == StringRef::npos)
89 StringRef lastLine = buffer.drop_front(newlineOffset).trim();
90 buffer = buffer.take_front(newlineOffset);
100 while (std::optional<StringRef> line = popLastLine()) {
102 if (!line->starts_with(
"//"))
106 commentLines.push_back(line->ltrim(
'/'));
109 if (commentLines.empty())
111 return llvm::join(llvm::reverse(commentLines),
"\n");
115 return range.Start.getPointer() <= loc.getPointer() &&
116 loc.getPointer() < range.End.getPointer();
127 hoverOS <<
"`" << llvm::sys::path::filename(
uri.
file()) <<
"`\n***\n"
135 for (
unsigned i = 1, e = sourceMgr.getNumBuffers(); i < e; ++i) {
137 SMLoc includeLoc = sourceMgr.getBufferInfo(i + 1).IncludeLoc;
138 if (!includeLoc.isValid() || sourceMgr.FindBufferContainingLoc(
139 includeLoc) != sourceMgr.getMainFileID())
143 auto *buffer = sourceMgr.getMemoryBuffer(i + 1);
145 llvm::sys::path::remove_dots(path,
true);
148 if (!includedFileURI)
152 const char *includeStart = includeLoc.getPointer() - 2;
153 while (*(--includeStart) !=
'\"')
157 SMRange includeRange(SMLoc::getFromPointer(includeStart), includeLoc);
158 includes.emplace_back(*includedFileURI,
Range(sourceMgr, includeRange));
static const char * lexLocStringTok(const char *curPtr)
Find the end of a string whose contents start at the given curPtr.
static llvm::Expected< URIForFile > fromFile(StringRef absoluteFilepath, StringRef scheme="file")
Try to build a URIForFile from the given absolute file path and optional scheme.
StringRef file() const
Returns the absolute path to the file.
void gatherIncludeFiles(llvm::SourceMgr &sourceMgr, SmallVectorImpl< SourceMgrInclude > &includes)
Given a source manager, gather all of the processed include files.
bool contains(SMRange range, SMLoc loc)
Returns true if the given range contains the given source location.
std::optional< std::string > extractSourceDocComment(llvm::SourceMgr &sourceMgr, SMLoc loc)
Extract a documentation comment for the given location within the source manager.
SMRange convertTokenLocToRange(SMLoc loc, StringRef identifierChars="")
Returns the range of a lexical token given a SMLoc corresponding to the start of an token location.
Include the generated interface declarations.
MarkupContent contents
The hover's content.
Hover buildHover() const
Build a hover for the current include file.
lsp::Range range
The range of the include directive.
lsp::URIForFile uri
The URI of the file that is included.