diff --git a/src/config.cc b/src/config.cc index ebd6e98..e97df05 100644 --- a/src/config.cc +++ b/src/config.cc @@ -202,8 +202,12 @@ void Config::get_source() { source.highlight_current_line = source_json.get("highlight_current_line"); source.show_line_numbers = source_json.get("show_line_numbers"); - for (auto &i : source_json.get_child("clang_types")) - source.clang_types[i.first] = i.second.get_value(); + for (auto &i : source_json.get_child("clang_types")) { + try { + source.clang_types[std::stoi(i.first)] = i.second.get_value(); + } + catch(const std::exception &) {} + } source.clang_format_style = source_json.get("clang_format_style"); diff --git a/src/config.h b/src/config.h index 96c9256..7212836 100644 --- a/src/config.h +++ b/src/config.h @@ -67,7 +67,7 @@ public: bool wrap_lines; bool highlight_current_line; bool show_line_numbers; - std::unordered_map clang_types; + std::unordered_map clang_types; std::string clang_format_style; std::unordered_map documentation_searches; diff --git a/src/source_clang.cc b/src/source_clang.cc index f087925..9639931 100644 --- a/src/source_clang.cc +++ b/src/source_clang.cc @@ -233,38 +233,37 @@ std::vector Source::ClangViewParse::get_compilation_commands() { } void Source::ClangViewParse::update_syntax() { - std::vector ranges; + std::vector, int> > ranges; for (auto &token : *clang_tokens) { - //if(token.get_kind()==0) // PunctuationToken - //ranges.emplace_back(token.offsets, (int) token.get_cursor().get_kind()); - if(token.get_kind()==1) // KeywordToken + //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); - else if(token.get_kind()==2) {// IdentifierToken - auto kind=static_cast(token.get_cursor().get_kind()); - if(kind==101 || kind==102) - kind=static_cast(token.get_cursor().get_referenced().get_kind()); - if(kind!=500) - ranges.emplace_back(token.offsets, kind); + 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)); } - else if(token.get_kind()==3) { // LiteralToken - ranges.emplace_back(token.offsets, 109); - } - else if(token.get_kind()==4) // CommentToken + else if(token_kind==clang::TokenKind::Token_Literal) + ranges.emplace_back(token.offsets, static_cast(clang::CursorKind::StringLiteral)); + else if(token_kind==clang::TokenKind::Token_Comment) ranges.emplace_back(token.offsets, 705); } - if (ranges.empty() || ranges.size() == 0) { + if (ranges.empty()) return; - } auto buffer = get_source_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(std::to_string(range.kind)); + 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.offsets.first.line-1, range.offsets.first.index-1); - Gtk::TextIter end_iter = buffer->get_iter_at_line_index(range.offsets.second.line-1, range.offsets.second.index-1); + 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); } } diff --git a/src/source_clang.h b/src/source_clang.h index 5083453..c485945 100644 --- a/src/source_clang.h +++ b/src/source_clang.h @@ -16,14 +16,6 @@ namespace Source { enum class ParseState {PROCESSING, RESTARTING, STOP}; enum class ParseProcessState {IDLE, STARTING, PREPROCESSING, PROCESSING, POSTPROCESSING}; - class TokenRange { - public: - TokenRange(std::pair offsets, int kind): - offsets(offsets), kind(kind) {} - std::pair offsets; - int kind; - }; - public: ClangViewParse(const boost::filesystem::path &file_path, Glib::RefPtr language);