Browse Source

Cleanup of Reload file

merge-requests/365/head
eidheim 9 years ago
parent
commit
bea112e22e
  1. 2
      CMakeLists.txt
  2. 1
      src/files.h
  3. 65
      src/notebook.cc
  4. 2
      src/notebook.h
  5. 35
      src/source.cc
  6. 1
      src/source.h
  7. 33
      src/window.cc

2
CMakeLists.txt

@ -1,7 +1,7 @@
cmake_minimum_required (VERSION 2.8.8)
project(juci)
set(JUCI_VERSION "1.2.1.7")
set(JUCI_VERSION "1.2.1.8")
set(CPACK_PACKAGE_NAME "jucipp")
set(CPACK_PACKAGE_CONTACT "Ole Christian Eidheim <eidheim@gmail.com>")

1
src/files.h

@ -86,6 +86,7 @@ R"RAW(
"new_folder": "<primary><shift>n",
"open_file": "<primary>o",
"open_folder": "<primary><shift>o",
"reload_file": "",
"save": "<primary>s",
"save_as": "<primary><shift>s",
"print": "<primary>p",

65
src/notebook.cc

@ -128,38 +128,6 @@ std::vector<Source::View*> &Notebook::get_views() {
return source_views;
}
void Notebook::reload(const boost::filesystem::path &file_path, size_t notebook_index) {
if(boost::filesystem::exists(file_path)) {
std::ifstream can_read(file_path.string());
if(!can_read) {
Terminal::get().print("Error: could not open "+file_path.string()+"\n", true);
return;
}
can_read.close();
}
auto last_view=get_current_view();
int offset = last_view->get_buffer()->get_insert()->get_iter().get_offset();
int line = last_view->get_buffer()->get_insert()->get_iter().get_line();
auto language=Source::guess_language(file_path);
last_view->get_buffer()->erase(last_view->get_buffer()->begin(), last_view->get_buffer()->end());
last_view->get_source_buffer()->begin_not_undoable_action();
if(language) {
if(filesystem::read_non_utf8(file_path, last_view->get_buffer())==-1)
Terminal::get().print("Warning: "+file_path.string()+" is not a valid UTF-8 file. Saving might corrupt the file.\n");
}
else {
if(filesystem::read(file_path, last_view->get_buffer())==-1)
Terminal::get().print("Error: "+file_path.string()+" is not a valid UTF-8 file.\n", true);
}
last_view->get_source_buffer()->end_not_undoable_action();
last_view->place_cursor_at_line_offset(line, offset);
last_view->get_buffer()->set_modified(true);
}
void Notebook::open(const boost::filesystem::path &file_path, size_t notebook_index) {
if(notebook_index==1 && !split)
toggle_split();
@ -552,20 +520,7 @@ bool Notebook::close(size_t index) {
if(on_close_page)
on_close_page(view);
size_t cursor_locations_index=0;
for(auto it=cursor_locations.begin();it!=cursor_locations.end();) {
if(it->view==view) {
it=cursor_locations.erase(it);
if(current_cursor_location!=static_cast<size_t>(-1) && current_cursor_location>cursor_locations_index)
--current_cursor_location;
}
else {
++it;
++cursor_locations_index;
}
}
if(current_cursor_location>=cursor_locations.size())
current_cursor_location=cursor_locations.size()-1;
delete_cursor_locations(view);
if(auto clang_view=dynamic_cast<Source::ClangView*>(view))
clang_view->async_delete();
@ -579,6 +534,24 @@ bool Notebook::close(size_t index) {
return true;
}
void Notebook::delete_cursor_locations(Source::View *view) {
size_t cursor_locations_index=0;
for(auto it=cursor_locations.begin();it!=cursor_locations.end();) {
if(it->view==view) {
view->get_buffer()->delete_mark(it->mark);
it=cursor_locations.erase(it);
if(current_cursor_location!=static_cast<size_t>(-1) && current_cursor_location>cursor_locations_index)
--current_cursor_location;
}
else {
++it;
++cursor_locations_index;
}
}
if(current_cursor_location>=cursor_locations.size())
current_cursor_location=cursor_locations.size()-1;
}
bool Notebook::close_current() {
return close(get_index(get_current_view()));
}

2
src/notebook.h

@ -37,7 +37,6 @@ public:
std::vector<Source::View*> &get_views();
void open(const boost::filesystem::path &file_path, size_t notebook_index=-1);
void reload(const boost::filesystem::path &file_path, size_t notebook_index=-1);
void configure(size_t index);
bool save(size_t index);
bool save_current();
@ -64,6 +63,7 @@ public:
std::vector<CursorLocation> cursor_locations;
size_t current_cursor_location=-1;
bool disable_next_update_cursor_locations=false;
void delete_cursor_locations(Source::View *view);
private:
size_t get_index(Source::View *view);

35
src/source.cc

@ -90,16 +90,7 @@ const std::regex Source::View::no_bracket_statement_regex("^([ \\t]*)(if|for|els
const std::regex Source::View::no_bracket_no_para_statement_regex("^([ \\t]*)(else) *$");
Source::View::View(const boost::filesystem::path &file_path, Glib::RefPtr<Gsv::Language> language): Gsv::View(), SpellCheckView(), DiffView(file_path), language(language), status_diagnostics(0, 0, 0) {
get_source_buffer()->begin_not_undoable_action();
if(language) {
if(filesystem::read_non_utf8(file_path, get_buffer())==-1)
Terminal::get().print("Warning: "+file_path.string()+" is not a valid UTF-8 file. Saving might corrupt the file.\n");
}
else {
if(filesystem::read(file_path, get_buffer())==-1)
Terminal::get().print("Error: "+file_path.string()+" is not a valid UTF-8 file.\n", true);
}
get_source_buffer()->end_not_undoable_action();
load();
get_buffer()->place_cursor(get_buffer()->get_iter_at_offset(0));
@ -343,6 +334,30 @@ Source::View::View(const boost::filesystem::path &file_path, Glib::RefPtr<Gsv::L
}
}
bool Source::View::load() {
get_source_buffer()->begin_not_undoable_action();
get_buffer()->erase(get_buffer()->begin(), get_buffer()->end());
bool status=true;
if(language) {
if(filesystem::read_non_utf8(file_path, get_buffer())==-1)
Terminal::get().print("Warning: "+file_path.string()+" is not a valid UTF-8 file. Saving might corrupt the file.\n");
}
else {
if(filesystem::read(file_path, get_buffer())==-1) {
Terminal::get().print("Error: "+file_path.string()+" is not a valid UTF-8 file.\n", true);
status=false;
}
}
get_source_buffer()->end_not_undoable_action();
boost::system::error_code ec;
last_write_time=boost::filesystem::last_write_time(file_path, ec);
if(ec)
last_write_time=static_cast<std::time_t>(-1);
return status;
}
void Source::View::rename(const boost::filesystem::path &path) {
{
std::unique_lock<std::mutex> lock(file_path_mutex);

1
src/source.h

@ -44,6 +44,7 @@ namespace Source {
View(const boost::filesystem::path &file_path, Glib::RefPtr<Gsv::Language> language);
~View();
bool load();
void rename(const boost::filesystem::path &path);
virtual bool save(const std::vector<Source::View*> &views);

33
src/window.cc

@ -348,8 +348,36 @@ void Window::set_menu_actions() {
});
menu.add_action("reload_file", [this]() {
auto path = Notebook::get().get_current_view()->file_path;
Notebook::get().reload(path);
if(auto view=Notebook::get().get_current_view()) {
Gtk::MessageDialog dialog(*static_cast<Gtk::Window*>(get_toplevel()), "Reload file!", false, Gtk::MESSAGE_QUESTION, Gtk::BUTTONS_YES_NO);
dialog.set_default_response(Gtk::RESPONSE_YES);
dialog.set_secondary_text("Do you want to reload: " + view->file_path.string()+" ? The current buffer will be lost.");
int result = dialog.run();
if(result==Gtk::RESPONSE_YES) {
if(boost::filesystem::exists(view->file_path)) {
std::ifstream can_read(view->file_path.string());
if(!can_read) {
Terminal::get().print("Error: could not read "+view->file_path.string()+"\n", true);
return;
}
can_read.close();
}
else {
Terminal::get().print("Error: "+view->file_path.string()+" does not exist\n", true);
return;
}
int line = view->get_buffer()->get_insert()->get_iter().get_line();
int offset = view->get_buffer()->get_insert()->get_iter().get_line_offset();
view->load();
while(Gtk::Main::events_pending())
Gtk::Main::iteration(false);
Notebook::get().delete_cursor_locations(view);
view->place_cursor_at_line_offset(line, offset);
view->scroll_to_cursor_delayed(view, true, false);
view->get_buffer()->set_modified(false);
}
}
});
menu.add_action("save", [this]() {
@ -1107,6 +1135,7 @@ void Window::activate_menu_items() {
auto &menu = Menu::get();
auto view=Notebook::get().get_current_view();
menu.actions["reload_file"]->set_enabled(view);
menu.actions["source_spellcheck"]->set_enabled(view);
menu.actions["source_spellcheck_clear"]->set_enabled(view);
menu.actions["source_spellcheck_next_error"]->set_enabled(view);

Loading…
Cancel
Save