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
* 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 {}

View File

@ -8,7 +8,7 @@ use glib::wrapper;
wrapper! {
pub struct EchidnaEditor(ObjectSubclass<imp::EchidnaEditor>) @extends gio::Application, gtk::Application, @implements gio::ActionGroup, gio::ActionMap;
}
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
* 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: &gtk::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: &gtk::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
),
}
}
}
}

View File

@ -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();

View File

@ -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<imp::EchidnaWindow>)
@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<P: IsA<gtk::Application>>(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),
}
}
}
}