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

Implement pre-task dispatching #23258

Open
wants to merge 29 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
51da186
modify task schema to accept pre-/post-task tasks and implement task …
cartercanedy Jan 16, 2025
fe40b41
remove dbg from tests
cartercanedy Jan 16, 2025
0b8b75b
correct cycle node idents
cartercanedy Jan 16, 2025
e64c4fb
remove unnecessary format invocation
cartercanedy Jan 16, 2025
9b074cc
use nohash-hasher
cartercanedy Jan 17, 2025
ca662e7
use topo sort for cycle detection
cartercanedy Jan 17, 2025
f269da0
optimize subgraph collection
cartercanedy Jan 18, 2025
9447647
accept vec of tasks when spawning terminal tasks
cartercanedy Jan 19, 2025
453733f
remove original task from pretask list
cartercanedy Jan 20, 2025
25f5a2b
terminal_panel can spawn task queue
cartercanedy Jan 21, 2025
cd975d1
clippy + fmt
cartercanedy Jan 21, 2025
e946dd1
fix rerunning last task not spawning task
cartercanedy Jan 21, 2025
020e19d
typos
cartercanedy Jan 21, 2025
2b105f8
better error reporting
cartercanedy Jan 21, 2025
5d73441
remove post-task dispatching from feature scope
cartercanedy Jan 21, 2025
96b5e27
remove unnecessary method
cartercanedy Jan 21, 2025
bbe7891
tests
cartercanedy Jan 22, 2025
d65a448
resolve colliding task labels from worktree before global tasks
cartercanedy Jan 22, 2025
7dff425
clippy + fmt
cartercanedy Jan 22, 2025
0a31015
improve task cycle error reporting in the ui
cartercanedy Jan 23, 2025
3780d36
fmt
cartercanedy Jan 24, 2025
47e75d9
fix grammar
cartercanedy Jan 26, 2025
7082cca
rename
cartercanedy Jan 27, 2025
3b14b21
use `read_entity` & `update_entity`
cartercanedy Jan 28, 2025
00f8d69
return fast if pre-task label list is empty or task isn't from a file…
cartercanedy Jan 28, 2025
1431613
fix tests
cartercanedy Jan 28, 2025
876f210
refactor task queueing out of `terminal_view`
cartercanedy Jan 28, 2025
69d307d
Merge branch 'main' into task-queue
cartercanedy Jan 30, 2025
34a2194
Merge branch 'main' into task-queue
cartercanedy Jan 31, 2025
File filter

Filter by extension

