Browse Source

Http(s) urls in tooltips are now marked, and behaves, as links

merge-requests/389/head
eidheim 7 years ago
parent
commit
8d2b85a680
  1. 15
      src/notebook.cc
  2. 1
      src/notebook.h
  3. 16
      src/project.cc
  4. 14
      src/source.cc
  5. 5
      src/source.h
  6. 2
      src/source_clang.cc
  7. 4
      src/source_language_protocol.cc
  8. 6
      src/tooltips.cc
  9. 20
      src/window.cc

15
src/notebook.cc

@ -417,6 +417,21 @@ void Notebook::open(const boost::filesystem::path &file_path_, size_t notebook_i
focus_view(source_view); focus_view(source_view);
} }
void Notebook::open_uri(const std::string &uri) {
#ifdef __APPLE__
Terminal::get().process("open " + filesystem::escape_argument(uri));
#else
GError *error = nullptr;
#if GTK_VERSION_GE(3, 22)
gtk_show_uri_on_window(nullptr, uri.c_str(), GDK_CURRENT_TIME, &error);
#else
gtk_show_uri(nullptr, uri.c_str(), GDK_CURRENT_TIME, &error);
#endif
g_clear_error(&error);
#endif
}
void Notebook::configure(size_t index) { void Notebook::configure(size_t index) {
auto source_font_description = Pango::FontDescription(Config::get().source.font); auto source_font_description = Pango::FontDescription(Config::get().source.font);
auto source_map_font_desc = Pango::FontDescription(static_cast<std::string>(source_font_description.get_family()) + " " + Config::get().source.map_font_size); auto source_map_font_desc = Pango::FontDescription(static_cast<std::string>(source_font_description.get_family()) + " " + Config::get().source.map_font_size);

1
src/notebook.h

@ -35,6 +35,7 @@ public:
std::vector<Source::View *> &get_views(); std::vector<Source::View *> &get_views();
void open(const boost::filesystem::path &file_path, size_t notebook_index = -1); void open(const boost::filesystem::path &file_path, size_t notebook_index = -1);
void open_uri(const std::string &uri);
void configure(size_t index); void configure(size_t index);
bool save(size_t index); bool save(size_t index);
bool save_current(); bool save_current();

16
src/project.cc

@ -983,21 +983,7 @@ void Project::JavaScript::compile_and_run() {
} }
void Project::HTML::compile_and_run() { void Project::HTML::compile_and_run() {
auto uri = Notebook::get().get_current_view()->file_path.string(); Notebook::get().open_uri(std::string("file://") + Notebook::get().get_current_view()->file_path.string());
#ifdef __APPLE__
Terminal::get().process("open " + filesystem::escape_argument(uri));
#else
#ifdef __linux
uri = "file://" + uri;
#endif
GError *error = nullptr;
#if GTK_VERSION_GE(3, 22)
gtk_show_uri_on_window(nullptr, uri.c_str(), GDK_CURRENT_TIME, &error);
#else
gtk_show_uri(nullptr, uri.c_str(), GDK_CURRENT_TIME, &error);
#endif
g_clear_error(&error);
#endif
} }
std::pair<std::string, std::string> Project::Rust::get_run_arguments() { std::pair<std::string, std::string> Project::Rust::get_run_arguments() {

14
src/source.cc

@ -952,6 +952,20 @@ void Source::View::hide_dialogs() {
CompletionDialog::get()->hide(); CompletionDialog::get()->hide();
} }
void Source::View::insert_with_links_tagged(const Glib::RefPtr<Gtk::TextBuffer> &buffer, const std::string &text) {
static std::regex http_regex("(https?://[a-zA-Z0-9\\-._~:/?#\\[\\]@!$&'()*+,;=]+[a-zA-Z0-9\\-_~/@$*+;=])");
std::smatch sm;
std::sregex_iterator it(text.begin(), text.end(), http_regex);
std::sregex_iterator end;
size_t start_pos = 0;
for(; it != end; ++it) {
buffer->insert(buffer->get_insert()->get_iter(), &text[start_pos], &text[it->position()]);
buffer->insert_with_tag(buffer->get_insert()->get_iter(), &text[it->position()], &text[it->position() + it->length()], link_tag);
start_pos = it->position() + it->length();
}
buffer->insert(buffer->get_insert()->get_iter(), &text[start_pos], &text[text.size()]);
}
void Source::View::add_diagnostic_tooltip(const Gtk::TextIter &start, const Gtk::TextIter &end, bool error, std::function<void(const Glib::RefPtr<Gtk::TextBuffer> &)> &&set_buffer) { void Source::View::add_diagnostic_tooltip(const Gtk::TextIter &start, const Gtk::TextIter &end, bool error, std::function<void(const Glib::RefPtr<Gtk::TextBuffer> &)> &&set_buffer) {
diagnostic_offsets.emplace(start.get_offset()); diagnostic_offsets.emplace(start.get_offset());

5
src/source.h

@ -101,6 +101,9 @@ namespace Source {
virtual void apply_clickable_tag(const Gtk::TextIter &iter) {} virtual void apply_clickable_tag(const Gtk::TextIter &iter) {}
bool clickable_tag_applied = false; bool clickable_tag_applied = false;
Glib::RefPtr<Gtk::TextTag> link_tag; /// Used in tooltips
void insert_with_links_tagged(const Glib::RefPtr<Gtk::TextBuffer> &buffer, const std::string &text);
virtual void show_diagnostic_tooltips(const Gdk::Rectangle &rectangle) { diagnostic_tooltips.show(rectangle); } virtual void show_diagnostic_tooltips(const Gdk::Rectangle &rectangle) { diagnostic_tooltips.show(rectangle); }
void add_diagnostic_tooltip(const Gtk::TextIter &start, const Gtk::TextIter &end, bool error, std::function<void(const Glib::RefPtr<Gtk::TextBuffer> &)> &&set_buffer); void add_diagnostic_tooltip(const Gtk::TextIter &start, const Gtk::TextIter &end, bool error, std::function<void(const Glib::RefPtr<Gtk::TextBuffer> &)> &&set_buffer);
void clear_diagnostic_tooltips(); void clear_diagnostic_tooltips();
@ -150,8 +153,6 @@ namespace Source {
int multiple_cursors_erase_backward_length; int multiple_cursors_erase_backward_length;
int multiple_cursors_erase_forward_length; int multiple_cursors_erase_forward_length;
bool on_key_press_event_multiple_cursors(GdkEventKey *key); bool on_key_press_event_multiple_cursors(GdkEventKey *key);
Glib::RefPtr<Gtk::TextTag> link_tag; /// Used in tooltips
}; };
class GenericView : public View { class GenericView : public View {

2
src/source_clang.cc

@ -389,7 +389,7 @@ void Source::ClangViewParse::show_type_tooltips(const Gdk::Rectangle &rectangle)
buffer->insert(buffer->get_insert()->get_iter(), "Type: " + token.get_cursor().get_type_description()); buffer->insert(buffer->get_insert()->get_iter(), "Type: " + token.get_cursor().get_type_description());
auto brief_comment = token.get_cursor().get_brief_comments(); auto brief_comment = token.get_cursor().get_brief_comments();
if(brief_comment != "") if(brief_comment != "")
buffer->insert(buffer->get_insert()->get_iter(), "\n\n" + brief_comment); insert_with_links_tagged(buffer, "\n\n" + brief_comment);
#ifdef JUCI_ENABLE_DEBUG #ifdef JUCI_ENABLE_DEBUG
if(Debug::LLDB::get().is_stopped()) { if(Debug::LLDB::get().is_stopped()) {

4
src/source_language_protocol.cc

@ -896,7 +896,7 @@ void Source::LanguageProtocolView::update_diagnostics(std::vector<LanguageProtoc
buffer->insert_at_cursor(diagnostic.related_informations[i].message); buffer->insert_at_cursor(diagnostic.related_informations[i].message);
buffer->insert_at_cursor(": "); buffer->insert_at_cursor(": ");
auto pos = buffer->get_insert()->get_iter(); auto pos = buffer->get_insert()->get_iter();
buffer->insert_with_tag(pos, link, "link"); buffer->insert_with_tag(pos, link, link_tag);
if(i != diagnostic.related_informations.size() - 1) if(i != diagnostic.related_informations.size() - 1)
buffer->insert_at_cursor("\n"); buffer->insert_at_cursor("\n");
} }
@ -979,7 +979,7 @@ void Source::LanguageProtocolView::show_type_tooltips(const Gdk::Rectangle &rect
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()) {
} }
type_tooltips.emplace_back(this, get_buffer()->create_mark(start), get_buffer()->create_mark(end), [this, offset, content](const Glib::RefPtr<Gtk::TextBuffer> &buffer) { type_tooltips.emplace_back(this, get_buffer()->create_mark(start), get_buffer()->create_mark(end), [this, offset, content](const Glib::RefPtr<Gtk::TextBuffer> &buffer) {
buffer->insert(buffer->get_insert()->get_iter(), *content); insert_with_links_tagged(buffer, *content);
#ifdef JUCI_ENABLE_DEBUG #ifdef JUCI_ENABLE_DEBUG
if(language_id == "rust" && capabilities.definition) { if(language_id == "rust" && capabilities.definition) {

6
src/tooltips.cc

@ -116,6 +116,12 @@ void Tooltip::show(bool disregard_drawn, const std::function<void()> &on_motion)
auto end = iter; auto end = iter;
end.forward_to_tag_toggle(link_tag); end.forward_to_tag_toggle(link_tag);
std::string text = tooltip_text_view->get_buffer()->get_text(start, end); std::string text = tooltip_text_view->get_buffer()->get_text(start, end);
if(text.compare(0, 7, "http://") == 0 || text.compare(0, 8, "https://") == 0) {
Notebook::get().open_uri(text);
return true;
}
static std::regex regex("^([^:]+):([^:]+):([^:]+)$"); static std::regex regex("^([^:]+):([^:]+):([^:]+)$");
std::smatch sm; std::smatch sm;
if(std::regex_match(text, sm, regex)) { if(std::regex_match(text, sm, regex)) {

20
src/window.cc

@ -724,9 +724,9 @@ void Window::set_menu_actions() {
if(auto view = Notebook::get().get_current_view()) { if(auto view = Notebook::get().get_current_view()) {
if(view->get_token_data) { if(view->get_token_data) {
auto data = view->get_token_data(); auto data = view->get_token_data();
std::string url; std::string uri;
if(data.size() == 1) if(data.size() == 1)
url = data[0]; uri = data[0];
else if(data.size() > 1) { else if(data.size() > 1) {
auto documentation_search = Config::get().source.documentation_searches.find(data[0]); auto documentation_search = Config::get().source.documentation_searches.find(data[0]);
if(documentation_search != Config::get().source.documentation_searches.end()) { if(documentation_search != Config::get().source.documentation_searches.end()) {
@ -748,22 +748,12 @@ void Window::set_menu_actions() {
query = documentation_search->second.queries.find("@any"); query = documentation_search->second.queries.find("@any");
if(query != documentation_search->second.queries.end()) if(query != documentation_search->second.queries.end())
url = query->second + token_query; uri = query->second + token_query;
} }
} }
} }
if(!url.empty()) { if(!uri.empty()) {
#ifdef __APPLE__ Notebook::get().open_uri(uri);
Terminal::get().process("open \"" + url + "\"");
#else
GError *error = nullptr;
#if GTK_VERSION_GE(3, 22)
gtk_show_uri_on_window(nullptr, url.c_str(), GDK_CURRENT_TIME, &error);
#else
gtk_show_uri(nullptr, url.c_str(), GDK_CURRENT_TIME, &error);
#endif
g_clear_error(&error);
#endif
} }
} }
} }

Loading…
Cancel
Save