Skip to content

Commit a5ccaba

Browse files
refactor: Introduce DocComment type (#146)
1 parent 267d827 commit a5ccaba

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

indexer/Indexer.cc

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,8 @@ void TuIndexer::saveEnumConstantDecl(
284284
auto symbol = optSymbol.value();
285285

286286
scip::SymbolInformation symbolInfo;
287-
for (auto &docComment : this->tryGetDocComment(enumConstantDecl)) {
288-
*symbolInfo.add_documentation() = std::move(docComment.Text);
287+
for (auto &docComment : this->tryGetDocComment(enumConstantDecl).lines) {
288+
*symbolInfo.add_documentation() = std::move(docComment);
289289
}
290290

291291
ENFORCE(enumConstantDecl.getBeginLoc() == enumConstantDecl.getLocation());
@@ -307,8 +307,8 @@ void TuIndexer::saveFieldDecl(const clang::FieldDecl &fieldDecl) {
307307
return;
308308
}
309309
scip::SymbolInformation symbolInfo{};
310-
for (auto &docComment : this->tryGetDocComment(fieldDecl)) {
311-
*symbolInfo.add_documentation() = std::move(docComment.Text);
310+
for (auto &docComment : this->tryGetDocComment(fieldDecl).lines) {
311+
*symbolInfo.add_documentation() = std::move(docComment);
312312
}
313313
this->saveDefinition(optSymbol.value(), fieldDecl.getLocation(), symbolInfo);
314314
}
@@ -329,8 +329,8 @@ void TuIndexer::saveFunctionDecl(const clang::FunctionDecl &functionDecl) {
329329

330330
if (functionDecl.isPure() || functionDecl.isThisDeclarationADefinition()) {
331331
scip::SymbolInformation symbolInfo{};
332-
for (auto &docComment : this->tryGetDocComment(functionDecl)) {
333-
*symbolInfo.add_documentation() = std::move(docComment.Text);
332+
for (auto &docComment : this->tryGetDocComment(functionDecl).lines) {
333+
*symbolInfo.add_documentation() = std::move(docComment);
334334
}
335335
if (auto *cxxMethodDecl =
336336
llvm::dyn_cast<clang::CXXMethodDecl>(&functionDecl)) {
@@ -493,8 +493,8 @@ void TuIndexer::saveTagDecl(const clang::TagDecl &tagDecl) {
493493
}
494494

495495
scip::SymbolInformation symbolInfo;
496-
for (auto &docComment : this->tryGetDocComment(tagDecl)) {
497-
*symbolInfo.add_documentation() = std::move(docComment.Text);
496+
for (auto &docComment : this->tryGetDocComment(tagDecl).lines) {
497+
*symbolInfo.add_documentation() = std::move(docComment);
498498
}
499499

500500
if (auto *cxxRecordDecl = llvm::dyn_cast<clang::CXXRecordDecl>(&tagDecl)) {
@@ -579,8 +579,8 @@ void TuIndexer::saveTypedefNameDecl(
579579
return;
580580
}
581581
scip::SymbolInformation symbolInfo{};
582-
for (auto &docComment : this->tryGetDocComment(typedefNameDecl)) {
583-
*symbolInfo.add_documentation() = std::move(docComment.Text);
582+
for (auto &docComment : this->tryGetDocComment(typedefNameDecl).lines) {
583+
*symbolInfo.add_documentation() = std::move(docComment);
584584
}
585585
this->saveDefinition(*optSymbol, typedefNameDecl.getLocation(),
586586
std::move(symbolInfo));
@@ -606,8 +606,8 @@ void TuIndexer::saveVarDecl(const clang::VarDecl &varDecl) {
606606
return;
607607
}
608608
scip::SymbolInformation symbolInfo{};
609-
for (auto &docComment : this->tryGetDocComment(varDecl)) {
610-
*symbolInfo.add_documentation() = std::move(docComment.Text);
609+
for (auto &docComment : this->tryGetDocComment(varDecl).lines) {
610+
*symbolInfo.add_documentation() = std::move(docComment);
611611
}
612612
this->saveDefinition(optSymbol.value(), varDecl.getLocation(), symbolInfo);
613613
}
@@ -763,17 +763,20 @@ checkIfCommentBelongsToPreviousEnumCase(const clang::Decl &decl,
763763

764764
namespace scip_clang {
765765

766-
std::vector<clang::RawComment::CommentLine>
767-
TuIndexer::tryGetDocComment(const clang::Decl &decl) const {
766+
DocComment TuIndexer::tryGetDocComment(const clang::Decl &decl) const {
768767
auto &astContext = decl.getASTContext();
769768
// FIXME(def: hovers, issue:
770769
// https://github.com/sourcegraph/scip-clang/issues/96)
771770
if (auto *rawComment = astContext.getRawCommentForAnyRedecl(&decl)) {
772771
if (::checkIfCommentBelongsToPreviousEnumCase(decl, *rawComment)) {
773772
return {};
774773
}
775-
return rawComment->getFormattedLines(this->sourceManager,
776-
astContext.getDiagnostics());
774+
DocComment out{};
775+
for (auto &line : rawComment->getFormattedLines(
776+
this->sourceManager, astContext.getDiagnostics())) {
777+
out.lines.emplace_back(std::move(line.Text));
778+
}
779+
return out;
777780
}
778781
return {};
779782
}

indexer/Indexer.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ struct PartialDocument {
201201
absl::flat_hash_map<std::string_view, scip::SymbolInformation> symbolInfos;
202202
};
203203

204+
struct DocComment {
205+
std::vector<std::string> lines;
206+
};
207+
204208
class TuIndexer final {
205209
const clang::SourceManager &sourceManager;
206210
const clang::LangOptions &langOptions;
@@ -264,8 +268,7 @@ class TuIndexer final {
264268
clang::SourceLocation loc,
265269
int32_t allRoles = 0);
266270

267-
std::vector<clang::RawComment::CommentLine>
268-
tryGetDocComment(const clang::Decl &) const;
271+
DocComment tryGetDocComment(const clang::Decl &) const;
269272
};
270273

271274
} // namespace scip_clang

0 commit comments

Comments
 (0)