From 18f6c9871039f32666e3a0e0ffb56a474133015b Mon Sep 17 00:00:00 2001 From: eidheim Date: Mon, 4 Jul 2016 23:43:17 +0200 Subject: [PATCH] Go to Declaration should now always go to declaration --- src/source.h | 2 +- src/source_clang.cc | 52 +++++--------------------------------- src/window.cc | 2 +- tests/source_clang_test.cc | 6 ++--- 4 files changed, 12 insertions(+), 50 deletions(-) diff --git a/src/source.h b/src/source.h index d6b01de..6e6519d 100644 --- a/src/source.h +++ b/src/source.h @@ -59,7 +59,7 @@ namespace Source { Glib::RefPtr language; std::function auto_indent; - std::function &views)> get_declaration_location; + std::function get_declaration_location; std::function &views)> get_implementation_location; std::function >(const std::vector &views)> get_usages; std::function get_method; diff --git a/src/source_clang.cc b/src/source_clang.cc index 0084802..6475391 100644 --- a/src/source_clang.cc +++ b/src/source_clang.cc @@ -908,55 +908,17 @@ Source::ClangViewRefactor::ClangViewRefactor(const boost::filesystem::path &file } }); - get_declaration_location=[this](const std::vector &views){ + get_declaration_location=[this](){ if(!parsed) { Info::get().print("Buffer is parsing"); return Offset(); } - auto iter=get_buffer()->get_insert()->get_iter(); - auto line=static_cast(iter.get_line()); - auto index=static_cast(iter.get_line_index()); - for(auto &token: *clang_tokens) { - auto cursor=token.get_cursor(); - if(token.get_kind()==clang::TokenKind::Token_Identifier && cursor.has_type()) { - if(line==token.offsets.first.line-1 && index>=token.offsets.first.index-1 && index <=token.offsets.second.index-1) { - auto referenced=cursor.get_referenced(); - if(referenced) { - auto file_path=referenced.get_source_location().get_path(); - - //if declaration is implementation instead, attempt to find declaration - if(file_path==this->file_path && this->language && this->language->get_id()!="chdr" && this->language->get_id()!="cpphdr") { - auto identifier=Identifier(referenced.get_kind(), token.get_spelling(), referenced.get_usr()); - - std::vector search_views; - for(auto &view: views) { - if(view->language && (view->language->get_id()=="chdr" || view->language->get_id()=="cpphdr")) - search_views.emplace_back(view); - } - search_views.emplace_back(this); - wait_parsing(search_views); - for(auto &view: search_views) { - if(auto clang_view=dynamic_cast(view)) { - for(auto &token: *clang_view->clang_tokens) { - auto cursor=token.get_cursor(); - if(token.get_kind()==clang::TokenKind::Token_Identifier && cursor.has_type()) { - auto referenced=cursor.get_referenced(); - if(referenced && identifier.kind==referenced.get_kind() && - identifier.spelling==token.get_spelling() && identifier.usr==referenced.get_usr()) { - auto offset=referenced.get_source_location().get_offset(); - return Offset(offset.line-1, offset.index-1, referenced.get_source_location().get_path()); - } - } - } - } - } - } - - auto offset=referenced.get_source_location().get_offset(); - return Offset(offset.line-1, offset.index-1, file_path); - } - } - } + auto identifier=get_identifier(); + if(identifier) { + auto canonical=clang::Cursor(clang_getCanonicalCursor(identifier.cursor.cx_cursor)); + auto source_location=canonical.get_source_location(); + auto offset=source_location.get_offset(); + return Offset(offset.line-1, offset.index-1, source_location.get_path()); } return Offset(); }; diff --git a/src/window.cc b/src/window.cc index 10d05ae..7bc947f 100644 --- a/src/window.cc +++ b/src/window.cc @@ -528,7 +528,7 @@ void Window::set_menu_actions() { menu.add_action("source_goto_declaration", [this]() { if(auto view=Notebook::get().get_current_view()) { if(view->get_declaration_location) { - auto location=view->get_declaration_location(Notebook::get().get_views()); + auto location=view->get_declaration_location(); if(location) { boost::filesystem::path declaration_file; boost::system::error_code ec; diff --git a/tests/source_clang_test.cc b/tests/source_clang_test.cc index fea9e41..b6e5ccc 100644 --- a/tests/source_clang_test.cc +++ b/tests/source_clang_test.cc @@ -36,7 +36,7 @@ int main() { //test get_declaration and get_implementation clang_view->place_cursor_at_line_index(13, 7); - auto location=clang_view->get_declaration_location({clang_view}); + auto location=clang_view->get_declaration_location(); g_assert_cmpuint(location.line, ==, 4); clang_view->place_cursor_at_line_index(location.line, location.index); @@ -44,7 +44,7 @@ int main() { g_assert_cmpuint(location.line, ==, 9); clang_view->place_cursor_at_line_index(location.line, location.index); - location=clang_view->get_declaration_location({clang_view}); + location=clang_view->get_declaration_location(); g_assert_cmpuint(location.line, ==, 4); //test get_usages and get_methods @@ -59,7 +59,7 @@ int main() { clang_view->place_cursor_at_line_index(0, 6); auto token=clang_view->get_token(clang_view->get_buffer()->get_insert()->get_iter()); g_assert_cmpstr(token.c_str(), ==, "TestClass"); - location=clang_view->get_declaration_location({clang_view}); + location=clang_view->get_declaration_location(); g_assert_cmpuint(location.line, ==, 0); clang_view->rename_similar_tokens({clang_view}, "RenamedTestClass"); while(!clang_view->parsed)