Browse Source

Initial restructure of source.h/cc. If its decided to restructure further, this will form an ok starting point for this work. Source::ClangView is now used for cpp-files, and Source::GenericView is used for other files. Source::View is inherited in both cases. See TODO-comment regarding new files.

merge-requests/365/head
eidheim 11 years ago
parent
commit
714d87a089
  1. 57
      juci/notebook.cc
  2. 4
      juci/notebook.h
  3. 387
      juci/source.cc
  4. 71
      juci/source.h
  5. 4
      juci/window.cc

57
juci/notebook.cc

@ -21,7 +21,6 @@ Notebook::Controller::Controller(Gtk::Window* window,
source_config_(source_cfg) { source_config_(source_cfg) {
INFO("Create notebook"); INFO("Create notebook");
window_ = window; window_ = window;
OnNewPage("untitled");
refClipboard_ = Gtk::Clipboard::get(); refClipboard_ = Gtk::Clipboard::get();
ispopup = false; ispopup = false;
view().pack1(directories_.widget(), true, true); view().pack1(directories_.widget(), true, true);
@ -147,7 +146,7 @@ void Notebook::Controller::CreateKeybindings(Keybindings::Controller
connect( connect(
[this]() { [this]() {
if (is_new_file_) { if (is_new_file_) {
OnNewPage(entry_.text()); //OnNewPage(entry_.text()); //TODO: rewrite new file (the file needs to be created before opened with Source::Controller in order to choose the correct view implementation)
entry_.OnHideEntries(is_new_file_); entry_.OnHideEntries(is_new_file_);
} else { } else {
Search(true); Search(true);
@ -156,7 +155,7 @@ void Notebook::Controller::CreateKeybindings(Keybindings::Controller
entry_.button_apply().signal_clicked(). entry_.button_apply().signal_clicked().
connect( connect(
[this]() { [this]() {
OnNewPage(entry_.text()); //OnNewPage(entry_.text()); //TODO: rewrite new file (the file needs to be created before opened with Source::Controller in order to choose the correct view implementation)
entry_.OnHideEntries(is_new_file_); entry_.OnHideEntries(is_new_file_);
}); });
entry_.button_close().signal_clicked(). entry_.button_close().signal_clicked().
@ -191,7 +190,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())->view->file_path;
if (!source_config().legal_extension(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();
@ -229,7 +228,7 @@ 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=text_vec_.at(CurrentPage())->parser. std::vector<Source::AutoCompleteData> acdata=text_vec_.at(CurrentPage())->view->
get_autocomplete_suggestions(beg.get_line()+1, get_autocomplete_suggestions(beg.get_line()+1,
beg.get_line_offset()+2); beg.get_line_offset()+2);
std::map<std::string, std::string> items; std::map<std::string, std::string> items;
@ -298,20 +297,14 @@ Gtk::Box& Notebook::Controller::entry_view() {
return entry_.view(); return entry_.view();
} }
void Notebook::Controller::OnNewPage(std::string name) {
INFO("Notebook Generate new page");
OnCreatePage();
Notebook().append_page(*editor_vec_.back(), name);
Notebook().show_all_children();
Notebook().set_current_page(Pages()-1);
Notebook().set_focus_child(text_vec_.at(Pages()-1)->view);
text_vec_.back()->on_new_empty_file();
set_source_handlers(*text_vec_.back());
}
void Notebook::Controller::OnOpenFile(std::string path) { void Notebook::Controller::OnOpenFile(std::string path) {
INFO("Notebook open file"); INFO("Notebook open file");
OnCreatePage(); INFO("Notebook create page");
text_vec_.emplace_back(new Source::Controller(source_config(), path, project_path));
scrolledtext_vec_.push_back(new Gtk::ScrolledWindow());
editor_vec_.push_back(new Gtk::HBox());
scrolledtext_vec_.back()->add(*text_vec_.back()->view);
editor_vec_.back()->pack_start(*scrolledtext_vec_.back(), true, true);
size_t pos = path.find_last_of("/\\"); size_t pos = path.find_last_of("/\\");
std::string filename=path; std::string filename=path;
if(pos!=std::string::npos) if(pos!=std::string::npos)
@ -319,20 +312,10 @@ void Notebook::Controller::OnOpenFile(std::string path) {
Notebook().append_page(*editor_vec_.back(), filename); 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);
text_vec_.back()->on_open_file(path);
set_source_handlers(*text_vec_.back()); set_source_handlers(*text_vec_.back());
} }
void Notebook::Controller::OnCreatePage() {
INFO("Notebook create page");
text_vec_.emplace_back(new Source::Controller(source_config(), text_vec_));
scrolledtext_vec_.push_back(new Gtk::ScrolledWindow());
editor_vec_.push_back(new Gtk::HBox());
scrolledtext_vec_.back()->add(text_vec_.back()->view);
editor_vec_.back()->pack_start(*scrolledtext_vec_.back(), true, true);
}
void Notebook::Controller::OnCloseCurrentPage() { void Notebook::Controller::OnCloseCurrentPage() {
INFO("Notebook close page"); INFO("Notebook close page");
if (Pages() != 0) { if (Pages() != 0) {
@ -456,7 +439,7 @@ void Notebook::Controller
Source::View& Notebook::Controller::CurrentTextView() { Source::View& Notebook::Controller::CurrentTextView() {
INFO("Getting sourceview"); INFO("Getting sourceview");
return text_vec_.at(CurrentPage())->view; return *text_vec_.at(CurrentPage())->view;
} }
int Notebook::Controller::CurrentPage() { int Notebook::Controller::CurrentPage() {
@ -465,7 +448,7 @@ int Notebook::Controller::CurrentPage() {
Glib::RefPtr<Gtk::TextBuffer> Glib::RefPtr<Gtk::TextBuffer>
Notebook::Controller::Buffer(Source::Controller &source) { Notebook::Controller::Buffer(Source::Controller &source) {
return source.view.get_buffer(); return source.view->get_buffer();
} }
int Notebook::Controller::Pages() { int Notebook::Controller::Pages() {
@ -484,16 +467,16 @@ void Notebook::Controller::BufferChangeHandler(Glib::RefPtr<Gtk::TextBuffer>
} }
void Notebook::Controller::set_source_handlers(Source::Controller& controller) { void Notebook::Controller::set_source_handlers(Source::Controller& controller) {
controller.view.signal_button_release_event(). controller.view->signal_button_release_event().
connect(sigc::mem_fun(*this, &Notebook::Controller::OnMouseRelease), false); connect(sigc::mem_fun(*this, &Notebook::Controller::OnMouseRelease), false);
controller.view.signal_key_release_event(). controller.view->signal_key_release_event().
connect(sigc::mem_fun(*this, &Notebook::Controller::OnKeyRelease), false); connect(sigc::mem_fun(*this, &Notebook::Controller::OnKeyRelease), false);
//Add star on tab label when the page is not saved: //Add star on tab label when the page is not saved:
controller.buffer()->signal_changed().connect([this]() { controller.buffer()->signal_changed().connect([this]() {
if(text_vec_.at(CurrentPage())->is_saved) { if(text_vec_.at(CurrentPage())->is_saved) {
std::string path=text_vec_.at(CurrentPage())->parser.file_path; std::string path=text_vec_.at(CurrentPage())->view->file_path;
size_t pos = path.find_last_of("/\\"); size_t pos = path.find_last_of("/\\");
std::string filename=path; std::string filename=path;
if(pos!=std::string::npos) if(pos!=std::string::npos)
@ -547,7 +530,7 @@ void Notebook::Controller::PopupSetSize(Gtk::ScrolledWindow &scroll,
} }
std::string Notebook::Controller::CurrentPagePath(){ std::string Notebook::Controller::CurrentPagePath(){
return text_vec_.at(CurrentPage())->parser.file_path; return text_vec_.at(CurrentPage())->view->file_path;
} }
void Notebook::Controller::FindPopupPosition(Gtk::TextView& textview, void Notebook::Controller::FindPopupPosition(Gtk::TextView& textview,
@ -585,7 +568,7 @@ void Notebook::Controller::FindPopupPosition(Gtk::TextView& textview,
} }
bool Notebook::Controller:: OnSaveFile() { bool Notebook::Controller:: OnSaveFile() {
std::string path=text_vec_.at(CurrentPage())->parser.file_path; std::string path=text_vec_.at(CurrentPage())->view->file_path;
return OnSaveFile(path); return OnSaveFile(path);
} }
bool Notebook::Controller:: OnSaveFile(std::string path) { bool Notebook::Controller:: OnSaveFile(std::string path) {
@ -595,7 +578,7 @@ bool Notebook::Controller:: OnSaveFile(std::string path) {
file.open (path); file.open (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())->view->file_path=path;
size_t pos = path.find_last_of("/\\"); size_t pos = path.find_last_of("/\\");
std::string filename=path; std::string filename=path;
if(pos!=std::string::npos) if(pos!=std::string::npos)
@ -646,7 +629,7 @@ void Notebook::Controller::AskToSaveDialog() {
false, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_YES_NO); false, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_YES_NO);
dialog.set_secondary_text( dialog.set_secondary_text(
"Do you want to save: " + "Do you want to save: " +
text_vec_.at(CurrentPage())->parser.file_path+" ?"); text_vec_.at(CurrentPage())->view->file_path+" ?");
DEBUG("AskToSaveDialog: run dialog"); DEBUG("AskToSaveDialog: run dialog");
int result = dialog.run(); int result = dialog.run();

4
juci/notebook.h

@ -58,9 +58,7 @@ namespace Notebook {
bool OnSaveFile(std::string path); bool OnSaveFile(std::string path);
void OnDirectoryNavigation(const Gtk::TreeModel::Path& path, void OnDirectoryNavigation(const Gtk::TreeModel::Path& path,
Gtk::TreeViewColumn* column); Gtk::TreeViewColumn* column);
void OnNewPage(std::string name);
void OnOpenFile(std::string filename); void OnOpenFile(std::string filename);
void OnCreatePage();
bool ScrollEventCallback(GdkEventScroll* scroll_event); bool ScrollEventCallback(GdkEventScroll* scroll_event);
int Pages(); int Pages();
Directories::Controller& directories() { return directories_; } Directories::Controller& directories() { return directories_; }
@ -72,7 +70,7 @@ namespace Notebook {
bool OnKeyRelease(GdkEventKey* key); bool OnKeyRelease(GdkEventKey* key);
std::string OnSaveFileAs(); std::string OnSaveFileAs();
bool LegalExtension(std::string extension); bool LegalExtension(std::string extension);
std::string project_path;
protected: protected:
void set_source_handlers(Source::Controller& controller); void set_source_handlers(Source::Controller& controller);
void PopupSelectHandler(Gtk::Dialog &popup, void PopupSelectHandler(Gtk::Dialog &popup,

387
juci/source.cc

@ -20,8 +20,17 @@ bool Source::Config::legal_extension(std::string e) const {
////////////// //////////////
//// View //// //// View ////
////////////// //////////////
Source::View::View() { Source::View::View(const Config& config, const std::string& file_path, const std::string& project_path):
config(config), file_path(file_path), project_path(project_path) {
Gsv::init(); Gsv::init();
set_smart_home_end(Gsv::SMART_HOME_END_BEFORE);
set_show_line_numbers(config.show_line_numbers);
set_highlight_current_line(config.highlight_current_line);
sourcefile s(file_path);
get_source_buffer()->get_undo_manager()->begin_not_undoable_action();
get_source_buffer()->set_text(s.get_content());
get_source_buffer()->get_undo_manager()->end_not_undoable_action();
} }
string Source::View::get_line(size_t line_number) { string Source::View::get_line(size_t line_number) {
@ -40,51 +49,113 @@ string Source::View::get_line_before_insert() {
return line; return line;
} }
/////////////// //////////////////
//// Parser /// //// ClangView ///
/////////////// //////////////////
clang::Index Source::Parser::clang_index(0, 1); clang::Index Source::ClangView::clang_index(0, 1);
Source::ClangView::ClangView(const Config& config, const std::string& file_path, const std::string& project_path):
Source::View::View(config, file_path, project_path),
parse_thread_go(true), parse_thread_mapped(false), parse_thread_stop(false) {
override_font(Pango::FontDescription(config.font));
override_background_color(Gdk::RGBA(config.background));
for (auto &item : config.tags) {
get_source_buffer()->create_tag(item.first)->property_foreground() = item.second;
}
int start_offset = get_source_buffer()->begin().get_offset();
int end_offset = get_source_buffer()->end().get_offset();
init_syntax_highlighting(get_buffer_map(),
start_offset,
end_offset,
&ClangView::clang_index);
update_syntax(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()) {
parse_thread_buffer_map=get_buffer_map();
parse_thread_mapped=true;
parse_thread_buffer_map_mutex.unlock();
}
parse_thread_go=true;
});
parse_done.connect([this](){
if(parse_thread_mapped) {
INFO("Updating syntax");
update_syntax(extract_tokens(0, get_source_buffer()->get_text().size()));
INFO("Syntax updated");
}
else {
parse_thread_go=true;
}
});
parse_thread=std::thread([this]() {
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_mapped) {
parse_thread_go=false;
parse_start();
}
else if (parse_thread_mapped && parsing_mutex.try_lock() && parse_thread_buffer_map_mutex.try_lock()) {
reparse(parse_thread_buffer_map);
parse_thread_go=false;
parsing_mutex.unlock();
parse_thread_buffer_map_mutex.unlock();
parse_done();
}
}
});
get_source_buffer()->signal_changed().connect([this]() {
parse_thread_mapped=false;
parse_thread_go=true;
});
Source::Parser::~Parser() { signal_key_press_event().connect(sigc::mem_fun(*this, &Source::ClangView::on_key_press), false);
}
Source::ClangView::~ClangView() {
parse_thread_stop=true;
if(parse_thread.joinable())
parse_thread.join();
parsing_mutex.lock(); //Be sure not to destroy while still parsing with libclang parsing_mutex.lock(); //Be sure not to destroy while still parsing with libclang
parsing_mutex.unlock(); parsing_mutex.unlock();
} }
void Source::Parser:: void Source::ClangView::
init_syntax_highlighting(const std::string &filepath, init_syntax_highlighting(const std::map<std::string, std::string>
const std::string &project_path,
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) {
this->project_path=project_path;
std::vector<string> arguments = get_compilation_commands(); std::vector<string> arguments = get_compilation_commands();
tu_ = std::unique_ptr<clang::TranslationUnit>(new clang::TranslationUnit(index, tu_ = std::unique_ptr<clang::TranslationUnit>(new clang::TranslationUnit(index,
filepath, file_path,
arguments, arguments,
buffers)); buffers));
} }
std::map<std::string, std::string> Source::Parser:: std::map<std::string, std::string> Source::ClangView::
get_buffer_map() const { get_buffer_map() const {
std::map<std::string, std::string> buffer_map; std::map<std::string, std::string> buffer_map;
for (auto &controller : controllers) { buffer_map[file_path]=get_source_buffer()->get_text().raw();
buffer_map.operator[](controller->parser.file_path) =
controller->buffer()->get_text().raw();
}
return buffer_map; return buffer_map;
} }
// Source::Model::UpdateLine int Source::ClangView::
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:: std::vector<Source::AutoCompleteData> Source::ClangView::
get_autocomplete_suggestions(int line_number, get_autocomplete_suggestions(int line_number, int column) {
int column) {
INFO("Getting auto complete suggestions"); INFO("Getting auto complete suggestions");
std::vector<Source::AutoCompleteData> suggestions; std::vector<Source::AutoCompleteData> suggestions;
auto buffer_map=get_buffer_map(); auto buffer_map=get_buffer_map();
@ -108,7 +179,7 @@ get_autocomplete_suggestions(int line_number,
return suggestions; return suggestions;
} }
std::vector<std::string> Source::Parser:: std::vector<std::string> Source::ClangView::
get_compilation_commands() { get_compilation_commands() {
clang::CompilationDatabase db(project_path+"/"); clang::CompilationDatabase db(project_path+"/");
clang::CompileCommands commands(file_path, &db); clang::CompileCommands commands(file_path, &db);
@ -123,7 +194,7 @@ get_compilation_commands() {
return arguments; return arguments;
} }
std::vector<Source::Range> Source::Parser:: std::vector<Source::Range> Source::ClangView::
extract_tokens(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);
@ -143,70 +214,11 @@ extract_tokens(int start_offset, int end_offset) {
return ranges; return ranges;
} }
void Source::Parser:: void Source::ClangView::update_syntax(const std::vector<Source::Range> &ranges) {
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);
clang::SourceRange range(&cursor);
clang::SourceLocation begin(&range, true);
clang::SourceLocation end(&range, false);
unsigned begin_line_num, begin_offset, end_line_num, end_offset;
begin.get_location_info(NULL, &begin_line_num, &begin_offset, NULL);
end.get_location_info(NULL, &end_line_num, &end_offset, NULL);
source_ranges->emplace_back(Source::Location(begin_line_num,
begin_offset),
Source::Location(end_line_num,
end_offset), (int) cursor.kind());
}
void Source::Parser::
highlight_token(clang::Token *token,
std::vector<Source::Range> *source_ranges,
int token_kind) {
clang::SourceRange range = token->get_source_range(tu_.get());
unsigned begin_line_num, begin_offset, end_line_num, end_offset;
clang::SourceLocation begin(&range, true);
clang::SourceLocation end(&range, false);
begin.get_location_info(NULL, &begin_line_num, &begin_offset, NULL);
end.get_location_info(NULL, &end_line_num, &end_offset, NULL);
source_ranges->emplace_back(Source::Location(begin_line_num,
begin_offset),
Source::Location(end_line_num,
end_offset), token_kind);
}
////////////////////
//// Controller ////
////////////////////
// Source::Controller::Controller()
// Constructor for Controller
Source::Controller::Controller(const Source::Config &config,
const std::vector<std::unique_ptr<Source::Controller> > &controllers) :
config(config), parser(controllers), parse_thread_go(true), 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::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.tags) {
buffer()->create_tag(item.first)->property_foreground() = item.second;
}
}
Source::Controller::~Controller() {
parse_thread_stop=true;
if(parse_thread.joinable())
parse_thread.join();
}
void Source::Controller::update_syntax(const std::vector<Source::Range> &ranges) {
if (ranges.empty() || ranges.size() == 0) { if (ranges.empty() || ranges.size() == 0) {
return; return;
} }
auto buffer = view.get_buffer(); auto buffer = get_source_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);
@ -231,93 +243,41 @@ void Source::Controller::update_syntax(const std::vector<Source::Range> &ranges)
} }
} }
void Source::Controller::set_handlers() { void Source::ClangView::
buffer()->signal_changed().connect([this]() { highlight_cursor(clang::Token *token,
parse_thread_mapped=false; std::vector<Source::Range> *source_ranges) {
parse_thread_go=true; clang::SourceLocation location = token->get_source_location(tu_.get());
}); clang::Cursor cursor(tu_.get(), &location);
} clang::SourceRange range(&cursor);
clang::SourceLocation begin(&range, true);
void Source::Controller::on_new_empty_file() { clang::SourceLocation end(&range, false);
string filename("/tmp/untitled"); unsigned begin_line_num, begin_offset, end_line_num, end_offset;
sourcefile s(filename); begin.get_location_info(NULL, &begin_line_num, &begin_offset, NULL);
parser.file_path=filename; end.get_location_info(NULL, &end_line_num, &end_offset, NULL);
parser.project_path=filename; source_ranges->emplace_back(Source::Location(begin_line_num,
s.save(""); begin_offset),
set_handlers(); Source::Location(end_line_num,
} end_offset), (int) cursor.kind());
void Source::Controller::on_open_file(const string &filepath) {
parser.file_path=filepath;
sourcefile s(filepath);
auto buffer_map=parser.get_buffer_map();
buffer_map[filepath] = s.get_content();
buffer()->get_undo_manager()->begin_not_undoable_action();
buffer()->set_text(s.get_content());
buffer()->get_undo_manager()->end_not_undoable_action();
set_handlers();
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.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.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;
});
parse_done.connect([this](){
if(parse_thread_mapped) {
INFO("Updating syntax");
update_syntax(parser.extract_tokens(0, buffer()->get_text().size()));
INFO("Syntax updated");
}
else {
parse_thread_go=true;
}
});
parse_thread=std::thread([this]() {
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_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();
}
}
});
}
} }
void Source::ClangView::
Glib::RefPtr<Gsv::Buffer> Source::Controller::buffer() { highlight_token(clang::Token *token,
return view.get_source_buffer(); std::vector<Source::Range> *source_ranges,
int token_kind) {
clang::SourceRange range = token->get_source_range(tu_.get());
unsigned begin_line_num, begin_offset, end_line_num, end_offset;
clang::SourceLocation begin(&range, true);
clang::SourceLocation end(&range, false);
begin.get_location_info(NULL, &begin_line_num, &begin_offset, NULL);
end.get_location_info(NULL, &end_line_num, &end_offset, NULL);
source_ranges->emplace_back(Source::Location(begin_line_num,
begin_offset),
Source::Location(end_line_num,
end_offset), token_kind);
} }
//TODO: move indentation to Parser, replace indentation methods with a better implementation or //TODO: replace indentation methods with a better implementation or
//maybe use libclang //maybe use libclang
bool Source::Controller::on_key_press(GdkEventKey* key) { bool Source::ClangView::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) *$");
@ -325,115 +285,140 @@ bool Source::Controller::on_key_press(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.get_line_before_insert()); string line(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()+"}"); get_source_buffer()->insert_at_cursor("\n"+sm[1].str()+config.tab+"\n"+sm[1].str()+"}");
auto insert_it = buffer()->get_insert()->get_iter(); auto insert_it = get_source_buffer()->get_insert()->get_iter();
for(size_t c=0;c<config.tab_size+sm[1].str().size();c++) for(size_t c=0;c<config.tab_size+sm[1].str().size();c++)
insert_it--; insert_it--;
view.scroll_to(buffer()->get_insert()); scroll_to(get_source_buffer()->get_insert());
buffer()->place_cursor(insert_it); get_source_buffer()->place_cursor(insert_it);
} }
else if(std::regex_match(line, sm, no_bracket_statement_regex)) { else if(std::regex_match(line, sm, no_bracket_statement_regex)) {
buffer()->insert_at_cursor("\n"+sm[1].str()+config.tab); get_source_buffer()->insert_at_cursor("\n"+sm[1].str()+config.tab);
view.scroll_to(buffer()->get_insert()); scroll_to(get_source_buffer()->get_insert());
} }
else if(std::regex_match(line, sm, no_bracket_no_para_statement_regex)) { else if(std::regex_match(line, sm, no_bracket_no_para_statement_regex)) {
buffer()->insert_at_cursor("\n"+sm[1].str()+config.tab); get_source_buffer()->insert_at_cursor("\n"+sm[1].str()+config.tab);
view.scroll_to(buffer()->get_insert()); scroll_to(get_source_buffer()->get_insert());
} }
else if(std::regex_match(line, sm, spaces_regex)) { else if(std::regex_match(line, sm, spaces_regex)) {
std::smatch sm2; std::smatch sm2;
size_t line_nr=buffer()->get_insert()->get_iter().get_line(); size_t line_nr=get_source_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.get_line(line_nr-1); string previous_line=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()); get_source_buffer()->insert_at_cursor("\n"+sm2[1].str());
view.scroll_to(buffer()->get_insert()); scroll_to(get_source_buffer()->get_insert());
return true; return true;
} }
else if(std::regex_match(previous_line, sm2, no_bracket_no_para_statement_regex)) { else if(std::regex_match(previous_line, sm2, no_bracket_no_para_statement_regex)) {
buffer()->insert_at_cursor("\n"+sm2[1].str()); get_source_buffer()->insert_at_cursor("\n"+sm2[1].str());
view.scroll_to(buffer()->get_insert()); scroll_to(get_source_buffer()->get_insert());
return true; return true;
} }
} }
} }
buffer()->insert_at_cursor("\n"+sm[1].str()); get_source_buffer()->insert_at_cursor("\n"+sm[1].str());
view.scroll_to(buffer()->get_insert()); scroll_to(get_source_buffer()->get_insert());
} }
return true; return true;
} }
//Indent right when clicking tab, no matter where in the line the cursor is. Also works on selected text. //Indent right when clicking tab, no matter where in the line the cursor is. Also works on selected text.
if(key->keyval==GDK_KEY_Tab && key->state==0) { if(key->keyval==GDK_KEY_Tab && key->state==0) {
Gtk::TextIter selection_start, selection_end; Gtk::TextIter selection_start, selection_end;
buffer()->get_selection_bounds(selection_start, selection_end); get_source_buffer()->get_selection_bounds(selection_start, selection_end);
int line_start=selection_start.get_line(); int line_start=selection_start.get_line();
int line_end=selection_end.get_line(); int line_end=selection_end.get_line();
for(int line=line_start;line<=line_end;line++) { for(int line=line_start;line<=line_end;line++) {
Gtk::TextIter line_it = buffer()->get_iter_at_line(line); Gtk::TextIter line_it = get_source_buffer()->get_iter_at_line(line);
buffer()->insert(line_it, config.tab); get_source_buffer()->insert(line_it, config.tab);
} }
return true; return true;
} }
//Indent left when clicking shift-tab, no matter where in the line the cursor is. Also works on selected text. //Indent left when clicking shift-tab, no matter where in the line the cursor is. Also works on selected text.
else if((key->keyval==GDK_KEY_ISO_Left_Tab || key->keyval==GDK_KEY_Tab) && key->state==GDK_SHIFT_MASK) { else if((key->keyval==GDK_KEY_ISO_Left_Tab || key->keyval==GDK_KEY_Tab) && key->state==GDK_SHIFT_MASK) {
Gtk::TextIter selection_start, selection_end; Gtk::TextIter selection_start, selection_end;
buffer()->get_selection_bounds(selection_start, selection_end); get_source_buffer()->get_selection_bounds(selection_start, selection_end);
int line_start=selection_start.get_line(); int line_start=selection_start.get_line();
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.get_line(line_nr); string line=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;
} }
for(int line_nr=line_start;line_nr<=line_end;line_nr++) { for(int line_nr=line_start;line_nr<=line_end;line_nr++) {
Gtk::TextIter line_it = buffer()->get_iter_at_line(line_nr); Gtk::TextIter line_it = get_source_buffer()->get_iter_at_line(line_nr);
Gtk::TextIter line_plus_it=line_it; Gtk::TextIter line_plus_it=line_it;
for(unsigned c=0;c<config.tab_size;c++) for(unsigned c=0;c<config.tab_size;c++)
line_plus_it++; line_plus_it++;
buffer()->erase(line_it, line_plus_it); get_source_buffer()->erase(line_it, line_plus_it);
} }
return true; return true;
} }
//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.get_line_before_insert(); string line=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!=' ')
return false; return false;
} }
Gtk::TextIter insert_it = buffer()->get_insert()->get_iter(); Gtk::TextIter insert_it = get_source_buffer()->get_insert()->get_iter();
Gtk::TextIter line_it = buffer()->get_iter_at_line(insert_it.get_line()); Gtk::TextIter line_it = get_source_buffer()->get_iter_at_line(insert_it.get_line());
Gtk::TextIter line_plus_it=line_it; Gtk::TextIter line_plus_it=line_it;
for(unsigned c=0;c<config.tab_size;c++) for(unsigned c=0;c<config.tab_size;c++)
line_plus_it++; line_plus_it++;
buffer()->erase(line_it, line_plus_it); get_source_buffer()->erase(line_it, line_plus_it);
} }
return false; return false;
} }
//"Smart" backspace key //"Smart" backspace key
else if(key->keyval==GDK_KEY_BackSpace) { else if(key->keyval==GDK_KEY_BackSpace) {
Gtk::TextIter insert_it=buffer()->get_insert()->get_iter(); Gtk::TextIter insert_it=get_source_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.get_line(line_nr); string line=get_line(line_nr);
string previous_line=view.get_line(line_nr-1); string previous_line=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]) {
Gtk::TextIter line_it = buffer()->get_iter_at_line(line_nr); Gtk::TextIter line_it = get_source_buffer()->get_iter_at_line(line_nr);
buffer()->erase(line_it, insert_it); get_source_buffer()->erase(line_it, insert_it);
} }
} }
} }
} }
return false; return false;
} }
////////////////////
//// Controller ////
////////////////////
// Source::Controller::Controller()
// Constructor for Controller
Source::Controller::Controller(const Source::Config &config,
const std::string& file_path, std::string project_path) :
config(config) {
if(project_path=="") {
size_t last_of=file_path.find_last_of("\\/");
if(last_of!=std::string::npos)
project_path=file_path.substr(0, last_of);
}
if (config.legal_extension(file_path.substr(file_path.find_last_of(".") + 1)))
view=std::unique_ptr<View>(new ClangView(config, file_path, project_path));
else
view=std::unique_ptr<View>(new GenericView(config, file_path, project_path));
INFO("Source Controller with childs constructed");
}
Glib::RefPtr<Gsv::Buffer> Source::Controller::buffer() {
return view->get_source_buffer();
}

