Browse Source

When renaming class/constructor/destructor, also rename class, constructor and destructor

merge-requests/365/head
eidheim 10 years ago
parent
commit
6e20fea01a
  1. 34
      src/source_clang.cc
  2. 6
      src/source_clang.h
  3. 2
      src/window.cc

34
src/source_clang.cc

@ -893,10 +893,39 @@ Source::ClangViewAutocomplete(file_path, language) {
auto token=get_token();
if(token) {
wait_parsing(views);
//If rename constructor or destructor, set token to class
if(token.kind==clang::CursorKind::Constructor || token.kind==clang::CursorKind::Destructor) {
auto parent_cursor=token.cursor.get_semantic_parent();
token=Token(parent_cursor.get_kind(), token.spelling, parent_cursor.get_usr(), parent_cursor);
}
std::vector<Source::View*> renamed_views;
for(auto &view: views) {
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);
//If rename class, also rename constructors and destructor
std::set<Token> tokens;
tokens.emplace(token);
if(token.cursor.get_kind()==clang::CursorKind::ClassDecl) {
for(auto &clang_token: *clang_view->clang_tokens) {
auto cursor=clang_token.get_cursor();
auto cursor_kind=cursor.get_kind();
auto parent_cursor=cursor.get_semantic_parent();
if(clang_token.get_kind()==clang::Token_Identifier &&
(cursor_kind==clang::CursorKind::Constructor || cursor_kind==clang::CursorKind::Destructor) &&
parent_cursor.get_usr()==token.cursor.get_usr() && cursor.has_type()) {
tokens.emplace(cursor.get_kind(), clang_token.get_spelling(), cursor.get_usr());
}
}
}
std::vector<std::pair<clang::Offset, clang::Offset> > offsets;
for(auto &token: tokens) {
auto token_offsets=clang_view->clang_tokens->get_similar_token_offsets(token.kind, token.spelling, token.usr);
for(auto &token_offset: token_offsets)
offsets.emplace_back(token_offset);
}
std::vector<std::pair<Glib::RefPtr<Gtk::TextMark>, Glib::RefPtr<Gtk::TextMark> > > marks;
for(auto &offset: offsets) {
marks.emplace_back(clang_view->get_buffer()->create_mark(clang_view->get_buffer()->get_iter_at_line_index(offset.first.line-1, offset.first.index-1)),
@ -1279,7 +1308,7 @@ Source::ClangViewRefactor::Token Source::ClangViewRefactor::get_token() {
continue;
auto referenced=cursor.get_referenced();
if(referenced)
return Token(referenced.get_kind(), token.get_spelling(), referenced.get_usr());
return Token(referenced.get_kind(), token.get_spelling(), referenced.get_usr(), referenced);
}
}
}
@ -1311,6 +1340,7 @@ void Source::ClangViewRefactor::wait_parsing(const std::vector<Source::View*> &v
}
if(all_parsed)
break;
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
message->hide();
}

6
src/source_clang.h

@ -94,14 +94,18 @@ namespace Source {
class ClangViewRefactor : public ClangViewAutocomplete {
class Token {
public:
Token(clang::CursorKind kind, const std::string &spelling, const std::string &usr) : kind(kind), spelling(spelling), usr(usr) {}
Token(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) {}
Token() : kind(static_cast<clang::CursorKind>(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 Token &other) const { return !(*this==other); }
bool operator<(const Token &other) const { return usr<other.usr; }
clang::CursorKind kind;
std::string spelling;
std::string usr;
clang::Cursor cursor;
};
public:
ClangViewRefactor(const boost::filesystem::path &file_path, Glib::RefPtr<Gsv::Language> language);

2
src/window.cc

@ -1083,7 +1083,7 @@ void Window::rename_token_entry() {
content!=*spelling && iter->get_buffer() && view->get_buffer()->get_insert()->get_iter()==*iter) {
auto renamed_pairs=view->rename_similar_tokens(notebook.source_views, content);
for(auto &renamed: renamed_pairs)
Terminal::get().print("Replaced "+std::to_string(renamed.second)+" occurrences in file "+renamed.first.string()+"\n");
Terminal::get().print("Replaced "+std::to_string(renamed.second)+" occurrence"+(renamed.second>1?"s":"")+" in file "+renamed.first.string()+"\n");
}
else
Info::get().print("Operation canceled");

Loading…
Cancel
Save