Skip to content
This repository was archived by the owner on Jun 25, 2020. It is now read-only.

Commit f2ed3b1

Browse files
committed
Added more tests to source_clang_test
1 parent 1443b08 commit f2ed3b1

File tree

5 files changed

+77
-29
lines changed

5 files changed

+77
-29
lines changed

src/source.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ namespace Source {
7676
std::function<Offset(const std::vector<Source::View*> &views)> get_declaration_location;
7777
std::function<Offset(const std::vector<Source::View*> &views)> get_implementation_location;
7878
std::function<std::vector<std::pair<Offset, std::string> >(const std::vector<Source::View*> &views)> get_usages;
79-
std::function<void()> goto_method;
79+
std::function<std::vector<std::pair<Offset, std::string> >()> get_methods;
8080
std::function<std::vector<std::string>()> get_token_data;
8181
std::function<std::string()> get_token_spelling;
8282
std::function<std::vector<std::pair<boost::filesystem::path, size_t> >(const std::vector<Source::View*> &views, const std::string &text)> rename_similar_tokens;

src/source_clang.cc

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,18 +1112,16 @@ Source::ClangViewAutocomplete(file_path, language) {
11121112
return usages;
11131113
};
11141114

1115-
goto_method=[this](){
1115+
get_methods=[this](){
1116+
std::vector<std::pair<Offset, std::string> > methods;
11161117
if(!parsed) {
11171118
Info::get().print("Buffer is parsing");
1118-
return;
1119+
return methods;
11191120
}
1120-
auto iter=get_iter_for_dialog();
1121-
selection_dialog=std::unique_ptr<SelectionDialog>(new SelectionDialog(*this, get_buffer()->create_mark(iter), true, true));
1122-
auto rows=std::make_shared<std::unordered_map<std::string, clang::Offset> >();
1123-
auto methods=clang_tokens->get_cxx_methods();
1124-
if(methods.size()==0)
1125-
return;
1126-
for(auto &method: methods) {
1121+
auto cxx_methods=clang_tokens->get_cxx_methods();
1122+
if(cxx_methods.size()==0)
1123+
return methods;
1124+
for(auto &method: cxx_methods) {
11271125
std::string row=std::to_string(method.second.line)+": "+Glib::Markup::escape_text(method.first);
11281126
//Add bold method token
11291127
size_t token_end_pos=row.find('(');
@@ -1153,16 +1151,9 @@ Source::ClangViewAutocomplete(file_path, language) {
11531151
(row[pos]>='0' && row[pos]<='9') || row[pos]=='_' || row[pos]=='~') && pos>0);
11541152
row.insert(token_end_pos, "</b>");
11551153
row.insert(pos+1, "<b>");
1156-
(*rows)[row]=method.second;
1157-
selection_dialog->add_row(row);
1154+
methods.emplace_back(Offset(method.second.line, method.second.index), row);
11581155
}
1159-
selection_dialog->on_select=[this, rows](const std::string& selected, bool hide_window) {
1160-
auto offset=rows->at(selected);
1161-
get_buffer()->place_cursor(get_buffer()->get_iter_at_line_index(offset.line-1, offset.index-1));
1162-
scroll_to(get_buffer()->get_insert(), 0.0, 1.0, 0.5);
1163-
delayed_tooltips_connection.disconnect();
1164-
};
1165-
selection_dialog->show();
1156+
return methods;
11661157
};
11671158

11681159
get_token_data=[this]() {

src/window.cc

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -517,8 +517,25 @@ void Window::set_menu_actions() {
517517
});
518518
menu.add_action("source_goto_method", [this]() {
519519
if(notebook.get_current_page()!=-1) {
520-
if(notebook.get_current_view()->goto_method) {
521-
notebook.get_current_view()->goto_method();
520+
auto view=notebook.get_current_view();
521+
if(view->get_methods) {
522+
auto methods=notebook.get_current_view()->get_methods();
523+
if(!methods.empty()) {
524+
auto iter=view->get_iter_for_dialog();
525+
view->selection_dialog=std::unique_ptr<SelectionDialog>(new SelectionDialog(*view, view->get_buffer()->create_mark(iter), true, true));
526+
auto rows=std::make_shared<std::unordered_map<std::string, Source::Offset> >();
527+
for(auto &method: methods) {
528+
(*rows)[method.second]=method.first;
529+
view->selection_dialog->add_row(method.second);
530+
}
531+
view->selection_dialog->on_select=[view, rows](const std::string& selected, bool hide_window) {
532+
auto offset=rows->at(selected);
533+
view->get_buffer()->place_cursor(view->get_buffer()->get_iter_at_line_index(offset.line-1, offset.index-1));
534+
view->scroll_to(view->get_buffer()->get_insert(), 0.0, 1.0, 0.5);
535+
view->delayed_tooltips_connection.disconnect();
536+
};
537+
view->selection_dialog->show();
538+
}
522539
}
523540
}
524541
});
@@ -790,7 +807,7 @@ void Window::activate_menu_items(bool activate) {
790807
menu.actions["source_goto_declaration"]->set_enabled(activate ? static_cast<bool>(notebook.get_current_view()->get_declaration_location) : false);
791808
menu.actions["source_goto_implementation"]->set_enabled(activate ? static_cast<bool>(notebook.get_current_view()->get_implementation_location) : false);
792809
menu.actions["source_goto_usage"]->set_enabled(activate ? static_cast<bool>(notebook.get_current_view()->get_usages) : false);
793-
menu.actions["source_goto_method"]->set_enabled(activate ? static_cast<bool>(notebook.get_current_view()->goto_method) : false);
810+
menu.actions["source_goto_method"]->set_enabled(activate ? static_cast<bool>(notebook.get_current_view()->get_methods) : false);
794811
menu.actions["source_rename"]->set_enabled(activate ? static_cast<bool>(notebook.get_current_view()->rename_similar_tokens) : false);
795812
menu.actions["source_goto_next_diagnostic"]->set_enabled(activate ? static_cast<bool>(notebook.get_current_view()->goto_next_diagnostic) : false);
796813
menu.actions["source_apply_fix_its"]->set_enabled(activate ? static_cast<bool>(notebook.get_current_view()->apply_fix_its) : false);

tests/clang_project/main.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
class Test {
1+
class TestClass {
22
public:
3+
TestClass();
4+
~TestClass() {}
35
void function();
46
};
57

6-
void Test::function() {}
8+
TestClass::TestClass() {}
9+
10+
void TestClass::function() {}
711

812
int main() {
9-
Test test;
13+
TestClass test;
1014
test.function();
1115
}

tests/source_clang_test.cc

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,55 @@ int main() {
3030
flush_events();
3131
g_assert_cmpuint(clang_view->diagnostics.size(), ==, 0);
3232

33-
clang_view->place_cursor_at_line_index(10-1, 8-1);
33+
//test get_declaration and get_implementation
34+
clang_view->place_cursor_at_line_index(14-1, 8-1);
3435
flush_events();
3536
auto location=clang_view->get_declaration_location({clang_view});
36-
g_assert_cmpuint(location.line, ==, 3);
37+
g_assert_cmpuint(location.line, ==, 5);
38+
3739
clang_view->place_cursor_at_line_index(location.line-1, location.index-1);
3840
flush_events();
3941
location=clang_view->get_implementation_location({clang_view});
40-
g_assert_cmpuint(location.line, ==, 6);
42+
g_assert_cmpuint(location.line, ==, 10);
43+
4144
clang_view->place_cursor_at_line_index(location.line-1, location.index-1);
4245
flush_events();
4346
location=clang_view->get_declaration_location({clang_view});
44-
g_assert_cmpuint(location.line, ==, 3);
47+
g_assert_cmpuint(location.line, ==, 5);
48+
49+
//test get_usages and get_methods
50+
auto locations=clang_view->get_usages({clang_view});
51+
flush_events();
52+
g_assert_cmpuint(locations.size(), >, 0);
53+
54+
locations=clang_view->get_methods();
55+
flush_events();
56+
g_assert_cmpuint(locations.size(), >, 0);
57+
58+
//Test rename class (error if not constructor and destructor is renamed as well)
59+
auto saved_main=clang_view->get_buffer()->get_text();
60+
clang_view->place_cursor_at_line_index(1-1, 7-1);
61+
flush_events();
62+
auto token=clang_view->get_token(clang_view->get_buffer()->get_insert()->get_iter());
63+
g_assert_cmpstr(token.c_str(), ==, "TestClass");
64+
location=clang_view->get_declaration_location({clang_view});
65+
g_assert_cmpuint(location.line, ==, 1);
66+
clang_view->rename_similar_tokens({clang_view}, "RenamedTestClass");
67+
flush_events();
68+
while(!clang_view->parsed)
69+
flush_events();
70+
flush_events();
71+
auto iter=clang_view->get_buffer()->get_insert()->get_iter();
72+
iter.backward_char();
73+
token=clang_view->get_token(iter);
74+
g_assert_cmpstr(token.c_str(), ==, "RenamedTestClass");
75+
flush_events();
76+
g_assert_cmpuint(clang_view->diagnostics.size(), ==, 0);
77+
clang_view->get_buffer()->set_text(saved_main);
78+
flush_events();
79+
clang_view->save({clang_view});
4580

81+
//test error
4682
clang_view->get_buffer()->set_text(main_error);
4783
flush_events();
4884
while(!clang_view->parsed)

0 commit comments

Comments
 (0)