Browse Source

Cleanp of Tooltip::insert_code

pipelines/235045657
eidheim 5 years ago
parent
commit
b97cc5ca37
  1. 2
      src/project.cpp
  2. 4
      src/source_clang.cpp
  3. 8
      src/source_language_protocol.cpp
  4. 2
      src/source_spellcheck.cpp
  5. 31
      src/tooltips.cpp
  6. 6
      src/tooltips.hpp

2
src/project.cpp

@ -629,7 +629,7 @@ void Project::LLDB::debug_show_variables() {
next_char_iter++; next_char_iter++;
value.replace(iter, 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) { if(view) {

4
src/source_clang.cpp

@ -515,7 +515,7 @@ void Source::ClangViewParse::show_type_tooltips(const Gdk::Rectangle &rectangle)
size_t pos = 0; size_t pos = 0;
while((pos = type_description.find("::__1", pos)) != std::string::npos) while((pos = type_description.find("::__1", pos)) != std::string::npos)
type_description.erase(pos, 5); 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(); auto brief_comment = cursor.get_brief_comments();
if(brief_comment != "") if(brief_comment != "")
tooltip.insert_with_links_tagged("\n\n" + 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; size_t pos = 0;
while((pos = value.find("::__1", pos)) != std::string::npos) while((pos = value.find("::__1", pos)) != std::string::npos)
value.erase(pos, 5); value.erase(pos, 5);
tooltip.insert_code(value, {}); tooltip.insert_code(value);
} }
} }
} }

8
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++) { for(size_t i = 0; i < contents.size(); i++) {
if(i > 0) if(i > 0)
tooltip.buffer->insert_at_cursor("\n\n"); 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); tooltip.insert_with_links_tagged(contents[i].value);
else if(contents[i].kind == "markdown") else if(contents[i].kind == "markdown")
tooltip.insert_markdown(contents[i].value); 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, "?"); debug_value.replace(iter, next_char_iter, "?");
} }
tooltip.buffer->insert_at_cursor("\n\n" + value_type + ":\n"); 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 nullptr;
return [this, autocomplete = std::move(autocomplete)](Tooltip &tooltip) { return [this, autocomplete = std::move(autocomplete)](Tooltip &tooltip) {
if(!autocomplete.detail.empty()) { 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(); tooltip.remove_trailing_newlines();
} }
if(!autocomplete.documentation.empty()) { if(!autocomplete.documentation.empty()) {
if(tooltip.buffer->size() > 0) if(tooltip.buffer->size() > 0)
tooltip.buffer->insert_at_cursor("\n\n"); 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); tooltip.insert_with_links_tagged(autocomplete.documentation);
else if(autocomplete.kind == "markdown") else if(autocomplete.kind == "markdown")
tooltip.insert_markdown(autocomplete.documentation); tooltip.insert_markdown(autocomplete.documentation);

2
src/source_spellcheck.cpp

@ -420,7 +420,7 @@ bool Source::SpellCheckView::is_code_iter(const Gtk::TextIter &iter) {
if(iter.has_tag(comment_tag)) if(iter.has_tag(comment_tag))
return true; return true;
#if GTKMM_MAJOR_VERSION > 3 || (GTKMM_MAJOR_VERSION == 3 && GTKMM_MINOR_VERSION >= 20) #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; return true;
#else #else
if(*iter == '/') { if(*iter == '/') {

31
src/tooltips.cpp

@ -126,7 +126,7 @@ void Tooltip::show(bool disregard_drawn, const std::function<void()> &on_motion)
tooltip_text_view->get_iter_at_location(iter, location_x, location_y); tooltip_text_view->get_iter_at_location(iter, location_x, location_y);
if(iter.has_tag(link_tag)) { if(iter.has_tag(link_tag)) {
auto start = iter; auto start = iter;
if(!start.starts_tag(link_tag)) if(!start.begins_tag(link_tag))
start.backward_to_tag_toggle(link_tag); start.backward_to_tag_toggle(link_tag);
auto end = iter; auto end = iter;
end.forward_to_tag_toggle(link_tag); end.forward_to_tag_toggle(link_tag);
@ -782,7 +782,7 @@ void Tooltip::insert_markdown(const std::string &input) {
remove_trailing_newlines(); remove_trailing_newlines();
} }
void Tooltip::insert_code(const std::string &code, const boost::optional<std::string> &language_identifier, bool block) { void Tooltip::insert_code(const std::string &code, boost::variant<std::string, Glib::RefPtr<Gsv::Language>> language_variant, bool block) {
create_tags(); create_tags();
auto insert_iter = buffer->get_insert()->get_iter(); auto insert_iter = buffer->get_insert()->get_iter();
@ -801,19 +801,24 @@ void Tooltip::insert_code(const std::string &code, const boost::optional<std::st
buffer->insert_with_tag(insert_iter, code, block ? code_block_tag : code_tag); buffer->insert_with_tag(insert_iter, code, block ? code_block_tag : code_tag);
if(view && language_identifier) { if(view) {
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);
Glib::RefPtr<Gsv::Language> language; Glib::RefPtr<Gsv::Language> language;
if(!language_identifier->empty()) if(auto language_ptr = boost::get<Glib::RefPtr<Gsv::Language>>(&language_variant))
language = Source::LanguageManager::get_default()->get_language(*language_identifier); language = *language_ptr;
if(!language) { else if(auto language_identifier = boost::get<std::string>(&language_variant)) {
if(auto source_view = dynamic_cast<Source::View *>(view)) if(!language_identifier->empty()) {
language = source_view->language; language = Source::LanguageManager::get_default()->get_language(*language_identifier);
if(!language) {
if(auto source_view = dynamic_cast<Source::View *>(view))
language = source_view->language;
}
}
} }
if(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_language(language);
tmp_view.get_source_buffer()->set_highlight_syntax(true); 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()); 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::optional<std::st
auto tmp_iter = tmp_view.get_source_buffer()->begin(); auto tmp_iter = tmp_view.get_source_buffer()->begin();
Gtk::TextIter tmp_start; Gtk::TextIter tmp_start;
if(tmp_iter.starts_tag(tmp_tag)) if(tmp_iter.begins_tag(tmp_tag))
tmp_start = tmp_iter; tmp_start = tmp_iter;
while(tmp_iter.forward_to_tag_toggle(tmp_tag)) { while(tmp_iter.forward_to_tag_toggle(tmp_tag)) {
if(tmp_iter.ends_tag(tmp_tag)) { if(tmp_iter.ends_tag(tmp_tag)) {

6
src/tooltips.hpp

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "source_base.hpp" #include "source_base.hpp"
#include <boost/optional.hpp> #include <boost/optional.hpp>
#include <boost/variant.hpp>
#include <functional> #include <functional>
#include <gtksourceviewmm.h> #include <gtksourceviewmm.h>
#include <list> #include <list>
@ -25,8 +26,9 @@ public:
void insert_with_links_tagged(const std::string &text); void insert_with_links_tagged(const std::string &text);
void insert_markdown(const std::string &text); void insert_markdown(const std::string &text);
void insert_code(const std::string &code, const boost::optional<std::string> &language_identifier, bool block = false); // TODO, c++17: use std::monostate instead of Void
// Remove empty lines at end of buffer void insert_code(const std::string &code, boost::variant<std::string, Glib::RefPtr<Gsv::Language>> language = Glib::RefPtr<Gsv::Language>{}, bool block = false);
/// Remove empty lines at end of buffer
void remove_trailing_newlines(); void remove_trailing_newlines();
private: private:

Loading…
Cancel
Save