Skip to content
This repository has been archived by the owner on Nov 11, 2023. It is now read-only.

Commit

Permalink
Merge pull request #2 from shanmukhateja/sourceview
Browse files Browse the repository at this point in the history
  • Loading branch information
shanmukhateja authored Jan 29, 2022
2 parents 2d64d7f + fe24d87 commit 2e4bb54
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 9 deletions.
38 changes: 38 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
static_init = "1.0.0"
sourceview4 = "0.3.0"
5 changes: 4 additions & 1 deletion res/ui/main_window.glade
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Author: Surya Teja K <shanmukhateja@gmail.com>
-->
<interface>
<requires lib="gtk+" version="3.24"/>
<requires lib="gtksourceview" version="4.0"/>
<!-- interface-license-type mit -->
<!-- interface-name MyStudio IDE -->
<!-- interface-description An IDE from scratch in Rust -->
Expand Down Expand Up @@ -157,10 +158,12 @@ Author: Surya Teja K <shanmukhateja@gmail.com>
<property name="can-focus">True</property>
<property name="shadow-type">in</property>
<child>
<object class="GtkTextView" id="main_text_editor">
<object class="GtkSourceView" id="main_text_editor">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="vexpand">True</property>
<property name="left-margin">2</property>
<property name="right-margin">2</property>
</object>
</child>
</object>
Expand Down
28 changes: 23 additions & 5 deletions src/comms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -19,7 +21,7 @@ pub enum CommEvents {
// used to read text files
RootTreeItemClicked(Option<RootTreeModel>),
// Sets text to RootTextView
UpdateRootTextViewContent(Option<String>),
UpdateRootTextViewContent(Option<String>, Option<String>),
// Save Changes
SaveEditorChanges(),
}
Expand All @@ -32,7 +34,7 @@ pub fn handle_comm_event(tx: Sender<CommEvents>, rx: Receiver<CommEvents>) {
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) => {
Expand All @@ -59,7 +61,8 @@ pub fn handle_comm_event(tx: Sender<CommEvents>, rx: Receiver<CommEvents>) {
}
}

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 => {
Expand All @@ -68,13 +71,28 @@ pub fn handle_comm_event(tx: Sender<CommEvents>, rx: Receiver<CommEvents>) {
}
}
}
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();
}
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,7 +21,7 @@ use workspace::Workspace;
// Declare GUI widgets in TLS for 'global' access
thread_local! { pub static G_WINDOW: RefCell<Option<ApplicationWindow>> = RefCell::new(None) }
thread_local! { pub static G_TREE: RefCell<Option<TreeView>> = RefCell::new(None) }
thread_local! { pub static G_TEXT_VIEW: RefCell<Option<TextView>> = RefCell::new(None) }
thread_local! { pub static G_TEXT_VIEW: RefCell<Option<sourceview4::View>> = RefCell::new(None) }
thread_local! { pub static G_STATUS_BAR: RefCell<Option<Statusbar>> = RefCell::new(None) }

fn build_ui(app: &Application) {
Expand Down

0 comments on commit 2e4bb54

Please sign in to comment.