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) {