Browse Source

Cleanup of source and source_clang.

merge-requests/365/head
eidheim 10 years ago
parent
commit
72d192e08c
  1. 17
      src/source.cc
  2. 27
      src/source.h
  3. 20
      src/source_clang.cc
  4. 1
      src/source_clang.h

17
src/source.cc

@ -52,22 +52,27 @@ Source::FixIt::FixIt(const std::string &source, const std::pair<Offset, Offset>
} }
} }
std::string Source::FixIt::string() { std::string Source::FixIt::string(Glib::RefPtr<Gtk::TextBuffer> buffer) {
auto iter=buffer->get_iter_at_line_index(offsets.first.line-1, offsets.first.index-1);
unsigned first_line_offset=iter.get_line_offset()+1;
iter=buffer->get_iter_at_line_index(offsets.second.line-1, offsets.second.index-1);
unsigned second_line_offset=iter.get_line_offset()+1;
std::string text; std::string text;
if(type==Type::INSERT) { if(type==Type::INSERT) {
text+="Insert "+source+" at "; text+="Insert "+source+" at ";
text+=std::to_string(offsets.first.line)+":"+std::to_string(offsets.first.offset); text+=std::to_string(offsets.first.line)+":"+std::to_string(first_line_offset);
} }
else if(type==Type::REPLACE) { else if(type==Type::REPLACE) {
text+="Replace "; text+="Replace ";
text+=std::to_string(offsets.first.line)+":"+std::to_string(offsets.first.offset)+" - "; text+=std::to_string(offsets.first.line)+":"+std::to_string(first_line_offset)+" - ";
text+=std::to_string(offsets.second.line)+":"+std::to_string(offsets.second.offset); text+=std::to_string(offsets.second.line)+":"+std::to_string(second_line_offset);
text+=" with "+source; text+=" with "+source;
} }
else { else {
text+="Erase "; text+="Erase ";
text+=std::to_string(offsets.first.line)+":"+std::to_string(offsets.first.offset)+" - "; text+=std::to_string(offsets.first.line)+":"+std::to_string(first_line_offset)+" - ";
text+=std::to_string(offsets.second.line)+":"+std::to_string(offsets.second.offset); text+=std::to_string(offsets.second.line)+":"+std::to_string(second_line_offset);
} }
return text; return text;

27
src/source.h

@ -3,7 +3,6 @@
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
#include "gtkmm.h" #include "gtkmm.h"
#include "clangmm.h"
#include <string> #include <string>
#include "gtksourceviewmm.h" #include "gtksourceviewmm.h"
#include "terminal.h" #include "terminal.h"
@ -57,25 +56,23 @@ namespace Source {
std::string usr; std::string usr;
}; };
class FixIt { class Offset {
public: public:
class Offset { Offset() {}
public: Offset(unsigned line, unsigned index): line(line), index(index) {}
Offset() {} bool operator==(const Offset &o) {return (line==o.line && index==o.index);}
Offset(unsigned line, unsigned offset): line(line), offset(offset) {}
bool operator==(const Offset &o) {return (line==o.line && offset==o.offset);}
unsigned line;
unsigned offset;
};
unsigned line;
unsigned index;
};
class FixIt {
public:
enum class Type {INSERT, REPLACE, ERASE}; enum class Type {INSERT, REPLACE, ERASE};
FixIt(Type type, const std::string &source, const std::pair<Offset, Offset> &offsets):
type(type), source(source), offsets(offsets) {}
FixIt(const std::string &source, const std::pair<Offset, Offset> &offsets); FixIt(const std::string &source, const std::pair<Offset, Offset> &offsets);
std::string string(); std::string string(Glib::RefPtr<Gtk::TextBuffer> buffer);
Type type; Type type;
std::string source; std::string source;
@ -103,7 +100,7 @@ namespace Source {
boost::filesystem::path project_path; boost::filesystem::path project_path;
Glib::RefPtr<Gsv::Language> language; Glib::RefPtr<Gsv::Language> language;
std::function<std::pair<std::string, clang::Offset>()> get_declaration_location; std::function<std::pair<std::string, Offset>()> get_declaration_location;
std::function<void()> goto_method; std::function<void()> goto_method;
std::function<Token()> get_token; std::function<Token()> get_token;
std::function<std::vector<std::string>()> get_token_data; std::function<std::vector<std::string>()> get_token_data;

20
src/source_clang.cc

@ -303,19 +303,17 @@ void Source::ClangViewParse::update_diagnostics() {
for(auto &fix_it: diagnostic.fix_its) { for(auto &fix_it: diagnostic.fix_its) {
//Convert line index to line offset for correct output: //Convert line index to line offset for correct output:
auto clang_offsets=fix_it.offsets; auto clang_offsets=fix_it.offsets;
std::pair<FixIt::Offset, FixIt::Offset> offsets; std::pair<Offset, Offset> offsets;
offsets.first.line=clang_offsets.first.line; offsets.first.line=clang_offsets.first.line;
offsets.first.index=clang_offsets.first.index;
offsets.second.line=clang_offsets.second.line; offsets.second.line=clang_offsets.second.line;
auto iter=get_buffer()->get_iter_at_line_index(clang_offsets.first.line-1, clang_offsets.first.index-1); offsets.second.index=clang_offsets.second.index;
offsets.first.offset=iter.get_line_offset()+1;
iter=get_buffer()->get_iter_at_line_index(clang_offsets.second.line-1, clang_offsets.second.index-1);
offsets.second.offset=iter.get_line_offset()+1;
fix_its.emplace_back(fix_it.source, offsets); fix_its.emplace_back(fix_it.source, offsets);
if(fix_its_string.size()>0) if(fix_its_string.size()>0)
fix_its_string+='\n'; fix_its_string+='\n';
fix_its_string+=fix_its.back().string(); fix_its_string+=fix_its.back().string(get_buffer());
fix_its_count++; fix_its_count++;
num_fix_its++; num_fix_its++;
} }
@ -986,7 +984,7 @@ Source::ClangViewAutocomplete(file_path, project_path, language) {
}); });
get_declaration_location=[this](){ get_declaration_location=[this](){
std::pair<std::string, clang::Offset> location; std::pair<std::string, Offset> location;
if(source_readable) { if(source_readable) {
auto iter=get_buffer()->get_insert()->get_iter(); auto iter=get_buffer()->get_insert()->get_iter();
auto line=(unsigned)iter.get_line(); auto line=(unsigned)iter.get_line();
@ -998,7 +996,9 @@ Source::ClangViewAutocomplete(file_path, project_path, language) {
auto referenced=cursor.get_referenced(); auto referenced=cursor.get_referenced();
if(referenced) { if(referenced) {
location.first=referenced.get_source_location().get_path(); location.first=referenced.get_source_location().get_path();
location.second=referenced.get_source_location().get_offset(); auto clang_offset=referenced.get_source_location().get_offset();
location.second.line=clang_offset.line;
location.second.index=clang_offset.index;
break; break;
} }
} }
@ -1166,8 +1166,8 @@ Source::ClangViewAutocomplete(file_path, project_path, language) {
std::vector<std::pair<Glib::RefPtr<Gtk::TextMark>, Glib::RefPtr<Gtk::TextMark> > > fix_it_marks; std::vector<std::pair<Glib::RefPtr<Gtk::TextMark>, Glib::RefPtr<Gtk::TextMark> > > fix_it_marks;
if(source_readable) { if(source_readable) {
for(auto &fix_it: fix_its) { for(auto &fix_it: fix_its) {
auto start_iter=get_buffer()->get_iter_at_line_offset(fix_it.offsets.first.line-1, fix_it.offsets.first.offset-1); auto start_iter=get_buffer()->get_iter_at_line_index(fix_it.offsets.first.line-1, fix_it.offsets.first.index-1);
auto end_iter=get_buffer()->get_iter_at_line_offset(fix_it.offsets.second.line-1, fix_it.offsets.second.offset-1); auto end_iter=get_buffer()->get_iter_at_line_index(fix_it.offsets.second.line-1, fix_it.offsets.second.index-1);
fix_it_marks.emplace_back(get_buffer()->create_mark(start_iter), get_buffer()->create_mark(end_iter)); fix_it_marks.emplace_back(get_buffer()->create_mark(start_iter), get_buffer()->create_mark(end_iter));
} }
size_t c=0; size_t c=0;

1
src/source_clang.h

@ -6,6 +6,7 @@
#include <mutex> #include <mutex>
#include <set> #include <set>
#include <regex> #include <regex>
#include "clangmm.h"
#include "source.h" #include "source.h"
namespace Source { namespace Source {

Loading…
Cancel
Save