-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
[WIP] file watching #588
Changes from all commits
05f3fa6
cfb1712
5834f91
1037066
338f9d9
20251af
6fbf898
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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, | ||
|
@@ -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; | ||
|
@@ -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>, | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(), | ||
|
@@ -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) { | ||
|
@@ -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 | ||
|
@@ -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 | ||
}; | ||
|
||
|
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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