diff --git a/src/source_clang.cc b/src/source_clang.cc index 4cedc66..499e124 100644 --- a/src/source_clang.cc +++ b/src/source_clang.cc @@ -231,39 +231,38 @@ std::vector Source::ClangViewParse::get_compilation_commands() { } void Source::ClangViewParse::update_syntax() { - std::vector, int> > ranges; + auto buffer=get_buffer(); + const auto apply_tag=[this, buffer](const std::pair &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) { //if(token.get_kind()==clang::TokenKind::Token_Punctuation) //ranges.emplace_back(token.offsets, static_cast(token.get_cursor().get_kind())); auto token_kind=token.get_kind(); 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) { auto cursor_kind=token.get_cursor().get_kind(); if(cursor_kind==clang::CursorKind::DeclRefExpr || cursor_kind==clang::CursorKind::MemberRefExpr) cursor_kind=token.get_cursor().get_referenced().get_kind(); if(cursor_kind!=clang::CursorKind::PreprocessingDirective) - ranges.emplace_back(token.offsets, static_cast(cursor_kind)); + apply_tag(token.offsets, static_cast(cursor_kind)); } else if(token_kind==clang::TokenKind::Token_Literal) - ranges.emplace_back(token.offsets, static_cast(clang::CursorKind::StringLiteral)); + apply_tag(token.offsets, static_cast(clang::CursorKind::StringLiteral)); else if(token_kind==clang::TokenKind::Token_Comment) - ranges.emplace_back(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); - } + apply_tag(token.offsets, 705); } } diff --git a/tests/source_clang_test.cc b/tests/source_clang_test.cc index 6a4c65e..a181e9c 100644 --- a/tests/source_clang_test.cc +++ b/tests/source_clang_test.cc @@ -31,17 +31,17 @@ int main() { g_assert_cmpuint(clang_view->diagnostics.size(), ==, 0); //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}); - 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}); - 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}); - g_assert_cmpuint(location.line, ==, 5); + g_assert_cmpuint(location.line, ==, 4); //test get_usages and get_methods 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) 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()); g_assert_cmpstr(token.c_str(), ==, "TestClass"); 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"); while(!clang_view->parsed) flush_events();