Browse Source

Merge branch 'master' into searchandreplace

merge-requests/365/head
Jørgen Lien Sellæg 11 years ago
parent
commit
6adc240bb8
  1. 37
      juci/notebook.cc
  2. 2
      juci/notebook.h
  3. 184
      juci/source.cc
  4. 50
      juci/source.h

37
juci/notebook.cc

@ -196,7 +196,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())->path(); std::string path = text_vec_.at(CurrentPage())->parser.file_path;
if (!LegalExtension(path.substr(path.find_last_of(".") + 1))) return false; if (!LegalExtension(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();
@ -312,14 +312,14 @@ void Notebook::Controller::OnNewPage(std::string name) {
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);
Notebook().set_focus_child(text_vec_.at(Pages()-1)->view()); Notebook().set_focus_child(text_vec_.at(Pages()-1)->view);
} }
void Notebook::Controller:: void Notebook::Controller::
MapBuffers(std::map<std::string, std::string> *buffers) { MapBuffers(std::map<std::string, std::string> *buffers) const {
for (auto &buffer : text_vec_) { for (auto &buffer : text_vec_) {
buffers->operator[](buffer->model().file_path()) = buffers->operator[](buffer->parser.file_path) =
buffer->buffer()->get_text().raw(); buffer->buffer()->get_text().raw();
} }
} }
@ -328,29 +328,28 @@ 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()->OnOpenFile(path);
text_vec_.back()->set_is_saved(true); text_vec_.back()->is_saved=true;
unsigned pos = path.find_last_of("/\\"); unsigned pos = path.find_last_of("/\\");
Notebook().append_page(*editor_vec_.back(), path.substr(pos+1)); Notebook().append_page(*editor_vec_.back(), path.substr(pos+1));
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()->set_is_changed(false);
} }
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(), *this));
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);
} }
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_changed){
AskToSaveDialog(); AskToSaveDialog();
} }
int page = CurrentPage(); int page = CurrentPage();
@ -458,7 +457,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() {
@ -467,7 +466,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() {
@ -536,7 +535,7 @@ void Notebook::Controller::PopupSetSize(Gtk::ScrolledWindow &scroll,
} }
std::string Notebook::Controller::CurrentPagePath(){ std::string Notebook::Controller::CurrentPagePath(){
return text_vec_.at(CurrentPage())->path(); return text_vec_.at(CurrentPage())->parser.file_path;
} }
void Notebook::Controller::FindPopupPosition(Gtk::TextView& textview, void Notebook::Controller::FindPopupPosition(Gtk::TextView& textview,
@ -575,9 +574,9 @@ void Notebook::Controller::FindPopupPosition(Gtk::TextView& textview,
bool Notebook::Controller:: OnSaveFile() { bool Notebook::Controller:: OnSaveFile() {
INFO("Notebook save file"); INFO("Notebook save file");
if (text_vec_.at(CurrentPage())->is_saved()) { if (text_vec_.at(CurrentPage())->is_saved) {
std::ofstream file; std::ofstream file;
file.open (text_vec_.at(CurrentPage())->path()); file.open (text_vec_.at(CurrentPage())->parser.file_path);
file << CurrentTextView().get_buffer()->get_text(); file << CurrentTextView().get_buffer()->get_text();
file.close(); file.close();
return true; return true;
@ -593,8 +592,8 @@ 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())->set_file_path(path); text_vec_.at(CurrentPage())->parser.file_path=path;
text_vec_.at(CurrentPage())->set_is_saved(true); text_vec_.at(CurrentPage())->is_saved=true;
return true; return true;
} }
return false; return false;
@ -639,7 +638,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())->path()+" ?"); text_vec_.at(CurrentPage())->parser.file_path+" ?");
DEBUG("AskToSaveDialog: run dialog"); DEBUG("AskToSaveDialog: run dialog");
int result = dialog.run(); int result = dialog.run();

2
juci/notebook.h

@ -62,7 +62,7 @@ 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); void MapBuffers(std::map<std::string, std::string> *buffers) const;
clang::Index* index() { return &index_; } clang::Index* index() { return &index_; }
int Pages(); int Pages();
Directories::Controller& directories() { return directories_; } Directories::Controller& directories() { return directories_; }

184
juci/source.cc

@ -30,7 +30,6 @@ Range(const Source::Range &org) :
////////////// //////////////
Source::View::View() { Source::View::View() {
Gsv::init(); Gsv::init();
set_smart_home_end(Gsv::SMART_HOME_END_BEFORE);
} }
string Source::View::GetLine(size_t line_number) { string Source::View::GetLine(size_t line_number) {
@ -49,18 +48,6 @@ string Source::View::GetLineBeforeInsert() {
return line; return line;
} }
// Source::View::ApplyConfig
// Applies theme in textview
void Source::View::ApplyConfig(const Source::Config &config) {
override_font(Pango::FontDescription(config.font));
set_show_line_numbers(config.show_line_numbers);
set_highlight_current_line(config.highlight_current_line);
this->override_background_color(Gdk::RGBA(config.background));
for (auto &item : config.tagtable()) {
get_buffer()->create_tag(item.first)->property_foreground() = item.second;
}
}
// Source::View::Config::tagtable() // Source::View::Config::tagtable()
// returns a const refrence to the tagtable // returns a const refrence to the tagtable
const std::unordered_map<string, string>& Source::Config::tagtable() const { const std::unordered_map<string, string>& Source::Config::tagtable() const {
@ -105,11 +92,7 @@ SetTagTable(const std::unordered_map<string, string> &tagtable) {
/////////////// ///////////////
//// Model //// //// Model ////
/////////////// ///////////////
Source::Model::Model(const Source::Config &config) : void Source::Parser::
config_(config) {
}
void Source::Model::
InitSyntaxHighlighting(const std::string &filepath, InitSyntaxHighlighting(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>
@ -117,7 +100,7 @@ InitSyntaxHighlighting(const std::string &filepath,
int start_offset, int start_offset,
int end_offset, int end_offset,
clang::Index *index) { clang::Index *index) {
set_project_path(project_path); 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, filepath,
@ -133,9 +116,9 @@ OnLineEdit(const std::vector<Source::Range> &locations,
} }
// Source::Model::UpdateLine // Source::Model::UpdateLine
int Source::Model:: 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);
} }
@ -151,8 +134,8 @@ GetAutoCompleteSuggestions(int line_number,
INFO("Getting auto complete suggestions"); INFO("Getting auto complete suggestions");
parsing.lock(); parsing.lock();
std::map<std::string, std::string> buffers; std::map<std::string, std::string> buffers;
notebook_->MapBuffers(&buffers); notebook.MapBuffers(&buffers);
model().GetAutoCompleteSuggestions(buffers, parser.GetAutoCompleteSuggestions(buffers,
line_number, line_number,
column, column,
suggestions); suggestions);
@ -161,14 +144,14 @@ GetAutoCompleteSuggestions(int line_number,
parsing.unlock(); parsing.unlock();
} }
void Source::Model:: void Source::Parser::
GetAutoCompleteSuggestions(const std::map<std::string, std::string> &buffers, GetAutoCompleteSuggestions(const std::map<std::string, std::string> &buffers,
int line_number, int line_number,
int column, int column,
std::vector<Source::AutoCompleteData> std::vector<Source::AutoCompleteData>
*suggestions) { *suggestions) {
clang::CodeCompleteResults results(tu_.get(), clang::CodeCompleteResults results(tu_.get(),
file_path(), file_path,
buffers, buffers,
line_number, line_number,
column); column);
@ -182,33 +165,10 @@ GetAutoCompleteSuggestions(const std::map<std::string, std::string> &buffers,
} }
} }
// sets the filepath for this mvc std::vector<std::string> Source::Parser::
void Source::Model::
set_file_path(const std::string &file_path) {
file_path_ = file_path;
}
// sets the project path for this mvc
void Source::Model::
set_project_path(const std::string &project_path) {
project_path_ = project_path;
}
// gets the file_path member
const std::string& Source::Model::file_path() const {
return file_path_;
}
// gets the project_path member
const std::string& Source::Model::project_path() const {
return project_path_;
}
// gets the config member
const Source::Config& Source::Model::config() const {
return config_;
}
std::vector<std::string> Source::Model::
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);
std::vector<clang::CompileCommand> cmds = commands.get_commands(); std::vector<clang::CompileCommand> cmds = commands.get_commands();
std::vector<std::string> arguments; std::vector<std::string> arguments;
for (auto &i : cmds) { for (auto &i : cmds) {
@ -220,11 +180,11 @@ get_compilation_commands() {
return arguments; return arguments;
} }
std::vector<Source::Range> Source::Model:: std::vector<Source::Range> Source::Parser::
ExtractTokens(int start_offset, int end_offset) { ExtractTokens(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);
clang::SourceRange range(&start, &end); clang::SourceRange range(&start, &end);
clang::Tokens tokens(tu_.get(), &range); clang::Tokens tokens(tu_.get(), &range);
std::vector<clang::Token> tks = tokens.tokens(); std::vector<clang::Token> tks = tokens.tokens();
@ -240,7 +200,7 @@ ExtractTokens(int start_offset, int end_offset) {
return ranges; return ranges;
} }
void Source::Model:: void Source::Parser::
HighlightCursor(clang::Token *token, HighlightCursor(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());
@ -256,7 +216,7 @@ HighlightCursor(clang::Token *token,
Source::Location(end_line_num, Source::Location(end_line_num,
end_offset), (int) cursor.kind()); end_offset), (int) cursor.kind());
} }
void Source::Model:: void Source::Parser::
HighlightToken(clang::Token *token, HighlightToken(clang::Token *token,
std::vector<Source::Range> *source_ranges, std::vector<Source::Range> *source_ranges,
int token_kind) { int token_kind) {
@ -279,10 +239,18 @@ 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) : Notebook::Controller &notebook) :
model_(config), notebook_(notebook) { config(config), notebook(notebook) {
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::OnKeyPress), 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()) {
buffer()->create_tag(item.first)->property_foreground() = item.second;
}
} }
Source::Controller::~Controller() { Source::Controller::~Controller() {
@ -290,29 +258,14 @@ Source::Controller::~Controller() {
parsing.unlock(); parsing.unlock();
} }
// Source::Controller::view()
// return shared_ptr to the view
Source::View& Source::Controller::view() {
return view_;
}
// Source::Controller::model()
// return shared_ptr to the model()
Source::Model& Source::Controller::model() {
return model_;
}
void Source::Controller::OnNewEmptyFile() { void Source::Controller::OnNewEmptyFile() {
string filename("/tmp/juci_t"); string filename("/tmp/juci_t");
sourcefile s(filename); sourcefile s(filename);
model().set_file_path(filename); parser.file_path=filename;
model().set_project_path(filename); parser.project_path=filename;
s.save(""); s.save("");
} }
string extract_file_path(const std::string &file_path) {
return file_path.substr(0, file_path.find_last_of('/'));
}
void Source::View::OnUpdateSyntax(const std::vector<Source::Range> &ranges, void Source::View::OnUpdateSyntax(const std::vector<Source::Range> &ranges,
const Source::Config &config) { const Source::Config &config) {
if (ranges.empty() || ranges.size() == 0) { if (ranges.empty() || ranges.size() == 0) {
@ -344,36 +297,33 @@ void Source::View::OnUpdateSyntax(const std::vector<Source::Range> &ranges,
} }
void Source::Controller::OnOpenFile(const string &filepath) { void Source::Controller::OnOpenFile(const string &filepath) {
set_file_path(filepath); parser.file_path=filepath;
sourcefile s(filepath); sourcefile s(filepath);
std::map<std::string, std::string> buffers; std::map<std::string, std::string> buffers;
notebook_->MapBuffers(&buffers); notebook.MapBuffers(&buffers);
buffers[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());
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();
view().ApplyConfig(model().config()); if (notebook.LegalExtension(filepath.substr(filepath.find_last_of(".") + 1))) {
if (notebook_->LegalExtension(filepath.substr(filepath.find_last_of(".") + 1))) { parser.InitSyntaxHighlighting(filepath,
model().InitSyntaxHighlighting(filepath, parser.file_path.substr(0, parser.file_path.find_last_of('/')),
extract_file_path(filepath),
buffers, buffers,
start_offset, start_offset,
end_offset, end_offset,
notebook_->index()); notebook.index());
view().OnUpdateSyntax(model().ExtractTokens(start_offset, end_offset), view.OnUpdateSyntax(parser.ExtractTokens(start_offset, end_offset), config);
model().config());
//OnUpdateSyntax must happen in main thread, so the parse-thread //OnUpdateSyntax must happen in main thread, so the parse-thread
//sends a signal to the main thread that it is to call the following function: //sends a signal to the main thread that it is to call the following function:
parsing_done.connect([this](){ parsing_done.connect([this](){
INFO("Updating syntax"); INFO("Updating syntax");
view(). view.
OnUpdateSyntax(model().ExtractTokens(0, buffer()->get_text().size()), OnUpdateSyntax(parser.ExtractTokens(0, buffer()->get_text().size()), config);
model().config()); INFO("Syntax updated");
INFO("Syntax updated"); });
});
buffer()->signal_end_user_action().connect([this]() { buffer()->signal_end_user_action().connect([this]() {
std::thread parse([this]() { std::thread parse([this]() {
@ -382,9 +332,9 @@ void Source::Controller::OnOpenFile(const string &filepath) {
while (true) { while (true) {
const std::string raw = buffer()->get_text().raw(); const std::string raw = buffer()->get_text().raw();
std::map<std::string, std::string> buffers; std::map<std::string, std::string> buffers;
notebook_->MapBuffers(&buffers); notebook.MapBuffers(&buffers);
buffers[model().file_path()] = raw; buffers[parser.file_path] = raw;
if (model().ReParse(buffers) == 0 && if (parser.ReParse(buffers) == 0 &&
raw == buffer()->get_text().raw()) { raw == buffer()->get_text().raw()) {
break; break;
} }
@ -400,7 +350,7 @@ void Source::Controller::OnOpenFile(const string &filepath) {
} }
Glib::RefPtr<Gsv::Buffer> Source::Controller::buffer() { Glib::RefPtr<Gsv::Buffer> Source::Controller::buffer() {
return view().get_source_buffer(); return view.get_source_buffer();
} }
bool Source::Controller::OnKeyPress(GdkEventKey* key) { bool Source::Controller::OnKeyPress(GdkEventKey* key) {
@ -411,44 +361,44 @@ 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.GetLineBeforeInsert());
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()+model().config().tab+"\n"+sm[1].str()+"}"); 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 = buffer()->get_insert()->get_iter();
for(size_t c=0;c<model().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()); view.scroll_to(buffer()->get_insert());
buffer()->place_cursor(insert_it); 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()+model().config().tab); buffer()->insert_at_cursor("\n"+sm[1].str()+config.tab);
view().scroll_to(buffer()->get_insert()); view.scroll_to(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()+model().config().tab); buffer()->insert_at_cursor("\n"+sm[1].str()+config.tab);
view().scroll_to(buffer()->get_insert()); view.scroll_to(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=buffer()->get_insert()->get_iter().get_line();
if(line_nr>0 && sm[1].str().size()>=model().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.GetLine(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());
view().scroll_to(buffer()->get_insert()); view.scroll_to(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()); buffer()->insert_at_cursor("\n"+sm2[1].str());
view().scroll_to(buffer()->get_insert()); view.scroll_to(buffer()->get_insert());
return true; return true;
} }
} }
} }
buffer()->insert_at_cursor("\n"+sm[1].str()); buffer()->insert_at_cursor("\n"+sm[1].str());
view().scroll_to(buffer()->get_insert()); view.scroll_to(buffer()->get_insert());
} }
return true; return true;
} }
@ -460,7 +410,7 @@ bool Source::Controller::OnKeyPress(GdkEventKey* key) {
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 = buffer()->get_iter_at_line(line);
buffer()->insert(line_it, model().config().tab); buffer()->insert(line_it, config.tab);
} }
return true; return true;
} }
@ -472,8 +422,8 @@ 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.GetLine(line_nr);
if(!(line.size()>=model().config().tab_size && line.substr(0, model().config().tab_size)==model().config().tab)) if(!(line.size()>=config.tab_size && line.substr(0, config.tab_size)==config.tab))
return true; return true;
} }
@ -481,7 +431,7 @@ bool Source::Controller::OnKeyPress(GdkEventKey* key) {
Gtk::TextIter line_it = buffer()->get_iter_at_line(line_nr); Gtk::TextIter line_it = 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<model().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); buffer()->erase(line_it, line_plus_it);
} }
@ -489,8 +439,8 @@ 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.GetLineBeforeInsert();
if(line.size()>=model().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;
@ -498,7 +448,7 @@ bool Source::Controller::OnKeyPress(GdkEventKey* key) {
Gtk::TextIter insert_it = buffer()->get_insert()->get_iter(); Gtk::TextIter insert_it = buffer()->get_insert()->get_iter();
Gtk::TextIter line_it = buffer()->get_iter_at_line(insert_it.get_line()); Gtk::TextIter line_it = 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<model().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); buffer()->erase(line_it, line_plus_it);
@ -510,8 +460,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.GetLine(line_nr);
string previous_line=view().GetLine(line_nr-1); string previous_line=view.GetLine(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]) {

50
juci/source.h

@ -64,7 +64,6 @@ namespace Source {
public: public:
View(); View();
virtual ~View() { } virtual ~View() { }
void ApplyConfig(const Config &config);
void OnLineEdit(const std::vector<Range> &locations, void OnLineEdit(const std::vector<Range> &locations,
const Config &config); const Config &config);
void OnUpdateSyntax(const std::vector<Range> &locations, void OnUpdateSyntax(const std::vector<Range> &locations,
@ -91,10 +90,8 @@ namespace Source {
std::vector<AutoCompleteChunk> chunks_; std::vector<AutoCompleteChunk> chunks_;
}; };
class Model{ class Parser{
public: public:
// constructor for Source::Model
explicit Model(const Source::Config &config);
// inits the syntax highligthing on file open // inits the syntax highligthing on file open
void InitSyntaxHighlighting(const std::string &filepath, void InitSyntaxHighlighting(const std::string &filepath,
const std::string &project_path, const std::string &project_path,
@ -103,31 +100,18 @@ namespace Source {
int start_offset, int start_offset,
int end_offset, int end_offset,
clang::Index *index); clang::Index *index);
// sets the filepath for this mvc
void set_file_path(const std::string &file_path);
// sets the project path for this mvc
void set_project_path(const std::string &project_path);
// gets the file_path member
const std::string& file_path() const;
// gets the project_path member
const std::string& project_path() const;
// gets the config member
const Config& config() const;
void GetAutoCompleteSuggestions(const std::map<std::string, std::string> void GetAutoCompleteSuggestions(const std::map<std::string, std::string>
&buffers, &buffers,
int line_number, int line_number,
int column, int column,
std::vector<AutoCompleteData> std::vector<AutoCompleteData>
*suggestions); *suggestions);
~Model() { }
int ReParse(const std::map<std::string, std::string> &buffers); int ReParse(const std::map<std::string, std::string> &buffers);
std::vector<Range> ExtractTokens(int, int); std::vector<Range> ExtractTokens(int, int);
std::string file_path;
std::string project_path;
private: private:
Config config_;
std::string file_path_;
std::string project_path_;
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 HighlightToken(clang::Token *token,
std::vector<Range> *source_ranges, std::vector<Range> *source_ranges,
@ -140,11 +124,9 @@ namespace Source {
class Controller { class Controller {
public: public:
Controller(const Source::Config &config, Controller(const Source::Config &config,
Notebook::Controller *notebook); Notebook::Controller &notebook);
Controller(); Controller();
~Controller(); ~Controller();
View& view();
Model& model();
void OnNewEmptyFile(); void OnNewEmptyFile();
void OnOpenFile(const std::string &filename); void OnOpenFile(const std::string &filename);
void GetAutoCompleteSuggestions(int line_number, void GetAutoCompleteSuggestions(int line_number,
@ -152,26 +134,22 @@ namespace Source {
std::vector<AutoCompleteData> std::vector<AutoCompleteData>
*suggestions); *suggestions);
Glib::RefPtr<Gsv::Buffer> buffer(); Glib::RefPtr<Gsv::Buffer> buffer();
bool is_saved() { return is_saved_; }
bool is_changed() { return is_changed_; }
std::string path() { return model().file_path(); }
void set_is_saved(bool isSaved) { is_saved_ = isSaved; }
void set_is_changed(bool isChanged) { is_changed_ = isChanged; }
void set_file_path(std::string path) { model().set_file_path(path); }
bool OnKeyPress(GdkEventKey* key); bool OnKeyPress(GdkEventKey* key);
bool is_saved = false; //TODO: Is never set to false in Notebook::Controller
bool is_changed = false; //TODO: Is never set to true
Parser parser;
View view;
private: private:
void OnLineEdit(); void OnLineEdit();
void OnSaveFile(); void OnSaveFile();
std::mutex parsing; std::mutex parsing;
Glib::Dispatcher parsing_done; Glib::Dispatcher parsing_done;
bool is_saved_ = false;
bool is_changed_ = false; const Config& config;
Notebook::Controller& notebook; //TODO: should maybe be const, but that involves a small change in libclangmm
protected:
View view_;
Model model_;
Notebook::Controller *notebook_;
}; // class Controller }; // class Controller
} // namespace Source } // namespace Source
#endif // JUCI_SOURCE_H_ #endif // JUCI_SOURCE_H_

Loading…
Cancel
Save