Browse Source

Cleanup of Source::ClangViewParse::update_syntax

merge-requests/365/head
eidheim 10 years ago
parent
commit
4190af65f6
  1. 8
      src/config.cc
  2. 2
      src/config.h
  3. 39
      src/source_clang.cc
  4. 8
      src/source_clang.h

8
src/config.cc

@ -202,8 +202,12 @@ void Config::get_source() {
source.highlight_current_line = source_json.get<bool>("highlight_current_line");
source.show_line_numbers = source_json.get<bool>("show_line_numbers");
for (auto &i : source_json.get_child("clang_types"))
source.clang_types[i.first] = i.second.get_value<std::string>();
for (auto &i : source_json.get_child("clang_types")) {
try {
source.clang_types[std::stoi(i.first)] = i.second.get_value<std::string>();
}
catch(const std::exception &) {}
}
source.clang_format_style = source_json.get<std::string>("clang_format_style");

2
src/config.h

@ -67,7 +67,7 @@ public:
bool wrap_lines;
bool highlight_current_line;
bool show_line_numbers;
std::unordered_map<std::string, std::string> clang_types;
std::unordered_map<int, std::string> clang_types;
std::string clang_format_style;
std::unordered_map<std::string, DocumentationSearch> documentation_searches;

39
src/source_clang.cc

@ -233,38 +233,37 @@ std::vector<std::string> Source::ClangViewParse::get_compilation_commands() {
}
void Source::ClangViewParse::update_syntax() {
std::vector<TokenRange> ranges;
std::vector<std::pair<std::pair<clang::Offset, clang::Offset>, 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<int>(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<int>(token.get_cursor().get_kind());
if(kind==101 || kind==102)
kind=static_cast<int>(token.get_cursor().get_referenced().get_kind());
if(kind!=500)
ranges.emplace_back(token.offsets, 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_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<int>(cursor_kind));
}
else if(token_kind==clang::TokenKind::Token_Literal)
ranges.emplace_back(token.offsets, static_cast<int>(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);
}
}

8
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<clang::Offset, clang::Offset> offsets, int kind):
offsets(offsets), kind(kind) {}
std::pair<clang::Offset, clang::Offset> offsets;
int kind;
};
public:
ClangViewParse(const boost::filesystem::path &file_path, Glib::RefPtr<Gsv::Language> language);

Loading…
Cancel
Save