-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Add support for path completion #2608
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
Merged
the-mikedavis
merged 19 commits into
helix-editor:master
from
Philipp-M:path-completion
Nov 22, 2024
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
cc19db1
Adds support for path completion for unix paths
Philipp-M aaf7c20
Handle windows CI
Philipp-M ee5b319
Micro-optimization and revert of composing additional text edits
Philipp-M be1e616
Use `PartialEq` for `LspCompletionItem` and don't prefix-filter path …
Philipp-M f20cd4c
Apply suggestions from code review
Philipp-M d02470f
Apply suggestions from code review
Philipp-M de75bde
Fix cargo fmt
Philipp-M 59b2cf5
Fix byte offsets in `matched_path`
Philipp-M b08e6b6
implement robust path detection and env expansion
pascalkuthe a3e12ac
use path::expand in path completions
pascalkuthe 1334606
use new path detection and expansions in goto_file
pascalkuthe 101bcc7
Fix some typos.
Philipp-M 8af7c67
Apply review suggestions, and other smaller fixes.
Philipp-M 57e34aa
Modularize path completion into separate functions
Philipp-M d4a873d
cargo fmt
Philipp-M ce885df
cargo clippy fix
Philipp-M 3817321
Address review feedback
Philipp-M d73f158
improve completion cancelation
pascalkuthe 3dbcc4e
Fix typos, and other cosmetic stuff
Philipp-M File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
use std::borrow::Cow; | ||
|
||
use crate::Transaction; | ||
|
||
#[derive(Debug, PartialEq, Clone)] | ||
pub struct CompletionItem { | ||
pub transaction: Transaction, | ||
pub label: Cow<'static, str>, | ||
pub kind: Cow<'static, str>, | ||
/// Containing Markdown | ||
pub documentation: String, | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,287 @@ | ||
use std::borrow::Borrow; | ||
use std::future::Future; | ||
use std::sync::atomic::AtomicU64; | ||
use std::sync::atomic::Ordering::Relaxed; | ||
use std::sync::Arc; | ||
|
||
pub use oneshot::channel as cancelation; | ||
use tokio::sync::oneshot; | ||
use tokio::sync::Notify; | ||
|
||
pub type CancelTx = oneshot::Sender<()>; | ||
pub type CancelRx = oneshot::Receiver<()>; | ||
|
||
pub async fn cancelable_future<T>(future: impl Future<Output = T>, cancel: CancelRx) -> Option<T> { | ||
pub async fn cancelable_future<T>( | ||
future: impl Future<Output = T>, | ||
cancel: impl Borrow<TaskHandle>, | ||
) -> Option<T> { | ||
tokio::select! { | ||
biased; | ||
_ = cancel => { | ||
_ = cancel.borrow().canceled() => { | ||
None | ||
} | ||
res = future => { | ||
Some(res) | ||
} | ||
} | ||
} | ||
|
||
#[derive(Default, Debug)] | ||
struct Shared { | ||
state: AtomicU64, | ||
// `Notify` has some features that we don't really need here because it | ||
// supports waking single tasks (`notify_one`) and does its own (more | ||
// complicated) state tracking, we could reimplement the waiter linked list | ||
// with modest effort and reduce memory consumption by one word/8 bytes and | ||
// reduce code complexity/number of atomic operations. | ||
// | ||
// I don't think that's worth the complexity (unsafe code). | ||
// | ||
// if we only cared about async code then we could also only use a notify | ||
// (without the generation count), this would be equivalent (or maybe more | ||
// correct if we want to allow cloning the TX) but it would be extremly slow | ||
// to frequently check for cancelation from sync code | ||
notify: Notify, | ||
} | ||
|
||
impl Shared { | ||
fn generation(&self) -> u32 { | ||
self.state.load(Relaxed) as u32 | ||
} | ||
|
||
fn num_running(&self) -> u32 { | ||
(self.state.load(Relaxed) >> 32) as u32 | ||
} | ||
|
||
/// Increments the generation count and sets `num_running` | ||
/// to the provided value, this operation is not with | ||
/// regard to the generation counter (doesn't use `fetch_add`) | ||
/// so the calling code must ensure it cannot execute concurrently | ||
/// to maintain correctness (but not safety) | ||
fn inc_generation(&self, num_running: u32) -> (u32, u32) { | ||
let state = self.state.load(Relaxed); | ||
let generation = state as u32; | ||
let prev_running = (state >> 32) as u32; | ||
// no need to create a new generation if the refcount is zero (fastpath) | ||
if prev_running == 0 && num_running == 0 { | ||
return (generation, 0); | ||
} | ||
let new_generation = generation.saturating_add(1); | ||
self.state.store( | ||
new_generation as u64 | ((num_running as u64) << 32), | ||
Relaxed, | ||
); | ||
self.notify.notify_waiters(); | ||
(new_generation, prev_running) | ||
} | ||
|
||
fn inc_running(&self, generation: u32) { | ||
let mut state = self.state.load(Relaxed); | ||
loop { | ||
let current_generation = state as u32; | ||
if current_generation != generation { | ||
break; | ||
} | ||
let off = 1 << 32; | ||
let res = self.state.compare_exchange_weak( | ||
state, | ||
state.saturating_add(off), | ||
Relaxed, | ||
Relaxed, | ||
); | ||
match res { | ||
Ok(_) => break, | ||
Err(new_state) => state = new_state, | ||
} | ||
} | ||
} | ||
|
||
fn dec_running(&self, generation: u32) { | ||
let mut state = self.state.load(Relaxed); | ||
loop { | ||
let current_generation = state as u32; | ||
if current_generation != generation { | ||
break; | ||
} | ||
let num_running = (state >> 32) as u32; | ||
// running can't be zero here, that would mean we miscounted somewhere | ||
assert_ne!(num_running, 0); | ||
let off = 1 << 32; | ||
let res = self | ||
.state | ||
.compare_exchange_weak(state, state - off, Relaxed, Relaxed); | ||
match res { | ||
Ok(_) => break, | ||
Err(new_state) => state = new_state, | ||
} | ||
} | ||
} | ||
} | ||
|
||
// This intentionally doesn't implement `Clone` and requires a mutable reference | ||
// for cancelation to avoid races (in inc_generation). | ||
|
||
/// A task controller allows managing a single subtask enabling the controller | ||
/// to cancel the subtask and to check whether it is still running. | ||
/// | ||
/// For efficiency reasons the controller can be reused/restarted, | ||
/// in that case the previous task is automatically canceled. | ||
/// | ||
/// If the controller is dropped, the subtasks are automatically canceled. | ||
#[derive(Default, Debug)] | ||
pub struct TaskController { | ||
shared: Arc<Shared>, | ||
} | ||
|
||
impl TaskController { | ||
pub fn new() -> Self { | ||
TaskController::default() | ||
} | ||
/// Cancels the active task (handle). | ||
/// | ||
/// Returns whether any tasks were still running before the cancelation. | ||
pub fn cancel(&mut self) -> bool { | ||
self.shared.inc_generation(0).1 != 0 | ||
} | ||
|
||
/// Checks whether there are any task handles | ||
/// that haven't been dropped (or canceled) yet. | ||
pub fn is_running(&self) -> bool { | ||
self.shared.num_running() != 0 | ||
} | ||
|
||
/// Starts a new task and cancels the previous task (handles). | ||
pub fn restart(&mut self) -> TaskHandle { | ||
TaskHandle { | ||
generation: self.shared.inc_generation(1).0, | ||
shared: self.shared.clone(), | ||
} | ||
} | ||
} | ||
|
||
impl Drop for TaskController { | ||
fn drop(&mut self) { | ||
self.cancel(); | ||
} | ||
} | ||
|
||
/// A handle that is used to link a task with a task controller. | ||
/// | ||
/// It can be used to cancel async futures very efficiently but can also be checked for | ||
/// cancelation very quickly (single atomic read) in blocking code. | ||
/// The handle can be cheaply cloned (reference counted). | ||
/// | ||
/// The TaskController can check whether a task is "running" by inspecting the | ||
/// refcount of the (current) tasks handles. Therefore, if that information | ||
/// is important, ensure that the handle is not dropped until the task fully | ||
/// completes. | ||
pub struct TaskHandle { | ||
shared: Arc<Shared>, | ||
generation: u32, | ||
} | ||
|
||
impl Clone for TaskHandle { | ||
fn clone(&self) -> Self { | ||
self.shared.inc_running(self.generation); | ||
TaskHandle { | ||
shared: self.shared.clone(), | ||
generation: self.generation, | ||
} | ||
} | ||
} | ||
|
||
impl Drop for TaskHandle { | ||
fn drop(&mut self) { | ||
self.shared.dec_running(self.generation); | ||
} | ||
} | ||
|
||
impl TaskHandle { | ||
/// Waits until [`TaskController::cancel`] is called for the corresponding | ||
/// [`TaskController`]. Immediately returns if `cancel` was already called since | ||
pub async fn canceled(&self) { | ||
let notified = self.shared.notify.notified(); | ||
if !self.is_canceled() { | ||
notified.await | ||
} | ||
} | ||
|
||
pub fn is_canceled(&self) -> bool { | ||
self.generation != self.shared.generation() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::future::poll_fn; | ||
|
||
use futures_executor::block_on; | ||
use tokio::task::yield_now; | ||
|
||
use crate::{cancelable_future, TaskController}; | ||
|
||
#[test] | ||
fn immediate_cancel() { | ||
let mut controller = TaskController::new(); | ||
let handle = controller.restart(); | ||
controller.cancel(); | ||
assert!(handle.is_canceled()); | ||
controller.restart(); | ||
assert!(handle.is_canceled()); | ||
|
||
let res = block_on(cancelable_future( | ||
poll_fn(|_cx| std::task::Poll::Ready(())), | ||
handle, | ||
)); | ||
assert!(res.is_none()); | ||
} | ||
|
||
#[test] | ||
fn running_count() { | ||
let mut controller = TaskController::new(); | ||
let handle = controller.restart(); | ||
assert!(controller.is_running()); | ||
assert!(!handle.is_canceled()); | ||
drop(handle); | ||
assert!(!controller.is_running()); | ||
assert!(!controller.cancel()); | ||
let handle = controller.restart(); | ||
assert!(!handle.is_canceled()); | ||
assert!(controller.is_running()); | ||
let handle2 = handle.clone(); | ||
assert!(!handle.is_canceled()); | ||
assert!(controller.is_running()); | ||
drop(handle2); | ||
assert!(!handle.is_canceled()); | ||
assert!(controller.is_running()); | ||
assert!(controller.cancel()); | ||
assert!(handle.is_canceled()); | ||
assert!(!controller.is_running()); | ||
} | ||
|
||
#[test] | ||
fn no_cancel() { | ||
let mut controller = TaskController::new(); | ||
let handle = controller.restart(); | ||
assert!(!handle.is_canceled()); | ||
|
||
let res = block_on(cancelable_future( | ||
poll_fn(|_cx| std::task::Poll::Ready(())), | ||
handle, | ||
)); | ||
assert!(res.is_some()); | ||
} | ||
|
||
#[test] | ||
fn delayed_cancel() { | ||
let mut controller = TaskController::new(); | ||
let handle = controller.restart(); | ||
|
||
let mut hit = false; | ||
let res = block_on(cancelable_future( | ||
async { | ||
controller.cancel(); | ||
hit = true; | ||
yield_now().await; | ||
}, | ||
handle, | ||
)); | ||
assert!(res.is_none()); | ||
assert!(hit); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.