Browse Source

Now opens files that are not UTF-8 valid, if a language is found for the file, that is, this does not include binary files.

merge-requests/365/head
eidheim 10 years ago
parent
commit
6d7ddd6235
  1. 31
      src/source.cc
  2. 8
      src/source.h
  3. 38
      src/sourcefile.cc
  4. 3
      src/sourcefile.h

31
src/source.cc

@ -48,13 +48,22 @@ Glib::RefPtr<Gsv::Language> Source::guess_language(const boost::filesystem::path
////////////// //////////////
AspellConfig* Source::View::spellcheck_config=NULL; AspellConfig* Source::View::spellcheck_config=NULL;
Source::View::View(const boost::filesystem::path &file_path): file_path(file_path) { Source::View::View(const boost::filesystem::path &file_path, Glib::RefPtr<Gsv::Language> language): file_path(file_path) {
set_smart_home_end(Gsv::SMART_HOME_END_BEFORE); set_smart_home_end(Gsv::SMART_HOME_END_BEFORE);
get_source_buffer()->begin_not_undoable_action(); get_source_buffer()->begin_not_undoable_action();
if(juci::filesystem::read(file_path, get_buffer())==-1) if(language) {
Singleton::terminal()->print("Error: "+file_path.string()+" is not a valid UTF-8 file."); if(juci::filesystem::read_non_utf8(file_path, get_buffer())==-1)
Singleton::terminal()->print("Warning: "+file_path.string()+" is not a valid UTF-8 file. Saving might corrupt the file.\n");
}
else {
if(juci::filesystem::read(file_path, get_buffer())==-1)
Singleton::terminal()->print("Error: "+file_path.string()+" is not a valid UTF-8 file.\n");
}
get_source_buffer()->end_not_undoable_action(); get_source_buffer()->end_not_undoable_action();
get_buffer()->place_cursor(get_buffer()->get_iter_at_offset(0)); get_buffer()->place_cursor(get_buffer()->get_iter_at_offset(0));
search_settings = gtk_source_search_settings_new(); search_settings = gtk_source_search_settings_new();
gtk_source_search_settings_set_wrap_around(search_settings, true); gtk_source_search_settings_set_wrap_around(search_settings, true);
search_context = gtk_source_search_context_new(get_source_buffer()->gobj(), search_settings); search_context = gtk_source_search_context_new(get_source_buffer()->gobj(), search_settings);
@ -908,7 +917,7 @@ std::vector<std::string> Source::View::spellcheck_get_suggestions(const Gtk::Tex
///////////////////// /////////////////////
//// GenericView //// //// GenericView ////
///////////////////// /////////////////////
Source::GenericView::GenericView(const boost::filesystem::path &file_path, Glib::RefPtr<Gsv::Language> language) : View(file_path) { Source::GenericView::GenericView(const boost::filesystem::path &file_path, Glib::RefPtr<Gsv::Language> language) : View(file_path, language) {
if(language) { if(language) {
get_source_buffer()->set_language(language); get_source_buffer()->set_language(language);
Singleton::terminal()->print("Language for file "+file_path.string()+" set to "+language->get_name()+".\n"); Singleton::terminal()->print("Language for file "+file_path.string()+" set to "+language->get_name()+".\n");
@ -928,8 +937,8 @@ Source::GenericView::GenericView(const boost::filesystem::path &file_path, Glib:
//////////////////////// ////////////////////////
clang::Index Source::ClangViewParse::clang_index(0, 0); clang::Index Source::ClangViewParse::clang_index(0, 0);
Source::ClangViewParse::ClangViewParse(const boost::filesystem::path &file_path, const boost::filesystem::path& project_path): Source::ClangViewParse::ClangViewParse(const boost::filesystem::path &file_path, const boost::filesystem::path& project_path, Glib::RefPtr<Gsv::Language> language):
Source::View(file_path), project_path(project_path), parse_error(false) { Source::View(file_path, language), project_path(project_path), parse_error(false) {
DEBUG("start"); DEBUG("start");
auto scheme = get_source_buffer()->get_style_scheme(); auto scheme = get_source_buffer()->get_style_scheme();
auto tag_table=get_buffer()->get_tag_table(); auto tag_table=get_buffer()->get_tag_table();
@ -1385,8 +1394,8 @@ bool Source::ClangViewParse::on_key_press_event(GdkEventKey* key) {
////////////////////////////// //////////////////////////////
//// ClangViewAutocomplete /// //// ClangViewAutocomplete ///
////////////////////////////// //////////////////////////////
Source::ClangViewAutocomplete::ClangViewAutocomplete(const boost::filesystem::path &file_path, const boost::filesystem::path& project_path): Source::ClangViewAutocomplete::ClangViewAutocomplete(const boost::filesystem::path &file_path, const boost::filesystem::path& project_path, Glib::RefPtr<Gsv::Language> language):
Source::ClangViewParse(file_path, project_path), autocomplete_cancel_starting(false) { Source::ClangViewParse(file_path, project_path, language), autocomplete_cancel_starting(false) {
get_buffer()->signal_changed().connect([this](){ get_buffer()->signal_changed().connect([this](){
if(completion_dialog_shown) if(completion_dialog_shown)
delayed_reparse_connection.disconnect(); delayed_reparse_connection.disconnect();
@ -1668,8 +1677,8 @@ bool Source::ClangViewAutocomplete::restart_parse() {
//// ClangViewRefactor ///// //// ClangViewRefactor /////
//////////////////////////// ////////////////////////////
Source::ClangViewRefactor::ClangViewRefactor(const boost::filesystem::path &file_path, const boost::filesystem::path& project_path): Source::ClangViewRefactor::ClangViewRefactor(const boost::filesystem::path &file_path, const boost::filesystem::path& project_path, Glib::RefPtr<Gsv::Language> language):
Source::ClangViewAutocomplete(file_path, project_path) { Source::ClangViewAutocomplete(file_path, project_path, language) {
similar_tokens_tag=get_buffer()->create_tag(); similar_tokens_tag=get_buffer()->create_tag();
similar_tokens_tag->property_weight()=Pango::WEIGHT_BOLD; similar_tokens_tag->property_weight()=Pango::WEIGHT_BOLD;
@ -1814,7 +1823,7 @@ Source::ClangViewRefactor::~ClangViewRefactor() {
delayed_tag_similar_tokens_connection.disconnect(); delayed_tag_similar_tokens_connection.disconnect();
} }
Source::ClangView::ClangView(const boost::filesystem::path &file_path, const boost::filesystem::path& project_path, Glib::RefPtr<Gsv::Language> language): ClangViewRefactor(file_path, project_path) { Source::ClangView::ClangView(const boost::filesystem::path &file_path, const boost::filesystem::path& project_path, Glib::RefPtr<Gsv::Language> language): ClangViewRefactor(file_path, project_path, language) {
if(language) { if(language) {
get_source_buffer()->set_highlight_syntax(true); get_source_buffer()->set_highlight_syntax(true);
get_source_buffer()->set_language(language); get_source_buffer()->set_language(language);

8
src/source.h

@ -52,7 +52,7 @@ namespace Source {
class View : public Gsv::View { class View : public Gsv::View {
public: public:
View(const boost::filesystem::path &file_path); View(const boost::filesystem::path &file_path, Glib::RefPtr<Gsv::Language> language);
~View(); ~View();
void search_highlight(const std::string &text, bool case_sensitive, bool regex); void search_highlight(const std::string &text, bool case_sensitive, bool regex);
@ -134,7 +134,7 @@ namespace Source {
class ClangViewParse : public View { class ClangViewParse : public View {
public: public:
ClangViewParse(const boost::filesystem::path &file_path, const boost::filesystem::path& project_path); ClangViewParse(const boost::filesystem::path &file_path, const boost::filesystem::path& project_path, Glib::RefPtr<Gsv::Language> language);
~ClangViewParse(); ~ClangViewParse();
boost::filesystem::path project_path; boost::filesystem::path project_path;
void start_reparse(); void start_reparse();
@ -176,7 +176,7 @@ namespace Source {
class ClangViewAutocomplete : public ClangViewParse { class ClangViewAutocomplete : public ClangViewParse {
public: public:
ClangViewAutocomplete(const boost::filesystem::path &file_path, const boost::filesystem::path& project_path); ClangViewAutocomplete(const boost::filesystem::path &file_path, const boost::filesystem::path& project_path, Glib::RefPtr<Gsv::Language> language);
void async_delete(); void async_delete();
bool restart_parse(); bool restart_parse();
protected: protected:
@ -206,7 +206,7 @@ namespace Source {
class ClangViewRefactor : public ClangViewAutocomplete { class ClangViewRefactor : public ClangViewAutocomplete {
public: public:
ClangViewRefactor(const boost::filesystem::path &file_path, const boost::filesystem::path& project_path); ClangViewRefactor(const boost::filesystem::path &file_path, const boost::filesystem::path& project_path, Glib::RefPtr<Gsv::Language> language);
~ClangViewRefactor(); ~ClangViewRefactor();
private: private:
Glib::RefPtr<Gtk::TextTag> similar_tokens_tag; Glib::RefPtr<Gtk::TextTag> similar_tokens_tag;

38
src/sourcefile.cc

@ -25,15 +25,6 @@ int juci::filesystem::read(const std::string &path, Glib::RefPtr<Gtk::TextBuffer
Glib::ustring ustr=std::move(ss.str()); Glib::ustring ustr=std::move(ss.str());
bool valid=true; bool valid=true;
//This was way too slow...
/*Glib::ustring::iterator iter;
while(!ustr.validate(iter)) {
auto next_char_iter=iter;
next_char_iter++;
ustr.replace(iter, next_char_iter, "?");
if(valid)
valid=false;
}*/
if(ustr.validate()) if(ustr.validate())
text_buffer->insert_at_cursor(ustr); text_buffer->insert_at_cursor(ustr);
@ -69,6 +60,35 @@ int juci::filesystem::read(const std::string &path, Glib::RefPtr<Gtk::TextBuffer
return 0; return 0;
} }
int juci::filesystem::read_non_utf8(const std::string &path, Glib::RefPtr<Gtk::TextBuffer> text_buffer) {
std::ifstream input(path, std::ofstream::binary);
if(input) {
//need to read the whole file to make this work...
std::stringstream ss;
ss << input.rdbuf();
Glib::ustring ustr=std::move(ss.str());
bool valid=true;
Glib::ustring::iterator iter;
while(!ustr.validate(iter)) {
auto next_char_iter=iter;
next_char_iter++;
ustr.replace(iter, next_char_iter, "?");
valid=false;
}
text_buffer->insert_at_cursor(ustr);
input.close();
if(valid)
return 1;
else
return -1;
}
return 0;
}
//Only use on small files //Only use on small files
std::vector<std::string> juci::filesystem::read_lines(const std::string &path) { std::vector<std::string> juci::filesystem::read_lines(const std::string &path) {
std::vector<std::string> res; std::vector<std::string> res;

3
src/sourcefile.h

@ -13,6 +13,9 @@ namespace juci {
static int read(const std::string &path, Glib::RefPtr<Gtk::TextBuffer> text_buffer); static int read(const std::string &path, Glib::RefPtr<Gtk::TextBuffer> text_buffer);
static int read(const boost::filesystem::path &path, Glib::RefPtr<Gtk::TextBuffer> text_buffer) { return read(path.string(), text_buffer); } static int read(const boost::filesystem::path &path, Glib::RefPtr<Gtk::TextBuffer> text_buffer) { return read(path.string(), text_buffer); }
static int read_non_utf8(const std::string &path, Glib::RefPtr<Gtk::TextBuffer> text_buffer);
static int read_non_utf8(const boost::filesystem::path &path, Glib::RefPtr<Gtk::TextBuffer> text_buffer) { return read_non_utf8(path.string(), text_buffer); }
static std::vector<std::string> read_lines(const std::string &path); static std::vector<std::string> read_lines(const std::string &path);
static std::vector<std::string> read_lines(const boost::filesystem::path &path) { return read_lines(path.string()); }; static std::vector<std::string> read_lines(const boost::filesystem::path &path) { return read_lines(path.string()); };

Loading…
Cancel
Save