|
|
|
|
@ -7,22 +7,15 @@
|
|
|
|
|
#include <algorithm> |
|
|
|
|
#include <regex> |
|
|
|
|
|
|
|
|
|
Source::Location:: |
|
|
|
|
Location(int line_number, int column_offset) : |
|
|
|
|
line_number_(line_number), column_offset_(column_offset) { } |
|
|
|
|
|
|
|
|
|
Source::Location:: |
|
|
|
|
Location(const Source::Location &org) : |
|
|
|
|
line_number_(org.line_number_), column_offset_(org.column_offset_) { } |
|
|
|
|
|
|
|
|
|
Source::Range:: |
|
|
|
|
Range(const Location &start, const Location &end, int kind) : |
|
|
|
|
start_(start), end_(end), kind_(kind) { } |
|
|
|
|
|
|
|
|
|
Source::Range:: |
|
|
|
|
Range(const Source::Range &org) : |
|
|
|
|
start_(org.start_), end_(org.end_), kind_(org.kind_) { } |
|
|
|
|
|
|
|
|
|
bool Source::Config::legal_extension(std::string e) const { |
|
|
|
|
std::transform(e.begin(), e.end(),e.begin(), ::tolower); |
|
|
|
|
if (find(extensions.begin(), extensions.end(), e) != extensions.end()) { |
|
|
|
|
DEBUG("Legal extension"); |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
DEBUG("Ilegal extension"); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//////////////
|
|
|
|
|
//// View ////
|
|
|
|
|
@ -31,7 +24,7 @@ Source::View::View() {
|
|
|
|
|
Gsv::init(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
string Source::View::GetLine(size_t line_number) { |
|
|
|
|
string Source::View::get_line(size_t line_number) { |
|
|
|
|
Gtk::TextIter line_it = get_source_buffer()->get_iter_at_line(line_number); |
|
|
|
|
Gtk::TextIter line_end_it = line_it; |
|
|
|
|
while(!line_end_it.ends_line()) |
|
|
|
|
@ -40,64 +33,13 @@ string Source::View::GetLine(size_t line_number) {
|
|
|
|
|
return line; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
string Source::View::GetLineBeforeInsert() { |
|
|
|
|
string Source::View::get_line_before_insert() { |
|
|
|
|
Gtk::TextIter insert_it = get_source_buffer()->get_insert()->get_iter(); |
|
|
|
|
Gtk::TextIter line_it = get_source_buffer()->get_iter_at_line(insert_it.get_line()); |
|
|
|
|
std::string line(get_source_buffer()->get_text(line_it, insert_it)); |
|
|
|
|
return line; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Source::View::Config::tagtable()
|
|
|
|
|
// returns a const refrence to the tagtable
|
|
|
|
|
const std::unordered_map<string, string>& Source::Config::tagtable() const { |
|
|
|
|
return tagtable_; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Source::View::Config::tagtable()
|
|
|
|
|
// returns a const refrence to the tagtable
|
|
|
|
|
const std::unordered_map<string, string>& Source::Config::typetable() const { |
|
|
|
|
return typetable_; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
std::vector<string>& Source::Config::extensiontable(){ |
|
|
|
|
return extensiontable_; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Source::Config::InsertTag(const string &key, const string &value) { |
|
|
|
|
tagtable_[key] = value; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Source::Config::InsertExtension(const string &ext) { |
|
|
|
|
extensiontable_.push_back(ext); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Source::View::Config::SetTagTable()
|
|
|
|
|
// sets the tagtable for the view
|
|
|
|
|
void Source::Config:: |
|
|
|
|
SetTypeTable(const std::unordered_map<string, string> &typetable) { |
|
|
|
|
typetable_ = typetable; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Source::Config::InsertType(const string &key, const string &value) { |
|
|
|
|
typetable_[key] = value; |
|
|
|
|
} |
|
|
|
|
// Source::View::Config::SetTagTable()
|
|
|
|
|
// sets the tagtable for the view
|
|
|
|
|
void Source::Config:: |
|
|
|
|
SetTagTable(const std::unordered_map<string, string> &tagtable) { |
|
|
|
|
tagtable_ = tagtable; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool Source::Config::legal_extension(std::string e) const { |
|
|
|
|
std::transform(e.begin(), e.end(),e.begin(), ::tolower); |
|
|
|
|
if (find(extensiontable_.begin(), extensiontable_.end(), e) != extensiontable_.end()) { |
|
|
|
|
DEBUG("Legal extension"); |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
DEBUG("Ilegal extension"); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
///////////////
|
|
|
|
|
//// Parser ///
|
|
|
|
|
///////////////
|
|
|
|
|
@ -109,7 +51,7 @@ Source::Parser::~Parser() {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Source::Parser:: |
|
|
|
|
InitSyntaxHighlighting(const std::string &filepath, |
|
|
|
|
init_syntax_highlighting(const std::string &filepath, |
|
|
|
|
const std::string &project_path, |
|
|
|
|
const std::map<std::string, std::string> |
|
|
|
|
&buffers, |
|
|
|
|
@ -136,7 +78,7 @@ get_buffer_map() const {
|
|
|
|
|
|
|
|
|
|
// Source::Model::UpdateLine
|
|
|
|
|
int Source::Parser:: |
|
|
|
|
ReParse(const std::map<std::string, std::string> &buffer) { |
|
|
|
|
reparse(const std::map<std::string, std::string> &buffer) { |
|
|
|
|
return tu_->ReparseTranslationUnit(file_path, buffer); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -182,7 +124,7 @@ get_compilation_commands() {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
std::vector<Source::Range> Source::Parser:: |
|
|
|
|
ExtractTokens(int start_offset, int end_offset) { |
|
|
|
|
extract_tokens(int start_offset, int end_offset) { |
|
|
|
|
std::vector<Source::Range> ranges; |
|
|
|
|
clang::SourceLocation start(tu_.get(), file_path, start_offset); |
|
|
|
|
clang::SourceLocation end(tu_.get(), file_path, end_offset); |
|
|
|
|
@ -191,18 +133,18 @@ ExtractTokens(int start_offset, int end_offset) {
|
|
|
|
|
std::vector<clang::Token> tks = tokens.tokens(); |
|
|
|
|
for (auto &token : tks) { |
|
|
|
|
switch (token.kind()) { |
|
|
|
|
case 0: HighlightCursor(&token, &ranges); break; // PunctuationToken
|
|
|
|
|
case 1: HighlightToken(&token, &ranges, 702); break; // KeywordToken
|
|
|
|
|
case 2: HighlightCursor(&token, &ranges); break; // IdentifierToken
|
|
|
|
|
case 3: HighlightToken(&token, &ranges, 109); break; // LiteralToken
|
|
|
|
|
case 4: HighlightToken(&token, &ranges, 705); break; // CommentToken
|
|
|
|
|
case 0: highlight_cursor(&token, &ranges); break; // PunctuationToken
|
|
|
|
|
case 1: highlight_token(&token, &ranges, 702); break; // KeywordToken
|
|
|
|
|
case 2: highlight_cursor(&token, &ranges); break; // IdentifierToken
|
|
|
|
|
case 3: highlight_token(&token, &ranges, 109); break; // LiteralToken
|
|
|
|
|
case 4: highlight_token(&token, &ranges, 705); break; // CommentToken
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return ranges; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Source::Parser:: |
|
|
|
|
HighlightCursor(clang::Token *token, |
|
|
|
|
highlight_cursor(clang::Token *token, |
|
|
|
|
std::vector<Source::Range> *source_ranges) { |
|
|
|
|
clang::SourceLocation location = token->get_source_location(tu_.get()); |
|
|
|
|
clang::Cursor cursor(tu_.get(), &location); |
|
|
|
|
@ -218,7 +160,7 @@ HighlightCursor(clang::Token *token,
|
|
|
|
|
end_offset), (int) cursor.kind()); |
|
|
|
|
} |
|
|
|
|
void Source::Parser:: |
|
|
|
|
HighlightToken(clang::Token *token, |
|
|
|
|
highlight_token(clang::Token *token, |
|
|
|
|
std::vector<Source::Range> *source_ranges, |
|
|
|
|
int token_kind) { |
|
|
|
|
clang::SourceRange range = token->get_source_range(tu_.get()); |
|
|
|
|
@ -243,13 +185,13 @@ Source::Controller::Controller(const Source::Config &config,
|
|
|
|
|
const std::vector<std::unique_ptr<Source::Controller> > &controllers) : |
|
|
|
|
config(config), parser(controllers), parse_thread_go(false), parse_thread_mapped(false), parse_thread_stop(false) { |
|
|
|
|
INFO("Source Controller with childs constructed"); |
|
|
|
|
view.signal_key_press_event().connect(sigc::mem_fun(*this, &Source::Controller::OnKeyPress), false); |
|
|
|
|
view.signal_key_press_event().connect(sigc::mem_fun(*this, &Source::Controller::on_key_press), false); |
|
|
|
|
view.set_smart_home_end(Gsv::SMART_HOME_END_BEFORE); |
|
|
|
|
view.override_font(Pango::FontDescription(config.font)); |
|
|
|
|
view.set_show_line_numbers(config.show_line_numbers); |
|
|
|
|
view.set_highlight_current_line(config.highlight_current_line); |
|
|
|
|
view.override_background_color(Gdk::RGBA(config.background)); |
|
|
|
|
for (auto &item : config.tagtable()) { |
|
|
|
|
for (auto &item : config.tags) { |
|
|
|
|
buffer()->create_tag(item.first)->property_foreground() = item.second; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
@ -267,16 +209,16 @@ void Source::Controller::update_syntax(const std::vector<Source::Range> &ranges)
|
|
|
|
|
auto buffer = view.get_buffer(); |
|
|
|
|
buffer->remove_all_tags(buffer->begin(), buffer->end()); |
|
|
|
|
for (auto &range : ranges) { |
|
|
|
|
std::string type = std::to_string(range.kind()); |
|
|
|
|
std::string type = std::to_string(range.kind); |
|
|
|
|
try { |
|
|
|
|
config.typetable().at(type); |
|
|
|
|
config.types.at(type); |
|
|
|
|
} catch (std::exception) { |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
int linum_start = range.start().line_number()-1; |
|
|
|
|
int linum_end = range.end().line_number()-1; |
|
|
|
|
int begin = range.start().column_offset()-1; |
|
|
|
|
int end = range.end().column_offset()-1; |
|
|
|
|
int linum_start = range.start.line_number-1; |
|
|
|
|
int linum_end = range.end.line_number-1; |
|
|
|
|
int begin = range.start.column_offset-1; |
|
|
|
|
int end = range.end.column_offset-1; |
|
|
|
|
|
|
|
|
|
if (end < 0) end = 0; |
|
|
|
|
if (begin < 0) begin = 0; |
|
|
|
|
@ -284,12 +226,12 @@ void Source::Controller::update_syntax(const std::vector<Source::Range> &ranges)
|
|
|
|
|
buffer->get_iter_at_line_offset(linum_start, begin); |
|
|
|
|
Gtk::TextIter end_iter = |
|
|
|
|
buffer->get_iter_at_line_offset(linum_end, end); |
|
|
|
|
buffer->apply_tag_by_name(config.typetable().at(type), |
|
|
|
|
buffer->apply_tag_by_name(config.types.at(type), |
|
|
|
|
begin_iter, end_iter); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Source::Controller::OnNewEmptyFile() { |
|
|
|
|
void Source::Controller::on_new_empty_file() { |
|
|
|
|
string filename("/tmp/juci_t"); |
|
|
|
|
sourcefile s(filename); |
|
|
|
|
parser.file_path=filename; |
|
|
|
|
@ -297,7 +239,7 @@ void Source::Controller::OnNewEmptyFile() {
|
|
|
|
|
s.save(""); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Source::Controller::OnOpenFile(const string &filepath) { |
|
|
|
|
void Source::Controller::on_open_file(const string &filepath) { |
|
|
|
|
parser.file_path=filepath; |
|
|
|
|
sourcefile s(filepath); |
|
|
|
|
auto buffer_map=parser.get_buffer_map(); |
|
|
|
|
@ -308,13 +250,13 @@ void Source::Controller::OnOpenFile(const string &filepath) {
|
|
|
|
|
int start_offset = buffer()->begin().get_offset(); |
|
|
|
|
int end_offset = buffer()->end().get_offset(); |
|
|
|
|
if (config.legal_extension(filepath.substr(filepath.find_last_of(".") + 1))) { |
|
|
|
|
parser.InitSyntaxHighlighting(filepath, |
|
|
|
|
parser.init_syntax_highlighting(filepath, |
|
|
|
|
parser.file_path.substr(0, parser.file_path.find_last_of('/')), |
|
|
|
|
buffer_map, |
|
|
|
|
start_offset, |
|
|
|
|
end_offset, |
|
|
|
|
&Parser::clang_index); |
|
|
|
|
update_syntax(parser.ExtractTokens(start_offset, end_offset)); |
|
|
|
|
update_syntax(parser.extract_tokens(start_offset, end_offset)); |
|
|
|
|
|
|
|
|
|
//GTK-calls must happen in main thread, so the parse_thread
|
|
|
|
|
//sends signals to the main thread that it is to call the following functions:
|
|
|
|
|
@ -330,7 +272,7 @@ void Source::Controller::OnOpenFile(const string &filepath) {
|
|
|
|
|
parse_done.connect([this](){ |
|
|
|
|
if(parse_thread_mapped) { |
|
|
|
|
INFO("Updating syntax"); |
|
|
|
|
update_syntax(parser.ExtractTokens(0, buffer()->get_text().size())); |
|
|
|
|
update_syntax(parser.extract_tokens(0, buffer()->get_text().size())); |
|
|
|
|
INFO("Syntax updated"); |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
@ -342,14 +284,14 @@ void Source::Controller::OnOpenFile(const string &filepath) {
|
|
|
|
|
while(true) { |
|
|
|
|
while(!parse_thread_go && !parse_thread_stop) |
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
|
|
|
|
if(parse_thread_stop) |
|
|
|
|
break; |
|
|
|
|
if(parse_thread_stop) |
|
|
|
|
break; |
|
|
|
|
if(!parse_thread_mapped) { |
|
|
|
|
parse_thread_go=false; |
|
|
|
|
parse_start(); |
|
|
|
|
} |
|
|
|
|
else if (parse_thread_mapped && parser.parsing_mutex.try_lock() && parse_thread_buffer_map_mutex.try_lock()) { |
|
|
|
|
parser.ReParse(this->parse_thread_buffer_map); |
|
|
|
|
parser.reparse(this->parse_thread_buffer_map); |
|
|
|
|
parse_thread_go=false; |
|
|
|
|
parser.parsing_mutex.unlock(); |
|
|
|
|
parse_thread_buffer_map_mutex.unlock(); |
|
|
|
|
@ -369,7 +311,9 @@ Glib::RefPtr<Gsv::Buffer> Source::Controller::buffer() {
|
|
|
|
|
return view.get_source_buffer(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool Source::Controller::OnKeyPress(GdkEventKey* key) { |
|
|
|
|
//TODO: move indentation to Parser, replace indentation methods with a better implementation or
|
|
|
|
|
//maybe use libclang
|
|
|
|
|
bool Source::Controller::on_key_press(GdkEventKey* key) { |
|
|
|
|
const std::regex bracket_regex("^( *).*\\{ *$"); |
|
|
|
|
const std::regex no_bracket_statement_regex("^( *)(if|for|else if|catch|while) *\\(.*[^;}] *$"); |
|
|
|
|
const std::regex no_bracket_no_para_statement_regex("^( *)(else|try|do) *$"); |
|
|
|
|
@ -377,7 +321,7 @@ bool Source::Controller::OnKeyPress(GdkEventKey* key) {
|
|
|
|
|
|
|
|
|
|
//Indent as in previous line, and indent right after if/else/etc
|
|
|
|
|
if(key->keyval==GDK_KEY_Return && key->state==0) { |
|
|
|
|
string line(view.GetLineBeforeInsert()); |
|
|
|
|
string line(view.get_line_before_insert()); |
|
|
|
|
std::smatch sm; |
|
|
|
|
if(std::regex_match(line, sm, bracket_regex)) { |
|
|
|
|
buffer()->insert_at_cursor("\n"+sm[1].str()+config.tab+"\n"+sm[1].str()+"}"); |
|
|
|
|
@ -399,7 +343,7 @@ bool Source::Controller::OnKeyPress(GdkEventKey* key) {
|
|
|
|
|
std::smatch sm2; |
|
|
|
|
size_t line_nr=buffer()->get_insert()->get_iter().get_line(); |
|
|
|
|
if(line_nr>0 && sm[1].str().size()>=config.tab_size) { |
|
|
|
|
string previous_line=view.GetLine(line_nr-1); |
|
|
|
|
string previous_line=view.get_line(line_nr-1); |
|
|
|
|
if(!std::regex_match(previous_line, sm2, bracket_regex)) { |
|
|
|
|
if(std::regex_match(previous_line, sm2, no_bracket_statement_regex)) { |
|
|
|
|
buffer()->insert_at_cursor("\n"+sm2[1].str()); |
|
|
|
|
@ -438,7 +382,7 @@ bool Source::Controller::OnKeyPress(GdkEventKey* key) {
|
|
|
|
|
int line_end=selection_end.get_line(); |
|
|
|
|
|
|
|
|
|
for(int line_nr=line_start;line_nr<=line_end;line_nr++) { |
|
|
|
|
string line=view.GetLine(line_nr); |
|
|
|
|
string line=view.get_line(line_nr); |
|
|
|
|
if(!(line.size()>=config.tab_size && line.substr(0, config.tab_size)==config.tab)) |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
@ -455,7 +399,7 @@ bool Source::Controller::OnKeyPress(GdkEventKey* key) {
|
|
|
|
|
} |
|
|
|
|
//Indent left when writing } on a new line
|
|
|
|
|
else if(key->keyval==GDK_KEY_braceright) { |
|
|
|
|
string line=view.GetLineBeforeInsert(); |
|
|
|
|
string line=view.get_line_before_insert(); |
|
|
|
|
if(line.size()>=config.tab_size) { |
|
|
|
|
for(auto c: line) { |
|
|
|
|
if(c!=' ') |
|
|
|
|
@ -476,8 +420,8 @@ bool Source::Controller::OnKeyPress(GdkEventKey* key) {
|
|
|
|
|
Gtk::TextIter insert_it=buffer()->get_insert()->get_iter(); |
|
|
|
|
int line_nr=insert_it.get_line(); |
|
|
|
|
if(line_nr>0) { |
|
|
|
|
string line=view.GetLine(line_nr); |
|
|
|
|
string previous_line=view.GetLine(line_nr-1); |
|
|
|
|
string line=view.get_line(line_nr); |
|
|
|
|
string previous_line=view.get_line(line_nr-1); |
|
|
|
|
smatch sm; |
|
|
|
|
if(std::regex_match(previous_line, sm, spaces_regex)) { |
|
|
|
|
if(line==sm[1]) { |
|
|
|
|
|