Limit the extension tasks in the modal to current language only (#10207)

Release Notes:

- N/A
This commit is contained in:
Kirill Bulatov 2024-04-05 23:18:32 +02:00 committed by GitHub
parent c851e6edba
commit 7b636d9774
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 128 additions and 186 deletions

View file

@ -1,8 +1,8 @@
use std::path::PathBuf;
use std::{path::PathBuf, sync::Arc};
use editor::Editor;
use gpui::{AppContext, ViewContext, WindowContext};
use language::Point;
use gpui::{AppContext, ViewContext, WeakView, WindowContext};
use language::{Language, Point};
use modal::{Spawn, TasksModal};
use project::{Location, WorktreeId};
use task::{Task, TaskContext, TaskVariables, VariableName};
@ -19,9 +19,7 @@ pub fn init(cx: &mut AppContext) {
.register_action(move |workspace, action: &modal::Rerun, cx| {
if let Some((task, old_context)) =
workspace.project().update(cx, |project, cx| {
project
.task_inventory()
.update(cx, |inventory, cx| inventory.last_scheduled_task(cx))
project.task_inventory().read(cx).last_scheduled_task()
})
{
let task_context = if action.reevaluate_context {
@ -30,7 +28,7 @@ pub fn init(cx: &mut AppContext) {
} else {
old_context
};
schedule_task(workspace, task.as_ref(), task_context, false, cx)
schedule_task(workspace, &task, task_context, false, cx)
};
});
},
@ -57,19 +55,16 @@ fn spawn_task_with_name(name: String, cx: &mut ViewContext<Workspace>) {
cx.spawn(|workspace, mut cx| async move {
let did_spawn = workspace
.update(&mut cx, |this, cx| {
let active_item = this
.active_item(cx)
.and_then(|item| item.project_path(cx))
.map(|path| path.worktree_id);
let (worktree, language) = active_item_selection_properties(&workspace, cx);
let tasks = this.project().update(cx, |project, cx| {
project.task_inventory().update(cx, |inventory, cx| {
inventory.list_tasks(None, active_item, false, cx)
inventory.list_tasks(language, worktree, false, cx)
})
});
let (_, target_task) = tasks.into_iter().find(|(_, task)| task.name() == name)?;
let cwd = task_cwd(this, cx).log_err().flatten();
let task_context = task_context(this, cwd, cx);
schedule_task(this, target_task.as_ref(), task_context, false, cx);
schedule_task(this, &target_task, task_context, false, cx);
Some(())
})
.ok()
@ -86,6 +81,33 @@ fn spawn_task_with_name(name: String, cx: &mut ViewContext<Workspace>) {
.detach();
}
fn active_item_selection_properties(
workspace: &WeakView<Workspace>,
cx: &mut WindowContext,
) -> (Option<WorktreeId>, Option<Arc<Language>>) {
let active_item = workspace
.update(cx, |workspace, cx| workspace.active_item(cx))
.ok()
.flatten();
let worktree_id = active_item
.as_ref()
.and_then(|item| item.project_path(cx))
.map(|path| path.worktree_id);
let language = active_item
.and_then(|active_item| active_item.act_as::<Editor>(cx))
.and_then(|editor| {
editor.update(cx, |editor, cx| {
let selection = editor.selections.newest::<usize>(cx);
let (buffer, buffer_position, _) = editor
.buffer()
.read(cx)
.point_to_buffer_offset(selection.start, cx)?;
buffer.read(cx).language_at(buffer_position)
})
});
(worktree_id, language)
}
fn task_context(
workspace: &Workspace,
cwd: Option<PathBuf>,
@ -93,8 +115,7 @@ fn task_context(
) -> TaskContext {
let current_editor = workspace
.active_item(cx)
.and_then(|item| item.act_as::<Editor>(cx))
.clone();
.and_then(|item| item.act_as::<Editor>(cx));
if let Some(current_editor) = current_editor {
(|| {
let editor = current_editor.read(cx);
@ -190,7 +211,7 @@ fn task_context(
fn schedule_task(
workspace: &Workspace,
task: &dyn Task,
task: &Arc<dyn Task>,
task_cx: TaskContext,
omit_history: bool,
cx: &mut ViewContext<'_, Workspace>,
@ -200,7 +221,7 @@ fn schedule_task(
if !omit_history {
workspace.project().update(cx, |project, cx| {
project.task_inventory().update(cx, |inventory, _| {
inventory.task_scheduled(task.id().clone(), task_cx);
inventory.task_scheduled(Arc::clone(task), task_cx);
})
});
}

View file

@ -1,5 +1,6 @@
use std::{path::PathBuf, sync::Arc};
use std::sync::Arc;
use crate::{active_item_selection_properties, schedule_task};
use fuzzy::{StringMatch, StringMatchCandidate};
use gpui::{
impl_actions, rems, AppContext, DismissEvent, EventEmitter, FocusableView, InteractiveElement,
@ -10,7 +11,7 @@ use picker::{
highlighted_match_with_paths::{HighlightedMatchWithPaths, HighlightedText},
Picker, PickerDelegate,
};
use project::{Inventory, ProjectPath, TaskSourceKind};
use project::{Inventory, TaskSourceKind};
use task::{oneshot_source::OneshotSource, Task, TaskContext};
use ui::{
div, v_flex, ButtonCommon, ButtonSize, Clickable, Color, FluentBuilder as _, IconButton,
@ -20,7 +21,6 @@ use ui::{
use util::{paths::PathExt, ResultExt};
use workspace::{ModalView, Workspace};
use crate::schedule_task;
use serde::Deserialize;
/// Spawn a task with name or open tasks modal
@ -116,20 +116,6 @@ impl TasksModalDelegate {
Some(())
});
}
fn active_item_path(
workspace: &WeakView<Workspace>,
cx: &mut ViewContext<'_, Picker<Self>>,
) -> Option<(PathBuf, ProjectPath)> {
let workspace = workspace.upgrade()?.read(cx);
let project = workspace.project().read(cx);
let active_item = workspace.active_item(cx)?;
active_item.project_path(cx).and_then(|project_path| {
project
.worktree_for_id(project_path.worktree_id, cx)
.map(|worktree| worktree.read(cx).abs_path().join(&project_path.path))
.zip(Some(project_path))
})
}
}
pub(crate) struct TasksModal {
@ -212,15 +198,10 @@ impl PickerDelegate for TasksModalDelegate {
let Some(candidates) = picker
.update(&mut cx, |picker, cx| {
let candidates = picker.delegate.candidates.get_or_insert_with(|| {
let (path, worktree) =
match Self::active_item_path(&picker.delegate.workspace, cx) {
Some((abs_path, project_path)) => {
(Some(abs_path), Some(project_path.worktree_id))
}
None => (None, None),
};
let (worktree, language) =
active_item_selection_properties(&picker.delegate.workspace, cx);
picker.delegate.inventory.update(cx, |inventory, cx| {
inventory.list_tasks(path.as_deref(), worktree, true, cx)
inventory.list_tasks(language, worktree, true, cx)
})
});
@ -283,7 +264,7 @@ impl PickerDelegate for TasksModalDelegate {
.update(cx, |workspace, cx| {
schedule_task(
workspace,
task.as_ref(),
&task,
self.task_context.clone(),
omit_history_entry,
cx,
@ -308,7 +289,7 @@ impl PickerDelegate for TasksModalDelegate {
let (source_kind, _) = &candidates.get(hit.candidate_id)?;
let details = match source_kind {
TaskSourceKind::UserInput => "user input".to_string(),
TaskSourceKind::Buffer => "language extension".to_string(),
TaskSourceKind::Language { name } => format!("{name} language"),
TaskSourceKind::Worktree { abs_path, .. } | TaskSourceKind::AbsPath(abs_path) => {
abs_path.compact().to_string_lossy().to_string()
}
@ -381,7 +362,7 @@ impl PickerDelegate for TasksModalDelegate {
.update(cx, |workspace, cx| {
schedule_task(
workspace,
task.as_ref(),
&task,
self.task_context.clone(),
omit_history_entry,
cx,