Browse Source

More code cleanup. Removed dependency notebook.h from the Source-classes. Support for other languages than C++ can soon be added by implementing an interface of Source::Parser, but this is not something I will prioritize.

merge-requests/365/head
eidheim 11 years ago
parent
commit
81229a287c
  1. 27
      juci/notebook.cc
  2. 3
      juci/notebook.h
  3. 56
      juci/source.cc
  4. 18
      juci/source.h

27
juci/notebook.cc

@ -18,8 +18,7 @@ Notebook::Controller::Controller(Gtk::Window* window,
Source::Config& source_cfg,
Directories::Config& dir_cfg) :
directories_(dir_cfg),
source_config_(source_cfg),
index_(0, 1) {
source_config_(source_cfg) {
INFO("Create notebook");
window_ = window;
OnNewPage("untitled");
@ -193,7 +192,7 @@ bool Notebook::Controller::OnKeyRelease(GdkEventKey* key) {
bool Notebook::Controller::GeneratePopup(int key_id) {
INFO("Notebook genereate popup, getting iters");
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
Gtk::TextIter beg = CurrentTextView().get_buffer()->get_insert()->get_iter();
Gtk::TextIter end = CurrentTextView().get_buffer()->get_insert()->get_iter();
@ -312,14 +311,6 @@ 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) {
INFO("Notebook open file");
OnCreatePage();
@ -334,7 +325,7 @@ void Notebook::Controller::OnOpenFile(std::string path) {
void Notebook::Controller::OnCreatePage() {
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());
editor_vec_.push_back(new Gtk::HBox());
scrolledtext_vec_.back()->add(text_vec_.back()->view);
@ -674,15 +665,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 OnCreatePage();
bool ScrollEventCallback(GdkEventScroll* scroll_event);
void MapBuffers(std::map<std::string, std::string> *buffers) const;
clang::Index* index() { return &index_; }
int Pages();
Directories::Controller& directories() { return directories_; }
Gtk::Paned& view();
@ -111,7 +109,6 @@ namespace Notebook {
bool ispopup;
Gtk::Dialog popup_;
Gtk::Window* window_;
clang::Index index_;
}; // class controller
} // namespace Notebook
#endif // JUCI_NOTEBOOK_H_

56
juci/source.cc

@ -3,7 +3,6 @@
#include <boost/property_tree/json_parser.hpp>
#include <fstream>
#include <boost/timer/timer.hpp>
#include "notebook.h"
#include "logging.h"
#include <algorithm>
#include <regex>
@ -89,9 +88,21 @@ SetTagTable(const std::unordered_map<string, string> &tagtable) {
tagtable_ = tagtable;
}
bool Source::Config::legal_extension(std::string e) const {
std::transform(e.begin(), e.end(),e.begin(), ::tolower);
if (find(extensiontable_.begin(), extensiontable_.end(), e) != extensiontable_.end()) {
DEBUG("Legal extension");
return true;
}
DEBUG("Ilegal extension");
return false;
}
///////////////
//// Model ////
//// Parser ///
///////////////
clang::Index Source::Parser::clang_index(0, 1);
void Source::Parser::
InitSyntaxHighlighting(const std::string &filepath,
const std::string &project_path,
@ -108,6 +119,16 @@ InitSyntaxHighlighting(const std::string &filepath,
buffers));
}
std::map<std::string, std::string> Source::Parser::
get_buffer_map() const {
std::map<std::string, std::string> buffer_map;
for (auto &controller : controllers) {
buffer_map.operator[](controller->parser.file_path) =
controller->buffer()->get_text().raw();
}
return buffer_map;
}
// Source::View::UpdateLine
void Source::View::
OnLineEdit(const std::vector<Source::Range> &locations,
@ -133,9 +154,7 @@ GetAutoCompleteSuggestions(int line_number,
*suggestions) {
INFO("Getting auto complete suggestions");
parsing.lock();
std::map<std::string, std::string> buffers;
notebook.MapBuffers(&buffers);
parser.GetAutoCompleteSuggestions(buffers,
parser.GetAutoCompleteSuggestions(parser.get_buffer_map(),
line_number,
column,
suggestions);
@ -239,8 +258,8 @@ HighlightToken(clang::Token *token,
// Source::Controller::Controller()
// Constructor for Controller
Source::Controller::Controller(const Source::Config &config,
Notebook::Controller &notebook) :
config(config), notebook(notebook), parse_thread_go(false), parse_thread_mapped(false) {
std::vector<std::unique_ptr<Source::Controller> > &controllers) :
config(config), parser(controllers), parse_thread_go(false), parse_thread_mapped(false) {
INFO("Source Controller with childs constructed");
view.signal_key_press_event().connect(sigc::mem_fun(*this, &Source::Controller::OnKeyPress), false);
view.set_smart_home_end(Gsv::SMART_HOME_END_BEFORE);
@ -299,30 +318,29 @@ void Source::View::OnUpdateSyntax(const std::vector<Source::Range> &ranges,
void Source::Controller::OnOpenFile(const string &filepath) {
parser.file_path=filepath;
sourcefile s(filepath);
std::map<std::string, std::string> buffers;
notebook.MapBuffers(&buffers);
buffers[filepath] = s.get_content();
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();
int start_offset = buffer()->begin().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.file_path.substr(0, parser.file_path.find_last_of('/')),
buffers,
buffer_map,
start_offset,
end_offset,
notebook.index());
&Parser::clang_index);
view.OnUpdateSyntax(parser.ExtractTokens(start_offset, end_offset), config);
//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_buffers_mutex.try_lock()) {
notebook.MapBuffers(&this->parse_thread_buffers);
if(parse_thread_buffer_map_mutex.try_lock()) {
this->parse_thread_buffer_map=parser.get_buffer_map();
parse_thread_mapped=true;
parse_thread_buffers_mutex.unlock();
parse_thread_buffer_map_mutex.unlock();
}
parse_thread_go=true;
});
@ -346,11 +364,11 @@ void Source::Controller::OnOpenFile(const string &filepath) {
parse_thread_go=false;
parse_start();
}
else if (parse_thread_mapped && parsing.try_lock() && parse_thread_buffers_mutex.try_lock()) {
parser.ReParse(this->parse_thread_buffers);
else if (parse_thread_mapped && parsing.try_lock() && parse_thread_buffer_map_mutex.try_lock()) {
parser.ReParse(this->parse_thread_buffer_map);
parse_thread_go=false;
parsing.unlock();
parse_thread_buffers_mutex.unlock();
parse_thread_buffer_map_mutex.unlock();
parse_done();
}
}

18
juci/source.h

@ -29,6 +29,7 @@ namespace Source {
void InsertType(const std::string &key, const std::string &value);
void InsertExtension(const std::string &ext);
std::vector<std::string> extensiontable_;
bool legal_extension(std::string e) const ;
// TODO: Have to clean away all the simple setter and getter methods at some point. It creates too much unnecessary code
unsigned tab_size;
bool show_line_numbers, highlight_current_line;
@ -90,9 +91,13 @@ namespace Source {
chunks_(chunks) { }
std::vector<AutoCompleteChunk> chunks_;
};
class Controller;
class Parser{
public:
Parser(std::vector<std::unique_ptr<Source::Controller> > &controllers):
controllers(controllers) {}
// inits the syntax highligthing on file open
void InitSyntaxHighlighting(const std::string &filepath,
const std::string &project_path,
@ -112,6 +117,8 @@ namespace Source {
std::string file_path;
std::string project_path;
static clang::Index clang_index;
std::map<std::string, std::string> get_buffer_map() const;
private:
std::unique_ptr<clang::TranslationUnit> tu_; //use unique_ptr since it is not initialized in constructor
void HighlightToken(clang::Token *token,
@ -120,13 +127,14 @@ namespace Source {
void HighlightCursor(clang::Token *token,
std::vector<Range> *source_ranges);
std::vector<std::string> get_compilation_commands();
//controllers is needed here, no way around that I think
std::vector<std::unique_ptr<Source::Controller> > &controllers;
};
class Controller {
public:
Controller(const Source::Config &config,
Notebook::Controller &notebook);
Controller();
std::vector<std::unique_ptr<Source::Controller> > &controllers);
~Controller();
void OnNewEmptyFile();
void OnOpenFile(const std::string &filename);
@ -136,6 +144,7 @@ namespace Source {
*suggestions);
Glib::RefPtr<Gsv::Buffer> buffer();
bool OnKeyPress(GdkEventKey* key);
bool LegalExtension(std::string e);
bool is_saved = false; //TODO: Is never set to false in Notebook::Controller
bool is_changed = false; //TODO: Is never set to true
@ -149,13 +158,12 @@ namespace Source {
std::mutex parsing;
Glib::Dispatcher parse_done;
Glib::Dispatcher parse_start;
std::map<std::string, std::string> parse_thread_buffers;
std::mutex parse_thread_buffers_mutex;
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;
const Config& config;
Notebook::Controller& notebook; //TODO: should maybe be const, but that involves a small change in libclangmm
}; // class Controller
} // namespace Source
#endif // JUCI_SOURCE_H_

Loading…
Cancel
Save