Filter by extension


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

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ markup5ever_rcdom = "0.3.0"
nanoid = "0.4"
nbformat = { version = "0.10.0" }
nix = "0.29"
nohash-hasher = "0.2.0"
num-format = "0.4.4"
ordered-float = "2.1.1"
palette = { version = "0.7.5", default-features = false, features = ["std"] }
Expand Down
6 changes: 4 additions & 2 deletions crates/editor/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4362,9 +4362,10 @@ impl Editor {
match action {
CodeActionsItem::Task(task_source_kind, resolved_task) => {
workspace.update(cx, |workspace, cx| {
workspace::tasks::schedule_resolved_task(
workspace::tasks::schedule_resolved_tasks(
workspace,
task_source_kind,
vec![],
resolved_task,
false,
cx,
Expand Down Expand Up @@ -5334,9 +5335,10 @@ impl Editor {

workspace
.update(&mut cx, |workspace, cx| {
workspace::tasks::schedule_resolved_task(
workspace::tasks::schedule_resolved_tasks(
workspace,
task_source_kind,
vec![],
resolved_task,
false,
cx,
Expand Down
1 change: 1 addition & 0 deletions crates/project/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ util.workspace = true
url.workspace = true
which.workspace = true
fancy-regex.workspace = true
nohash-hasher.workspace = true

[dev-dependencies]
client = { workspace = true, features = ["test-support"] }
Expand Down
185 changes: 185 additions & 0 deletions crates/project/src/graph.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
use nohash_hasher::BuildNoHashHasher;

type HashMap = std::collections::hash_map::HashMap<u32, Vec<u32>, BuildNoHashHasher<u32>>;

/// A minimal graph implementation
/// Only useful for detecting cycles and reported the identities of the nodes that form the cycle
#[derive(Debug)]
pub(crate) struct Graph {
// This might end up being more cache efficient if implemented with Vec<Vec<NodeIndex>> instead
// and we assuming continuous indexing but this enables arbitrary node identities
adjacencies: HashMap,
}

#[derive(Debug)]
pub(crate) enum Error {
Cycle { src_node: u32, dst_node: u32 },
NodeNotFound,
}

impl Graph {
/// Creates a new, empty [`Graph`]
pub fn new() -> Self {
Self {
adjacencies: HashMap::default(),
}
}

/// Adds an edge to the graph, adding the nodes if not present
pub fn add_edge(&mut self, src_node: u32, dst_node: u32) {
match self.adjacencies.get_mut(&src_node) {
Some(neighbors) if !neighbors.contains(&dst_node) => {
neighbors.push(dst_node);
}
None => {
self.adjacencies.insert(src_node, vec![dst_node]);
}
_ => (),
}
}

/// Adds an arbitrarily identified node to the graph
pub fn add_node(&mut self, node: u32) {
if let None = self.adjacencies.get(&node) {
self.adjacencies.insert(node, vec![]);
}
}

fn dfs(&self, node: u32, visited: &mut Vec<u32>, stack: &mut Vec<u32>) -> Option<Error> {
if visited.contains(&node) {
return None;
}

let Some(neighbors) = self.adjacencies.get(&node) else {
visited.push(node);
return None;
};

stack.push(node);

for neighbor in neighbors {
if stack.contains(&neighbor) {
return Some(Error::Cycle {
src_node: *neighbor,
dst_node: node,
});
} else if let cycle @ Some(_) = self.dfs(*neighbor, visited, stack) {
return cycle;
}
}

stack.pop();
visited.push(node);

None
}

/// Build a subgraph starting from `start_node` from the nodes of this graph
pub fn subgraph(&self, start_node: u32) -> Result<Graph, Error> {
let Some(_) = self.adjacencies.get(&start_node) else {
return Err(Error::NodeNotFound);
};

let mut graph = Self::new();

self.collect_subgraph_nodes(&mut graph, start_node);

Ok(graph)
}

fn collect_subgraph_nodes(&self, other: &mut Self, node: u32) {
if !other.adjacencies.contains_key(&node) {
other.add_node(node);

let Some(neighbors) = self.adjacencies.get(&node) else {
return;
};

for neighbor in neighbors {
self.collect_subgraph_nodes(other, *neighbor);
other.add_edge(node, *neighbor);
}
}
}

/// Topologically sorts nodes, or return the nodes that form a cycle
pub fn topo_sort(&self) -> Result<Vec<u32>, Error> {
let mut visited = vec![];
let mut stack = vec![];

for node in self.adjacencies.keys() {
if let Some(error) = self.dfs(*node, &mut visited, &mut stack) {
return Err(error);
}
}

visited.reverse();

Ok(visited)
}
}

#[cfg(test)]
mod graph_test {
use itertools::Itertools;
use pretty_assertions::assert_matches;

use super::Graph;

#[test]
fn finds_no_cycle() {
const GRAPH: [[u32; 2]; 2] = [[1, 2], [2, 3]];

let mut graph = Graph::new();

for edge in GRAPH.iter() {
graph.add_edge(edge[0], edge[1]);
}

assert_matches!(graph.topo_sort(), Ok(_));
}

#[test]
fn subgraph_correct() {
const GRAPH: [[u32; 2]; 6] = [[3, 1], [2, 1], [1, 4], [4, 5], [3, 4], [2, 4]];

let mut graph = Graph::new();
for [src, dst] in &GRAPH {
graph.add_edge(*src, *dst);
}

let subgraph = graph.subgraph(2).unwrap();
for i in &[1, 2, 4, 5] {
assert!(
subgraph.adjacencies.contains_key(i),
"subgraph didn't contain key {i}; subgraph keys: {:#?}",
subgraph.adjacencies.keys().collect_vec()
);
}
}

#[test]
fn finds_cycle() {
const GRAPH: [[u32; 2]; 3] = [[1, 2], [2, 3], [3, 1]];

let mut graph = Graph::new();
for edge in GRAPH {
graph.add_edge(edge[0], edge[1]);
}

assert_matches!(graph.topo_sort(), Err(_));
}

#[test]
fn sorts_correctly() {
const GRAPH: [[u32; 2]; 6] = [[2, 3], [2, 1], [4, 1], [2, 4], [5, 4], [5, 1]];

let mut graph = Graph::new();

for [src, dst] in &GRAPH {
graph.add_edge(*src, *dst);
}

assert_eq!(graph.topo_sort().unwrap(), vec![2, 3, 5, 4, 1]);
}
}
1 change: 1 addition & 0 deletions crates/project/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod color_extractor;
pub mod connection_manager;
pub mod debounced_delay;
pub mod git;
mod graph;
pub mod image_store;
pub mod lsp_command;
pub mod lsp_ext_command;
Expand Down
Loading