style: format source files with Rustfmt tooling

merge-requests/8/head
Nefo Fortressia 2021-10-26 16:46:41 +07:00
parent 8c05235e13
commit 6c6afa740f
5 changed files with 75 additions and 128 deletions

View File

@ -1,25 +1,20 @@
/* This Source Code Form is subject to the terms of the Mozilla Public /* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use super::super::window::EchidnaWindow;
use super::super::window::menubar::MenubarImplementedEditor; use super::super::window::menubar::MenubarImplementedEditor;
use super::super::window::EchidnaWindow;
use gtk::subclass::prelude::*;
use gtk::prelude::*; use gtk::prelude::*;
use gtk::subclass::prelude::*;
use gtk::{ use gtk::Application;
Application,
};
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct EchidnaEditor { pub struct EchidnaEditor {
pub name: &'static str, pub name: &'static str,
pub app_id: &'static str pub app_id: &'static str,
} }
#[glib::object_subclass] #[glib::object_subclass]
impl ObjectSubclass for EchidnaEditor { impl ObjectSubclass for EchidnaEditor {
const NAME: &'static str = "EchidnaEditorApplication"; const NAME: &'static str = "EchidnaEditorApplication";
@ -27,34 +22,24 @@ impl ObjectSubclass for EchidnaEditor {
type ParentType = Application; type ParentType = Application;
fn new() -> Self { fn new() -> Self {
Self { Self {
name: "Echidna Code Editor", name: "Echidna Code Editor",
app_id: "land.echidna.editor" app_id: "land.echidna.editor",
} }
} }
} }
impl ObjectImpl for EchidnaEditor {}
impl ObjectImpl for EchidnaEditor {
}
impl ApplicationImpl for EchidnaEditor { impl ApplicationImpl for EchidnaEditor {
fn activate(&self, app: &Self::Type) {
fn activate(&self, app: &Self::Type){
let window = EchidnaWindow::new(app); let window = EchidnaWindow::new(app);
window.setup_menubar(); window.setup_menubar();
window.set_application(Some(app)); window.set_application(Some(app));
window.present();
}
window.present();
}
} }
impl GtkApplicationImpl for EchidnaEditor {} impl GtkApplicationImpl for EchidnaEditor {}

View File

@ -8,7 +8,7 @@ use glib::wrapper;
wrapper! { wrapper! {
pub struct EchidnaEditor(ObjectSubclass<imp::EchidnaEditor>) @extends gio::Application, gtk::Application, @implements gio::ActionGroup, gio::ActionMap; pub struct EchidnaEditor(ObjectSubclass<imp::EchidnaEditor>) @extends gio::Application, gtk::Application, @implements gio::ActionGroup, gio::ActionMap;
} }
impl Default for EchidnaEditor { impl Default for EchidnaEditor {

View File

@ -2,27 +2,12 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use glib::{ use gio::Cancellable;
clone use glib::clone;
};
use sourceview::{
View,
Buffer,
LanguageManager,
prelude::*
};
use gio::{
Cancellable
};
use gtk::{ use gtk::{
FileChooserDialog, prelude::*, subclass::prelude::*, FileChooserAction, FileChooserDialog, Label, ResponseType,
FileChooserAction,
ResponseType,
Label,
prelude::*,
subclass::prelude::*
}; };
use sourceview::{prelude::*, Buffer, LanguageManager, View};
pub trait FileImplementedEditor { pub trait FileImplementedEditor {
fn action_open_file(&self); fn action_open_file(&self);
@ -63,75 +48,66 @@ impl FileImplementedEditor for super::EchidnaWindow {
// This function sets the callback function as 'static, which for some reasons ban cloning self into it. Idk why. // This function sets the callback function as 'static, which for some reasons ban cloning self into it. Idk why.
dialog.connect_response(clone!( @weak self as window, => dialog.connect_response(clone!( @weak self as window, =>
move |dialog, response| { move |dialog, response| {
if response == ResponseType::Accept { if response == ResponseType::Accept {
let file_option = dialog.file(); let file_option = dialog.file();
match file_option { match file_option {
Some(file) => { Some(file) => {
dialog.destroy(); dialog.destroy();
Self::open_file(&super::imp::EchidnaWindow::from_instance(&window).notebook, file); Self::open_file(&super::imp::EchidnaWindow::from_instance(&window).notebook, file);
}, },
None => { None => {},
}
},
}
} else if response == ResponseType::Cancel { } else if response == ResponseType::Cancel {
dialog.destroy(); dialog.destroy();
} }));
} }));
} }
fn open_file(notebook: &gtk::Notebook, file: gio::File){ fn open_file(notebook: &gtk::Notebook, file: gio::File) {
let cancellable = Cancellable::new(); let cancellable = Cancellable::new();
let filepath = file.path().expect("No filepath"); let filepath = file.path().expect("No filepath");
let file_info_result = file.query_info( let file_info_result =
"*", file.query_info("*", gio::FileQueryInfoFlags::NONE, Some(&cancellable));
gio::FileQueryInfoFlags::NONE, match file_info_result {
Some(&cancellable)); Ok(info) => match info.content_type() {
match file_info_result { Some(content_type) => {
Ok(info) => { println!(
match info.content_type() { "Opened {} and found its content type is {}.",
Some(content_type) => { "file",
println!("Opened {} and found its content type is {}.", "file", content_type.to_string()); content_type.to_string()
let content_cancellable = Cancellable::new(); );
let file_content = file.load_contents(Some(&content_cancellable)); let content_cancellable = Cancellable::new();
match file_content { let file_content = file.load_contents(Some(&content_cancellable));
Ok(content) => { match file_content {
let (int_vec, _text_option) = content; Ok(content) => {
let language_manager = LanguageManager::new(); let (int_vec, _text_option) = content;
let language = language_manager.guess_language(Some(&info.name().to_str().expect("Could not open the file because its name is not supported by Unicode.")), None); let language_manager = LanguageManager::new();
let language = language_manager.guess_language(Some(&info.name().to_str().expect("Could not open the file because its name is not supported by Unicode.")), None);
let buffer = Buffer::new(None);
buffer.set_text(std::str::from_utf8(&int_vec).expect(format!("Could not parse the contents of {:?} as it's unsupported by UTF-8", filepath).as_str())); let buffer = Buffer::new(None);
match language { buffer.set_text(std::str::from_utf8(&int_vec).expect(format!("Could not parse the contents of {:?} as it's unsupported by UTF-8", filepath).as_str()));
match language {
Some(lang) => buffer.set_language(Some(&lang)), Some(lang) => buffer.set_language(Some(&lang)),
None => {} None => {}
}
let sourceview = View::with_buffer(&buffer);
notebook.prepend_page(
&sourceview,
Some(&Label::new(Some(
&info.name().to_str().expect("Could not parse file's name"),
))),
);
} }
Err(e) => println!("Could not open {:?} because:\n{:#?}", filepath, e),
let sourceview = View::with_buffer(&buffer);
notebook.prepend_page(&sourceview,
Some(&Label::new(Some(&info.name().to_str()
.expect("Could not parse file's name")))));
} }
Err(e) => println!("Could not open {:?} because:\n{:#?}", filepath, e), }
} None => println!("It does not seem like {:?} has a type", filepath),
}, },
None => println!("It does not seem like {:?} has a type", filepath), Err(e) => println!(
"Could not retrieve file information for {:?} because:\n{}",
} filepath, e
),
}, }
Err(e) => println!("Could not retrieve file information for {:?} because:\n{}", filepath, e),
}
} }
}
}

View File

@ -87,7 +87,6 @@ impl MenubarImplementedEditor for EchidnaWindow {
let act_window_close = SimpleAction::new("close", None); let act_window_close = SimpleAction::new("close", None);
&self.add_action(&act_window_close); &self.add_action(&act_window_close);
{ {
let window = self.clone(); let window = self.clone();

View File

@ -7,34 +7,21 @@ mod file;
mod imp; mod imp;
pub mod menubar; pub mod menubar;
use glib::{ use glib::object::IsA;
object::IsA,
};
glib::wrapper! { glib::wrapper! {
pub struct EchidnaWindow(ObjectSubclass<imp::EchidnaWindow>) pub struct EchidnaWindow(ObjectSubclass<imp::EchidnaWindow>)
@extends gtk::Widget, gtk::Window, gtk::ApplicationWindow, @extends gtk::Widget, gtk::Window, gtk::ApplicationWindow,
@implements gio::ActionGroup, gio::ActionMap, gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget, gtk::Native, gtk::Root, gtk::ShortcutManager; @implements gio::ActionGroup, gio::ActionMap, gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget, gtk::Native, gtk::Root, gtk::ShortcutManager;
} }
impl EchidnaWindow { impl EchidnaWindow {
pub fn new<P: IsA<gtk::Application>>(application: &P) -> Self { pub fn new<P: IsA<gtk::Application>>(application: &P) -> Self {
let object = glib::Object::new(&[("application", &application)]);
let object = glib::Object::new(&[
("application", &application) match object {
]); Ok(o) => o,
Err(e) => panic!("Error in making EchidnaApplication {}", e),
match object { }
Ok(o) => o,
Err(e) => panic!("Error in making EchidnaApplication {}", e),
}
} }
}
}