diff --git a/Cargo.lock b/Cargo.lock index d8a14ee..4b30e09 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -517,6 +517,7 @@ dependencies = [ "arc-swap", "gtk", "jwalk", + "sourceview4", "static_init", ] @@ -750,6 +751,43 @@ version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e" +[[package]] +name = "sourceview4" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93b6d77ea4d6ae2edfd0eef76b597b779faa082865812a34357db8a239614ea7" +dependencies = [ + "bitflags", + "cairo-rs", + "gdk", + "gdk-pixbuf", + "gio", + "glib", + "gtk", + "libc", + "pango", + "sourceview4-sys", +] + +[[package]] +name = "sourceview4-sys" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52112e8dbba7d4aa28106c289ddb398ede3bc3bcd91f185368ca5e2e542eea30" +dependencies = [ + "cairo-sys-rs", + "gdk-pixbuf-sys", + "gdk-sys", + "gio-sys", + "glib-sys", + "gobject-sys", + "gtk-sys", + "libc", + "pango-sys", + "pkg-config", + "system-deps", +] + [[package]] name = "static_init" version = "1.0.1" diff --git a/Cargo.toml b/Cargo.toml index 5ae5bb4..8086b95 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,4 +9,5 @@ edition = "2018" gtk = { version = "0.15.2", features = ["v3_24"] } jwalk = "0.6.0" arc-swap="1.3.2" -static_init = "1.0.0" \ No newline at end of file +static_init = "1.0.0" +sourceview4 = "0.3.0" \ No newline at end of file diff --git a/res/ui/main_window.glade b/res/ui/main_window.glade index 1aaf0c0..4ba940b 100644 --- a/res/ui/main_window.glade +++ b/res/ui/main_window.glade @@ -28,6 +28,7 @@ Author: Surya Teja K --> + @@ -157,10 +158,12 @@ Author: Surya Teja K True in - + True True True + 2 + 2 diff --git a/src/comms.rs b/src/comms.rs index c4bcb0d..b4ceb46 100644 --- a/src/comms.rs +++ b/src/comms.rs @@ -2,12 +2,14 @@ use std::path::Path; use gtk::glib::{self, Receiver, Sender}; use gtk::prelude::{ObjectExt, StatusbarExt, TextBufferExt, TextViewExt, WidgetExt}; +use sourceview4::LanguageManager; use crate::{ action_handler, ui::{self, tree_model::RootTreeModel}, workspace::Workspace, }; +use sourceview4::prelude::*; use crate::{G_STATUS_BAR, G_TEXT_VIEW, G_TREE}; @@ -19,7 +21,7 @@ pub enum CommEvents { // used to read text files RootTreeItemClicked(Option), // Sets text to RootTextView - UpdateRootTextViewContent(Option), + UpdateRootTextViewContent(Option, Option), // Save Changes SaveEditorChanges(), } @@ -32,7 +34,7 @@ pub fn handle_comm_event(tx: Sender, rx: Receiver) { ui::tree_view::update_tree_model(&tree.borrow().clone().unwrap()); // Reset UI tx.send(CommEvents::RootTreeItemClicked(None)).ok(); - tx.send(CommEvents::UpdateRootTextViewContent(None)).ok(); + tx.send(CommEvents::UpdateRootTextViewContent(None, None)).ok(); }); } CommEvents::RootTreeItemClicked(tree_model) => { @@ -59,7 +61,8 @@ pub fn handle_comm_event(tx: Sender, rx: Receiver) { } } - tx.send(CommEvents::UpdateRootTextViewContent(Some(content))) + let file_path_string: String = String::from(file_path.to_str().unwrap()); + tx.send(CommEvents::UpdateRootTextViewContent(Some(file_path_string), Some(content))) .ok(); } None => { @@ -68,13 +71,28 @@ pub fn handle_comm_event(tx: Sender, rx: Receiver) { } } } - CommEvents::UpdateRootTextViewContent(content) => { + CommEvents::UpdateRootTextViewContent(path, content) => { G_TEXT_VIEW.with(|editor| { let text_editor = &editor.borrow().clone().unwrap(); match content { Some(content) => { - text_editor.buffer().unwrap().set_text(content.as_str()); + let source_buffer = sourceview4::Buffer::builder() + .text(content.as_str()) + .build(); + + // Detect language for syntax highlight + let lang_manager = LanguageManager::new(); + match lang_manager.guess_language(Some(path.unwrap()), None) { + Some(lang) => { + source_buffer.set_language(Some(&lang)); + }, + None => { + source_buffer.set_language(sourceview4::Language::NONE); + } + } + // update buffer in View + text_editor.set_buffer(Some(&source_buffer)); // Show cursor on text_view so user can start modifying file text_editor.grab_focus(); } diff --git a/src/main.rs b/src/main.rs index 52faa78..0eb1d3b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ use gtk::{ ApplicationCommandLineExt, ApplicationExt, ApplicationExtManual, BuilderExtManual, GtkWindowExt, WidgetExt, }, - Application, ApplicationWindow, Builder, Statusbar, TextView, TreeView, + Application, ApplicationWindow, Builder, Statusbar, TreeView, }; mod action_handler; @@ -21,7 +21,7 @@ use workspace::Workspace; // Declare GUI widgets in TLS for 'global' access thread_local! { pub static G_WINDOW: RefCell> = RefCell::new(None) } thread_local! { pub static G_TREE: RefCell> = RefCell::new(None) } -thread_local! { pub static G_TEXT_VIEW: RefCell> = RefCell::new(None) } +thread_local! { pub static G_TEXT_VIEW: RefCell> = RefCell::new(None) } thread_local! { pub static G_STATUS_BAR: RefCell> = RefCell::new(None) } fn build_ui(app: &Application) {