Browse Source

Language protocol: improved hover and completion item documentation

merge-requests/365/head
eidheim 8 years ago
parent
commit
7357979ac9
  1. 115
      src/source_language_protocol.cc

115
src/source_language_protocol.cc

@ -829,64 +829,71 @@ void Source::LanguageProtocolView::show_type_tooltips(const Gdk::Rectangle &rect
auto offset=iter.get_offset(); auto offset=iter.get_offset();
client->write_request("textDocument/hover", "\"textDocument\": {\"uri\":\"file://"+file_path.string()+"\"}, \"position\": {\"line\": "+std::to_string(iter.get_line())+", \"character\": "+std::to_string(iter.get_line_offset())+"}", [this, offset](const boost::property_tree::ptree &result, bool error) { client->write_request("textDocument/hover", "\"textDocument\": {\"uri\":\"file://"+file_path.string()+"\"}, \"position\": {\"line\": "+std::to_string(iter.get_line())+", \"character\": "+std::to_string(iter.get_line_offset())+"}", [this, offset](const boost::property_tree::ptree &result, bool error) {
if(!error) { if(!error) {
auto contents=result.get_child("contents", boost::property_tree::ptree()); std::string content;
auto it=contents.begin(); {
if(it!=contents.end()) { auto contents_pt=result.get_child("contents", boost::property_tree::ptree());
auto value=it->second.get<std::string>("value", ""); auto contents_it=contents_pt.begin();
if(!value.empty()) { if(contents_it!=contents_pt.end())
dispatcher.post([this, offset, value=std::move(value)] { content=contents_it->second.get<std::string>("value", "");
if(offset>=get_buffer()->get_char_count()) }
return; if(content.empty()) {
type_tooltips.clear(); auto contents_it=result.find("contents");
auto create_tooltip_buffer=[this, offset, value=std::move(value)]() { if(contents_it!=result.not_found())
auto tooltip_buffer=Gtk::TextBuffer::create(get_buffer()->get_tag_table()); content=contents_it->second.get_value<std::string>("");
tooltip_buffer->insert_with_tag(tooltip_buffer->get_insert()->get_iter(), "Type: "+value, "def:note"); }
if(!content.empty()) {
dispatcher.post([this, offset, content=std::move(content)] {
if(offset>=get_buffer()->get_char_count())
return;
type_tooltips.clear();
auto create_tooltip_buffer=[this, offset, content=std::move(content)]() {
auto tooltip_buffer=Gtk::TextBuffer::create(get_buffer()->get_tag_table());
tooltip_buffer->insert_with_tag(tooltip_buffer->get_insert()->get_iter(), content, "def:note");
#ifdef JUCI_ENABLE_DEBUG #ifdef JUCI_ENABLE_DEBUG
if(language_id=="rust" && capabilities.definition) { if(language_id=="rust" && capabilities.definition) {
if(Debug::LLDB::get().is_stopped()) { if(Debug::LLDB::get().is_stopped()) {
Glib::ustring value_type="Value"; Glib::ustring value_type="Value";
auto start=get_buffer()->get_iter_at_offset(offset); auto start=get_buffer()->get_iter_at_offset(offset);
auto end=start; auto end=start;
auto previous=start; auto previous=start;
while(previous.backward_char() && ((*previous>='A' && *previous<='Z') || (*previous>='a' && *previous<='z') || (*previous>='0' && *previous<='9') || *previous=='_') && start.backward_char()) {} while(previous.backward_char() && ((*previous>='A' && *previous<='Z') || (*previous>='a' && *previous<='z') || (*previous>='0' && *previous<='9') || *previous=='_') && start.backward_char()) {}
while(((*end>='A' && *end<='Z') || (*end>='a' && *end<='z') || (*end>='0' && *end<='9') || *end=='_') && end.forward_char()) {} while(((*end>='A' && *end<='Z') || (*end>='a' && *end<='z') || (*end>='0' && *end<='9') || *end=='_') && end.forward_char()) {}
auto offset=get_declaration(start); auto offset=get_declaration(start);
Glib::ustring debug_value=Debug::LLDB::get().get_value(get_buffer()->get_text(start, end), offset.file_path, offset.line+1, offset.index+1); Glib::ustring debug_value=Debug::LLDB::get().get_value(get_buffer()->get_text(start, end), offset.file_path, offset.line+1, offset.index+1);
if(debug_value.empty()) { if(debug_value.empty()) {
value_type="Return value"; value_type="Return value";
debug_value=Debug::LLDB::get().get_return_value(file_path, start.get_line()+1, start.get_line_index()+1); debug_value=Debug::LLDB::get().get_return_value(file_path, start.get_line()+1, start.get_line_index()+1);
} }
if(!debug_value.empty()) { if(!debug_value.empty()) {
size_t pos=debug_value.find(" = "); size_t pos=debug_value.find(" = ");
if(pos!=Glib::ustring::npos) { if(pos!=Glib::ustring::npos) {
Glib::ustring::iterator iter; Glib::ustring::iterator iter;
while(!debug_value.validate(iter)) { while(!debug_value.validate(iter)) {
auto next_char_iter=iter; auto next_char_iter=iter;
next_char_iter++; next_char_iter++;
debug_value.replace(iter, next_char_iter, "?"); debug_value.replace(iter, next_char_iter, "?");
}
tooltip_buffer->insert_with_tag(tooltip_buffer->get_insert()->get_iter(), "\n\n"+value_type+": "+debug_value.substr(pos+3, debug_value.size()-(pos+3)-1), "def:note");
} }
tooltip_buffer->insert_with_tag(tooltip_buffer->get_insert()->get_iter(), "\n\n"+value_type+": "+debug_value.substr(pos+3, debug_value.size()-(pos+3)-1), "def:note");
} }
} }
} }
}
#endif #endif
return tooltip_buffer;
};
auto start=get_buffer()->get_iter_at_offset(offset); return tooltip_buffer;
auto end=start; };
auto previous=start;
while(previous.backward_char() && ((*previous>='A' && *previous<='Z') || (*previous>='a' && *previous<='z') || (*previous>='0' && *previous<='9') || *previous=='_') && start.backward_char()) {} auto start=get_buffer()->get_iter_at_offset(offset);
while(((*end>='A' && *end<='Z') || (*end>='a' && *end<='z') || (*end>='0' && *end<='9') || *end=='_') && end.forward_char()) {} auto end=start;
type_tooltips.emplace_back(create_tooltip_buffer, this, get_buffer()->create_mark(start), get_buffer()->create_mark(end)); auto previous=start;
type_tooltips.show(); while(previous.backward_char() && ((*previous>='A' && *previous<='Z') || (*previous>='a' && *previous<='z') || (*previous>='0' && *previous<='9') || *previous=='_') && start.backward_char()) {}
}); while(((*end>='A' && *end<='Z') || (*end>='a' && *end<='z') || (*end>='0' && *end<='9') || *end=='_') && end.forward_char()) {}
} type_tooltips.emplace_back(create_tooltip_buffer, this, get_buffer()->create_mark(start), get_buffer()->create_mark(end));
type_tooltips.show();
});
} }
} }
}); });
@ -1072,10 +1079,16 @@ void Source::LanguageProtocolView::setup_autocomplete() {
for(auto it=begin;it!=end;++it) { for(auto it=begin;it!=end;++it) {
auto label=it->second.get<std::string>("label", ""); auto label=it->second.get<std::string>("label", "");
auto detail=it->second.get<std::string>("detail", ""); auto detail=it->second.get<std::string>("detail", "");
auto documentation=it->second.get<std::string>("documentation", "");
auto insert=it->second.get<std::string>("insertText", label); auto insert=it->second.get<std::string>("insertText", label);
if(!label.empty()) { if(!label.empty()) {
autocomplete.rows.emplace_back(std::move(label)); autocomplete.rows.emplace_back(std::move(label));
autocomplete_comment.emplace_back(std::move(detail)); autocomplete_comment.emplace_back(std::move(detail));
if(!documentation.empty()) {
if(!autocomplete_comment.back().empty())
autocomplete_comment.back()+="\n\n";
autocomplete_comment.back()+=documentation;
}
autocomplete_insert.emplace_back(std::move(insert)); autocomplete_insert.emplace_back(std::move(insert));
} }
} }

Loading…
Cancel
Save