From 6c6afa740ff70e3fcfca4a9958e09bfad8a81504 Mon Sep 17 00:00:00 2001 From: Nefomemes Date: Tue, 26 Oct 2021 16:46:41 +0700 Subject: [PATCH] style: format source files with Rustfmt tooling --- src/components/app/imp.rs | 43 ++++------- src/components/app/mod.rs | 2 +- src/components/window/file.rs | 124 +++++++++++++------------------ src/components/window/menubar.rs | 1 - src/components/window/mod.rs | 33 +++----- 5 files changed, 75 insertions(+), 128 deletions(-) diff --git a/src/components/app/imp.rs b/src/components/app/imp.rs index 9dd1c98..1beda96 100644 --- a/src/components/app/imp.rs +++ b/src/components/app/imp.rs @@ -1,25 +1,20 @@ /* 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 * 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::EchidnaWindow; -use gtk::subclass::prelude::*; use gtk::prelude::*; +use gtk::subclass::prelude::*; -use gtk::{ - Application, -}; +use gtk::Application; #[derive(Debug, Default)] pub struct EchidnaEditor { pub name: &'static str, - pub app_id: &'static str + pub app_id: &'static str, } - - - #[glib::object_subclass] impl ObjectSubclass for EchidnaEditor { const NAME: &'static str = "EchidnaEditorApplication"; @@ -27,34 +22,24 @@ impl ObjectSubclass for EchidnaEditor { type ParentType = Application; fn new() -> Self { - Self { - name: "Echidna Code Editor", - app_id: "land.echidna.editor" - } + Self { + name: "Echidna Code Editor", + app_id: "land.echidna.editor", + } } } - -impl ObjectImpl for EchidnaEditor { - - - -} +impl ObjectImpl for EchidnaEditor {} impl ApplicationImpl for EchidnaEditor { - - fn activate(&self, app: &Self::Type){ - + fn activate(&self, app: &Self::Type) { let window = EchidnaWindow::new(app); - window.setup_menubar(); - window.set_application(Some(app)); - - window.present(); - - } + window.setup_menubar(); + window.set_application(Some(app)); + window.present(); + } } impl GtkApplicationImpl for EchidnaEditor {} - diff --git a/src/components/app/mod.rs b/src/components/app/mod.rs index 8b3c9c1..609ec16 100644 --- a/src/components/app/mod.rs +++ b/src/components/app/mod.rs @@ -8,7 +8,7 @@ use glib::wrapper; wrapper! { pub struct EchidnaEditor(ObjectSubclass) @extends gio::Application, gtk::Application, @implements gio::ActionGroup, gio::ActionMap; - + } impl Default for EchidnaEditor { diff --git a/src/components/window/file.rs b/src/components/window/file.rs index d0826bc..1d73e2a 100644 --- a/src/components/window/file.rs +++ b/src/components/window/file.rs @@ -2,27 +2,12 @@ * 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/. */ -use glib::{ - clone -}; -use sourceview::{ - View, - Buffer, - LanguageManager, - prelude::* -}; -use gio::{ - Cancellable - -}; +use gio::Cancellable; +use glib::clone; use gtk::{ - FileChooserDialog, - FileChooserAction, - ResponseType, - Label, - prelude::*, - subclass::prelude::* + prelude::*, subclass::prelude::*, FileChooserAction, FileChooserDialog, Label, ResponseType, }; +use sourceview::{prelude::*, Buffer, LanguageManager, View}; pub trait FileImplementedEditor { 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. dialog.connect_response(clone!( @weak self as window, => move |dialog, response| { - if response == ResponseType::Accept { - let file_option = dialog.file(); - match file_option { Some(file) => { dialog.destroy(); - Self::open_file(&super::imp::EchidnaWindow::from_instance(&window).notebook, file); - }, - None => { - - }, - } + None => {}, + } } else if response == ResponseType::Cancel { dialog.destroy(); - - } })); - + } })); } - fn open_file(notebook: >k::Notebook, file: gio::File){ - let cancellable = Cancellable::new(); - let filepath = file.path().expect("No filepath"); - let file_info_result = file.query_info( - "*", - gio::FileQueryInfoFlags::NONE, - Some(&cancellable)); - match file_info_result { - Ok(info) => { - match info.content_type() { - Some(content_type) => { - println!("Opened {} and found its content type is {}.", "file", content_type.to_string()); - let content_cancellable = Cancellable::new(); - let file_content = file.load_contents(Some(&content_cancellable)); - match file_content { - Ok(content) => { - let (int_vec, _text_option) = content; - 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())); - match language { + fn open_file(notebook: >k::Notebook, file: gio::File) { + let cancellable = Cancellable::new(); + let filepath = file.path().expect("No filepath"); + let file_info_result = + file.query_info("*", gio::FileQueryInfoFlags::NONE, Some(&cancellable)); + match file_info_result { + Ok(info) => match info.content_type() { + Some(content_type) => { + println!( + "Opened {} and found its content type is {}.", + "file", + content_type.to_string() + ); + let content_cancellable = Cancellable::new(); + let file_content = file.load_contents(Some(&content_cancellable)); + match file_content { + Ok(content) => { + let (int_vec, _text_option) = content; + 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())); + match language { Some(lang) => buffer.set_language(Some(&lang)), 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"), + ))), + ); } - - 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), } - 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 + ), + } } - -} \ No newline at end of file +} diff --git a/src/components/window/menubar.rs b/src/components/window/menubar.rs index 0e37b41..eb0468c 100644 --- a/src/components/window/menubar.rs +++ b/src/components/window/menubar.rs @@ -87,7 +87,6 @@ impl MenubarImplementedEditor for EchidnaWindow { let act_window_close = SimpleAction::new("close", None); &self.add_action(&act_window_close); - { let window = self.clone(); diff --git a/src/components/window/mod.rs b/src/components/window/mod.rs index 3b8470a..45975ca 100644 --- a/src/components/window/mod.rs +++ b/src/components/window/mod.rs @@ -7,34 +7,21 @@ mod file; mod imp; pub mod menubar; -use glib::{ - - - object::IsA, - -}; +use glib::object::IsA; glib::wrapper! { pub struct EchidnaWindow(ObjectSubclass) - @extends gtk::Widget, gtk::Window, gtk::ApplicationWindow, - @implements gio::ActionGroup, gio::ActionMap, gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget, gtk::Native, gtk::Root, gtk::ShortcutManager; + @extends gtk::Widget, gtk::Window, gtk::ApplicationWindow, + @implements gio::ActionGroup, gio::ActionMap, gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget, gtk::Native, gtk::Root, gtk::ShortcutManager; } impl EchidnaWindow { pub fn new>(application: &P) -> Self { - - let object = glib::Object::new(&[ - ("application", &application) - ]); - - match object { - Ok(o) => o, - Err(e) => panic!("Error in making EchidnaApplication {}", e), - } + let object = glib::Object::new(&[("application", &application)]); + + match object { + Ok(o) => o, + Err(e) => panic!("Error in making EchidnaApplication {}", e), + } } - - - - -} - +}