66#include " Support/Compare.h"
77#include " Support/Ranges.h"
88#include " Feature/Hover.h"
9+ #include " Support/Logger.h"
910
1011namespace clice ::feature {
1112
1213namespace {
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
0 commit comments