Browse Source

Renamed ClangViewRefactor::Token to ClangViewRefactor::Identifier to avoid confusion

merge-requests/365/head
eidheim 10 years ago
parent
commit
b3b7711393
  1. 106
      src/source_clang.cc
  2. 24
      src/source_clang.h

106
src/source_clang.cc

@ -861,18 +861,18 @@ bool Source::ClangViewAutocomplete::full_reparse() {
//////////////////////////// ////////////////////////////
Source::ClangViewRefactor::ClangViewRefactor(const boost::filesystem::path &file_path, Glib::RefPtr<Gsv::Language> language): Source::ClangViewRefactor::ClangViewRefactor(const boost::filesystem::path &file_path, Glib::RefPtr<Gsv::Language> language):
Source::ClangViewAutocomplete(file_path, language) { Source::ClangViewAutocomplete(file_path, language) {
similar_tokens_tag=get_buffer()->create_tag(); similar_identifiers_tag=get_buffer()->create_tag();
similar_tokens_tag->property_weight()=1000; //TODO: replace with Pango::WEIGHT_ULTRAHEAVY in 2016 or so (when Ubuntu 14 is history) similar_identifiers_tag->property_weight()=1000; //TODO: replace with Pango::WEIGHT_ULTRAHEAVY in 2016 or so (when Ubuntu 14 is history)
get_buffer()->signal_changed().connect([this]() { get_buffer()->signal_changed().connect([this]() {
if(!renaming && last_tagged_token) { if(!renaming && last_tagged_identifier) {
for(auto &mark: similar_token_marks) { for(auto &mark: similar_identifiers_marks) {
get_buffer()->remove_tag(similar_tokens_tag, mark.first->get_iter(), mark.second->get_iter()); get_buffer()->remove_tag(similar_identifiers_tag, mark.first->get_iter(), mark.second->get_iter());
get_buffer()->delete_mark(mark.first); get_buffer()->delete_mark(mark.first);
get_buffer()->delete_mark(mark.second); get_buffer()->delete_mark(mark.second);
} }
similar_token_marks.clear(); similar_identifiers_marks.clear();
last_tagged_token=Token(); last_tagged_identifier=Identifier();
} }
}); });
@ -881,7 +881,7 @@ Source::ClangViewAutocomplete(file_path, language) {
Info::get().print("Buffer is parsing"); Info::get().print("Buffer is parsing");
return std::string(); return std::string();
} }
return get_token().spelling; return get_identifier().spelling;
}; };
rename_similar_tokens=[this](const std::vector<Source::View*> &views, const std::string &text) { rename_similar_tokens=[this](const std::vector<Source::View*> &views, const std::string &text) {
@ -890,14 +890,14 @@ Source::ClangViewAutocomplete(file_path, language) {
Info::get().print("Buffer is parsing"); Info::get().print("Buffer is parsing");
return renamed; return renamed;
} }
auto token=get_token(); auto identifier=get_identifier();
if(token) { if(identifier) {
wait_parsing(views); wait_parsing(views);
//If rename constructor or destructor, set token to class //If rename constructor or destructor, set token to class
if(token.kind==clang::CursorKind::Constructor || token.kind==clang::CursorKind::Destructor) { if(identifier.kind==clang::CursorKind::Constructor || identifier.kind==clang::CursorKind::Destructor) {
auto parent_cursor=token.cursor.get_semantic_parent(); auto parent_cursor=identifier.cursor.get_semantic_parent();
token=Token(parent_cursor.get_kind(), token.spelling, parent_cursor.get_usr(), parent_cursor); identifier=Identifier(parent_cursor.get_kind(), identifier.spelling, parent_cursor.get_usr(), parent_cursor);
} }
std::vector<Source::View*> renamed_views; std::vector<Source::View*> renamed_views;
@ -905,24 +905,24 @@ Source::ClangViewAutocomplete(file_path, language) {
if(auto clang_view=dynamic_cast<Source::ClangView*>(view)) { if(auto clang_view=dynamic_cast<Source::ClangView*>(view)) {
//If rename class, also rename constructors and destructor //If rename class, also rename constructors and destructor
std::set<Token> tokens; std::set<Identifier> identifiers;
tokens.emplace(token); identifiers.emplace(identifier);
if(token.cursor.get_kind()==clang::CursorKind::ClassDecl) { if(identifier.cursor.get_kind()==clang::CursorKind::ClassDecl) {
for(auto &clang_token: *clang_view->clang_tokens) { for(auto &token: *clang_view->clang_tokens) {
auto cursor=clang_token.get_cursor(); auto cursor=token.get_cursor();
auto cursor_kind=cursor.get_kind(); auto cursor_kind=cursor.get_kind();
auto parent_cursor=cursor.get_semantic_parent(); auto parent_cursor=cursor.get_semantic_parent();
if(clang_token.get_kind()==clang::Token_Identifier && if(token.get_kind()==clang::Token_Identifier &&
(cursor_kind==clang::CursorKind::Constructor || cursor_kind==clang::CursorKind::Destructor) && (cursor_kind==clang::CursorKind::Constructor || cursor_kind==clang::CursorKind::Destructor) &&
parent_cursor.get_usr()==token.cursor.get_usr() && cursor.has_type()) { parent_cursor.get_usr()==identifier.cursor.get_usr() && cursor.has_type()) {
tokens.emplace(cursor.get_kind(), clang_token.get_spelling(), cursor.get_usr()); identifiers.emplace(cursor.get_kind(), token.get_spelling(), cursor.get_usr());
} }
} }
} }
std::vector<std::pair<clang::Offset, clang::Offset> > offsets; std::vector<std::pair<clang::Offset, clang::Offset> > offsets;
for(auto &token: tokens) { for(auto &identifier: identifiers) {
auto token_offsets=clang_view->clang_tokens->get_similar_token_offsets(token.kind, token.spelling, token.usr); auto token_offsets=clang_view->clang_tokens->get_similar_token_offsets(identifier.kind, identifier.spelling, identifier.usr);
for(auto &token_offset: token_offsets) for(auto &token_offset: token_offsets)
offsets.emplace_back(token_offset); offsets.emplace_back(token_offset);
} }
@ -956,10 +956,10 @@ Source::ClangViewAutocomplete(file_path, language) {
get_buffer()->signal_mark_set().connect([this](const Gtk::TextBuffer::iterator& iterator, const Glib::RefPtr<Gtk::TextBuffer::Mark>& mark){ get_buffer()->signal_mark_set().connect([this](const Gtk::TextBuffer::iterator& iterator, const Glib::RefPtr<Gtk::TextBuffer::Mark>& mark){
if(mark->get_name()=="insert") { if(mark->get_name()=="insert") {
delayed_tag_similar_tokens_connection.disconnect(); delayed_tag_similar_identifiers_connection.disconnect();
delayed_tag_similar_tokens_connection=Glib::signal_timeout().connect([this]() { delayed_tag_similar_identifiers_connection=Glib::signal_timeout().connect([this]() {
auto token=get_token(); auto identifier=get_identifier();
tag_similar_tokens(token); tag_similar_identifiers(identifier);
return false; return false;
}, 100); }, 100);
} }
@ -998,8 +998,8 @@ Source::ClangViewAutocomplete(file_path, language) {
Info::get().print("Buffer is parsing"); Info::get().print("Buffer is parsing");
return location; return location;
} }
auto token=get_token(); auto identifier=get_identifier();
if(token) { if(identifier) {
wait_parsing(views); wait_parsing(views);
for(auto &view: views) { for(auto &view: views) {
if(auto clang_view=dynamic_cast<Source::ClangView*>(view)) { if(auto clang_view=dynamic_cast<Source::ClangView*>(view)) {
@ -1011,8 +1011,8 @@ Source::ClangViewAutocomplete(file_path, language) {
kind==clang::CursorKind::Constructor || kind==clang::CursorKind::Destructor) && kind==clang::CursorKind::Constructor || kind==clang::CursorKind::Destructor) &&
token_it->get_kind()==clang::Token_Identifier && cursor.has_type()) { token_it->get_kind()==clang::Token_Identifier && cursor.has_type()) {
auto referenced=cursor.get_referenced(); auto referenced=cursor.get_referenced();
if(referenced && token.kind==referenced.get_kind() && if(referenced && identifier.kind==referenced.get_kind() &&
token.spelling==token_it->get_spelling() && token.usr==referenced.get_usr()) { identifier.spelling==token_it->get_spelling() && identifier.usr==referenced.get_usr()) {
location.file_path=cursor.get_source_location().get_path(); location.file_path=cursor.get_source_location().get_path();
auto clang_offset=cursor.get_source_location().get_offset(); auto clang_offset=cursor.get_source_location().get_offset();
location.line=clang_offset.line; location.line=clang_offset.line;
@ -1035,8 +1035,8 @@ Source::ClangViewAutocomplete(file_path, language) {
Info::get().print("Buffer is parsing"); Info::get().print("Buffer is parsing");
return usages; return usages;
} }
auto token=get_token(); auto identifier=get_identifier();
if(token) { if(identifier) {
wait_parsing(views); wait_parsing(views);
std::vector<Source::View*> views_reordered; std::vector<Source::View*> views_reordered;
views_reordered.emplace_back(this); views_reordered.emplace_back(this);
@ -1046,7 +1046,7 @@ Source::ClangViewAutocomplete(file_path, language) {
} }
for(auto &view: views_reordered) { for(auto &view: views_reordered) {
if(auto clang_view=dynamic_cast<Source::ClangView*>(view)) { if(auto clang_view=dynamic_cast<Source::ClangView*>(view)) {
auto offsets=clang_view->clang_tokens->get_similar_token_offsets(token.kind, token.spelling, token.usr); auto offsets=clang_view->clang_tokens->get_similar_token_offsets(identifier.kind, identifier.spelling, identifier.usr);
for(auto &offset: offsets) { for(auto &offset: offsets) {
size_t whitespaces_removed=0; size_t whitespaces_removed=0;
auto start_iter=clang_view->get_buffer()->get_iter_at_line(offset.first.line-1); auto start_iter=clang_view->get_buffer()->get_iter_at_line(offset.first.line-1);
@ -1294,9 +1294,9 @@ Source::ClangViewAutocomplete(file_path, language) {
}; };
} }
Source::ClangViewRefactor::Token Source::ClangViewRefactor::get_token() { Source::ClangViewRefactor::Identifier Source::ClangViewRefactor::get_identifier() {
if(!parsed) if(!parsed)
return Token(); return Identifier();
auto iter=get_buffer()->get_insert()->get_iter(); auto iter=get_buffer()->get_insert()->get_iter();
auto line=static_cast<unsigned>(iter.get_line()); auto line=static_cast<unsigned>(iter.get_line());
auto index=static_cast<unsigned>(iter.get_line_index()); auto index=static_cast<unsigned>(iter.get_line_index());
@ -1308,11 +1308,11 @@ Source::ClangViewRefactor::Token Source::ClangViewRefactor::get_token() {
continue; continue;
auto referenced=cursor.get_referenced(); auto referenced=cursor.get_referenced();
if(referenced) if(referenced)
return Token(referenced.get_kind(), token.get_spelling(), referenced.get_usr(), referenced); return Identifier(referenced.get_kind(), token.get_spelling(), referenced.get_usr(), referenced);
} }
} }
} }
return Token(); return Identifier();
} }
void Source::ClangViewRefactor::wait_parsing(const std::vector<Source::View*> &views) { void Source::ClangViewRefactor::wait_parsing(const std::vector<Source::View*> &views) {
@ -1346,33 +1346,33 @@ void Source::ClangViewRefactor::wait_parsing(const std::vector<Source::View*> &v
} }
} }
void Source::ClangViewRefactor::tag_similar_tokens(const Token &token) { void Source::ClangViewRefactor::tag_similar_identifiers(const Identifier &identifier) {
if(parsed) { if(parsed) {
if(token && last_tagged_token!=token) { if(identifier && last_tagged_identifier!=identifier) {
for(auto &mark: similar_token_marks) { for(auto &mark: similar_identifiers_marks) {
get_buffer()->remove_tag(similar_tokens_tag, mark.first->get_iter(), mark.second->get_iter()); get_buffer()->remove_tag(similar_identifiers_tag, mark.first->get_iter(), mark.second->get_iter());
get_buffer()->delete_mark(mark.first); get_buffer()->delete_mark(mark.first);
get_buffer()->delete_mark(mark.second); get_buffer()->delete_mark(mark.second);
} }
similar_token_marks.clear(); similar_identifiers_marks.clear();
auto offsets=clang_tokens->get_similar_token_offsets(token.kind, token.spelling, token.usr); auto offsets=clang_tokens->get_similar_token_offsets(identifier.kind, identifier.spelling, identifier.usr);
for(auto &offset: offsets) { for(auto &offset: offsets) {
auto start_iter=get_buffer()->get_iter_at_line_index(offset.first.line-1, offset.first.index-1); auto start_iter=get_buffer()->get_iter_at_line_index(offset.first.line-1, offset.first.index-1);
auto end_iter=get_buffer()->get_iter_at_line_index(offset.second.line-1, offset.second.index-1); auto end_iter=get_buffer()->get_iter_at_line_index(offset.second.line-1, offset.second.index-1);
get_buffer()->apply_tag(similar_tokens_tag, start_iter, end_iter); get_buffer()->apply_tag(similar_identifiers_tag, start_iter, end_iter);
similar_token_marks.emplace_back(get_buffer()->create_mark(start_iter), get_buffer()->create_mark(end_iter)); similar_identifiers_marks.emplace_back(get_buffer()->create_mark(start_iter), get_buffer()->create_mark(end_iter));
} }
last_tagged_token=token; last_tagged_identifier=identifier;
} }
} }
if(!token && last_tagged_token) { if(!identifier && last_tagged_identifier) {
for(auto &mark: similar_token_marks) { for(auto &mark: similar_identifiers_marks) {
get_buffer()->remove_tag(similar_tokens_tag, mark.first->get_iter(), mark.second->get_iter()); get_buffer()->remove_tag(similar_identifiers_tag, mark.first->get_iter(), mark.second->get_iter());
get_buffer()->delete_mark(mark.first); get_buffer()->delete_mark(mark.first);
get_buffer()->delete_mark(mark.second); get_buffer()->delete_mark(mark.second);
} }
similar_token_marks.clear(); similar_identifiers_marks.clear();
last_tagged_token=Token(); last_tagged_identifier=Identifier();
} }
} }
@ -1386,7 +1386,7 @@ Source::ClangView::ClangView(const boost::filesystem::path &file_path, Glib::Ref
void Source::ClangView::async_delete() { void Source::ClangView::async_delete() {
dispatcher.disconnect(); dispatcher.disconnect();
delayed_reparse_connection.disconnect(); delayed_reparse_connection.disconnect();
delayed_tag_similar_tokens_connection.disconnect(); delayed_tag_similar_identifiers_connection.disconnect();
ClangViewAutocomplete::async_delete(); ClangViewAutocomplete::async_delete();
} }

24
src/source_clang.h

@ -92,16 +92,16 @@ namespace Source {
}; };
class ClangViewRefactor : public ClangViewAutocomplete { class ClangViewRefactor : public ClangViewAutocomplete {
class Token { class Identifier {
public: public:
Token(clang::CursorKind kind, const std::string &spelling, const std::string &usr, const clang::Cursor &cursor=clang::Cursor()) : Identifier(clang::CursorKind kind, const std::string &spelling, const std::string &usr, const clang::Cursor &cursor=clang::Cursor()) :
kind(kind), spelling(spelling), usr(usr), cursor(cursor) {} kind(kind), spelling(spelling), usr(usr), cursor(cursor) {}
Token() : kind(static_cast<clang::CursorKind>(0)) {} Identifier() : kind(static_cast<clang::CursorKind>(0)) {}
operator bool() const { return static_cast<int>(kind)!=0; } operator bool() const { return static_cast<int>(kind)!=0; }
bool operator==(const Token &other) const { return (kind==other.kind && spelling==other.spelling && usr==other.usr); } bool operator==(const Identifier &rhs) const { return (kind==rhs.kind && spelling==rhs.spelling && usr==rhs.usr); }
bool operator!=(const Token &other) const { return !(*this==other); } bool operator!=(const Identifier &rhs) const { return !(*this==rhs); }
bool operator<(const Token &other) const { return usr<other.usr; } bool operator<(const Identifier &rhs) const { return usr<rhs.usr; }
clang::CursorKind kind; clang::CursorKind kind;
std::string spelling; std::string spelling;
std::string usr; std::string usr;
@ -110,15 +110,15 @@ namespace Source {
public: public:
ClangViewRefactor(const boost::filesystem::path &file_path, Glib::RefPtr<Gsv::Language> language); ClangViewRefactor(const boost::filesystem::path &file_path, Glib::RefPtr<Gsv::Language> language);
protected: protected:
sigc::connection delayed_tag_similar_tokens_connection; sigc::connection delayed_tag_similar_identifiers_connection;
private: private:
Token get_token(); Identifier get_identifier();
void wait_parsing(const std::vector<Source::View*> &views); void wait_parsing(const std::vector<Source::View*> &views);
std::list<std::pair<Glib::RefPtr<Gtk::TextMark>, Glib::RefPtr<Gtk::TextMark> > > similar_token_marks; std::list<std::pair<Glib::RefPtr<Gtk::TextMark>, Glib::RefPtr<Gtk::TextMark> > > similar_identifiers_marks;
void tag_similar_tokens(const Token &token); void tag_similar_identifiers(const Identifier &identifier);
Glib::RefPtr<Gtk::TextTag> similar_tokens_tag; Glib::RefPtr<Gtk::TextTag> similar_identifiers_tag;
Token last_tagged_token; Identifier last_tagged_identifier;
bool renaming=false; bool renaming=false;
}; };

Loading…
Cancel
Save