From f746ad5a36c706fd3742b4f39cc83a77e94200de Mon Sep 17 00:00:00 2001 From: eidheim Date: Tue, 3 Jan 2017 11:50:16 +0100 Subject: [PATCH] Processing speed improvements of format style through clang-format. Can now also format selected text only. --- src/source.cc | 65 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 62 insertions(+), 3 deletions(-) diff --git a/src/source.cc b/src/source.cc index 3522248..df87a54 100644 --- a/src/source.cc +++ b/src/source.cc @@ -168,9 +168,15 @@ Source::View::View(const boost::filesystem::path &file_path, Glib::RefPtrfile_path.parent_path()); if(exit_status==0) { - replace_text(stdout_stream.str()); + // The following code is complex due to clang-format returning offsets in byte offsets instead of char offsets + + // Create bytes_in_lines cache to significantly speed up the processing of finding iterators from byte offsets + std::vector bytes_in_lines; + auto line_count=get_buffer()->get_line_count(); + for(int line_nr=0;line_nrget_iter_at_line(line_nr); + bytes_in_lines.emplace_back(iter.get_bytes_in_line()); + } + + get_buffer()->begin_user_action(); + try { + boost::property_tree::ptree pt; + boost::property_tree::xml_parser::read_xml(stdout_stream, pt); + auto replacements_pt=pt.get_child("replacements"); + for(auto it=replacements_pt.rbegin();it!=replacements_pt.rend();++it) { + if(it->first=="replacement") { + auto offset=it->second.get(".offset"); + auto length=it->second.get(".length"); + auto replacement_str=it->second.get(""); + + size_t bytes=0; + for(size_t c=0;c line_index(c, offset-previous_bytes); + auto start=get_buffer()->get_iter_at_line_index(line_index.first, line_index.second); + + if(length>0) { + auto offset_end=offset+length; + size_t bytes=0; + for(size_t c=0;cget_iter_at_line_index(c, offset_end-previous_bytes); + get_buffer()->erase(start, end); + break; + } + } + } + start=get_buffer()->get_iter_at_line_index(line_index.first, line_index.second); + get_buffer()->insert(start, replacement_str); + break; + } + } + } + } + } + catch(const std::exception &e) { + Terminal::get().print(std::string("Error: error parsing clang-format output: ")+e.what()+'\n', true); + } + get_buffer()->end_user_action(); } }; }