mirror of
https://github.com/mawww/kakoune.git
synced 2024-12-23 11:32:20 +03:00
exception: refactoring
This commit is contained in:
parent
0676eaec5c
commit
3caf962110
@ -1,14 +1,15 @@
|
||||
#include "buffer_manager.hh"
|
||||
|
||||
#include <cassert>
|
||||
|
||||
#include "buffer.hh"
|
||||
#include "window.hh"
|
||||
#include <cassert>
|
||||
#include "exception.hh"
|
||||
|
||||
namespace Kakoune
|
||||
{
|
||||
|
||||
struct name_not_unique {};
|
||||
|
||||
struct name_not_unique : logic_error {};
|
||||
|
||||
BufferManager* BufferManager::ms_instance = nullptr;
|
||||
|
||||
|
@ -37,10 +37,10 @@ static std::vector<std::string> split(const std::string& line)
|
||||
return result;
|
||||
}
|
||||
|
||||
struct command_not_found : public std::runtime_error
|
||||
struct command_not_found : runtime_error
|
||||
{
|
||||
command_not_found(const std::string& what)
|
||||
: std::runtime_error("command not found: " + what) {}
|
||||
command_not_found(const std::string& command)
|
||||
: runtime_error(command + " : no such command") {}
|
||||
};
|
||||
|
||||
void CommandManager::execute(const std::string& command_line)
|
||||
|
@ -4,17 +4,17 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
#include <stdexcept>
|
||||
#include <functional>
|
||||
|
||||
#include "exception.hh"
|
||||
|
||||
|
||||
namespace Kakoune
|
||||
{
|
||||
|
||||
struct wrong_argument_count : public std::runtime_error
|
||||
struct wrong_argument_count : runtime_error
|
||||
{
|
||||
wrong_argument_count()
|
||||
: std::runtime_error("wrong argument count") {}
|
||||
wrong_argument_count() : runtime_error("wrong argument count") {}
|
||||
};
|
||||
|
||||
typedef std::vector<std::string> CommandParameters;
|
||||
|
14
src/exception.cc
Normal file
14
src/exception.cc
Normal file
@ -0,0 +1,14 @@
|
||||
#include "exception.hh"
|
||||
|
||||
#include <string>
|
||||
#include <typeinfo>
|
||||
|
||||
namespace Kakoune
|
||||
{
|
||||
|
||||
std::string exception::description() const
|
||||
{
|
||||
return typeid(*this).name();
|
||||
}
|
||||
|
||||
}
|
32
src/exception.hh
Normal file
32
src/exception.hh
Normal file
@ -0,0 +1,32 @@
|
||||
#ifndef exception_hh_INCLUDED
|
||||
#define exception_hh_INCLUDED
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace Kakoune
|
||||
{
|
||||
|
||||
struct exception
|
||||
{
|
||||
virtual ~exception() {}
|
||||
virtual std::string description() const;
|
||||
};
|
||||
|
||||
struct runtime_error : exception
|
||||
{
|
||||
runtime_error(const std::string description)
|
||||
: m_description(description) {}
|
||||
|
||||
std::string description() const { return m_description; }
|
||||
|
||||
private:
|
||||
std::string m_description;
|
||||
};
|
||||
|
||||
struct logic_error : exception
|
||||
{
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // exception_hh_INCLUDED
|
14
src/file.cc
14
src/file.cc
@ -1,5 +1,7 @@
|
||||
#include "file.hh"
|
||||
|
||||
#include "buffer.hh"
|
||||
#include "buffer_manager.hh"
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
@ -16,9 +18,9 @@ Buffer* create_buffer_from_file(const std::string& filename)
|
||||
if (fd == -1)
|
||||
{
|
||||
if (errno == ENOENT)
|
||||
throw file_not_found(strerror(errno));
|
||||
throw file_not_found(filename);
|
||||
|
||||
throw open_file_error(strerror(errno));
|
||||
throw file_access_error(filename, strerror(errno));
|
||||
}
|
||||
|
||||
std::string content;
|
||||
@ -32,6 +34,10 @@ Buffer* create_buffer_from_file(const std::string& filename)
|
||||
content += std::string(buf, size);
|
||||
}
|
||||
close(fd);
|
||||
|
||||
if (Buffer* buffer = BufferManager::instance().get_buffer(filename))
|
||||
BufferManager::instance().delete_buffer(buffer);
|
||||
|
||||
return new Buffer(filename, content);
|
||||
}
|
||||
|
||||
@ -39,7 +45,7 @@ void write_buffer_to_file(const Buffer& buffer, const std::string& filename)
|
||||
{
|
||||
int fd = open(filename.c_str(), O_CREAT | O_WRONLY, 0644);
|
||||
if (fd == -1)
|
||||
throw open_file_error(strerror(errno));
|
||||
throw file_access_error(filename, strerror(errno));
|
||||
|
||||
const BufferString& content = buffer.content();
|
||||
ssize_t count = content.length() * sizeof(BufferChar);
|
||||
@ -52,7 +58,7 @@ void write_buffer_to_file(const Buffer& buffer, const std::string& filename)
|
||||
count -= written;
|
||||
|
||||
if (written == -1)
|
||||
throw write_file_error(strerror(errno));
|
||||
throw file_access_error(filename, strerror(errno));
|
||||
}
|
||||
close(fd);
|
||||
}
|
||||
|
23
src/file.hh
23
src/file.hh
@ -2,27 +2,24 @@
|
||||
#define file_hh_INCLUDED
|
||||
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
|
||||
#include "exception.hh"
|
||||
|
||||
namespace Kakoune
|
||||
{
|
||||
|
||||
struct open_file_error : public std::runtime_error
|
||||
struct file_access_error : runtime_error
|
||||
{
|
||||
open_file_error(const std::string& what)
|
||||
: std::runtime_error(what) {}
|
||||
public:
|
||||
file_access_error(const std::string& filename,
|
||||
const std::string& error_desc)
|
||||
: runtime_error(filename + ": " + error_desc) {}
|
||||
};
|
||||
|
||||
struct file_not_found : public open_file_error
|
||||
struct file_not_found : file_access_error
|
||||
{
|
||||
file_not_found(const std::string& what)
|
||||
: open_file_error(what) {}
|
||||
};
|
||||
|
||||
struct write_file_error : public std::runtime_error
|
||||
{
|
||||
write_file_error(const std::string& what)
|
||||
: std::runtime_error(what) {}
|
||||
file_not_found(const std::string& filename)
|
||||
: file_access_error(filename, "file not found") {}
|
||||
};
|
||||
|
||||
class Buffer;
|
||||
|
30
src/main.cc
30
src/main.cc
@ -216,10 +216,6 @@ void do_command()
|
||||
command_manager.execute(prompt(":"));
|
||||
}
|
||||
catch (prompt_aborted&) {}
|
||||
catch (std::runtime_error& err)
|
||||
{
|
||||
print_status(err.what());
|
||||
}
|
||||
}
|
||||
|
||||
bool is_blank(char c)
|
||||
@ -270,7 +266,6 @@ void do_search(Window& window)
|
||||
std::string ex = prompt("/");
|
||||
window.select(false, RegexSelector(ex));
|
||||
}
|
||||
catch (boost::regex_error&) {}
|
||||
catch (prompt_aborted&) {}
|
||||
}
|
||||
|
||||
@ -313,18 +308,25 @@ int main()
|
||||
int count = 0;
|
||||
while(not quit_requested)
|
||||
{
|
||||
char c = getch();
|
||||
|
||||
if (isdigit(c))
|
||||
count = count * 10 + c - '0';
|
||||
else
|
||||
try
|
||||
{
|
||||
if (keymap.find(c) != keymap.end())
|
||||
char c = getch();
|
||||
|
||||
if (isdigit(c))
|
||||
count = count * 10 + c - '0';
|
||||
else
|
||||
{
|
||||
keymap[c](*current_window, count);
|
||||
draw_window(*current_window);
|
||||
if (keymap.find(c) != keymap.end())
|
||||
{
|
||||
keymap[c](*current_window, count);
|
||||
draw_window(*current_window);
|
||||
}
|
||||
count = 0;
|
||||
}
|
||||
count = 0;
|
||||
}
|
||||
catch (Kakoune::runtime_error& error)
|
||||
{
|
||||
print_status(error.description());
|
||||
}
|
||||
}
|
||||
deinit_ncurses();
|
||||
|
@ -1,6 +1,5 @@
|
||||
#include "regex_selector.hh"
|
||||
|
||||
void print_status(const std::string&);
|
||||
#include "exception.hh"
|
||||
|
||||
namespace Kakoune
|
||||
{
|
||||
@ -21,7 +20,7 @@ Selection RegexSelector::operator()(const BufferIterator& cursor) const
|
||||
}
|
||||
catch (boost::regex_error& err)
|
||||
{
|
||||
print_status("regex error");
|
||||
throw runtime_error("regex error");
|
||||
}
|
||||
|
||||
return Selection(cursor, cursor);
|
||||
|
Loading…
Reference in New Issue
Block a user