71
juci/source.h

@ -43,13 +43,6 @@ namespace Source {
int kind; int kind;
}; };
class View : public Gsv::View {
public:
View();
std::string get_line(size_t line_number);
std::string get_line_before_insert();
}; // class View
class AutoCompleteChunk { class AutoCompleteChunk {
public: public:
explicit AutoCompleteChunk(const clang::CompletionChunk &clang_chunk) : explicit AutoCompleteChunk(const clang::CompletionChunk &clang_chunk) :
@ -65,17 +58,30 @@ namespace Source {
std::vector<AutoCompleteChunk> chunks; std::vector<AutoCompleteChunk> chunks;
}; };
class Controller; class View : public Gsv::View {
public:
View(const Config& config, const std::string& file_path, const std::string& project_path);
std::string get_line(size_t line_number);
std::string get_line_before_insert();
virtual std::vector<Source::AutoCompleteData> get_autocomplete_suggestions(int line_number, int column) {return std::vector<Source::AutoCompleteData>();}
std::string file_path;
std::string project_path;
protected:
const Config& config;
}; // class View
class GenericView : public View {
public:
GenericView(const Config& config, const std::string& file_path, const std::string& project_path):
View(config, file_path, project_path) {}
};
class Parser{ class ClangView : public View {
public: public:
Parser(const std::vector<std::unique_ptr<Source::Controller> > &controllers): ClangView(const Config& config, const std::string& file_path, const std::string& project_path);
controllers(controllers) {} ~ClangView();
~Parser();
// inits the syntax highligthing on file open // inits the syntax highligthing on file open
void init_syntax_highlighting(const std::string &filepath, void init_syntax_highlighting(const std::map<std::string, std::string>
const std::string &project_path,
const std::map<std::string, std::string>
&buffers, &buffers,
int start_offset, int start_offset,
int end_offset, int end_offset,
@ -83,9 +89,8 @@ namespace Source {
std::vector<Source::AutoCompleteData> get_autocomplete_suggestions(int line_number, int column); std::vector<Source::AutoCompleteData> get_autocomplete_suggestions(int line_number, int column);
int reparse(const std::map<std::string, std::string> &buffers); int reparse(const std::map<std::string, std::string> &buffers);
std::vector<Range> extract_tokens(int, int); std::vector<Range> extract_tokens(int, int);
void update_syntax(const std::vector<Range> &locations);
std::string file_path;
std::string project_path;
static clang::Index clang_index; static clang::Index clang_index;
std::map<std::string, std::string> get_buffer_map() const; std::map<std::string, std::string> get_buffer_map() const;
std::mutex parsing_mutex; std::mutex parsing_mutex;
@ -97,28 +102,8 @@ namespace Source {
void highlight_cursor(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 {
public:
Controller(const Source::Config &config,
const std::vector<std::unique_ptr<Source::Controller> > &controllers);
~Controller();
void update_syntax(const std::vector<Range> &locations);
void set_handlers();
void on_new_empty_file();
void on_open_file(const std::string &filename);
Glib::RefPtr<Gsv::Buffer> buffer();
bool on_key_press(GdkEventKey* key); bool on_key_press(GdkEventKey* key);
bool is_saved = true;
Parser parser;
View view;
private:
Glib::Dispatcher parse_done; Glib::Dispatcher parse_done;
Glib::Dispatcher parse_start; Glib::Dispatcher parse_start;
std::thread parse_thread; std::thread parse_thread;
@ -127,7 +112,19 @@ namespace Source {
std::atomic<bool> parse_thread_go; std::atomic<bool> parse_thread_go;
std::atomic<bool> parse_thread_mapped; std::atomic<bool> parse_thread_mapped;
std::atomic<bool> parse_thread_stop; std::atomic<bool> parse_thread_stop;
};
class Controller {
public:
Controller(const Source::Config &config,
const std::string& file_path, std::string project_path);
Glib::RefPtr<Gsv::Buffer> buffer();
bool is_saved = true;
std::unique_ptr<View> view;
private:
const Config& config; const Config& config;
}; // class Controller }; // class Controller
} // namespace Source } // namespace Source

4
juci/window.cc

@ -142,7 +142,9 @@ void Window::OnFileOpenFolder() {
{ {
case(Gtk::RESPONSE_OK): case(Gtk::RESPONSE_OK):
{ {
notebook_.directories().open_folder(dialog.get_filename()); std::string project_path=dialog.get_filename();
notebook_.project_path=project_path;
notebook_.directories().open_folder(project_path);
break; break;
} }
case(Gtk::RESPONSE_CANCEL): case(Gtk::RESPONSE_CANCEL):

Loading…
Cancel
Save