From b8bfeea26b9c1517f7f461c74efd0bf7ac261382 Mon Sep 17 00:00:00 2001 From: eidheim Date: Sun, 28 Aug 2016 11:00:23 +0200 Subject: [PATCH] Added Toggle Comments for various languages, and updated bracket language check --- CMakeLists.txt | 2 +- src/files.h | 4 +- src/menu.cc | 11 +++++ src/source.cc | 107 ++++++++++++++++++++++++++++++++++++++++++++++++- src/source.h | 2 + src/window.cc | 16 ++++++++ 6 files changed, 139 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c8d73bc..b14a6d1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required (VERSION 2.8.8) project(juci) -set(JUCI_VERSION "1.2.1") +set(JUCI_VERSION "1.2.1.1") set(CPACK_PACKAGE_NAME "jucipp") set(CPACK_PACKAGE_CONTACT "Ole Christian Eidheim ") diff --git a/src/files.h b/src/files.h index 4ce002e..95b4f24 100644 --- a/src/files.h +++ b/src/files.h @@ -26,7 +26,7 @@ const std::string default_config_file = R"RAW({ "font_comment": "Use \"\" for default font, and for instance \"Monospace 12\" to also set size",)RAW" #ifdef __APPLE__ R"RAW( - "font": "Menlo 11",)RAW" + "font": "Menlo 12",)RAW" #else #ifdef _WIN32 R"RAW( @@ -100,6 +100,8 @@ R"RAW( "source_goto_line": "g", "source_center_cursor": "l", "source_find_symbol_ctags": "f", + "source_comments_toggle": "slash", + "source_comments_add_documentation": "slash", "source_find_documentation": "d", "source_goto_declaration": "d", "source_goto_implementation": "i", diff --git a/src/menu.cc b/src/menu.cc index 4cf8254..f17805c 100644 --- a/src/menu.cc +++ b/src/menu.cc @@ -175,6 +175,17 @@ Menu::Menu() {
+ + _Comments + + _Toggle _Comments + app.source_comments_toggle + + + _Add _Documentation (not yet implemented) + app.source_comments_add_documentation + + _Find _Documentation app.source_find_documentation diff --git a/src/source.cc b/src/source.cc index c541eaa..fcd0866 100644 --- a/src/source.cc +++ b/src/source.cc @@ -149,7 +149,9 @@ Source::View::View(const boost::filesystem::path &file_path, Glib::RefPtrget_id()=="chdr" || language->get_id()=="cpphdr" || language->get_id()=="c" || language->get_id()=="cpp" || language->get_id()=="objc" || language->get_id()=="java" || language->get_id()=="js" || language->get_id()=="ts" || language->get_id()=="proto" || - language->get_id()=="c-sharp" || language->get_id()=="html" || language->get_id()=="cuda")) { + language->get_id()=="c-sharp" || language->get_id()=="html" || language->get_id()=="cuda" || + language->get_id()=="php" || language->get_id()=="rust" || language->get_id()=="swift" || + language->get_id()=="go" || language->get_id()=="scala" || language->get_id()=="opencl")) { is_bracket_language=true; auto_indent=[this]() { @@ -254,6 +256,109 @@ Source::View::View(const boost::filesystem::path &file_path, Glib::RefPtr comment_characters; + if(is_bracket_language) + comment_characters=std::make_shared("//"); + else if(language) { + if(language->get_id()=="cmake" || language->get_id()=="makefile" || language->get_id()=="python" || + language->get_id()=="python3" || language->get_id()=="sh" || language->get_id()=="perl" || + language->get_id()=="ruby" || language->get_id()=="r" || language->get_id()=="asm" || + language->get_id()=="automake") + comment_characters=std::make_shared("#"); + else if(language->get_id()=="latex" || language->get_id()=="matlab" || language->get_id()=="octave" || + language->get_id()=="bibtex") + comment_characters=std::make_shared("%"); + else if(language->get_id()=="fortran") + comment_characters=std::make_shared("!"); + else if(language->get_id()=="pascal") + comment_characters=std::make_shared("//"); + else if(language->get_id()=="lua") + comment_characters=std::make_shared("--"); + } + if(comment_characters) { + toggle_comments=[this, comment_characters] { + std::vector lines; + Gtk::TextIter selection_start, selection_end; + get_buffer()->get_selection_bounds(selection_start, selection_end); + auto line_start=selection_start.get_line(); + auto line_end=selection_end.get_line(); + if(line_start!=line_end && selection_end.starts_line()) + --line_end; + bool lines_commented=true; + bool extra_spaces=true; + int min_indentation=-1; + for(auto line=line_start;line<=line_end;++line) { + auto iter=get_buffer()->get_iter_at_line(line); + bool line_added=false; + bool line_commented=false; + bool extra_space=false; + int indentation=0; + for(;;) { + if(iter.ends_line()) + break; + else if(*iter==' ' || *iter=='\t') { + ++indentation; + iter.forward_char(); + continue; + } + else { + lines.emplace_back(line); + line_added=true; + for(size_t c=0;csize();++c) { + if(iter.ends_line()) { + break; + } + else if(*iter==static_cast((*comment_characters)[c])) { + if(csize()-1) { + iter.forward_char(); + continue; + } + else { + line_commented=true; + if(!iter.ends_line()) { + iter.forward_char(); + if(*iter==' ') + extra_space=true; + } + break; + } + } + else + break; + } + break; + } + } + if(line_added) { + lines_commented&=line_commented; + extra_spaces&=extra_space; + if(min_indentation==-1 || indentationbegin_user_action(); + for(auto &line: lines) { + auto iter=get_buffer()->get_iter_at_line(line); + iter.forward_chars(min_indentation); + if(lines_commented) { + auto end_iter=iter; + end_iter.forward_chars(comment_characters->size()+static_cast(extra_spaces)); + while(*iter==' ' || *iter=='\t') { + iter.forward_char(); + end_iter.forward_char(); + } + get_buffer()->erase(iter, end_iter); + } + else + get_buffer()->insert(iter, comment_characters_and_space); + } + get_buffer()->end_user_action(); + } + }; + } } void Source::View::set_tab_char_and_size(char tab_char, unsigned tab_size) { diff --git a/src/source.h b/src/source.h index 7129170..01ef30f 100644 --- a/src/source.h +++ b/src/source.h @@ -69,6 +69,8 @@ namespace Source { std::function >(const std::vector &views, const std::string &text)> rename_similar_tokens; std::function goto_next_diagnostic; std::function()> get_fix_its; + std::function toggle_comments; + std::function add_documentation; std::unique_ptr autocomplete_dialog; std::unique_ptr selection_dialog; diff --git a/src/window.cc b/src/window.cc index 9f8c0c4..3516e1e 100644 --- a/src/window.cc +++ b/src/window.cc @@ -515,6 +515,20 @@ void Window::set_menu_actions() { } }); + menu.add_action("source_comments_toggle", [this]() { + if(auto view=Notebook::get().get_current_view()) { + if(view->toggle_comments) { + view->toggle_comments(); + } + } + }); + menu.add_action("source_comments_add_documentation", [this]() { + if(auto view=Notebook::get().get_current_view()) { + if(view->add_documentation) { + view->add_documentation(); + } + } + }); menu.add_action("source_find_documentation", [this]() { if(auto view=Notebook::get().get_current_view()) { if(view->get_token_data) { @@ -1033,6 +1047,8 @@ void Window::activate_menu_items(bool activate) { auto ¬ebook = Notebook::get(); menu.actions["source_indentation_auto_indent_buffer"]->set_enabled(activate ? static_cast(notebook.get_current_view()->auto_indent) : false); menu.actions["source_find_symbol_ctags"]->set_enabled(activate); + menu.actions["source_comments_toggle"]->set_enabled(activate ? static_cast(notebook.get_current_view()->toggle_comments) : false); + menu.actions["source_comments_add_documentation"]->set_enabled(activate ? static_cast(notebook.get_current_view()->add_documentation) : false); menu.actions["source_find_documentation"]->set_enabled(activate ? static_cast(notebook.get_current_view()->get_token_data) : false); menu.actions["source_goto_declaration"]->set_enabled(activate ? static_cast(notebook.get_current_view()->get_declaration_location) : false); menu.actions["source_goto_implementation"]->set_enabled(activate ? static_cast(notebook.get_current_view()->get_implementation_locations) : false);