From 291c0509bdedb35d3cfe0d5c8e201fd2f2a2dec6 Mon Sep 17 00:00:00 2001 From: eidheim Date: Sat, 12 Dec 2015 12:50:23 +0100 Subject: [PATCH 1/2] Better find tab char and size function --- src/source.cc | 159 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 132 insertions(+), 27 deletions(-) diff --git a/src/source.cc b/src/source.cc index 9987a15..a34755e 100644 --- a/src/source.cc +++ b/src/source.cc @@ -520,7 +520,7 @@ void Source::View::replace_backward(const std::string &replacement) { auto &start=selection_bound; Gtk::TextIter match_start, match_end; if(gtk_source_search_context_backward(search_context, start.gobj(), match_start.gobj(), match_end.gobj())) { - auto offset=match_start.get_offset(); + auto offset=match_start.get_offset(); gtk_source_search_context_replace(search_context, match_start.gobj(), match_end.gobj(), replacement.c_str(), replacement.size(), NULL); get_buffer()->select_range(get_buffer()->get_iter_at_offset(offset), get_buffer()->get_iter_at_offset(offset+replacement.size())); @@ -557,7 +557,7 @@ void Source::View::paste() { bool first_paste_line=true; size_t paste_line_tabs=-1; bool first_paste_line_has_tabs=false; - for(size_t c=0;c Source::View::find_tab_char_and_size() { - auto size=get_buffer()->get_line_count(); std::unordered_map tab_chars; std::unordered_map tab_sizes; - unsigned last_tab_size=0; - for(int c=0;c(str.size()-last_tab_size)); - if(tab_diff>0) { - unsigned tab_diff_unsigned=static_cast(tab_diff); - auto it_size=tab_sizes.find(tab_diff_unsigned); - if(it_size!=tab_sizes.end()) - it_size->second++; - else - tab_sizes[tab_diff_unsigned]=1; + auto iter=get_buffer()->begin(); + long tab_count=-1; + long last_tab_count=0; + bool single_quoted=false; + bool double_quoted=false; + //For bracket languages, TODO: add more language ids + 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()=="javascript")) { + bool line_comment=false; + bool comment=false; + bool bracket_last_line=false; + char last_char=0; + long last_tab_diff=-1; + while(iter) { + if(iter.starts_line()) { + line_comment=false; + single_quoted=false; + double_quoted=false; + tab_count=0; + if(last_char=='{') + bracket_last_line=true; + else + bracket_last_line=false; + } + if(bracket_last_line && tab_count!=-1) { + if(*iter==' ') { + tab_chars[' ']++; + tab_count++; + } + else if(*iter=='\t') { + tab_chars['\t']++; + tab_count++; + } + else { + auto line_iter=iter; + char last_line_char=0; + while(line_iter && !line_iter.ends_line()) { + if(*line_iter!=' ' && *line_iter!='\t') + last_line_char=*line_iter; + if(*line_iter=='(') + break; + line_iter.forward_char(); + } + if(last_line_char==':' || *iter=='#') { + tab_count=0; + if((iter.get_line()+1) < get_buffer()->get_line_count()) { + iter=get_buffer()->get_iter_at_line(iter.get_line()+1); + continue; + } + } + else if(!iter.ends_line()) { + if(tab_count!=last_tab_count) + tab_sizes[abs(tab_count-last_tab_count)]++; + last_tab_diff=abs(tab_count-last_tab_count); + last_tab_count=tab_count; + last_char=0; + } + } + } + + auto prev_iter=iter; + prev_iter.backward_char(); + auto prev_prev_iter=prev_iter; + prev_prev_iter.backward_char(); + if(!double_quoted && *iter=='\'' && !(*prev_iter=='\\' && *prev_prev_iter!='\\')) + single_quoted=!single_quoted; + else if(!single_quoted && *iter=='\"' && !(*prev_iter=='\\' && *prev_prev_iter!='\\')) + double_quoted=!double_quoted; + else if(!single_quoted && !double_quoted) { + auto next_iter=iter; + next_iter.forward_char(); + if(*iter=='/' && *next_iter=='/') + line_comment=true; + else if(*iter=='/' && *next_iter=='*') + comment=true; + else if(*iter=='*' && *next_iter=='/') { + iter.forward_char(); + iter.forward_char(); + comment=false; + } + } + if(!single_quoted && !double_quoted && !comment && !line_comment && *iter!=' ' && *iter!='\t' && !iter.ends_line()) + last_char=*iter; + if(!single_quoted && !double_quoted && !comment && !line_comment && *iter=='}' && tab_count!=-1 && last_tab_diff!=-1) + last_tab_count-=last_tab_diff; + if(*iter!=' ' && *iter!='\t') + tab_count=-1; + + iter.forward_char(); } - - last_tab_size=str.size(); - - if(str.size()>0) { - auto it_char=tab_chars.find(str[0]); - if(it_char!=tab_chars.end()) - it_char->second++; - else - tab_chars[str[0]]=1; + } + else { + long para_count=0; + while(iter) { + if(iter.starts_line()) + tab_count=0; + if(tab_count!=-1 && para_count==0 && single_quoted==false && double_quoted==false) { + if(*iter==' ') { + tab_chars[' ']++; + tab_count++; + } + else if(*iter=='\t') { + tab_chars['\t']++; + tab_count++; + } + else if(!iter.ends_line()) { + if(tab_count!=last_tab_count) + tab_sizes[abs(tab_count-last_tab_count)]++; + last_tab_count=tab_count; + } + } + auto prev_iter=iter; + prev_iter.backward_char(); + auto prev_prev_iter=prev_iter; + prev_prev_iter.backward_char(); + if(!double_quoted && *iter=='\'' && !(*prev_iter=='\\' && *prev_prev_iter!='\\')) + single_quoted=!single_quoted; + else if(!single_quoted && *iter=='\"' && !(*prev_iter=='\\' && *prev_prev_iter!='\\')) + double_quoted=!double_quoted; + else if(!single_quoted && !double_quoted) { + if(*iter=='(') + para_count++; + else if(*iter==')') + para_count--; + } + if(*iter!=' ' && *iter!='\t') + tab_count=-1; + + iter.forward_char(); } } + char found_tab_char=0; size_t occurences=0; for(auto &tab_char: tab_chars) { From 3b089e7ec6763cd19aad545c289aab855c2f53bc Mon Sep 17 00:00:00 2001 From: eidheim Date: Sat, 12 Dec 2015 12:58:00 +0100 Subject: [PATCH 2/2] Now linked to v1.0.2 of tiny-process-library, which most importingly fixes some file descriptor closing issues --- .gitmodules | 2 +- tiny-process-library | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitmodules b/.gitmodules index 6d3904f..dca293e 100644 --- a/.gitmodules +++ b/.gitmodules @@ -5,4 +5,4 @@ [submodule "tiny-process-library"] path = tiny-process-library url = https://github.com/eidheim/tiny-process-library - branch = v1.0.1 + branch = v1.0.2 diff --git a/tiny-process-library b/tiny-process-library index 3307488..2805253 160000 --- a/tiny-process-library +++ b/tiny-process-library @@ -1 +1 @@ -Subproject commit 3307488fbf733b18de9b274fe3249d9d54497a06 +Subproject commit 2805253ae779ac6f8fc28c0c55e96c37b87e571d