From 35d96b453acb8692008e8a744f5c0d4a7f183b38 Mon Sep 17 00:00:00 2001 From: eidheim Date: Mon, 25 May 2020 09:29:10 +0200 Subject: [PATCH] Improved C/C++ fixit text --- src/source.cc | 36 ++++++++++++------------------------ src/source.h | 2 +- src/source_clang.cc | 2 +- 3 files changed, 14 insertions(+), 26 deletions(-) diff --git a/src/source.cc b/src/source.cc index 17095a6..fe7547a 100644 --- a/src/source.cc +++ b/src/source.cc @@ -98,30 +98,18 @@ Source::FixIt::FixIt(std::string source_, std::pair offsets_) : } } -std::string Source::FixIt::string(const Glib::RefPtr &buffer) { - auto iter = buffer->get_iter_at_line_index(offsets.first.line, offsets.first.index); - unsigned first_line_offset = iter.get_line_offset() + 1; - iter = buffer->get_iter_at_line_index(offsets.second.line, offsets.second.index); - unsigned second_line_offset = iter.get_line_offset() + 1; - - std::string text; - if(type == Type::insert) { - text += "Insert " + source + " at "; - text += std::to_string(offsets.first.line + 1) + ":" + std::to_string(first_line_offset); - } - else if(type == Type::replace) { - text += "Replace "; - text += std::to_string(offsets.first.line + 1) + ":" + std::to_string(first_line_offset) + " - "; - text += std::to_string(offsets.second.line + 1) + ":" + std::to_string(second_line_offset); - text += " with " + source; - } - else { - text += "Erase "; - text += std::to_string(offsets.first.line + 1) + ":" + std::to_string(first_line_offset) + " - "; - text += std::to_string(offsets.second.line + 1) + ":" + std::to_string(second_line_offset); - } - - return text; +std::string Source::FixIt::string(BaseView &view) { + auto pos_str = " at " + std::to_string(offsets.first.line + 1) + ":" + std::to_string(view.get_iter_at_line_index(offsets.first.line, offsets.first.index).get_line_offset() + 1); + + if(type == Type::insert) + return "Insert " + source + pos_str; + else if(type == Type::replace) + return "Replace " + view.get_buffer()->get_text(view.get_iter_at_line_index(offsets.first.line, offsets.first.index), + view.get_iter_at_line_index(offsets.second.line, offsets.second.index)) + + pos_str + " with " + source; + else + return "Erase " + view.get_buffer()->get_text(view.get_iter_at_line_index(offsets.first.line, offsets.first.index), + view.get_iter_at_line_index(offsets.second.line, offsets.second.index)) + pos_str; } ////////////// diff --git a/src/source.h b/src/source.h index 12fbf2c..52cf285 100644 --- a/src/source.h +++ b/src/source.h @@ -45,7 +45,7 @@ namespace Source { FixIt(std::string source_, std::pair offsets_); - std::string string(const Glib::RefPtr &buffer); + std::string string(BaseView &view); Type type; std::string source; diff --git a/src/source_clang.cc b/src/source_clang.cc index 21c57dd..0a609e2 100644 --- a/src/source_clang.cc +++ b/src/source_clang.cc @@ -337,7 +337,7 @@ void Source::ClangViewParse::update_diagnostics() { if(fix_its_string.size() > 0) fix_its_string += '\n'; - fix_its_string += fix_its.back().string(get_buffer()); + fix_its_string += fix_its.back().string(*this); fix_its_count++; num_fix_its++; }