Browse Source

Added Toggle Comments for various languages, and updated bracket language check

merge-requests/365/head
eidheim 9 years ago
parent
commit
b8bfeea26b
  1. 2
      CMakeLists.txt
  2. 4
      src/files.h
  3. 11
      src/menu.cc
  4. 107
      src/source.cc
  5. 2
      src/source.h
  6. 16
      src/window.cc

2
CMakeLists.txt

@ -1,7 +1,7 @@
cmake_minimum_required (VERSION 2.8.8) cmake_minimum_required (VERSION 2.8.8)
project(juci) project(juci)
set(JUCI_VERSION "1.2.1") set(JUCI_VERSION "1.2.1.1")
set(CPACK_PACKAGE_NAME "jucipp") set(CPACK_PACKAGE_NAME "jucipp")
set(CPACK_PACKAGE_CONTACT "Ole Christian Eidheim <eidheim@gmail.com>") set(CPACK_PACKAGE_CONTACT "Ole Christian Eidheim <eidheim@gmail.com>")

4
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" "font_comment": "Use \"\" for default font, and for instance \"Monospace 12\" to also set size",)RAW"
#ifdef __APPLE__ #ifdef __APPLE__
R"RAW( R"RAW(
"font": "Menlo 11",)RAW" "font": "Menlo 12",)RAW"
#else #else
#ifdef _WIN32 #ifdef _WIN32
R"RAW( R"RAW(
@ -100,6 +100,8 @@ R"RAW(
"source_goto_line": "<primary>g", "source_goto_line": "<primary>g",
"source_center_cursor": "<primary>l", "source_center_cursor": "<primary>l",
"source_find_symbol_ctags": "<primary><shift>f", "source_find_symbol_ctags": "<primary><shift>f",
"source_comments_toggle": "<primary>slash",
"source_comments_add_documentation": "<primary><alt>slash",
"source_find_documentation": "<primary><shift>d", "source_find_documentation": "<primary><shift>d",
"source_goto_declaration": "<primary>d", "source_goto_declaration": "<primary>d",
"source_goto_implementation": "<primary>i", "source_goto_implementation": "<primary>i",

11
src/menu.cc

@ -175,6 +175,17 @@ Menu::Menu() {
</item> </item>
</section> </section>
<section> <section>
<submenu>
<attribute name='label' translatable='yes'>_Comments</attribute>
<item>
<attribute name='label' translatable='yes'>_Toggle _Comments</attribute>
<attribute name='action'>app.source_comments_toggle</attribute>
</item>
<item>
<attribute name='label' translatable='yes'>_Add _Documentation (not yet implemented)</attribute>
<attribute name='action'>app.source_comments_add_documentation</attribute>
</item>
</submenu>
<item> <item>
<attribute name='label' translatable='yes'>_Find _Documentation</attribute> <attribute name='label' translatable='yes'>_Find _Documentation</attribute>
<attribute name='action'>app.source_find_documentation</attribute> <attribute name='action'>app.source_find_documentation</attribute>

107
src/source.cc

@ -149,7 +149,9 @@ Source::View::View(const boost::filesystem::path &file_path, Glib::RefPtr<Gsv::L
if(language && (language->get_id()=="chdr" || language->get_id()=="cpphdr" || language->get_id()=="c" || if(language && (language->get_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()=="cpp" || language->get_id()=="objc" || language->get_id()=="java" ||
language->get_id()=="js" || language->get_id()=="ts" || language->get_id()=="proto" || 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; is_bracket_language=true;
auto_indent=[this]() { auto_indent=[this]() {
@ -254,6 +256,109 @@ Source::View::View(const boost::filesystem::path &file_path, Glib::RefPtr<Gsv::L
} }
} }
set_tab_char_and_size(tab_char, tab_size); set_tab_char_and_size(tab_char, tab_size);
std::shared_ptr<std::string> comment_characters;
if(is_bracket_language)
comment_characters=std::make_shared<std::string>("//");
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<std::string>("#");
else if(language->get_id()=="latex" || language->get_id()=="matlab" || language->get_id()=="octave" ||
language->get_id()=="bibtex")
comment_characters=std::make_shared<std::string>("%");
else if(language->get_id()=="fortran")
comment_characters=std::make_shared<std::string>("!");
else if(language->get_id()=="pascal")
comment_characters=std::make_shared<std::string>("//");
else if(language->get_id()=="lua")
comment_characters=std::make_shared<std::string>("--");
}
if(comment_characters) {
toggle_comments=[this, comment_characters] {
std::vector<int> 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;c<comment_characters->size();++c) {
if(iter.ends_line()) {
break;
}
else if(*iter==static_cast<unsigned int>((*comment_characters)[c])) {
if(c<comment_characters->size()-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 || indentation<min_indentation)
min_indentation=indentation;
}
}
if(lines.size()) {
auto comment_characters_and_space=*comment_characters+' ';
get_buffer()->begin_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<int>(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) { void Source::View::set_tab_char_and_size(char tab_char, unsigned tab_size) {

2
src/source.h

@ -69,6 +69,8 @@ namespace Source {
std::function<std::vector<std::pair<boost::filesystem::path, size_t> >(const std::vector<Source::View*> &views, const std::string &text)> rename_similar_tokens; std::function<std::vector<std::pair<boost::filesystem::path, size_t> >(const std::vector<Source::View*> &views, const std::string &text)> rename_similar_tokens;
std::function<void()> goto_next_diagnostic; std::function<void()> goto_next_diagnostic;
std::function<std::vector<FixIt>()> get_fix_its; std::function<std::vector<FixIt>()> get_fix_its;
std::function<void()> toggle_comments;
std::function<void()> add_documentation;
std::unique_ptr<CompletionDialog> autocomplete_dialog; std::unique_ptr<CompletionDialog> autocomplete_dialog;
std::unique_ptr<SelectionDialog> selection_dialog; std::unique_ptr<SelectionDialog> selection_dialog;

16
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]() { menu.add_action("source_find_documentation", [this]() {
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) {
@ -1033,6 +1047,8 @@ void Window::activate_menu_items(bool activate) {
auto &notebook = Notebook::get(); auto &notebook = Notebook::get();
menu.actions["source_indentation_auto_indent_buffer"]->set_enabled(activate ? static_cast<bool>(notebook.get_current_view()->auto_indent) : false); menu.actions["source_indentation_auto_indent_buffer"]->set_enabled(activate ? static_cast<bool>(notebook.get_current_view()->auto_indent) : false);
menu.actions["source_find_symbol_ctags"]->set_enabled(activate); menu.actions["source_find_symbol_ctags"]->set_enabled(activate);
menu.actions["source_comments_toggle"]->set_enabled(activate ? static_cast<bool>(notebook.get_current_view()->toggle_comments) : false);
menu.actions["source_comments_add_documentation"]->set_enabled(activate ? static_cast<bool>(notebook.get_current_view()->add_documentation) : false);
menu.actions["source_find_documentation"]->set_enabled(activate ? static_cast<bool>(notebook.get_current_view()->get_token_data) : false); menu.actions["source_find_documentation"]->set_enabled(activate ? static_cast<bool>(notebook.get_current_view()->get_token_data) : false);
menu.actions["source_goto_declaration"]->set_enabled(activate ? static_cast<bool>(notebook.get_current_view()->get_declaration_location) : false); menu.actions["source_goto_declaration"]->set_enabled(activate ? static_cast<bool>(notebook.get_current_view()->get_declaration_location) : false);
menu.actions["source_goto_implementation"]->set_enabled(activate ? static_cast<bool>(notebook.get_current_view()->get_implementation_locations) : false); menu.actions["source_goto_implementation"]->set_enabled(activate ? static_cast<bool>(notebook.get_current_view()->get_implementation_locations) : false);

Loading…
Cancel
Save