Browse Source

Notebook::open now returns bool, and added a few safety checks

pipelines/235045657
eidheim 6 years ago
parent
commit
d5bf3b945f
  1. 10
      src/juci.cpp
  2. 10
      src/notebook.cpp
  3. 2
      src/notebook.hpp
  4. 52
      src/project.cpp
  5. 4
      src/terminal.cpp
  6. 8
      src/tooltips.cpp
  7. 127
      src/window.cpp
  8. 2
      tests/stubs/notebook.cpp

10
src/juci.cpp

@ -88,9 +88,9 @@ void Application::on_activate() {
}
for(size_t i = 0; i < files.size(); ++i) {
Notebook::get().open(files[i].first, files[i].second == 0 ? Notebook::Position::left : Notebook::Position::right);
if(i < file_offsets.size()) {
if(auto view = Notebook::get().get_current_view()) {
if(Notebook::get().open(files[i].first, files[i].second == 0 ? Notebook::Position::left : Notebook::Position::right)) {
if(i < file_offsets.size()) {
auto view = Notebook::get().get_current_view();
view->place_cursor_at_line_offset(file_offsets[i].first, file_offsets[i].second);
view->scroll_to_cursor_delayed(true, false);
}
@ -101,8 +101,8 @@ void Application::on_activate() {
Terminal::get().print(error, true);
if(!current_file.empty()) {
Notebook::get().open(current_file);
if(auto view = Notebook::get().get_current_view()) {
if(Notebook::get().open(current_file)) {
auto view = Notebook::get().get_current_view();
auto iter = view->get_buffer()->get_insert()->get_iter();
// To update cursor history
view->place_cursor_at_line_offset(iter.get_line(), iter.get_line_offset());

10
src/notebook.cpp

@ -102,11 +102,11 @@ std::vector<Source::View *> &Notebook::get_views() {
return source_views;
}
void Notebook::open(const boost::filesystem::path &file_path_, Position position) {
bool Notebook::open(const boost::filesystem::path &file_path_, Position position) {
boost::system::error_code ec;
if(file_path_.empty() || (boost::filesystem::exists(file_path_, ec) && !boost::filesystem::is_regular_file(file_path_, ec))) {
Terminal::get().print("Error: could not open " + file_path_.string() + "\n", true);
return;
return false;
}
auto file_path = filesystem::get_normal_path(file_path_);
@ -127,7 +127,7 @@ void Notebook::open(const boost::filesystem::path &file_path_, Position position
auto notebook_page = get_notebook_page(c);
notebooks[notebook_page.first].set_current_page(notebook_page.second);
focus_view(source_views[c]);
return;
return true;
}
}
}
@ -136,7 +136,7 @@ void Notebook::open(const boost::filesystem::path &file_path_, Position position
std::ifstream can_read(file_path.string());
if(!can_read) {
Terminal::get().print("Error: could not open " + file_path.string() + "\n", true);
return;
return false;
}
can_read.close();
}
@ -451,6 +451,8 @@ void Notebook::open(const boost::filesystem::path &file_path_, Position position
set_focus_child(*source_views.back());
focus_view(view);
return true;
}
void Notebook::open_uri(const std::string &uri) {

2
src/notebook.hpp

@ -35,7 +35,7 @@ public:
std::vector<Source::View *> &get_views();
enum class Position { left, right, infer, split };
void open(const boost::filesystem::path &file_path, Position position = Position::infer);
bool open(const boost::filesystem::path &file_path, Position position = Position::infer);
void open_uri(const std::string &uri);
void configure(size_t index);
bool save(size_t index);

52
src/project.cpp

@ -252,10 +252,11 @@ void Project::Base::show_symbols() {
boost::system::error_code ec;
if(!boost::filesystem::is_regular_file(full_path, ec))
return;
Notebook::get().open(full_path);
auto view = Notebook::get().get_current_view();
view->place_cursor_at_line_index(offset.line, offset.index);
view->scroll_to_cursor_delayed(true, false);
if(Notebook::get().open(full_path)) {
auto view = Notebook::get().get_current_view();
view->place_cursor_at_line_index(offset.line, offset.index);
view->scroll_to_cursor_delayed(true, false);
}
};
if(view)
view->hide_tooltips();
@ -458,10 +459,11 @@ void Project::LLDB::debug_start() {
debug_update_stop();
if(Config::get().source.debug_place_cursor_at_stop && !stop_path.empty()) {
Notebook::get().open(stop_path);
auto view = Notebook::get().get_current_view();
view->place_cursor_at_line_index(stop_line, stop_column);
view->scroll_to_cursor_delayed(true, false);
if(Notebook::get().open(stop_path)) {
auto view = Notebook::get().get_current_view();
view->place_cursor_at_line_index(stop_line, stop_column);
view->scroll_to_cursor_delayed(true, false);
}
}
else if(auto view = Notebook::get().get_current_view())
view->get_buffer()->place_cursor(view->get_buffer()->get_insert()->get_iter());
@ -558,10 +560,9 @@ void Project::LLDB::debug_backtrace() {
return;
auto frame = rows[index];
if(!frame.file_path.empty()) {
Notebook::get().open(frame.file_path);
if(auto view = Notebook::get().get_current_view()) {
if(Notebook::get().open(frame.file_path)) {
Debug::LLDB::get().select_frame(frame.index);
auto view = Notebook::get().get_current_view();
view->place_cursor_at_line_index(frame.line_nr - 1, frame.line_index - 1);
view->scroll_to_cursor_delayed(true, true);
}
@ -601,8 +602,8 @@ void Project::LLDB::debug_show_variables() {
auto variable = (*rows)[index];
Debug::LLDB::get().select_frame(variable.frame_index, variable.thread_index_id);
if(!variable.file_path.empty()) {
Notebook::get().open(variable.file_path);
if(auto view = Notebook::get().get_current_view()) {
if(Notebook::get().open(variable.file_path)) {
auto view = Notebook::get().get_current_view();
view->place_cursor_at_line_index(variable.line_nr - 1, variable.line_index - 1);
view->scroll_to_cursor_delayed(true, true);
}
@ -805,10 +806,11 @@ void Project::LanguageProtocol::show_symbols() {
boost::system::error_code ec;
if(!boost::filesystem::is_regular_file(location.file, ec))
return;
Notebook::get().open(location.file);
auto view = Notebook::get().get_current_view();
view->place_cursor_at_line_offset(location.range.start.line, location.range.start.character);
view->scroll_to_cursor_delayed(true, false);
if(Notebook::get().open(location.file)) {
auto view = Notebook::get().get_current_view();
view->place_cursor_at_line_offset(location.range.start.line, location.range.start.character);
view->scroll_to_cursor_delayed(true, false);
}
};
if(view)
@ -964,11 +966,13 @@ void Project::Clang::recreate_build() {
void Project::Markdown::compile_and_run() {
auto command = Config::get().project.markdown_command + ' ' + filesystem::escape_argument(filesystem::get_short_path(Notebook::get().get_current_view()->file_path).string());
Terminal::get().async_process(command, "", [command](int exit_status) {
if(exit_status == 127)
Terminal::get().async_print("Error: executable not found: " + command + "\n", true);
}, true);
if(auto view = Notebook::get().get_current_view()) {
auto command = Config::get().project.markdown_command + ' ' + filesystem::escape_argument(filesystem::get_short_path(view->file_path).string());
Terminal::get().async_process(command, "", [command](int exit_status) {
if(exit_status == 127)
Terminal::get().async_print("Error: executable not found: " + command + "\n", true);
}, true);
}
}
void Project::Python::compile_and_run() {
@ -1035,8 +1039,8 @@ void Project::HTML::compile_and_run() {
Terminal::get().async_print(command + " returned: " + std::to_string(exit_status) + '\n');
});
}
else
Notebook::get().open_uri(std::string("file://") + Notebook::get().get_current_view()->file_path.string());
else if(auto view = Notebook::get().get_current_view())
Notebook::get().open_uri(std::string("file://") + view->file_path.string());
}
std::pair<std::string, std::string> Project::Rust::get_run_arguments() {

4
src/terminal.cpp

@ -378,11 +378,11 @@ bool Terminal::on_button_press_event(GdkEventButton *button_event) {
}
boost::system::error_code ec;
if(boost::filesystem::is_regular_file(path, ec)) {
Notebook::get().open(path);
if(auto view = Notebook::get().get_current_view()) {
if(Notebook::get().open(path)) {
try {
int line_int = std::stoi(line) - 1;
int index_int = std::stoi(index) - 1;
auto view = Notebook::get().get_current_view();
view->place_cursor_at_line_index(line_int, index_int);
view->scroll_to_cursor_delayed(true, true);
return true;

8
src/tooltips.cpp

@ -167,11 +167,11 @@ void Tooltip::show(bool disregard_drawn, const std::function<void()> &on_motion)
boost::system::error_code ec;
if(boost::filesystem::is_regular_file(path, ec)) {
Notebook::get().open(path);
if(auto view = Notebook::get().get_current_view()) {
if(Notebook::get().open(path)) {
try {
auto line = std::stoi(sm[2].str()) - 1;
auto offset = std::stoi(sm[3].str()) - 1;
auto view = Notebook::get().get_current_view();
view->place_cursor_at_line_offset(line, offset);
view->scroll_to_cursor_delayed(true, false);
}
@ -186,10 +186,8 @@ void Tooltip::show(bool disregard_drawn, const std::function<void()> &on_motion)
if(auto view = dynamic_cast<Source::View *>(text_view))
path = filesystem::get_normal_path(view->file_path.parent_path() / path);
boost::system::error_code ec;
if(boost::filesystem::is_regular_file(path, ec)) {
Notebook::get().open(path);
if(boost::filesystem::is_regular_file(path, ec) && Notebook::get().open(path))
return true;
}
Info::get().print("Could not open: " + link);
}
}

127
src/window.cpp

@ -486,7 +486,7 @@ void Window::set_menu_actions() {
if(!Directories::get().path.empty())
Directories::get().update();
Notebook::get().open(path);
Terminal::get().print("File saved to: " + Notebook::get().get_current_view()->file_path.string() + "\n");
Terminal::get().print("File saved to: " + filesystem::get_short_path(filesystem::get_normal_path(path)).string() + "\n");
}
else
Terminal::get().print("Error saving file\n", true);
@ -740,8 +740,8 @@ void Window::set_menu_actions() {
if(pos != std::string::npos)
postfix = diff_details.substr(0, pos + 2);
}
Notebook::get().open(view->file_path.string() + postfix + ".diff");
if(auto new_view = Notebook::get().get_current_view()) {
if(Notebook::get().open(view->file_path.string() + postfix + ".diff")) {
auto new_view = Notebook::get().get_current_view();
if(new_view->get_buffer()->get_text().empty()) {
new_view->get_source_buffer()->begin_not_undoable_action();
new_view->get_buffer()->set_text(diff_details);
@ -873,10 +873,11 @@ void Window::set_menu_actions() {
}
SelectionDialog::get()->on_select = [grep](unsigned int index, const std::string &text, bool hide_window) {
auto location = grep->get_location(text, false, true);
Notebook::get().open(grep->project_path / location.file_path);
auto view = Notebook::get().get_current_view();
view->place_cursor_at_line_offset(location.line, location.offset);
view->scroll_to_cursor_delayed(true, false);
if(Notebook::get().open(grep->project_path / location.file_path)) {
auto view = Notebook::get().get_current_view();
view->place_cursor_at_line_offset(location.line, location.offset);
view->scroll_to_cursor_delayed(true, false);
}
};
SelectionDialog::get()->show();
}
@ -956,9 +957,10 @@ void Window::set_menu_actions() {
SelectionDialog::get()->on_select = [paths = std::move(paths)](unsigned int index, const std::string &text, bool hide_window) {
if(index >= paths.size())
return;
Notebook::get().open(paths[index]);
if(auto view = Notebook::get().get_current_view())
if(Notebook::get().open(paths[index])) {
auto view = Notebook::get().get_current_view();
view->hide_tooltips();
}
};
if(view)
@ -982,14 +984,15 @@ void Window::set_menu_actions() {
boost::system::error_code ec;
if(!boost::filesystem::is_regular_file(offset.file_path, ec))
return;
Notebook::get().open(offset.file_path);
auto view = Notebook::get().get_current_view();
auto iter = view->get_iter_at_line_pos(offset.line, offset.index);
view->get_buffer()->insert(iter, std::get<1>(documentation_template));
iter = view->get_iter_at_line_pos(offset.line, offset.index);
iter.forward_chars(std::get<2>(documentation_template));
view->get_buffer()->place_cursor(iter);
view->scroll_to_cursor_delayed(true, false);
if(Notebook::get().open(offset.file_path)) {
auto view = Notebook::get().get_current_view();
auto iter = view->get_iter_at_line_pos(offset.line, offset.index);
view->get_buffer()->insert(iter, std::get<1>(documentation_template));
iter = view->get_iter_at_line_pos(offset.line, offset.index);
iter.forward_chars(std::get<2>(documentation_template));
view->get_buffer()->place_cursor(iter);
view->scroll_to_cursor_delayed(true, false);
}
}
}
}
@ -1041,12 +1044,13 @@ void Window::set_menu_actions() {
boost::system::error_code ec;
if(!boost::filesystem::is_regular_file(location.file_path, ec))
return;
Notebook::get().open(location.file_path);
auto view = Notebook::get().get_current_view();
auto line = static_cast<int>(location.line);
auto index = static_cast<int>(location.index);
view->place_cursor_at_line_pos(line, index);
view->scroll_to_cursor_delayed(true, false);
if(Notebook::get().open(location.file_path)) {
auto view = Notebook::get().get_current_view();
auto line = static_cast<int>(location.line);
auto index = static_cast<int>(location.index);
view->place_cursor_at_line_pos(line, index);
view->scroll_to_cursor_delayed(true, false);
}
}
}
}
@ -1059,12 +1063,13 @@ void Window::set_menu_actions() {
boost::system::error_code ec;
if(!boost::filesystem::is_regular_file(location.file_path, ec))
return;
Notebook::get().open(location.file_path);
auto view = Notebook::get().get_current_view();
auto line = static_cast<int>(location.line);
auto index = static_cast<int>(location.index);
view->place_cursor_at_line_pos(line, index);
view->scroll_to_cursor_delayed(true, false);
if(Notebook::get().open(location.file_path)) {
auto view = Notebook::get().get_current_view();
auto line = static_cast<int>(location.line);
auto index = static_cast<int>(location.index);
view->place_cursor_at_line_pos(line, index);
view->scroll_to_cursor_delayed(true, false);
}
}
}
}
@ -1094,12 +1099,13 @@ void Window::set_menu_actions() {
boost::system::error_code ec;
if(!boost::filesystem::is_regular_file(location.file_path, ec))
return;
Notebook::get().open(location.file_path);
auto view = Notebook::get().get_current_view();
auto line = static_cast<int>(location.line);
auto index = static_cast<int>(location.index);
view->place_cursor_at_line_pos(line, index);
view->scroll_to_cursor_delayed(true, false);
if(Notebook::get().open(location.file_path)) {
auto view = Notebook::get().get_current_view();
auto line = static_cast<int>(location.line);
auto index = static_cast<int>(location.index);
view->place_cursor_at_line_pos(line, index);
view->scroll_to_cursor_delayed(true, false);
}
return;
}
SelectionDialog::get()->on_select = [rows = std::move(rows)](unsigned int index, const std::string &text, bool hide_window) {
@ -1109,10 +1115,11 @@ void Window::set_menu_actions() {
boost::system::error_code ec;
if(!boost::filesystem::is_regular_file(location.file_path, ec))
return;
Notebook::get().open(location.file_path);
auto view = Notebook::get().get_current_view();
view->place_cursor_at_line_pos(location.line, location.index);
view->scroll_to_cursor_delayed(true, false);
if(Notebook::get().open(location.file_path)) {
auto view = Notebook::get().get_current_view();
view->place_cursor_at_line_pos(location.line, location.index);
view->scroll_to_cursor_delayed(true, false);
}
};
view->hide_tooltips();
SelectionDialog::get()->show();
@ -1168,10 +1175,11 @@ void Window::set_menu_actions() {
boost::system::error_code ec;
if(!boost::filesystem::is_regular_file(offset.file_path, ec))
return;
Notebook::get().open(offset.file_path);
auto view = Notebook::get().get_current_view();
view->place_cursor_at_line_pos(offset.line, offset.index);
view->scroll_to_cursor_delayed(true, false);
if(Notebook::get().open(offset.file_path)) {
auto view = Notebook::get().get_current_view();
view->place_cursor_at_line_pos(offset.line, offset.index);
view->scroll_to_cursor_delayed(true, false);
}
};
view->hide_tooltips();
SelectionDialog::get()->show();
@ -1182,7 +1190,7 @@ void Window::set_menu_actions() {
menu.add_action("source_goto_method", []() {
if(auto view = Notebook::get().get_current_view()) {
if(view->get_methods) {
auto methods = Notebook::get().get_current_view()->get_methods();
auto methods = view->get_methods();
if(!methods.empty()) {
SelectionDialog::create(view, true, true);
std::vector<Source::Offset> rows;
@ -1257,10 +1265,11 @@ void Window::set_menu_actions() {
for(auto &fix_it : fix_its) {
auto view = current_view;
if(fix_it.path != current_view->file_path) {
Notebook::get().open(fix_it.path);
view = Notebook::get().get_current_view();
if(CompileCommands::is_header(view->file_path))
header_views.emplace(view);
if(Notebook::get().open(fix_it.path)) {
view = Notebook::get().get_current_view();
if(CompileCommands::is_header(view->file_path))
header_views.emplace(view);
}
}
auto start_iter = view->get_iter_at_line_pos(fix_it.offsets.first.line, fix_it.offsets.first.index);
auto end_iter = view->get_iter_at_line_pos(fix_it.offsets.second.line, fix_it.offsets.second.index);
@ -1557,10 +1566,11 @@ void Window::set_menu_actions() {
SelectionDialog::get()->on_select = [rows = std::move(rows)](unsigned int index, const std::string &text, bool hide_window) {
if(index >= rows.size())
return;
Notebook::get().open(rows[index].file_path);
auto view = Notebook::get().get_current_view();
view->place_cursor_at_line_pos(rows[index].line, rows[index].index);
view->scroll_to_cursor_delayed(true, false);
if(Notebook::get().open(rows[index].file_path)) {
auto view = Notebook::get().get_current_view();
view->place_cursor_at_line_pos(rows[index].line, rows[index].index);
view->scroll_to_cursor_delayed(true, false);
}
};
SelectionDialog::get()->show();
@ -1568,10 +1578,10 @@ void Window::set_menu_actions() {
menu.add_action("debug_goto_stop", []() {
if(Project::debugging) {
if(!Project::debug_stop.first.empty()) {
Notebook::get().open(Project::debug_stop.first);
if(auto view = Notebook::get().get_current_view()) {
if(Notebook::get().open(Project::debug_stop.first)) {
int line = Project::debug_stop.second.first;
int index = Project::debug_stop.second.second;
auto view = Notebook::get().get_current_view();
view->place_cursor_at_line_index(line, index);
view->scroll_to_cursor_delayed(true, true);
}
@ -1600,10 +1610,11 @@ void Window::set_menu_actions() {
auto iter = view->get_buffer()->get_insert()->get_iter();
Notebook::get().open(view->file_path, Notebook::Position::split);
auto new_view = Notebook::get().get_current_view();
new_view->place_cursor_at_line_offset(iter.get_line(), iter.get_line_offset());
new_view->scroll_to_cursor_delayed(true, false);
if(Notebook::get().open(view->file_path, Notebook::Position::split)) {
auto new_view = Notebook::get().get_current_view();
new_view->place_cursor_at_line_offset(iter.get_line(), iter.get_line_offset());
new_view->scroll_to_cursor_delayed(true, false);
}
});
menu.add_action("window_toggle_full_screen", [this] {
if(this->get_window()->get_state() & Gdk::WindowState::WINDOW_STATE_FULLSCREEN)

2
tests/stubs/notebook.cpp

@ -6,6 +6,6 @@ Source::View *Notebook::get_current_view() {
return nullptr;
}
void Notebook::open(const boost::filesystem::path &file_path, Position position) {}
bool Notebook::open(const boost::filesystem::path &file_path, Position position) { return true; }
void Notebook::open_uri(const std::string &uri) {}

Loading…
Cancel
Save