Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] file watching #588

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
90 changes: 90 additions & 0 deletions Cargo.lock

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

18 changes: 16 additions & 2 deletions helix-term/src/application.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use helix_core::syntax;
use helix_lsp::{lsp, util::lsp_pos_to_pos, LspProgressMap};
use helix_view::{theme, Editor};
use helix_view::{file_watcher, theme, Editor};

use crate::{args::Args, compositor::Compositor, config::Config, job::Jobs, ui};

Expand Down Expand Up @@ -85,7 +85,7 @@ impl Application {
theme_loader.clone(),
syn_loader.clone(),
config.editor.clone(),
);
)?;

let editor_view = Box::new(ui::EditorView::new(std::mem::take(&mut config.keys)));
compositor.push(editor_view);
Expand Down Expand Up @@ -192,6 +192,9 @@ impl Application {
self.jobs.handle_callback(&mut self.editor, &mut self.compositor, callback);
self.render();
}
Some(msg) = self.editor.watcher_receiver.recv() => {
self.handle_watcher_message(msg)
}
}
}
}
Expand Down Expand Up @@ -490,6 +493,17 @@ impl Application {
}
}

fn handle_watcher_message(&mut self, msg: file_watcher::Message) {
match msg {
file_watcher::Message::NotifyEvents(event) => {
log::info!("handling file watcher event: {:?}", event);
}
// match event.unwrap() {
// _ => unimplemented!(),
// },
}
}

async fn claim_term(&mut self) -> Result<(), Error> {
terminal::enable_raw_mode()?;
let mut stdout = stdout();
Expand Down
2 changes: 2 additions & 0 deletions helix-view/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ log = "~0.4"

which = "4.2"

notify = "=5.0.0-pre.11" # check that it builds on NetBSD before upgrading
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason we need to use a pre-release version instead of current stable? (Which I believe is 4.x)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last time I looked at it, it seemed like they were supporting both versions and 5 seemed stable enough maybe


[target.'cfg(windows)'.dependencies]
clipboard-win = { version = "4.2", features = ["std"] }

Expand Down
38 changes: 30 additions & 8 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
clipboard::{get_clipboard_provider, ClipboardProvider},
file_watcher::{self, ActorMessage, ActorMessageKind, NotifyActor, NotifyHandle},
graphics::{CursorKind, Rect},
theme::{self, Theme},
tree::Tree,
Expand All @@ -8,14 +9,19 @@ use crate::{

use futures_util::future;
use std::{
collections::HashMap,
path::{Path, PathBuf},
sync::Arc,
time::Duration,
};
use tokio::sync::{
mpsc::{unbounded_channel, UnboundedReceiver},
oneshot,
};

use slotmap::SlotMap;

use anyhow::Error;
use anyhow::{Context, Error};

pub use helix_core::diagnostic::Severity;
pub use helix_core::register::Registers;
Expand Down Expand Up @@ -65,12 +71,15 @@ impl Default for Config {
pub struct Editor {
pub tree: Tree,
pub documents: SlotMap<DocumentId, Document>,
pub path_to_doc: HashMap<PathBuf, DocumentId>,
pub count: Option<std::num::NonZeroUsize>,
pub selected_register: RegisterSelection,
pub registers: Registers,
pub theme: Theme,
pub language_servers: helix_lsp::Registry,
pub clipboard_provider: Box<dyn ClipboardProvider>,
pub watcher: Arc<NotifyHandle>,
pub watcher_receiver: UnboundedReceiver<file_watcher::Message>,

pub syn_loader: Arc<syntax::Loader>,
pub theme_loader: Arc<theme::Loader>,
Expand All @@ -94,15 +103,22 @@ impl Editor {
themes: Arc<theme::Loader>,
config_loader: Arc<syntax::Loader>,
config: Config,
) -> Self {
) -> anyhow::Result<Self> {
let language_servers = helix_lsp::Registry::new();
let (watcher, watcher_receiver) = {
let (watcher_sender, watcher_receiver) = unbounded_channel();
let watcher = NotifyActor::spawn(Box::new(move |e| watcher_sender.send(e).unwrap()))
.context("Failed to spawn watcher")?;
(Arc::new(watcher), watcher_receiver)
};
Comment on lines +106 to +113
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless I'm misunderstanding something, it looks like this API makes it impossible to open an editor unless spawning the file watcher is successful. Presumably, though, we still want to succeed in opening the editor, and just report to the user that file watching isn't active, right? For example, I still want Helix to work even if I'm on a system that doesn't provide the expected notify capabilities.


// HAXX: offset the render area height by 1 to account for prompt/commandline
area.height -= 1;

Self {
Ok(Self {
tree: Tree::new(area),
documents: SlotMap::with_key(),
path_to_doc: HashMap::default(),
count: None,
selected_register: RegisterSelection::default(),
theme: themes.default(),
Expand All @@ -111,9 +127,11 @@ impl Editor {
theme_loader: themes,
registers: Registers::default(),
clipboard_provider: get_clipboard_provider(),
watcher,
watcher_receiver,
status_msg: None,
config,
}
})
}

pub fn clear_status(&mut self) {
Expand Down Expand Up @@ -231,10 +249,7 @@ impl Editor {
pub fn open(&mut self, path: PathBuf, action: Action) -> Result<DocumentId, Error> {
let path = crate::document::canonicalize_path(&path)?;

let id = self
.documents()
.find(|doc| doc.path() == Some(&path))
.map(|doc| doc.id);
let id = self.path_to_doc.get(&path).map(|it| *it);

let id = if let Some(id) = id {
id
Expand Down Expand Up @@ -266,6 +281,13 @@ impl Editor {

let id = self.documents.insert(doc);
self.documents[id].id = id;
self.path_to_doc.insert(path.clone(), id);
{
let watcher = self.watcher.clone();
tokio::spawn(async move {
watcher.watch(path.clone()).await;
});
}
id
};

Expand Down