From b97cc5ca37bce7f47f47cd884d8e4bbed40a2403 Mon Sep 17 00:00:00 2001 From: eidheim Date: Sun, 5 Jul 2020 21:36:53 +0200 Subject: [PATCH] Cleanp of Tooltip::insert_code --- src/project.cpp | 2 +- src/source_clang.cpp | 4 ++-- src/source_language_protocol.cpp | 8 ++++---- src/source_spellcheck.cpp | 2 +- src/tooltips.cpp | 31 ++++++++++++++++++------------- src/tooltips.hpp | 6 ++++-- 6 files changed, 30 insertions(+), 23 deletions(-) diff --git a/src/project.cpp b/src/project.cpp index 51fcfce..e4db98c 100644 --- a/src/project.cpp +++ b/src/project.cpp @@ -629,7 +629,7 @@ void Project::LLDB::debug_show_variables() { next_char_iter++; value.replace(iter, next_char_iter, "?"); } - tooltip.insert_code(value.substr(0, value.size() - 1), {}); + tooltip.insert_code(value.substr(0, value.size() - 1)); } }; if(view) { diff --git a/src/source_clang.cpp b/src/source_clang.cpp index fb20861..ff6e384 100644 --- a/src/source_clang.cpp +++ b/src/source_clang.cpp @@ -515,7 +515,7 @@ void Source::ClangViewParse::show_type_tooltips(const Gdk::Rectangle &rectangle) size_t pos = 0; while((pos = type_description.find("::__1", pos)) != std::string::npos) type_description.erase(pos, 5); - tooltip.insert_code(type_description, std::string{}); + tooltip.insert_code(type_description, language); auto brief_comment = cursor.get_brief_comments(); if(brief_comment != "") tooltip.insert_with_links_tagged("\n\n" + brief_comment); @@ -656,7 +656,7 @@ void Source::ClangViewParse::show_type_tooltips(const Gdk::Rectangle &rectangle) size_t pos = 0; while((pos = value.find("::__1", pos)) != std::string::npos) value.erase(pos, 5); - tooltip.insert_code(value, {}); + tooltip.insert_code(value); } } } diff --git a/src/source_language_protocol.cpp b/src/source_language_protocol.cpp index a5fae17..a81d593 100644 --- a/src/source_language_protocol.cpp +++ b/src/source_language_protocol.cpp @@ -1180,7 +1180,7 @@ void Source::LanguageProtocolView::show_type_tooltips(const Gdk::Rectangle &rect for(size_t i = 0; i < contents.size(); i++) { if(i > 0) tooltip.buffer->insert_at_cursor("\n\n"); - if(contents[i].kind == "plaintext" || contents[i].kind.empty()) + if(contents[i].kind == "plaintext" || contents[i].kind.empty() || (language_id == "python" && contents[i].kind == "markdown")) // Python might support markdown in the future tooltip.insert_with_links_tagged(contents[i].value); else if(contents[i].kind == "markdown") tooltip.insert_markdown(contents[i].value); @@ -1234,7 +1234,7 @@ void Source::LanguageProtocolView::show_type_tooltips(const Gdk::Rectangle &rect debug_value.replace(iter, next_char_iter, "?"); } tooltip.buffer->insert_at_cursor("\n\n" + value_type + ":\n"); - tooltip.insert_code(debug_value.substr(pos + 3, debug_value.size() - (pos + 3) - 1), {}); + tooltip.insert_code(debug_value.substr(pos + 3, debug_value.size() - (pos + 3) - 1)); } } } @@ -1695,13 +1695,13 @@ void Source::LanguageProtocolView::setup_autocomplete() { return nullptr; return [this, autocomplete = std::move(autocomplete)](Tooltip &tooltip) { if(!autocomplete.detail.empty()) { - tooltip.insert_code(autocomplete.detail, language ? language->get_id().raw() : std::string{}); + tooltip.insert_code(autocomplete.detail, language); tooltip.remove_trailing_newlines(); } if(!autocomplete.documentation.empty()) { if(tooltip.buffer->size() > 0) tooltip.buffer->insert_at_cursor("\n\n"); - if(autocomplete.kind == "plaintext" || autocomplete.kind.empty()) + if(autocomplete.kind == "plaintext" || autocomplete.kind.empty() || (language_id == "python" && autocomplete.kind == "markdown")) // Python might support markdown in the future tooltip.insert_with_links_tagged(autocomplete.documentation); else if(autocomplete.kind == "markdown") tooltip.insert_markdown(autocomplete.documentation); diff --git a/src/source_spellcheck.cpp b/src/source_spellcheck.cpp index 7525d94..92f14cb 100644 --- a/src/source_spellcheck.cpp +++ b/src/source_spellcheck.cpp @@ -420,7 +420,7 @@ bool Source::SpellCheckView::is_code_iter(const Gtk::TextIter &iter) { if(iter.has_tag(comment_tag)) return true; #if GTKMM_MAJOR_VERSION > 3 || (GTKMM_MAJOR_VERSION == 3 && GTKMM_MINOR_VERSION >= 20) - if(iter.starts_tag(comment_tag)) + if(iter.begins_tag(comment_tag)) return true; #else if(*iter == '/') { diff --git a/src/tooltips.cpp b/src/tooltips.cpp index 321fbb4..7ef3e91 100644 --- a/src/tooltips.cpp +++ b/src/tooltips.cpp @@ -126,7 +126,7 @@ void Tooltip::show(bool disregard_drawn, const std::function &on_motion) tooltip_text_view->get_iter_at_location(iter, location_x, location_y); if(iter.has_tag(link_tag)) { auto start = iter; - if(!start.starts_tag(link_tag)) + if(!start.begins_tag(link_tag)) start.backward_to_tag_toggle(link_tag); auto end = iter; end.forward_to_tag_toggle(link_tag); @@ -782,7 +782,7 @@ void Tooltip::insert_markdown(const std::string &input) { remove_trailing_newlines(); } -void Tooltip::insert_code(const std::string &code, const boost::optional &language_identifier, bool block) { +void Tooltip::insert_code(const std::string &code, boost::variant> language_variant, bool block) { create_tags(); auto insert_iter = buffer->get_insert()->get_iter(); @@ -801,19 +801,24 @@ void Tooltip::insert_code(const std::string &code, const boost::optionalinsert_with_tag(insert_iter, code, block ? code_block_tag : code_tag); - if(view && language_identifier) { - auto tmp_view = Gsv::View(); - tmp_view.get_buffer()->set_text(code); - auto scheme = view->get_source_buffer()->get_style_scheme(); - tmp_view.get_source_buffer()->set_style_scheme(scheme); + if(view) { Glib::RefPtr language; - if(!language_identifier->empty()) - language = Source::LanguageManager::get_default()->get_language(*language_identifier); - if(!language) { - if(auto source_view = dynamic_cast(view)) - language = source_view->language; + if(auto language_ptr = boost::get>(&language_variant)) + language = *language_ptr; + else if(auto language_identifier = boost::get(&language_variant)) { + if(!language_identifier->empty()) { + language = Source::LanguageManager::get_default()->get_language(*language_identifier); + if(!language) { + if(auto source_view = dynamic_cast(view)) + language = source_view->language; + } + } } if(language) { + auto tmp_view = Gsv::View(); + tmp_view.get_buffer()->set_text(code); + auto scheme = view->get_source_buffer()->get_style_scheme(); + tmp_view.get_source_buffer()->set_style_scheme(scheme); tmp_view.get_source_buffer()->set_language(language); tmp_view.get_source_buffer()->set_highlight_syntax(true); tmp_view.get_source_buffer()->ensure_highlight(tmp_view.get_buffer()->begin(), tmp_view.get_buffer()->end()); @@ -826,7 +831,7 @@ void Tooltip::insert_code(const std::string &code, const boost::optionalbegin(); Gtk::TextIter tmp_start; - if(tmp_iter.starts_tag(tmp_tag)) + if(tmp_iter.begins_tag(tmp_tag)) tmp_start = tmp_iter; while(tmp_iter.forward_to_tag_toggle(tmp_tag)) { if(tmp_iter.ends_tag(tmp_tag)) { diff --git a/src/tooltips.hpp b/src/tooltips.hpp index b110b70..f1df39c 100644 --- a/src/tooltips.hpp +++ b/src/tooltips.hpp @@ -1,6 +1,7 @@ #pragma once #include "source_base.hpp" #include +#include #include #include #include @@ -25,8 +26,9 @@ public: void insert_with_links_tagged(const std::string &text); void insert_markdown(const std::string &text); - void insert_code(const std::string &code, const boost::optional &language_identifier, bool block = false); - // Remove empty lines at end of buffer + // TODO, c++17: use std::monostate instead of Void + void insert_code(const std::string &code, boost::variant> language = Glib::RefPtr{}, bool block = false); + /// Remove empty lines at end of buffer void remove_trailing_newlines(); private: