Skip to content

Commit 3501fc3

Browse files
committed
Feat: Hover
1 parent 83c6b2d commit 3501fc3

File tree

3 files changed

+90
-25
lines changed

3 files changed

+90
-25
lines changed

include/Feature/Hover.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ struct Hover {
6565
std::string source;
6666
};
6767

68-
/// Generate the hover information for the given declaration(for test).
69-
Hover hover(CompilationUnit& unit, const clang::NamedDecl* decl);
68+
// /// Generate the hover information for the given declaration(for test).
69+
// Hover hover(CompilationUnit& unit, const clang::NamedDecl* decl, const config::HoverOptions& opt);
7070

7171
/// Generate the hover information for the symbol at the given offset.
72-
Hover hover(CompilationUnit& unit, std::uint32_t offset);
72+
std::optional<Hover> hover(CompilationUnit& unit, std::uint32_t offset, const config::HoverOptions& opt);
7373

7474
} // namespace clice::feature

src/Feature/Hover.cpp

Lines changed: 78 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
#include "Support/Compare.h"
77
#include "Support/Ranges.h"
88
#include "Feature/Hover.h"
9+
#include "Support/Logger.h"
910

1011
namespace clice::feature {
1112

1213
namespace {
1314

14-
std::vector<HoverItem> getHoverItems(CompilationUnit& unit, const clang::NamedDecl* decl) {
15+
std::vector<HoverItem> get_hover_items(CompilationUnit& unit,
16+
const clang::NamedDecl* decl,
17+
const config::HoverOptions& opt) {
1518
clang::ASTContext& Ctx = unit.context();
1619
std::vector<HoverItem> items;
1720

@@ -34,7 +37,15 @@ std::vector<HoverItem> getHoverItems(CompilationUnit& unit, const clang::NamedDe
3437
return items;
3538
}
3639

37-
std::string getDocument(CompilationUnit& unit, const clang::NamedDecl* decl) {
40+
std::vector<HoverItem> get_hover_items(CompilationUnit& unit,
41+
const clang::TypeLoc* typeloc,
42+
const config::HoverOptions& opt) {
43+
return {};
44+
}
45+
46+
std::string getDocument(CompilationUnit& unit,
47+
const clang::NamedDecl* decl,
48+
config::HoverOptions opt) {
3849
clang::ASTContext& Ctx = unit.context();
3950
const clang::RawComment* comment = Ctx.getRawCommentForAnyRedecl(decl);
4051
if(!comment) {
@@ -44,50 +55,102 @@ std::string getDocument(CompilationUnit& unit, const clang::NamedDecl* decl) {
4455
return comment->getRawText(Ctx.getSourceManager()).str();
4556
}
4657

47-
std::string getQualifier(CompilationUnit& unit, const clang::NamedDecl* decl) {
58+
std::string getQualifier(CompilationUnit& unit,
59+
const clang::NamedDecl* decl,
60+
config::HoverOptions opt) {
4861
std::string result;
4962
llvm::raw_string_ostream os(result);
5063
decl->printNestedNameSpecifier(os);
5164
return result;
5265
}
5366

54-
std::string getSourceCode(CompilationUnit& unit, const clang::NamedDecl* decl) {
67+
std::string getSourceCode(CompilationUnit& unit,
68+
const clang::NamedDecl* decl,
69+
config::HoverOptions opt) {
5570
clang::SourceRange range = decl->getSourceRange();
5671
// auto& TB = unit.tokBuf();
57-
// auto& SM = unit.srcMgr();
72+
auto& sm = unit.context().getSourceManager();
5873
// auto tokens = TB.expandedTokens(range);
5974
/// FIXME: How to cut off the tokens?
6075
return "";
6176
}
6277

6378
} // namespace
6479

65-
Hover hover(CompilationUnit& unit, const clang::NamedDecl* decl) {
80+
static std::optional<clang::SourceLocation> src_loc_in_main_file(clang::SourceManager& sm,
81+
uint32_t off) {
82+
return sm.getLocForStartOfFile(sm.getMainFileID()).getLocWithOffset(off);
83+
}
84+
85+
static std::optional<Hover> hover(CompilationUnit& unit,
86+
const clang::NamedDecl* decl,
87+
const config::HoverOptions& opt) {
6688
return Hover{
6789
.kind = SymbolKind::from(decl),
6890
.name = ast::name_of(decl),
69-
.items = getHoverItems(unit, decl),
70-
.document = getDocument(unit, decl),
71-
.qualifier = getQualifier(unit, decl),
72-
.source = getSourceCode(unit, decl),
91+
.items = get_hover_items(unit, decl, opt),
92+
.document = getDocument(unit, decl, opt),
93+
.qualifier = getQualifier(unit, decl, opt),
94+
.source = getSourceCode(unit, decl, opt),
7395
};
7496
}
7597

76-
Hover hover(CompilationUnit& unit, std::uint32_t offset) {
77-
Hover info;
98+
static std::optional<Hover> hover(CompilationUnit& unit,
99+
const clang::TypeLoc* typeloc,
100+
const config::HoverOptions& opt) {
101+
// TODO: Hover for type
102+
clice::log::warn("Hit a typeloc");
103+
return std::nullopt;
104+
}
105+
106+
std::optional<Hover> hover(CompilationUnit& unit,
107+
std::uint32_t offset,
108+
const config::HoverOptions& opt) {
109+
auto& sm = unit.context().getSourceManager();
110+
auto loc = src_loc_in_main_file(sm, offset);
111+
if(!loc.has_value()) {
112+
return std::nullopt;
113+
}
114+
115+
auto tokens_under_cursor = unit.spelled_tokens_touch(*loc);
116+
for(auto& tk: tokens_under_cursor) {
117+
clice::log::info("Hit token '{}'", tk.str());
118+
}
119+
120+
// Find the token under cursor
121+
122+
// TODO: Hover include and macros
123+
// TODO: Range of highlighted tokens
124+
// TODO: Handle `auto` and `decltype`
78125

79126
auto tree = SelectionTree::create_right(unit, {offset, offset});
80127
if(auto node = tree.common_ancestor()) {
81128
if(auto decl = node->get<clang::NamedDecl>()) {
82-
return hover(unit, decl);
129+
return hover(unit, decl, opt);
83130
} else if(auto ref = node->get<clang::DeclRefExpr>()) {
84-
return hover(unit, ref->getDecl());
131+
return hover(unit, ref->getDecl(), opt);
132+
} else if(auto typeloc = node->get<clang::TypeLoc>()) {
133+
return hover(unit, typeloc, opt);
85134
}
86135

136+
clice::log::warn("Not selected");
137+
138+
node->data.dump(llvm::errs(), unit.context());
139+
140+
/// FIXME: ...
141+
/// - param var pointed at var decl, no param info
142+
87143
/// TODO: add ....
144+
/// not captured:
145+
/// - TypeLocs
146+
/// - Fields inside template
147+
/// - NestedNameSpecifierLoc
148+
/// - Template specification
149+
} else {
150+
clice::log::warn("Not an ast node");
88151
}
89152

90-
return Hover{};
153+
return std::nullopt;
91154
}
92155

93156
} // namespace clice::feature

src/Server/Feature.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,17 @@ auto Server::on_hover(proto::HoverParams params) -> Result {
5757

5858
auto offset = to_offset(kind, opening_file->content, params.position);
5959

60-
co_return co_await async::submit([kind = this->kind, offset, &ast] {
61-
auto hover = feature::hover(*ast, offset);
62-
if(hover.kind == SymbolKind::Invalid) {
63-
return json::Value(nullptr);
64-
}
65-
60+
// NOTE: Should be initialized somewhere else
61+
config::HoverOptions opt = {.enable_doxygen_parsing = true,
62+
.parse_comment_as_markdown = true,
63+
.show_aka = true};
64+
65+
co_return co_await async::submit([kind = this->kind, offset, &ast, &opt] {
66+
auto hover = feature::hover(*ast, offset, opt);
67+
// TODO: Join comment with ast info, build structed text
6668
proto::Hover result;
6769
result.contents.kind = "markdown";
68-
result.contents.value = std::format("{}: {}", hover.kind.name(), hover.name);
70+
result.contents.value = std::format("{}: {}", hover->kind.name(), hover->name);
6971
return json::serialize(result);
7072
});
7173
}

0 commit comments

Comments
 (0)