tasks.rs

 1use crate::Editor;
 2
 3use gpui::{App, Task, Window};
 4use project::Location;
 5use task::{TaskContext, TaskVariables, VariableName};
 6use text::{ToOffset, ToPoint};
 7
 8impl Editor {
 9    pub fn task_context(&self, window: &mut Window, cx: &mut App) -> Task<Option<TaskContext>> {
10        let Some(project) = self.project.clone() else {
11            return Task::ready(None);
12        };
13        let (selection, buffer, editor_snapshot) = {
14            let selection = self.selections.newest_adjusted(cx);
15            let Some((buffer, _)) = self
16                .buffer()
17                .read(cx)
18                .point_to_buffer_offset(selection.start, cx)
19            else {
20                return Task::ready(None);
21            };
22            let snapshot = self.snapshot(window, cx);
23            (selection, buffer, snapshot)
24        };
25        let selection_range = selection.range();
26        let start = editor_snapshot
27            .display_snapshot
28            .buffer_snapshot
29            .anchor_after(selection_range.start)
30            .text_anchor;
31        let end = editor_snapshot
32            .display_snapshot
33            .buffer_snapshot
34            .anchor_after(selection_range.end)
35            .text_anchor;
36        let location = Location {
37            buffer,
38            range: start..end,
39        };
40        let captured_variables = {
41            let mut variables = TaskVariables::default();
42            let buffer = location.buffer.read(cx);
43            let buffer_id = buffer.remote_id();
44            let snapshot = buffer.snapshot();
45            let starting_point = location.range.start.to_point(&snapshot);
46            let starting_offset = starting_point.to_offset(&snapshot);
47            for (_, tasks) in self
48                .tasks
49                .range((buffer_id, 0)..(buffer_id, starting_point.row + 1))
50            {
51                if !tasks
52                    .context_range
53                    .contains(&crate::BufferOffset(starting_offset))
54                {
55                    continue;
56                }
57                for (capture_name, value) in tasks.extra_variables.iter() {
58                    variables.insert(
59                        VariableName::Custom(capture_name.to_owned().into()),
60                        value.clone(),
61                    );
62                }
63            }
64            variables
65        };
66
67        project.update(cx, |project, cx| {
68            project.task_store().update(cx, |task_store, cx| {
69                task_store.task_context_for_location(captured_variables, location, cx)
70            })
71        })
72    }
73}