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. 25
      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;
if(type==Type::INSERT) {
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) {
text+="Replace ";
text+=std::to_string(offsets.first.line)+":"+std::to_string(offsets.first.offset)+" - ";
text+=std::to_string(offsets.second.line)+":"+std::to_string(offsets.second.offset);
text+=std::to_string(offsets.first.line)+":"+std::to_string(first_line_offset)+" - ";
text+=std::to_string(offsets.second.line)+":"+std::to_string(second_line_offset);
text+=" with "+source;
}
else {
text+="Erase ";
text+=std::to_string(offsets.first.line)+":"+std::to_string(offsets.first.offset)+" - ";
text+=std::to_string(offsets.second.line)+":"+std::to_string(offsets.second.offset);
text+=std::to_string(offsets.first.line)+":"+std::to_string(first_line_offset)+" - ";
text+=std::to_string(offsets.second.line)+":"+std::to_string(second_line_offset);
}
return text;

25
src/source.h

@ -3,7 +3,6 @@
#include <unordered_map>
#include <vector>
#include "gtkmm.h"
#include "clangmm.h"
#include <string>
#include "gtksourceviewmm.h"
#include "terminal.h"
@ -57,25 +56,23 @@ namespace Source {
std::string usr;
};
class FixIt {
class Offset {
public:
class Offset {
public:
Offset() {}
Offset(unsigned line, unsigned offset): line(line), offset(offset) {}
bool operator==(const Offset &o) {return (line==o.line && offset==o.offset);}
Offset() {}
Offset(unsigned line, unsigned index): line(line), index(index) {}
bool operator==(const Offset &o) {return (line==o.line && index==o.index);}
unsigned line;
unsigned offset;
};
unsigned line;
unsigned index;
};
class FixIt {
public:
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);
std::string string();
std::string string(Glib::RefPtr<Gtk::TextBuffer> buffer);
Type type;
std::string source;
@ -103,7 +100,7 @@ namespace Source {
boost::filesystem::path project_path;
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<Token()> get_token;
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) {
//Convert line index to line offset for correct output:
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.index=clang_offsets.first.index;
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.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;
offsets.second.index=clang_offsets.second.index;
fix_its.emplace_back(fix_it.source, offsets);
if(fix_its_string.size()>0)
fix_its_string+='\n';
fix_its_string+=fix_its.back().string();
fix_its_string+=fix_its.back().string(get_buffer());
fix_its_count++;
num_fix_its++;
}
@ -986,7 +984,7 @@ Source::ClangViewAutocomplete(file_path, project_path, language) {
});
get_declaration_location=[this](){
std::pair<std::string, clang::Offset> location;
std::pair<std::string, Offset> location;
if(source_readable) {
auto iter=get_buffer()->get_insert()->get_iter();
auto line=(unsigned)iter.get_line();
@ -998,7 +996,9 @@ Source::ClangViewAutocomplete(file_path, project_path, language) {
auto referenced=cursor.get_referenced();
if(referenced) {
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;
}
}
@ -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;
if(source_readable) {
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 end_iter=get_buffer()->get_iter_at_line_offset(fix_it.offsets.second.line-1, fix_it.offsets.second.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_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));
}
size_t c=0;

1
src/source_clang.h

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

Loading…
Cancel
Save