Browse Source

Merge pull request #9 from eidheim/master

Fixed threading crash
merge-requests/365/head
Jørgen Lien Sellæg 11 years ago
parent
commit
f20cfd927f
  1. 6
      juci/config.cc
  2. 86
      juci/notebook.cc
  3. 3
      juci/notebook.h
  4. 295
      juci/source.cc
  5. 126
      juci/source.h

6
juci/config.cc

@ -39,13 +39,13 @@ void MainConfig::GenerateSource() {
source_cfg.tab+=" "; source_cfg.tab+=" ";
} }
for (auto &i : colors_json) { for (auto &i : colors_json) {
source_cfg.InsertTag(i.first, i.second.get_value<std::string>()); source_cfg.tags[i.first]=i.second.get_value<std::string>();
} }
for (auto &i : syntax_json) { for (auto &i : syntax_json) {
source_cfg.InsertType(i.first, i.second.get_value<std::string>()); source_cfg.types[i.first]=i.second.get_value<std::string>();
} }
for (auto &i : extensions_json) { for (auto &i : extensions_json) {
source_cfg.InsertExtension(i.second.get_value<std::string>()); source_cfg.extensions.emplace_back(i.second.get_value<std::string>());
} }
DEBUG("Source cfg fetched"); DEBUG("Source cfg fetched");
} }

86
juci/notebook.cc

