From 74434779c3463c72be6e6691d46bab59af03de36 Mon Sep 17 00:00:00 2001 From: eidheim Date: Tue, 22 Sep 2015 08:42:20 +0200 Subject: [PATCH] Source token cleanup. Rename refactor should now work even better. --- src/source.cc | 36 ++++++++++-------------------------- src/source.h | 16 ++++++++++++---- src/window.cc | 11 +++++------ 3 files changed, 27 insertions(+), 36 deletions(-) diff --git a/src/source.cc b/src/source.cc index 994e18b..fe02258 100644 --- a/src/source.cc +++ b/src/source.cc @@ -1943,7 +1943,7 @@ Source::ClangViewAutocomplete(file_path, project_path, language) { } }); - get_token=[this]() -> std::pair { + get_token=[this]() -> Token { if(source_readable) { auto iter=get_buffer()->get_insert()->get_iter(); auto line=(unsigned)iter.get_line(); @@ -1956,51 +1956,35 @@ Source::ClangViewAutocomplete(file_path, project_path, language) { continue; auto referenced=cursor.get_referenced(); if(referenced) - return {referenced.get_usr(), static_cast(referenced.get_kind())}; + return Token(static_cast(referenced.get_kind()), token.get_spelling(), referenced.get_usr()); } } } } - return {"", -1}; + return Token(-1, "", ""); }; - get_token_name=[this]() -> std::string { + tag_similar_tokens=[this](const Token &token){ if(source_readable) { - auto iter=get_buffer()->get_insert()->get_iter(); - auto line=(unsigned)iter.get_line(); - auto index=(unsigned)iter.get_line_index(); - for(auto &token: *clang_tokens) { - if(token.get_kind()==clang::Token_Identifier && token.get_cursor().has_type()) { - if(line==token.offsets.first.line-1 && index>=token.offsets.first.index-1 && index <=token.offsets.second.index-1) { - return token.get_spelling(); - } - } - } - } - return ""; - }; - - tag_similar_tokens=[this](const std::pair &token){ - if(source_readable) { - if(token.second>=0 && token.first.size()>0 && last_similar_tokens_tagged!=token.first) { + if(token.type>=0 && token.usr.size()>0 && last_similar_tokens_tagged!=token.usr) { get_buffer()->remove_tag(similar_tokens_tag, get_buffer()->begin(), get_buffer()->end()); - auto offsets=clang_tokens->get_similar_token_offsets(token.first, static_cast(token.second)); + auto offsets=clang_tokens->get_similar_token_offsets(static_cast(token.type), token.spelling, token.usr); for(auto &offset: offsets) { get_buffer()->apply_tag(similar_tokens_tag, get_buffer()->get_iter_at_line_index(offset.first.line-1, offset.first.index-1), get_buffer()->get_iter_at_line_index(offset.second.line-1, offset.second.index-1)); } - last_similar_tokens_tagged=token.first; + last_similar_tokens_tagged=token.usr; } } - if(token.second<0 && token.first.size()==0 && last_similar_tokens_tagged!="") { + if(token.type<0 && token.usr.size()==0 && last_similar_tokens_tagged!="") { get_buffer()->remove_tag(similar_tokens_tag, get_buffer()->begin(), get_buffer()->end()); last_similar_tokens_tagged=""; } }; - rename_similar_tokens=[this](const std::pair &token, const std::string &text) { + rename_similar_tokens=[this](const Token &token, const std::string &text) { size_t number=0; if(source_readable) { - auto offsets=clang_tokens->get_similar_token_offsets(token.first, static_cast(token.second)); + auto offsets=clang_tokens->get_similar_token_offsets(static_cast(token.type), token.spelling, token.usr); std::vector, Glib::RefPtr > > marks; for(auto &offset: offsets) { marks.emplace_back(get_buffer()->create_mark(get_buffer()->get_iter_at_line_index(offset.first.line-1, offset.first.index-1)), get_buffer()->create_mark(get_buffer()->get_iter_at_line_index(offset.second.line-1, offset.second.index-1))); diff --git a/src/source.h b/src/source.h index 143265e..3608143 100644 --- a/src/source.h +++ b/src/source.h @@ -49,6 +49,15 @@ namespace Source { std::vector chunks; std::string brief_comments; }; + + class Token { + public: + Token(int type, const std::string &spelling, const std::string &usr): + type(type), spelling(spelling), usr(usr) {} + int type; + std::string spelling; + std::string usr; + }; class View : public Gsv::View { public: @@ -71,10 +80,9 @@ namespace Source { std::function()> get_declaration_location; std::function goto_method; - std::function()> get_token; - std::function get_token_name; - std::function &token)> tag_similar_tokens; - std::function &token, const std::string &text)> rename_similar_tokens; + std::function get_token; + std::function tag_similar_tokens; + std::function rename_similar_tokens; std::function goto_next_diagnostic; std::function on_update_status; diff --git a/src/window.cc b/src/window.cc index 1ff51fe..d4187da 100644 --- a/src/window.cc +++ b/src/window.cc @@ -722,10 +722,9 @@ void Window::goto_line_entry() { void Window::rename_token_entry() { entry_box.clear(); if(notebook.get_current_page()!=-1) { - if(notebook.get_current_view()->get_token && notebook.get_current_view()->get_token_name) { - auto token=std::make_shared >(notebook.get_current_view()->get_token()); - if(token->second>=0 && token->first.size()>0 && notebook.get_current_view()->get_token_name) { - auto token_name=std::make_shared(notebook.get_current_view()->get_token_name()); + if(notebook.get_current_view()->get_token) { + auto token=std::make_shared(notebook.get_current_view()->get_token()); + if(token->type>=0 && token->usr.size()>0) { for(int c=0;ctag_similar_tokens) { notebook.get_view(c)->tag_similar_tokens(*token); @@ -737,8 +736,8 @@ void Window::rename_token_entry() { label_it->set_text("Warning: only opened and parsed tabs will have its content renamed, and modified files will be saved."); }; label_it->update(0, ""); - entry_box.entries.emplace_back(*token_name, [this, token_name, token](const std::string& content){ - if(notebook.get_current_page()!=-1 && content!=*token_name) { + entry_box.entries.emplace_back(token->spelling, [this, token](const std::string& content){ + if(notebook.get_current_page()!=-1 && content!=token->spelling) { for(int c=0;crename_similar_tokens) {