Browse Source

Optimized ClangView's update_syntax and fixed source_clang_test

merge-requests/365/head
eidheim 10 years ago
parent
commit
a1a523d684
  1. 39
      src/source_clang.cc
  2. 16
      tests/source_clang_test.cc

39
src/source_clang.cc

@ -231,39 +231,38 @@ std::vector<std::string> Source::ClangViewParse::get_compilation_commands() {
} }
void Source::ClangViewParse::update_syntax() { void Source::ClangViewParse::update_syntax() {
std::vector<std::pair<std::pair<clang::Offset, clang::Offset>, int> > ranges; auto buffer=get_buffer();
const auto apply_tag=[this, buffer](const std::pair<clang::Offset, clang::Offset> &offsets, int type) {
auto type_it=Config::get().source.clang_types.find(type);
if(type_it!=Config::get().source.clang_types.end()) {
last_syntax_tags.emplace(type_it->second);
Gtk::TextIter begin_iter = buffer->get_iter_at_line_index(offsets.first.line-1, offsets.first.index-1);
Gtk::TextIter end_iter = buffer->get_iter_at_line_index(offsets.second.line-1, offsets.second.index-1);
buffer->apply_tag_by_name(type_it->second, begin_iter, end_iter);
}
};
for(auto &tag: last_syntax_tags)
buffer->remove_tag_by_name(tag, buffer->begin(), buffer->end());
last_syntax_tags.clear();
for (auto &token : *clang_tokens) { for (auto &token : *clang_tokens) {
//if(token.get_kind()==clang::TokenKind::Token_Punctuation) //if(token.get_kind()==clang::TokenKind::Token_Punctuation)
//ranges.emplace_back(token.offsets, static_cast<int>(token.get_cursor().get_kind())); //ranges.emplace_back(token.offsets, static_cast<int>(token.get_cursor().get_kind()));
auto token_kind=token.get_kind(); auto token_kind=token.get_kind();
if(token_kind==clang::TokenKind::Token_Keyword) if(token_kind==clang::TokenKind::Token_Keyword)
ranges.emplace_back(token.offsets, 702); apply_tag(token.offsets, 702);
else if(token_kind==clang::TokenKind::Token_Identifier) { else if(token_kind==clang::TokenKind::Token_Identifier) {
auto cursor_kind=token.get_cursor().get_kind(); auto cursor_kind=token.get_cursor().get_kind();
if(cursor_kind==clang::CursorKind::DeclRefExpr || cursor_kind==clang::CursorKind::MemberRefExpr) if(cursor_kind==clang::CursorKind::DeclRefExpr || cursor_kind==clang::CursorKind::MemberRefExpr)
cursor_kind=token.get_cursor().get_referenced().get_kind(); cursor_kind=token.get_cursor().get_referenced().get_kind();
if(cursor_kind!=clang::CursorKind::PreprocessingDirective) if(cursor_kind!=clang::CursorKind::PreprocessingDirective)
ranges.emplace_back(token.offsets, static_cast<int>(cursor_kind)); apply_tag(token.offsets, static_cast<int>(cursor_kind));
} }
else if(token_kind==clang::TokenKind::Token_Literal) else if(token_kind==clang::TokenKind::Token_Literal)
ranges.emplace_back(token.offsets, static_cast<int>(clang::CursorKind::StringLiteral)); apply_tag(token.offsets, static_cast<int>(clang::CursorKind::StringLiteral));
else if(token_kind==clang::TokenKind::Token_Comment) else if(token_kind==clang::TokenKind::Token_Comment)
ranges.emplace_back(token.offsets, 705); apply_tag(token.offsets, 705);
}
if (ranges.empty())
return;
auto buffer = get_buffer();
for(auto &tag: last_syntax_tags)
buffer->remove_tag_by_name(tag, buffer->begin(), buffer->end());
last_syntax_tags.clear();
for (auto &range : ranges) {
auto type_it=Config::get().source.clang_types.find(range.second);
if(type_it!=Config::get().source.clang_types.end()) {
last_syntax_tags.emplace(type_it->second);
Gtk::TextIter begin_iter = buffer->get_iter_at_line_index(range.first.first.line-1, range.first.first.index-1);
Gtk::TextIter end_iter = buffer->get_iter_at_line_index(range.first.second.line-1, range.first.second.index-1);
buffer->apply_tag_by_name(type_it->second, begin_iter, end_iter);
}
} }
} }

16
tests/source_clang_test.cc

@ -31,17 +31,17 @@ int main() {
g_assert_cmpuint(clang_view->diagnostics.size(), ==, 0); g_assert_cmpuint(clang_view->diagnostics.size(), ==, 0);
//test get_declaration and get_implementation //test get_declaration and get_implementation
clang_view->place_cursor_at_line_index(14-1, 8-1); 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({clang_view});
g_assert_cmpuint(location.line, ==, 5); g_assert_cmpuint(location.line, ==, 4);
clang_view->place_cursor_at_line_index(location.line-1, location.index-1); clang_view->place_cursor_at_line_index(location.line, location.index);
location=clang_view->get_implementation_location({clang_view}); location=clang_view->get_implementation_location({clang_view});
g_assert_cmpuint(location.line, ==, 10); g_assert_cmpuint(location.line, ==, 9);
clang_view->place_cursor_at_line_index(location.line-1, location.index-1); 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({clang_view});
g_assert_cmpuint(location.line, ==, 5); g_assert_cmpuint(location.line, ==, 4);
//test get_usages and get_methods //test get_usages and get_methods
auto locations=clang_view->get_usages({clang_view}); auto locations=clang_view->get_usages({clang_view});
@ -52,11 +52,11 @@ int main() {
//Test rename class (error if not constructor and destructor is renamed as well) //Test rename class (error if not constructor and destructor is renamed as well)
auto saved_main=clang_view->get_buffer()->get_text(); auto saved_main=clang_view->get_buffer()->get_text();
clang_view->place_cursor_at_line_index(1-1, 7-1); clang_view->place_cursor_at_line_index(0, 6);
auto token=clang_view->get_token(clang_view->get_buffer()->get_insert()->get_iter()); auto token=clang_view->get_token(clang_view->get_buffer()->get_insert()->get_iter());
g_assert_cmpstr(token.c_str(), ==, "TestClass"); g_assert_cmpstr(token.c_str(), ==, "TestClass");
location=clang_view->get_declaration_location({clang_view}); location=clang_view->get_declaration_location({clang_view});
g_assert_cmpuint(location.line, ==, 1); g_assert_cmpuint(location.line, ==, 0);
clang_view->rename_similar_tokens({clang_view}, "RenamedTestClass"); clang_view->rename_similar_tokens({clang_view}, "RenamedTestClass");
while(!clang_view->parsed) while(!clang_view->parsed)
flush_events(); flush_events();

Loading…
Cancel
Save