@ -18,8 +18,7 @@ Notebook::Controller::Controller(Gtk::Window* window,
Source::Config& source_cfg, Source::Config& source_cfg,
Directories::Config& dir_cfg) : Directories::Config& dir_cfg) :
directories_(dir_cfg), directories_(dir_cfg),
source_config_(source_cfg), source_config_(source_cfg) {
index_(0, 1) {
INFO("Create notebook"); INFO("Create notebook");
window_ = window; window_ = window;
OnNewPage("untitled"); OnNewPage("untitled");
@ -193,7 +192,7 @@ bool Notebook::Controller::OnKeyRelease(GdkEventKey* key) {
bool Notebook::Controller::GeneratePopup(int key_id) { bool Notebook::Controller::GeneratePopup(int key_id) {
INFO("Notebook genereate popup, getting iters"); INFO("Notebook genereate popup, getting iters");
std::string path = text_vec_.at(CurrentPage())->parser.file_path; std::string path = text_vec_.at(CurrentPage())->parser.file_path;
if (!LegalExtension(path.substr(path.find_last_of(".") + 1))) return false; if (!source_config().legal_extension(path.substr(path.find_last_of(".") + 1))) return false;
// Get function to fill popup with suggests item vector under is for testing // Get function to fill popup with suggests item vector under is for testing
Gtk::TextIter beg = CurrentTextView().get_buffer()->get_insert()->get_iter(); Gtk::TextIter beg = CurrentTextView().get_buffer()->get_insert()->get_iter();
Gtk::TextIter end = CurrentTextView().get_buffer()->get_insert()->get_iter(); Gtk::TextIter end = CurrentTextView().get_buffer()->get_insert()->get_iter();
@ -230,22 +229,20 @@ bool Notebook::Controller::GeneratePopup(int key_id) {
return false; return false;
} }
INFO("Notebook genereate popup, getting autocompletions"); INFO("Notebook genereate popup, getting autocompletions");
std::vector<Source::AutoCompleteData> acdata; std::vector<Source::AutoCompleteData> acdata=text_vec_.at(CurrentPage())->parser.
text_vec_.at(CurrentPage())-> get_autocomplete_suggestions(beg.get_line()+1,
GetAutoCompleteSuggestions(beg.get_line()+1, beg.get_line_offset()+2);
beg.get_line_offset()+2,
&acdata);
std::map<std::string, std::string> items; std::map<std::string, std::string> items;
for (auto &data : acdata) { for (auto &data : acdata) {
std::stringstream ss; std::stringstream ss;
std::string return_value; std::string return_value;
for (auto &chunk : data.chunks_) { for (auto &chunk : data.chunks) {
switch (chunk.kind()) { switch (chunk.kind) {
case clang::CompletionChunk_ResultType: case clang::CompletionChunk_ResultType:
return_value = chunk.chunk(); return_value = chunk.chunk;
break; break;
case clang::CompletionChunk_Informative: break; case clang::CompletionChunk_Informative: break;
default: ss << chunk.chunk(); break; default: ss << chunk.chunk; break;
} }
} }
if (ss.str().length() > 0) { // if length is 0 the result is empty if (ss.str().length() > 0) { // if length is 0 the result is empty
@ -304,7 +301,7 @@ Gtk::Box& Notebook::Controller::entry_view() {
void Notebook::Controller::OnNewPage(std::string name) { void Notebook::Controller::OnNewPage(std::string name) {
INFO("Notebook Generate new page"); INFO("Notebook Generate new page");
OnCreatePage(); OnCreatePage();
text_vec_.back()->OnNewEmptyFile(); text_vec_.back()->on_new_empty_file();
Notebook().append_page(*editor_vec_.back(), name); Notebook().append_page(*editor_vec_.back(), name);
Notebook().show_all_children(); Notebook().show_all_children();
Notebook().set_current_page(Pages()-1); Notebook().set_current_page(Pages()-1);
@ -312,21 +309,15 @@ void Notebook::Controller::OnNewPage(std::string name) {
} }
void Notebook::Controller::
MapBuffers(std::map<std::string, std::string> *buffers) const {
for (auto &buffer : text_vec_) {
buffers->operator[](buffer->parser.file_path) =
buffer->buffer()->get_text().raw();
}
}
void Notebook::Controller::OnOpenFile(std::string path) { void Notebook::Controller::OnOpenFile(std::string path) {
INFO("Notebook open file"); INFO("Notebook open file");
OnCreatePage(); OnCreatePage();
text_vec_.back()->OnOpenFile(path); text_vec_.back()->on_open_file(path);
text_vec_.back()->is_saved=true; size_t pos = path.find_last_of("/\\");
unsigned pos = path.find_last_of("/\\"); std::string filename=path;
Notebook().append_page(*editor_vec_.back(), path.substr(pos+1)); if(pos!=std::string::npos)
filename=path.substr(pos+1);
Notebook().append_page(*editor_vec_.back(), filename);
Notebook().show_all_children(); Notebook().show_all_children();
Notebook().set_current_page(Pages()-1); Notebook().set_current_page(Pages()-1);
Notebook().set_focus_child(text_vec_.back()->view); Notebook().set_focus_child(text_vec_.back()->view);
@ -334,18 +325,29 @@ void Notebook::Controller::OnOpenFile(std::string path) {
void Notebook::Controller::OnCreatePage() { void Notebook::Controller::OnCreatePage() {
INFO("Notebook create page"); INFO("Notebook create page");
text_vec_.emplace_back(new Source::Controller(source_config(), *this)); text_vec_.emplace_back(new Source::Controller(source_config(), text_vec_));
scrolledtext_vec_.push_back(new Gtk::ScrolledWindow()); scrolledtext_vec_.push_back(new Gtk::ScrolledWindow());
editor_vec_.push_back(new Gtk::HBox()); editor_vec_.push_back(new Gtk::HBox());
scrolledtext_vec_.back()->add(text_vec_.back()->view); scrolledtext_vec_.back()->add(text_vec_.back()->view);
editor_vec_.back()->pack_start(*scrolledtext_vec_.back(), true, true); editor_vec_.back()->pack_start(*scrolledtext_vec_.back(), true, true);
TextViewHandlers(text_vec_.back()->view); TextViewHandlers(text_vec_.back()->view);
//Add star on tab label when the page is not saved:
text_vec_.back()->signal_buffer_changed=[this](bool was_saved) {
if(was_saved) {
std::string path=text_vec_.at(CurrentPage())->parser.file_path;
size_t pos = path.find_last_of("/\\");
std::string filename=path;
if(pos!=std::string::npos)
filename=path.substr(pos+1);
Notebook().set_tab_label_text(*Notebook().get_nth_page(CurrentPage()), filename+"*");
}
};
} }
void Notebook::Controller::OnCloseCurrentPage() { void Notebook::Controller::OnCloseCurrentPage() {
INFO("Notebook close page"); INFO("Notebook close page");
if (Pages() != 0) { if (Pages() != 0) {
if(text_vec_.back()->is_changed){ if(!text_vec_.back()->is_saved){
AskToSaveDialog(); AskToSaveDialog();
} }
int page = CurrentPage(); int page = CurrentPage();
@ -581,17 +583,8 @@ void Notebook::Controller::FindPopupPosition(Gtk::TextView& textview,
} }
bool Notebook::Controller:: OnSaveFile() { bool Notebook::Controller:: OnSaveFile() {
INFO("Notebook save file"); std::string path=text_vec_.at(CurrentPage())->parser.file_path;
if (text_vec_.at(CurrentPage())->is_saved) { return OnSaveFile(path);
std::ofstream file;
file.open (text_vec_.at(CurrentPage())->parser.file_path);
file << CurrentTextView().get_buffer()->get_text();
file.close();
return true;
} else {
return OnSaveFile(OnSaveFileAs());
}
return false;
} }
bool Notebook::Controller:: OnSaveFile(std::string path) { bool Notebook::Controller:: OnSaveFile(std::string path) {
INFO("Notebook save file with path"); INFO("Notebook save file with path");
@ -601,6 +594,11 @@ bool Notebook::Controller:: OnSaveFile(std::string path) {
file << CurrentTextView().get_buffer()->get_text(); file << CurrentTextView().get_buffer()->get_text();
file.close(); file.close();
text_vec_.at(CurrentPage())->parser.file_path=path; text_vec_.at(CurrentPage())->parser.file_path=path;
size_t pos = path.find_last_of("/\\");
std::string filename=path;
if(pos!=std::string::npos)
filename=path.substr(pos+1);
Notebook().set_tab_label_text(*Notebook().get_nth_page(CurrentPage()), filename);
text_vec_.at(CurrentPage())->is_saved=true; text_vec_.at(CurrentPage())->is_saved=true;
return true; return true;
} }
@ -674,15 +672,3 @@ void Notebook::Controller::AskToSaveDialog() {
} }
} }
bool Notebook::Controller::LegalExtension(std::string e) {
std::transform(e.begin(), e.end(),e.begin(), ::tolower);
std::vector<std::string> extensions =
source_config().extensiontable();
if (find(extensions.begin(), extensions.end(), e) != extensions.end()) {
DEBUG("Legal extension");
return true;
}
DEBUG("Ilegal extension");
return false;
}

3
juci/notebook.h

@ -62,8 +62,6 @@ namespace Notebook {
void OnOpenFile(std::string filename); void OnOpenFile(std::string filename);
void OnCreatePage(); void OnCreatePage();
bool ScrollEventCallback(GdkEventScroll* scroll_event); bool ScrollEventCallback(GdkEventScroll* scroll_event);
void MapBuffers(std::map<std::string, std::string> *buffers) const;
clang::Index* index() { return &index_; }
int Pages(); int Pages();
Directories::Controller& directories() { return directories_; } Directories::Controller& directories() { return directories_; }
Gtk::Paned& view(); Gtk::Paned& view();
@ -111,7 +109,6 @@ namespace Notebook {
bool ispopup; bool ispopup;
Gtk::Dialog popup_; Gtk::Dialog popup_;
Gtk::Window* window_; Gtk::Window* window_;
clang::Index index_;
}; // class controller }; // class controller
} // namespace Notebook } // namespace Notebook
#endif // JUCI_NOTEBOOK_H_ #endif // JUCI_NOTEBOOK_H_

295
juci/source.cc

@ -3,27 +3,19 @@
#include <boost/property_tree/json_parser.hpp> #include <boost/property_tree/json_parser.hpp>
#include <fstream> #include <fstream>
#include <boost/timer/timer.hpp> #include <boost/timer/timer.hpp>
#include "notebook.h"
#include "logging.h" #include "logging.h"
#include <algorithm> #include <algorithm>
#include <regex> #include <regex>
Source::Location:: bool Source::Config::legal_extension(std::string e) const {
Location(int line_number, int column_offset) : std::transform(e.begin(), e.end(),e.begin(), ::tolower);
line_number_(line_number), column_offset_(column_offset) { } if (find(extensions.begin(), extensions.end(), e) != extensions.end()) {
DEBUG("Legal extension");
Source::Location:: return true;
Location(const Source::Location &org) : }
line_number_(org.line_number_), column_offset_(org.column_offset_) { } DEBUG("Ilegal extension");
return false;
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_) { }
////////////// //////////////
//// View //// //// View ////
@ -32,7 +24,7 @@ Source::View::View() {
Gsv::init(); 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_it = get_source_buffer()->get_iter_at_line(line_number);
Gtk::TextIter line_end_it = line_it; Gtk::TextIter line_end_it = line_it;
while(!line_end_it.ends_line()) while(!line_end_it.ends_line())
@ -41,59 +33,25 @@ string Source::View::GetLine(size_t line_number) {
return line; 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 insert_it = get_source_buffer()->get_insert()->get_iter();
Gtk::TextIter line_it = get_source_buffer()->get_iter_at_line(insert_it.get_line()); 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)); std::string line(get_source_buffer()->get_text(line_it, insert_it));
return line; return line;
} }
// Source::View::Config::tagtable() ///////////////
// returns a const refrence to the tagtable //// Parser ///
const std::unordered_map<string, string>& Source::Config::tagtable() const { ///////////////
return tagtable_; clang::Index Source::Parser::clang_index(0, 1);
}
// 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) { Source::Parser::~Parser() {
typetable_[key] = value; parsing_mutex.lock(); //Be sure not to destroy while still parsing with libclang
} parsing_mutex.unlock();
// Source::View::Config::SetTagTable()
// sets the tagtable for the view
void Source::Config::
SetTagTable(const std::unordered_map<string, string> &tagtable) {
tagtable_ = tagtable;
} }
///////////////
//// Model ////
///////////////
void Source::Parser:: void Source::Parser::
InitSyntaxHighlighting(const std::string &filepath, init_syntax_highlighting(const std::string &filepath,
const std::string &project_path, const std::string &project_path,
const std::map<std::string, std::string> const std::map<std::string, std::string>
&buffers, &buffers,
@ -108,51 +66,32 @@ InitSyntaxHighlighting(const std::string &filepath,
buffers)); buffers));
} }
// Source::View::UpdateLine std::map<std::string, std::string> Source::Parser::
void Source::View:: get_buffer_map() const {
OnLineEdit(const std::vector<Source::Range> &locations, std::map<std::string, std::string> buffer_map;
const Source::Config &config) { for (auto &controller : controllers) {
OnUpdateSyntax(locations, config); buffer_map.operator[](controller->parser.file_path) =
controller->buffer()->get_text().raw();
}
return buffer_map;
} }
// Source::Model::UpdateLine // Source::Model::UpdateLine
int Source::Parser:: 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); return tu_->ReparseTranslationUnit(file_path, buffer);
} }
std::vector<Source::AutoCompleteData> Source::Parser::
// Source::Controller::OnLineEdit() get_autocomplete_suggestions(int line_number,
// fired when a line in the buffer is edited int column) {
void Source::Controller::OnLineEdit() { }
void Source::Controller::
GetAutoCompleteSuggestions(int line_number,
int column,
std::vector<Source::AutoCompleteData>
*suggestions) {
INFO("Getting auto complete suggestions"); INFO("Getting auto complete suggestions");
parsing.lock(); std::vector<Source::AutoCompleteData> suggestions;
std::map<std::string, std::string> buffers; auto buffer_map=get_buffer_map();
notebook.MapBuffers(&buffers); parsing_mutex.lock();
parser.GetAutoCompleteSuggestions(buffers,
line_number,
column,
suggestions);
DEBUG("Number of suggestions");
DEBUG_VAR(suggestions->size());
parsing.unlock();
}
void Source::Parser::
GetAutoCompleteSuggestions(const std::map<std::string, std::string> &buffers,
int line_number,
int column,
std::vector<Source::AutoCompleteData>
*suggestions) {
clang::CodeCompleteResults results(tu_.get(), clang::CodeCompleteResults results(tu_.get(),
file_path, file_path,
buffers, buffer_map,
line_number, line_number,
column); column);
for (int i = 0; i < results.size(); i++) { for (int i = 0; i < results.size(); i++) {
@ -161,8 +100,12 @@ GetAutoCompleteSuggestions(const std::map<std::string, std::string> &buffers,
for (auto &chunk : chunks_) { for (auto &chunk : chunks_) {
chunks.emplace_back(chunk); chunks.emplace_back(chunk);
} }
suggestions->emplace_back(chunks); suggestions.emplace_back(chunks);
} }
parsing_mutex.unlock();
DEBUG("Number of suggestions");
DEBUG_VAR(suggestions.size());
return suggestions;
} }
std::vector<std::string> Source::Parser:: std::vector<std::string> Source::Parser::
@ -181,7 +124,7 @@ get_compilation_commands() {
} }
std::vector<Source::Range> Source::Parser:: 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; std::vector<Source::Range> ranges;
clang::SourceLocation start(tu_.get(), file_path, start_offset); clang::SourceLocation start(tu_.get(), file_path, start_offset);
clang::SourceLocation end(tu_.get(), file_path, end_offset); clang::SourceLocation end(tu_.get(), file_path, end_offset);
@ -190,18 +133,18 @@ ExtractTokens(int start_offset, int end_offset) {
std::vector<clang::Token> tks = tokens.tokens(); std::vector<clang::Token> tks = tokens.tokens();
for (auto &token : tks) { for (auto &token : tks) {
switch (token.kind()) { switch (token.kind()) {
case 0: HighlightCursor(&token, &ranges); break; // PunctuationToken case 0: highlight_cursor(&token, &ranges); break; // PunctuationToken
case 1: HighlightToken(&token, &ranges, 702); break; // KeywordToken case 1: highlight_token(&token, &ranges, 702); break; // KeywordToken
case 2: HighlightCursor(&token, &ranges); break; // IdentifierToken case 2: highlight_cursor(&token, &ranges); break; // IdentifierToken
case 3: HighlightToken(&token, &ranges, 109); break; // LiteralToken case 3: highlight_token(&token, &ranges, 109); break; // LiteralToken
case 4: HighlightToken(&token, &ranges, 705); break; // CommentToken case 4: highlight_token(&token, &ranges, 705); break; // CommentToken
} }
} }
return ranges; return ranges;
} }
void Source::Parser:: void Source::Parser::
HighlightCursor(clang::Token *token, highlight_cursor(clang::Token *token,
std::vector<Source::Range> *source_ranges) { std::vector<Source::Range> *source_ranges) {
clang::SourceLocation location = token->get_source_location(tu_.get()); clang::SourceLocation location = token->get_source_location(tu_.get());
clang::Cursor cursor(tu_.get(), &location); clang::Cursor cursor(tu_.get(), &location);
@ -217,7 +160,7 @@ HighlightCursor(clang::Token *token,
end_offset), (int) cursor.kind()); end_offset), (int) cursor.kind());
} }
void Source::Parser:: void Source::Parser::
HighlightToken(clang::Token *token, highlight_token(clang::Token *token,
std::vector<Source::Range> *source_ranges, std::vector<Source::Range> *source_ranges,
int token_kind) { int token_kind) {
clang::SourceRange range = token->get_source_range(tu_.get()); clang::SourceRange range = token->get_source_range(tu_.get());
@ -239,51 +182,50 @@ HighlightToken(clang::Token *token,
// Source::Controller::Controller() // Source::Controller::Controller()
// Constructor for Controller // Constructor for Controller
Source::Controller::Controller(const Source::Config &config, Source::Controller::Controller(const Source::Config &config,
Notebook::Controller &notebook) : const std::vector<std::unique_ptr<Source::Controller> > &controllers) :
config(config), notebook(notebook) { config(config), parser(controllers), parse_thread_go(false), parse_thread_mapped(false), parse_thread_stop(false) {
INFO("Source Controller with childs constructed"); 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.set_smart_home_end(Gsv::SMART_HOME_END_BEFORE);
view.override_font(Pango::FontDescription(config.font)); view.override_font(Pango::FontDescription(config.font));
view.set_show_line_numbers(config.show_line_numbers); view.set_show_line_numbers(config.show_line_numbers);
view.set_highlight_current_line(config.highlight_current_line); view.set_highlight_current_line(config.highlight_current_line);
view.override_background_color(Gdk::RGBA(config.background)); 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; buffer()->create_tag(item.first)->property_foreground() = item.second;
} }
buffer()->signal_changed().connect([this]() {
if(signal_buffer_changed)
signal_buffer_changed(is_saved);
is_saved=false;
parse_thread_mapped=false;
parse_thread_go=true;
});
} }
Source::Controller::~Controller() { Source::Controller::~Controller() {
parsing.lock(); //Be sure not to destroy while still parsing with libclang parse_thread_stop=true;
parsing.unlock(); if(parse_thread.joinable())
parse_thread.join();
} }
void Source::Controller::OnNewEmptyFile() { void Source::Controller::update_syntax(const std::vector<Source::Range> &ranges) {
string filename("/tmp/juci_t");
sourcefile s(filename);
parser.file_path=filename;
parser.project_path=filename;
s.save("");
}
void Source::View::OnUpdateSyntax(const std::vector<Source::Range> &ranges,
const Source::Config &config) {
if (ranges.empty() || ranges.size() == 0) { if (ranges.empty() || ranges.size() == 0) {
return; return;
} }
Glib::RefPtr<Gtk::TextBuffer> buffer = get_buffer(); auto buffer = view.get_buffer();
buffer->remove_all_tags(buffer->begin(), buffer->end()); buffer->remove_all_tags(buffer->begin(), buffer->end());
for (auto &range : ranges) { for (auto &range : ranges) {
std::string type = std::to_string(range.kind()); std::string type = std::to_string(range.kind);
try { try {
config.typetable().at(type); config.types.at(type);
} catch (std::exception) { } catch (std::exception) {
continue; continue;
} }
int linum_start = range.start().line_number()-1; int linum_start = range.start.line_number-1;
int linum_end = range.end().line_number()-1; int linum_end = range.end.line_number-1;
int begin = range.start().column_offset()-1; int begin = range.start.column_offset-1;
int end = range.end().column_offset()-1; int end = range.end.column_offset-1;
if (end < 0) end = 0; if (end < 0) end = 0;
if (begin < 0) begin = 0; if (begin < 0) begin = 0;
@ -291,61 +233,80 @@ void Source::View::OnUpdateSyntax(const std::vector<Source::Range> &ranges,
buffer->get_iter_at_line_offset(linum_start, begin); buffer->get_iter_at_line_offset(linum_start, begin);
Gtk::TextIter end_iter = Gtk::TextIter end_iter =
buffer->get_iter_at_line_offset(linum_end, end); 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); begin_iter, end_iter);
} }
} }
void Source::Controller::OnOpenFile(const string &filepath) { void Source::Controller::on_new_empty_file() {
string filename("/tmp/untitled");
sourcefile s(filename);
parser.file_path=filename;
parser.project_path=filename;
s.save("");
}
void Source::Controller::on_open_file(const string &filepath) {
parser.file_path=filepath; parser.file_path=filepath;
sourcefile s(filepath); sourcefile s(filepath);
std::map<std::string, std::string> buffers; auto buffer_map=parser.get_buffer_map();
notebook.MapBuffers(&buffers); buffer_map[filepath] = s.get_content();
buffers[filepath] = s.get_content();
buffer()->get_undo_manager()->begin_not_undoable_action(); buffer()->get_undo_manager()->begin_not_undoable_action();
buffer()->set_text(s.get_content()); buffer()->set_text(s.get_content());
is_saved=true;
buffer()->get_undo_manager()->end_not_undoable_action(); buffer()->get_undo_manager()->end_not_undoable_action();
int start_offset = buffer()->begin().get_offset(); int start_offset = buffer()->begin().get_offset();
int end_offset = buffer()->end().get_offset(); int end_offset = buffer()->end().get_offset();
if (notebook.LegalExtension(filepath.substr(filepath.find_last_of(".") + 1))) { 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('/')), parser.file_path.substr(0, parser.file_path.find_last_of('/')),
buffers, buffer_map,
start_offset, start_offset,
end_offset, end_offset,
notebook.index()); &Parser::clang_index);
view.OnUpdateSyntax(parser.ExtractTokens(start_offset, end_offset), config); 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:
parse_start.connect([this]{
if(parse_thread_buffer_map_mutex.try_lock()) {
this->parse_thread_buffer_map=parser.get_buffer_map();
parse_thread_mapped=true;
parse_thread_buffer_map_mutex.unlock();
}
parse_thread_go=true;
});
//OnUpdateSyntax must happen in main thread, so the parse-thread parse_done.connect([this](){
//sends a signal to the main thread that it is to call the following function: if(parse_thread_mapped) {
parsing_done.connect([this](){
INFO("Updating syntax"); INFO("Updating syntax");
view. update_syntax(parser.extract_tokens(0, buffer()->get_text().size()));
OnUpdateSyntax(parser.ExtractTokens(0, buffer()->get_text().size()), config);
INFO("Syntax updated"); INFO("Syntax updated");
}
else {
parse_thread_go=true;
}
}); });
buffer()->signal_end_user_action().connect([this]() { parse_thread=std::thread([this]() {
std::thread parse([this]() { while(true) {
if (parsing.try_lock()) { while(!parse_thread_go && !parse_thread_stop)
INFO("Starting parsing"); std::this_thread::sleep_for(std::chrono::milliseconds(10));
while (true) { if(parse_thread_stop)
const std::string raw = buffer()->get_text().raw();
std::map<std::string, std::string> buffers;
notebook.MapBuffers(&buffers);
buffers[parser.file_path] = raw;
if (parser.ReParse(buffers) == 0 &&
raw == buffer()->get_text().raw()) {
break; 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);
parse_thread_go=false;
parser.parsing_mutex.unlock();
parse_thread_buffer_map_mutex.unlock();
parse_done();
} }
parsing.unlock();
parsing_done();
INFO("Parsing completed");
} }
}); });
parse.detach();
});
} }
} }
@ -353,7 +314,9 @@ Glib::RefPtr<Gsv::Buffer> Source::Controller::buffer() {
return view.get_source_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 bracket_regex("^( *).*\\{ *$");
const std::regex no_bracket_statement_regex("^( *)(if|for|else if|catch|while) *\\(.*[^;}] *$"); 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) *$"); const std::regex no_bracket_no_para_statement_regex("^( *)(else|try|do) *$");
@ -361,7 +324,7 @@ bool Source::Controller::OnKeyPress(GdkEventKey* key) {
//Indent as in previous line, and indent right after if/else/etc //Indent as in previous line, and indent right after if/else/etc
if(key->keyval==GDK_KEY_Return && key->state==0) { if(key->keyval==GDK_KEY_Return && key->state==0) {
string line(view.GetLineBeforeInsert()); string line(view.get_line_before_insert());
std::smatch sm; std::smatch sm;
if(std::regex_match(line, sm, bracket_regex)) { if(std::regex_match(line, sm, bracket_regex)) {
buffer()->insert_at_cursor("\n"+sm[1].str()+config.tab+"\n"+sm[1].str()+"}"); buffer()->insert_at_cursor("\n"+sm[1].str()+config.tab+"\n"+sm[1].str()+"}");
@ -383,7 +346,7 @@ bool Source::Controller::OnKeyPress(GdkEventKey* key) {
std::smatch sm2; std::smatch sm2;
size_t line_nr=buffer()->get_insert()->get_iter().get_line(); size_t line_nr=buffer()->get_insert()->get_iter().get_line();
if(line_nr>0 && sm[1].str().size()>=config.tab_size) { 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, bracket_regex)) {
if(std::regex_match(previous_line, sm2, no_bracket_statement_regex)) { if(std::regex_match(previous_line, sm2, no_bracket_statement_regex)) {
buffer()->insert_at_cursor("\n"+sm2[1].str()); buffer()->insert_at_cursor("\n"+sm2[1].str());
@ -422,7 +385,7 @@ bool Source::Controller::OnKeyPress(GdkEventKey* key) {
int line_end=selection_end.get_line(); int line_end=selection_end.get_line();
for(int line_nr=line_start;line_nr<=line_end;line_nr++) { 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)) if(!(line.size()>=config.tab_size && line.substr(0, config.tab_size)==config.tab))
return true; return true;
} }
@ -439,7 +402,7 @@ bool Source::Controller::OnKeyPress(GdkEventKey* key) {
} }
//Indent left when writing } on a new line //Indent left when writing } on a new line
else if(key->keyval==GDK_KEY_braceright) { else if(key->keyval==GDK_KEY_braceright) {
string line=view.GetLineBeforeInsert(); string line=view.get_line_before_insert();
if(line.size()>=config.tab_size) { if(line.size()>=config.tab_size) {
for(auto c: line) { for(auto c: line) {
if(c!=' ') if(c!=' ')
@ -460,8 +423,8 @@ bool Source::Controller::OnKeyPress(GdkEventKey* key) {
Gtk::TextIter insert_it=buffer()->get_insert()->get_iter(); Gtk::TextIter insert_it=buffer()->get_insert()->get_iter();
int line_nr=insert_it.get_line(); int line_nr=insert_it.get_line();
if(line_nr>0) { if(line_nr>0) {
string line=view.GetLine(line_nr); string line=view.get_line(line_nr);
string previous_line=view.GetLine(line_nr-1); string previous_line=view.get_line(line_nr-1);
smatch sm; smatch sm;
if(std::regex_match(previous_line, sm, spaces_regex)) { if(std::regex_match(previous_line, sm, spaces_regex)) {
if(line==sm[1]) { if(line==sm[1]) {

126
juci/source.h

@ -8,6 +8,7 @@
#include <thread> #include <thread>
#include <mutex> #include <mutex>
#include <string> #include <string>
#include <atomic>
#include "gtksourceviewmm.h" #include "gtksourceviewmm.h"
namespace Notebook { namespace Notebook {
@ -17,139 +18,118 @@ namespace Notebook {
namespace Source { namespace Source {
class Config { class Config {
public: public:
const std::unordered_map<std::string, std::string>& tagtable() const; bool legal_extension(std::string e) const ;
const std::unordered_map<std::string, std::string>& typetable() const;
std::vector<std::string>& extensiontable();
void SetTagTable(const std::unordered_map<std::string, std::string>
&tagtable);
void InsertTag(const std::string &key, const std::string &value);
void SetTypeTable(const std::unordered_map<std::string, std::string>
&tagtable);
void InsertType(const std::string &key, const std::string &value);
void InsertExtension(const std::string &ext);
std::vector<std::string> extensiontable_;
// TODO: Have to clean away all the simple setter and getter methods at some point. It creates too much unnecessary code
unsigned tab_size; unsigned tab_size;
bool show_line_numbers, highlight_current_line; bool show_line_numbers, highlight_current_line;
std::string tab, background, font; std::string tab, background, font;
private: std::vector<std::string> extensions;
std::unordered_map<std::string, std::string> tagtable_, typetable_; std::unordered_map<std::string, std::string> tags, types;
}; // class Config }; // class Config
class Location { class Location {
public: public:
Location(const Location &location); Location(int line_number, int column_offset):
Location(int line_number, int column_offset); line_number(line_number), column_offset(column_offset) {}
int line_number() const { return line_number_; } int line_number;
int column_offset() const { return column_offset_; } int column_offset;
private:
int line_number_;
int column_offset_;
}; };
class Range { class Range {
public: public:
Range(const Location &start, const Location &end, int kind); Range(const Location &start, const Location &end, int kind):
Range(const Range &org); start(start), end(end), kind(kind) {}
const Location& start() const { return start_; } Location start;
const Location& end() const { return end_; } Location end;
int kind() const { return kind_; } int kind;
private:
Location start_;
Location end_;
int kind_;
}; };
class View : public Gsv::View { class View : public Gsv::View {
public: public:
View(); View();
virtual ~View() { } std::string get_line(size_t line_number);
void OnLineEdit(const std::vector<Range> &locations, std::string get_line_before_insert();
const Config &config);
void OnUpdateSyntax(const std::vector<Range> &locations,
const Config &config);
std::string GetLine(size_t line_number);
std::string GetLineBeforeInsert();
}; // class View }; // class View
class AutoCompleteChunk { class AutoCompleteChunk {
public: public:
explicit AutoCompleteChunk(const clang::CompletionChunk &chunk) : explicit AutoCompleteChunk(const clang::CompletionChunk &clang_chunk) :
chunk_(chunk.chunk()), kind_(chunk.kind()) { } chunk(clang_chunk.chunk()), kind(clang_chunk.kind()) { }
const std::string& chunk() const { return chunk_; } std::string chunk;
const clang::CompletionChunkKind& kind() const { return kind_; } enum clang::CompletionChunkKind kind;
private:
std::string chunk_;
enum clang::CompletionChunkKind kind_;
}; };
class AutoCompleteData { class AutoCompleteData {
public: public:
explicit AutoCompleteData(const std::vector<AutoCompleteChunk> &chunks) : explicit AutoCompleteData(const std::vector<AutoCompleteChunk> &chunks) :
chunks_(chunks) { } chunks(chunks) { }
std::vector<AutoCompleteChunk> chunks_; std::vector<AutoCompleteChunk> chunks;
}; };
class Controller;
class Parser{ class Parser{
public: public:
Parser(const std::vector<std::unique_ptr<Source::Controller> > &controllers):
controllers(controllers) {}
~Parser();
// inits the syntax highligthing on file open // inits the syntax highligthing on file open
void InitSyntaxHighlighting(const std::string &filepath, void init_syntax_highlighting(const std::string &filepath,
const std::string &project_path, const std::string &project_path,
const std::map<std::string, std::string> const std::map<std::string, std::string>
&buffers, &buffers,
int start_offset, int start_offset,
int end_offset, int end_offset,
clang::Index *index); clang::Index *index);
void GetAutoCompleteSuggestions(const std::map<std::string, std::string> std::vector<Source::AutoCompleteData> get_autocomplete_suggestions(int line_number, int column);
&buffers, int reparse(const std::map<std::string, std::string> &buffers);
int line_number, std::vector<Range> extract_tokens(int, int);
int column,
std::vector<AutoCompleteData>
*suggestions);
int ReParse(const std::map<std::string, std::string> &buffers);
std::vector<Range> ExtractTokens(int, int);
std::string file_path; std::string file_path;
std::string project_path; std::string project_path;
static clang::Index clang_index;
std::map<std::string, std::string> get_buffer_map() const;
std::mutex parsing_mutex;
private: private:
std::unique_ptr<clang::TranslationUnit> tu_; //use unique_ptr since it is not initialized in constructor std::unique_ptr<clang::TranslationUnit> tu_; //use unique_ptr since it is not initialized in constructor
void HighlightToken(clang::Token *token, void highlight_token(clang::Token *token,
std::vector<Range> *source_ranges, std::vector<Range> *source_ranges,
int token_kind); int token_kind);
void HighlightCursor(clang::Token *token, void highlight_cursor(clang::Token *token,
std::vector<Range> *source_ranges); std::vector<Range> *source_ranges);
std::vector<std::string> get_compilation_commands(); std::vector<std::string> get_compilation_commands();
//controllers is needed here, no way around that I think
const std::vector<std::unique_ptr<Source::Controller> > &controllers;
}; };
class Controller { class Controller {
public: public:
Controller(const Source::Config &config, Controller(const Source::Config &config,
Notebook::Controller &notebook); const std::vector<std::unique_ptr<Source::Controller> > &controllers);
Controller();
~Controller(); ~Controller();
void OnNewEmptyFile(); void update_syntax(const std::vector<Range> &locations);
void OnOpenFile(const std::string &filename); void on_new_empty_file();
void GetAutoCompleteSuggestions(int line_number, void on_open_file(const std::string &filename);
int column,
std::vector<AutoCompleteData>
*suggestions);
Glib::RefPtr<Gsv::Buffer> buffer(); Glib::RefPtr<Gsv::Buffer> buffer();
bool OnKeyPress(GdkEventKey* key); bool on_key_press(GdkEventKey* key);
bool is_saved = false; //TODO: Is never set to false in Notebook::Controller bool is_saved = true;
bool is_changed = false; //TODO: Is never set to true
Parser parser; Parser parser;
View view; View view;
std::function<void(bool was_saved)> signal_buffer_changed;
private: private:
void OnLineEdit(); Glib::Dispatcher parse_done;
void OnSaveFile(); Glib::Dispatcher parse_start;
std::mutex parsing; std::thread parse_thread;
Glib::Dispatcher parsing_done; std::map<std::string, std::string> parse_thread_buffer_map;
std::mutex parse_thread_buffer_map_mutex;
std::atomic<bool> parse_thread_go;
std::atomic<bool> parse_thread_mapped;
std::atomic<bool> parse_thread_stop;
const Config& config; const Config& config;
Notebook::Controller& notebook; //TODO: should maybe be const, but that involves a small change in libclangmm
}; // class Controller }; // class Controller
} // namespace Source } // namespace Source
#endif // JUCI_SOURCE_H_ #endif // JUCI_SOURCE_H_

Loading…
Cancel
Save