Merge branch 'main' into zed2-breadcrumbs

This commit is contained in:
Julia 2023-11-30 11:38:29 -05:00
commit 428c517693
197 changed files with 11947 additions and 8365 deletions

88
Cargo.lock generated
View File

@ -19,6 +19,25 @@ dependencies = [
"workspace",
]
[[package]]
name = "activity_indicator2"
version = "0.1.0"
dependencies = [
"anyhow",
"auto_update2",
"editor2",
"futures 0.3.28",
"gpui2",
"language2",
"project2",
"settings2",
"smallvec",
"theme2",
"ui2",
"util",
"workspace2",
]
[[package]]
name = "addr2line"
version = "0.17.0"
@ -1210,6 +1229,7 @@ dependencies = [
"fs2",
"futures 0.3.28",
"gpui2",
"image",
"language2",
"live_kit_client2",
"log",
@ -1221,6 +1241,8 @@ dependencies = [
"serde_derive",
"serde_json",
"settings2",
"smallvec",
"ui2",
"util",
"workspace2",
]
@ -9485,6 +9507,27 @@ dependencies = [
"workspace",
]
[[package]]
name = "theme_selector2"
version = "0.1.0"
dependencies = [
"editor2",
"feature_flags2",
"fs2",
"fuzzy2",
"gpui2",
"log",
"parking_lot 0.11.2",
"picker2",
"postage",
"settings2",
"smol",
"theme2",
"ui2",
"util",
"workspace2",
]
[[package]]
name = "thiserror"
version = "1.0.48"
@ -9944,7 +9987,7 @@ dependencies = [
[[package]]
name = "tree-sitter"
version = "0.20.10"
source = "git+https://github.com/tree-sitter/tree-sitter?rev=35a6052fbcafc5e5fc0f9415b8652be7dcaf7222#35a6052fbcafc5e5fc0f9415b8652be7dcaf7222"
source = "git+https://github.com/tree-sitter/tree-sitter?rev=3b0159d25559b603af566ade3c83d930bf466db1#3b0159d25559b603af566ade3c83d930bf466db1"
dependencies = [
"cc",
"regex",
@ -10192,6 +10235,15 @@ dependencies = [
"tree-sitter",
]
[[package]]
name = "tree-sitter-uiua"
version = "0.3.3"
source = "git+https://github.com/shnarazk/tree-sitter-uiua?rev=9260f11be5900beda4ee6d1a24ab8ddfaf5a19b2#9260f11be5900beda4ee6d1a24ab8ddfaf5a19b2"
dependencies = [
"cc",
"tree-sitter",
]
[[package]]
name = "tree-sitter-vue"
version = "0.0.1"
@ -11059,6 +11111,31 @@ dependencies = [
"workspace",
]
[[package]]
name = "welcome2"
version = "0.1.0"
dependencies = [
"anyhow",
"client2",
"db2",
"editor2",
"fs2",
"fuzzy2",
"gpui2",
"install_cli2",
"log",
"picker2",
"project2",
"schemars",
"serde",
"settings2",
"theme2",
"theme_selector2",
"ui2",
"util",
"workspace2",
]
[[package]]
name = "which"
version = "4.4.2"
@ -11513,7 +11590,7 @@ dependencies = [
[[package]]
name = "zed"
version = "0.115.0"
version = "0.116.0"
dependencies = [
"activity_indicator",
"ai",
@ -11630,6 +11707,7 @@ dependencies = [
"tree-sitter-svelte",
"tree-sitter-toml",
"tree-sitter-typescript",
"tree-sitter-uiua",
"tree-sitter-vue",
"tree-sitter-yaml",
"unindent",
@ -11655,16 +11733,19 @@ dependencies = [
name = "zed2"
version = "0.109.0"
dependencies = [
"activity_indicator2",
"ai2",
"anyhow",
"async-compression",
"async-recursion 0.3.2",
"async-tar",
"async-trait",
"audio2",
"auto_update2",
"backtrace",
"breadcrumbs2",
"call2",
"channel2",
"chrono",
"cli",
"client2",
@ -11723,6 +11804,7 @@ dependencies = [
"terminal_view2",
"text2",
"theme2",
"theme_selector2",
"thiserror",
"tiny_http",
"toml 0.5.11",
@ -11752,6 +11834,7 @@ dependencies = [
"tree-sitter-svelte",
"tree-sitter-toml",
"tree-sitter-typescript",
"tree-sitter-uiua",
"tree-sitter-vue",
"tree-sitter-yaml",
"unindent",
@ -11759,6 +11842,7 @@ dependencies = [
"urlencoding",
"util",
"uuid 1.4.1",
"welcome2",
"workspace2",
"zed_actions2",
]

View File

@ -1,6 +1,7 @@
[workspace]
members = [
"crates/activity_indicator",
"crates/activity_indicator2",
"crates/ai",
"crates/assistant",
"crates/audio",
@ -108,6 +109,7 @@ members = [
"crates/theme2",
"crates/theme_importer",
"crates/theme_selector",
"crates/theme_selector2",
"crates/ui2",
"crates/util",
"crates/semantic_index",
@ -116,6 +118,7 @@ members = [
"crates/vcs_menu",
"crates/workspace2",
"crates/welcome",
"crates/welcome2",
"crates/xtask",
"crates/zed",
"crates/zed2",
@ -196,8 +199,10 @@ tree-sitter-lua = "0.0.14"
tree-sitter-nix = { git = "https://github.com/nix-community/tree-sitter-nix", rev = "66e3e9ce9180ae08fc57372061006ef83f0abde7" }
tree-sitter-nu = { git = "https://github.com/nushell/tree-sitter-nu", rev = "786689b0562b9799ce53e824cb45a1a2a04dc673"}
tree-sitter-vue = {git = "https://github.com/zed-industries/tree-sitter-vue", rev = "9b6cb221ccb8d0b956fcb17e9a1efac2feefeb58"}
tree-sitter-uiua = {git = "https://github.com/shnarazk/tree-sitter-uiua", rev = "9260f11be5900beda4ee6d1a24ab8ddfaf5a19b2"}
[patch.crates-io]
tree-sitter = { git = "https://github.com/tree-sitter/tree-sitter", rev = "35a6052fbcafc5e5fc0f9415b8652be7dcaf7222" }
tree-sitter = { git = "https://github.com/tree-sitter/tree-sitter", rev = "3b0159d25559b603af566ade3c83d930bf466db1" }
async-task = { git = "https://github.com/zed-industries/async-task", rev = "341b57d6de98cdfd7b418567b8de2022ca993a6e" }
# TODO - Remove when a version is released with this PR: https://github.com/servo/core-foundation-rs/pull/457

View File

@ -0,0 +1,3 @@
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M8.00001 12L3.5 7.50001M8.00001 12L12.5 7.50001M8.00001 12L8.00001 3.00001" stroke="black" stroke-width="1.25" stroke-linecap="round" stroke-linejoin="round"/>
</svg>

After

Width:  |  Height:  |  Size: 272 B

View File

@ -1,3 +1,3 @@
<svg width="14" height="14" viewBox="0 0 14 14" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M3.125 6.99344L6.35938 3.63281M3.125 6.99344L6.35938 10.3672M3.125 6.99344H11" stroke="black" stroke-width="1.25" stroke-linecap="round" stroke-linejoin="round"/>
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M3.5 7.50001L8 3M3.5 7.50001L8 12M3.5 7.50001H12.5" stroke="black" stroke-width="1.25" stroke-linecap="round" stroke-linejoin="round"/>
</svg>

Before

Width:  |  Height:  |  Size: 275 B

After

Width:  |  Height:  |  Size: 248 B

View File

@ -1,3 +1,3 @@
<svg width="14" height="14" viewBox="0 0 14 14" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M10.8906 7.00125L7.64062 3.64062M10.8906 7.00125L7.64062 10.375M10.8906 7.00125H3" stroke="black" stroke-width="1.25" stroke-linecap="round" stroke-linejoin="round"/>
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M12.5 7.5L8 12M12.5 7.5L8 3M12.5 7.5L3.5 7.5" stroke="black" stroke-width="1.25" stroke-linecap="round" stroke-linejoin="round"/>
</svg>

Before

Width:  |  Height:  |  Size: 279 B

After

Width:  |  Height:  |  Size: 242 B

View File

@ -0,0 +1,3 @@
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M7.99999 3.00001L12.5 7.50001M7.99999 3.00001L3.49999 7.50001M7.99999 3.00001L7.99999 12" stroke="black" stroke-width="1.25" stroke-linecap="round" stroke-linejoin="round"/>
</svg>

After

Width:  |  Height:  |  Size: 286 B

3
assets/icons/command.svg Normal file
View File

@ -0,0 +1,3 @@
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M11 3.625C11 2.86561 11.6156 2.25 12.375 2.25C13.1344 2.25 13.75 2.86561 13.75 3.625C13.75 4.38401 13.135 4.99939 12.3761 5C12.3758 5 12.3754 5 12.375 5H11V3.625ZM9.75 5V3.625C9.75 2.17525 10.9253 1 12.375 1C13.8247 1 15 2.17525 15 3.625C15 4.98872 13.9601 6.10955 12.63 6.23777V6.25H12.3766C12.376 6.25 12.3755 6.25 12.375 6.25H11V9.75H12.375C13.8247 9.75 15 10.9253 15 12.375C15 13.8247 13.8247 15 12.375 15C11.0113 15 9.89045 13.9601 9.76223 12.63H9.75V12.3773L9.75 12.375V11H6.25V12.375C6.25 13.8247 5.07475 15 3.625 15C2.17525 15 1 13.8247 1 12.375C1 11.0113 2.03991 9.89045 3.37 9.76223V9.75H3.62274L3.625 9.75H5L5 6.25H3.625C2.17525 6.25 1 5.07475 1 3.625C1 2.17525 2.17525 1 3.625 1C4.98872 1 6.10955 2.03991 6.23777 3.37H6.25L6.25 5L9.75 5ZM9.75 6.25L6.25 6.25L6.25 9.75H9.75V6.25ZM3.625 11H5V12.375C5 13.1344 4.38439 13.75 3.625 13.75C2.86561 13.75 2.25 13.1344 2.25 12.375C2.25 11.6162 2.86472 11.0009 3.62336 11L3.625 11ZM11 12.3766C11.0009 13.1353 11.6162 13.75 12.375 13.75C13.1344 13.75 13.75 13.1344 13.75 12.375C13.75 11.6156 13.1344 11 12.375 11H11V12.375L11 12.3766ZM3.625 5C2.86561 5 2.25 4.38439 2.25 3.625C2.25 2.86561 2.86561 2.25 3.625 2.25C4.38439 2.25 5 2.86561 5 3.625V5H3.625Z" fill="black"/>
</svg>

After

Width:  |  Height:  |  Size: 1.3 KiB

3
assets/icons/control.svg Normal file
View File

@ -0,0 +1,3 @@
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M3.5 6.12488L7.64656 1.97853C7.84183 1.78328 8.1584 1.78329 8.35366 1.97854L12.5 6.12488" stroke="black" stroke-width="1.25" stroke-linecap="round"/>
</svg>

After

Width:  |  Height:  |  Size: 262 B

3
assets/icons/option.svg Normal file
View File

@ -0,0 +1,3 @@
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M5.35606 1.005H1.62545C1.28002 1.005 1 1.28502 1 1.63044C1 1.97587 1.28002 2.25589 1.62545 2.25589L5.35606 2.25589C5.62311 2.25589 5.8607 2.42545 5.94752 2.67799L9.75029 13.7387C10.0108 14.4963 10.7235 15.005 11.5247 15.005H14.3746C14.72 15.005 15 14.725 15 14.3796C15 14.0341 14.72 13.7541 14.3746 13.7541H11.5247C11.2576 13.7541 11.02 13.5845 10.9332 13.332L7.13046 2.27128C6.86998 1.51366 6.15721 1.005 5.35606 1.005ZM14.3745 1.005H9.75125C9.40582 1.005 9.1258 1.28502 9.1258 1.63044C9.1258 1.97587 9.40582 2.25589 9.75125 2.25589L14.3745 2.25589C14.72 2.25589 15 1.97587 15 1.63044C15 1.28502 14.72 1.005 14.3745 1.005Z" fill="black"/>
</svg>

After

Width:  |  Height:  |  Size: 792 B

3
assets/icons/return.svg Normal file
View File

@ -0,0 +1,3 @@
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M8.375 1.63C8.375 1.28482 8.65482 1.005 9 1.005H12.375C13.8247 1.005 15 2.18025 15 3.63V7.625C15 9.07474 13.8247 10.25 12.375 10.25H3.13388L6.07194 13.1881C6.31602 13.4321 6.31602 13.8279 6.07194 14.0719C5.82786 14.316 5.43214 14.316 5.18806 14.0719L1.18306 10.0669C0.938981 9.82286 0.938981 9.42714 1.18306 9.18306L5.18306 5.18306C5.42714 4.93898 5.82286 4.93898 6.06694 5.18306C6.31102 5.42714 6.31102 5.82286 6.06694 6.06694L3.13388 9H12.375C13.1344 9 13.75 8.38439 13.75 7.625V3.63C13.75 2.87061 13.1344 2.255 12.375 2.255H9C8.65482 2.255 8.375 1.97518 8.375 1.63Z" fill="black"/>
</svg>

After

Width:  |  Height:  |  Size: 737 B

3
assets/icons/shift.svg Normal file
View File

@ -0,0 +1,3 @@
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M2.46475 7.99652L7.85304 2.15921C7.93223 2.07342 8.06777 2.07341 8.14696 2.15921L13.5352 7.99652C13.7126 8.18869 13.5763 8.5 13.3148 8.5H10.5V13.7C10.5 13.8657 10.3657 14 10.2 14H5.8C5.63431 14 5.5 13.8657 5.5 13.7V8.5H2.6852C2.42367 8.5 2.28737 8.18869 2.46475 7.99652Z" stroke="black" stroke-width="1.25"/>
</svg>

After

Width:  |  Height:  |  Size: 421 B

View File

@ -0,0 +1,28 @@
[package]
name = "activity_indicator2"
version = "0.1.0"
edition = "2021"
publish = false
[lib]
path = "src/activity_indicator.rs"
doctest = false
[dependencies]
auto_update = { path = "../auto_update2", package = "auto_update2" }
editor = { path = "../editor2", package = "editor2" }
language = { path = "../language2", package = "language2" }
gpui = { path = "../gpui2", package = "gpui2" }
project = { path = "../project2", package = "project2" }
settings = { path = "../settings2", package = "settings2" }
ui = { path = "../ui2", package = "ui2" }
util = { path = "../util" }
theme = { path = "../theme2", package = "theme2" }
workspace = { path = "../workspace2", package = "workspace2" }
anyhow.workspace = true
futures.workspace = true
smallvec.workspace = true
[dev-dependencies]
editor = { path = "../editor2", package = "editor2", features = ["test-support"] }

View File

@ -0,0 +1,333 @@
use auto_update::{AutoUpdateStatus, AutoUpdater, DismissErrorMessage};
use editor::Editor;
use futures::StreamExt;
use gpui::{
actions, svg, AppContext, CursorStyle, Div, EventEmitter, InteractiveElement as _, Model,
ParentElement as _, Render, SharedString, Stateful, StatefulInteractiveElement, Styled, View,
ViewContext, VisualContext as _,
};
use language::{LanguageRegistry, LanguageServerBinaryStatus};
use project::{LanguageServerProgress, Project};
use smallvec::SmallVec;
use std::{cmp::Reverse, fmt::Write, sync::Arc};
use ui::h_stack;
use util::ResultExt;
use workspace::{item::ItemHandle, StatusItemView, Workspace};
actions!(ShowErrorMessage);
const DOWNLOAD_ICON: &str = "icons/download.svg";
const WARNING_ICON: &str = "icons/warning.svg";
pub enum Event {
ShowError { lsp_name: Arc<str>, error: String },
}
pub struct ActivityIndicator {
statuses: Vec<LspStatus>,
project: Model<Project>,
auto_updater: Option<Model<AutoUpdater>>,
}
struct LspStatus {
name: Arc<str>,
status: LanguageServerBinaryStatus,
}
struct PendingWork<'a> {
language_server_name: &'a str,
progress_token: &'a str,
progress: &'a LanguageServerProgress,
}
#[derive(Default)]
struct Content {
icon: Option<&'static str>,
message: String,
on_click: Option<Arc<dyn Fn(&mut ActivityIndicator, &mut ViewContext<ActivityIndicator>)>>,
}
impl ActivityIndicator {
pub fn new(
workspace: &mut Workspace,
languages: Arc<LanguageRegistry>,
cx: &mut ViewContext<Workspace>,
) -> View<ActivityIndicator> {
let project = workspace.project().clone();
let auto_updater = AutoUpdater::get(cx);
let this = cx.build_view(|cx: &mut ViewContext<Self>| {
let mut status_events = languages.language_server_binary_statuses();
cx.spawn(|this, mut cx| async move {
while let Some((language, event)) = status_events.next().await {
this.update(&mut cx, |this, cx| {
this.statuses.retain(|s| s.name != language.name());
this.statuses.push(LspStatus {
name: language.name(),
status: event,
});
cx.notify();
})?;
}
anyhow::Ok(())
})
.detach();
cx.observe(&project, |_, _, cx| cx.notify()).detach();
if let Some(auto_updater) = auto_updater.as_ref() {
cx.observe(auto_updater, |_, _, cx| cx.notify()).detach();
}
// cx.observe_active_labeled_tasks(|_, cx| cx.notify())
// .detach();
Self {
statuses: Default::default(),
project: project.clone(),
auto_updater,
}
});
cx.subscribe(&this, move |workspace, _, event, cx| match event {
Event::ShowError { lsp_name, error } => {
if let Some(buffer) = project
.update(cx, |project, cx| project.create_buffer(error, None, cx))
.log_err()
{
buffer.update(cx, |buffer, cx| {
buffer.edit(
[(0..0, format!("Language server error: {}\n\n", lsp_name))],
None,
cx,
);
});
workspace.add_item(
Box::new(cx.build_view(|cx| {
Editor::for_buffer(buffer, Some(project.clone()), cx)
})),
cx,
);
}
}
})
.detach();
this
}
fn show_error_message(&mut self, _: &ShowErrorMessage, cx: &mut ViewContext<Self>) {
self.statuses.retain(|status| {
if let LanguageServerBinaryStatus::Failed { error } = &status.status {
cx.emit(Event::ShowError {
lsp_name: status.name.clone(),
error: error.clone(),
});
false
} else {
true
}
});
cx.notify();
}
fn dismiss_error_message(&mut self, _: &DismissErrorMessage, cx: &mut ViewContext<Self>) {
if let Some(updater) = &self.auto_updater {
updater.update(cx, |updater, cx| {
updater.dismiss_error(cx);
});
}
cx.notify();
}
fn pending_language_server_work<'a>(
&self,
cx: &'a AppContext,
) -> impl Iterator<Item = PendingWork<'a>> {
self.project
.read(cx)
.language_server_statuses()
.rev()
.filter_map(|status| {
if status.pending_work.is_empty() {
None
} else {
let mut pending_work = status
.pending_work
.iter()
.map(|(token, progress)| PendingWork {
language_server_name: status.name.as_str(),
progress_token: token.as_str(),
progress,
})
.collect::<SmallVec<[_; 4]>>();
pending_work.sort_by_key(|work| Reverse(work.progress.last_update_at));
Some(pending_work)
}
})
.flatten()
}
fn content_to_render(&mut self, cx: &mut ViewContext<Self>) -> Content {
// Show any language server has pending activity.
let mut pending_work = self.pending_language_server_work(cx);
if let Some(PendingWork {
language_server_name,
progress_token,
progress,
}) = pending_work.next()
{
let mut message = language_server_name.to_string();
message.push_str(": ");
if let Some(progress_message) = progress.message.as_ref() {
message.push_str(progress_message);
} else {
message.push_str(progress_token);
}
if let Some(percentage) = progress.percentage {
write!(&mut message, " ({}%)", percentage).unwrap();
}
let additional_work_count = pending_work.count();
if additional_work_count > 0 {
write!(&mut message, " + {} more", additional_work_count).unwrap();
}
return Content {
icon: None,
message,
on_click: None,
};
}
// Show any language server installation info.
let mut downloading = SmallVec::<[_; 3]>::new();
let mut checking_for_update = SmallVec::<[_; 3]>::new();
let mut failed = SmallVec::<[_; 3]>::new();
for status in &self.statuses {
let name = status.name.clone();
match status.status {
LanguageServerBinaryStatus::CheckingForUpdate => checking_for_update.push(name),
LanguageServerBinaryStatus::Downloading => downloading.push(name),
LanguageServerBinaryStatus::Failed { .. } => failed.push(name),
LanguageServerBinaryStatus::Downloaded | LanguageServerBinaryStatus::Cached => {}
}
}
if !downloading.is_empty() {
return Content {
icon: Some(DOWNLOAD_ICON),
message: format!(
"Downloading {} language server{}...",
downloading.join(", "),
if downloading.len() > 1 { "s" } else { "" }
),
on_click: None,
};
} else if !checking_for_update.is_empty() {
return Content {
icon: Some(DOWNLOAD_ICON),
message: format!(
"Checking for updates to {} language server{}...",
checking_for_update.join(", "),
if checking_for_update.len() > 1 {
"s"
} else {
""
}
),
on_click: None,
};
} else if !failed.is_empty() {
return Content {
icon: Some(WARNING_ICON),
message: format!(
"Failed to download {} language server{}. Click to show error.",
failed.join(", "),
if failed.len() > 1 { "s" } else { "" }
),
on_click: Some(Arc::new(|this, cx| {
this.show_error_message(&Default::default(), cx)
})),
};
}
// Show any application auto-update info.
if let Some(updater) = &self.auto_updater {
return match &updater.read(cx).status() {
AutoUpdateStatus::Checking => Content {
icon: Some(DOWNLOAD_ICON),
message: "Checking for Zed updates…".to_string(),
on_click: None,
},
AutoUpdateStatus::Downloading => Content {
icon: Some(DOWNLOAD_ICON),
message: "Downloading Zed update…".to_string(),
on_click: None,
},
AutoUpdateStatus::Installing => Content {
icon: Some(DOWNLOAD_ICON),
message: "Installing Zed update…".to_string(),
on_click: None,
},
AutoUpdateStatus::Updated => Content {
icon: None,
message: "Click to restart and update Zed".to_string(),
on_click: Some(Arc::new(|_, cx| {
workspace::restart(&Default::default(), cx)
})),
},
AutoUpdateStatus::Errored => Content {
icon: Some(WARNING_ICON),
message: "Auto update failed".to_string(),
on_click: Some(Arc::new(|this, cx| {
this.dismiss_error_message(&Default::default(), cx)
})),
},
AutoUpdateStatus::Idle => Default::default(),
};
}
// todo!(show active tasks)
// if let Some(most_recent_active_task) = cx.active_labeled_tasks().last() {
// return Content {
// icon: None,
// message: most_recent_active_task.to_string(),
// on_click: None,
// };
// }
Default::default()
}
}
impl EventEmitter<Event> for ActivityIndicator {}
impl Render for ActivityIndicator {
type Element = Stateful<Div>;
fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
let content = self.content_to_render(cx);
let mut result = h_stack()
.id("activity-indicator")
.on_action(cx.listener(Self::show_error_message))
.on_action(cx.listener(Self::dismiss_error_message));
if let Some(on_click) = content.on_click {
result = result
.cursor(CursorStyle::PointingHand)
.on_click(cx.listener(move |this, _, cx| {
on_click(this, cx);
}))
}
result
.children(content.icon.map(|icon| svg().path(icon)))
.child(SharedString::from(content.message))
}
}
impl StatusItemView for ActivityIndicator {
fn set_active_pane_item(&mut self, _: Option<&dyn ItemHandle>, _: &mut ViewContext<Self>) {}
}

View File

@ -84,8 +84,8 @@ impl Settings for AutoUpdateSetting {
pub fn init(http_client: Arc<dyn HttpClient>, server_url: String, cx: &mut AppContext) {
AutoUpdateSetting::register(cx);
cx.observe_new_views(|wokrspace: &mut Workspace, _cx| {
wokrspace
cx.observe_new_views(|workspace: &mut Workspace, _cx| {
workspace
.register_action(|_, action: &Check, cx| check(action, cx))
.register_action(|_, _action: &CheckThatAutoUpdaterWorks, cx| {
let prompt = cx.prompt(gpui::PromptLevel::Info, "It does!", &["Ok"]);
@ -94,6 +94,11 @@ pub fn init(http_client: Arc<dyn HttpClient>, server_url: String, cx: &mut AppCo
})
.detach();
});
// @nate - code to trigger update notification on launch
// workspace.show_notification(0, _cx, |cx| {
// cx.build_view(|_| UpdateNotification::new(SemanticVersion::from_str("1.1.1").unwrap()))
// });
})
.detach();
@ -131,7 +136,7 @@ pub fn check(_: &Check, cx: &mut AppContext) {
}
}
fn _view_release_notes(_: &ViewReleaseNotes, cx: &mut AppContext) {
pub fn view_release_notes(_: &ViewReleaseNotes, cx: &mut AppContext) {
if let Some(auto_updater) = AutoUpdater::get(cx) {
let auto_updater = auto_updater.read(cx);
let server_url = &auto_updater.server_url;

View File

@ -1,87 +1,56 @@
use gpui::{div, Div, EventEmitter, ParentElement, Render, SemanticVersion, ViewContext};
use menu::Cancel;
use workspace::notifications::NotificationEvent;
use gpui::{
div, DismissEvent, Div, EventEmitter, InteractiveElement, ParentElement, Render,
SemanticVersion, StatefulInteractiveElement, Styled, ViewContext,
};
use util::channel::ReleaseChannel;
use workspace::ui::{h_stack, v_stack, Icon, IconElement, Label, StyledExt};
pub struct UpdateNotification {
_version: SemanticVersion,
version: SemanticVersion,
}
impl EventEmitter<NotificationEvent> for UpdateNotification {}
impl EventEmitter<DismissEvent> for UpdateNotification {}
impl Render for UpdateNotification {
type Element = Div;
fn render(&mut self, _cx: &mut gpui::ViewContext<Self>) -> Self::Element {
div().child("Updated zed!")
// let theme = theme::current(cx).clone();
// let theme = &theme.update_notification;
fn render(&mut self, cx: &mut gpui::ViewContext<Self>) -> Self::Element {
let app_name = cx.global::<ReleaseChannel>().display_name();
// let app_name = cx.global::<ReleaseChannel>().display_name();
// MouseEventHandler::new::<ViewReleaseNotes, _>(0, cx, |state, cx| {
// Flex::column()
// .with_child(
// Flex::row()
// .with_child(
// Text::new(
// format!("Updated to {app_name} {}", self.version),
// theme.message.text.clone(),
// )
// .contained()
// .with_style(theme.message.container)
// .aligned()
// .top()
// .left()
// .flex(1., true),
// )
// .with_child(
// MouseEventHandler::new::<Cancel, _>(0, cx, |state, _| {
// let style = theme.dismiss_button.style_for(state);
// Svg::new("icons/x.svg")
// .with_color(style.color)
// .constrained()
// .with_width(style.icon_width)
// .aligned()
// .contained()
// .with_style(style.container)
// .constrained()
// .with_width(style.button_width)
// .with_height(style.button_width)
// })
// .with_padding(Padding::uniform(5.))
// .on_click(MouseButton::Left, move |_, this, cx| {
// this.dismiss(&Default::default(), cx)
// })
// .aligned()
// .constrained()
// .with_height(cx.font_cache().line_height(theme.message.text.font_size))
// .aligned()
// .top()
// .flex_float(),
// ),
// )
// .with_child({
// let style = theme.action_message.style_for(state);
// Text::new("View the release notes", style.text.clone())
// .contained()
// .with_style(style.container)
// })
// .contained()
// })
// .with_cursor_style(CursorStyle::PointingHand)
// .on_click(MouseButton::Left, |_, _, cx| {
// crate::view_release_notes(&Default::default(), cx)
// })
// .into_any_named("update notification")
v_stack()
.elevation_3(cx)
.p_4()
.child(
h_stack()
.justify_between()
.child(Label::new(format!(
"Updated to {app_name} {}",
self.version
)))
.child(
div()
.id("cancel")
.child(IconElement::new(Icon::Close))
.cursor_pointer()
.on_click(cx.listener(|this, _, cx| this.dismiss(cx))),
),
)
.child(
div()
.id("notes")
.child(Label::new("View the release notes"))
.cursor_pointer()
.on_click(|_, cx| crate::view_release_notes(&Default::default(), cx)),
)
}
}
impl UpdateNotification {
pub fn new(version: SemanticVersion) -> Self {
Self { _version: version }
Self { version }
}
pub fn _dismiss(&mut self, _: &Cancel, cx: &mut ViewContext<Self>) {
cx.emit(NotificationEvent::Dismiss);
pub fn dismiss(&mut self, cx: &mut ViewContext<Self>) {
cx.emit(DismissEvent);
}
}

View File

@ -31,16 +31,19 @@ media = { path = "../media" }
project = { package = "project2", path = "../project2" }
settings = { package = "settings2", path = "../settings2" }
util = { path = "../util" }
ui = {package = "ui2", path = "../ui2"}
workspace = {package = "workspace2", path = "../workspace2"}
async-trait.workspace = true
anyhow.workspace = true
async-broadcast = "0.4"
futures.workspace = true
image = "0.23"
postage.workspace = true
schemars.workspace = true
serde.workspace = true
serde_json.workspace = true
serde_derive.workspace = true
smallvec.workspace = true
[dev-dependencies]
client = { package = "client2", path = "../client2", features = ["test-support"] }

View File

@ -1,8 +1,9 @@
pub mod call_settings;
pub mod participant;
pub mod room;
mod shared_screen;
use anyhow::{anyhow, bail, Result};
use anyhow::{anyhow, Result};
use async_trait::async_trait;
use audio::Audio;
use call_settings::CallSettings;
@ -13,8 +14,8 @@ use client::{
use collections::HashSet;
use futures::{channel::oneshot, future::Shared, Future, FutureExt};
use gpui::{
AppContext, AsyncAppContext, AsyncWindowContext, Context, EventEmitter, Model, ModelContext,
Subscription, Task, View, ViewContext, WeakModel, WeakView,
AppContext, AsyncAppContext, Context, EventEmitter, Model, ModelContext, PromptLevel,
Subscription, Task, View, ViewContext, VisualContext, WeakModel, WindowHandle,
};
pub use participant::ParticipantLocation;
use postage::watch;
@ -22,6 +23,7 @@ use project::Project;
use room::Event;
pub use room::Room;
use settings::Settings;
use shared_screen::SharedScreen;
use std::sync::Arc;
use util::ResultExt;
use workspace::{item::ItemHandle, CallHandler, Pane, Workspace};
@ -332,12 +334,55 @@ impl ActiveCall {
pub fn join_channel(
&mut self,
channel_id: u64,
requesting_window: Option<WindowHandle<Workspace>>,
cx: &mut ModelContext<Self>,
) -> Task<Result<Option<Model<Room>>>> {
if let Some(room) = self.room().cloned() {
if room.read(cx).channel_id() == Some(channel_id) {
return Task::ready(Ok(Some(room)));
} else {
return cx.spawn(|_, _| async move {
todo!();
// let future = room.update(&mut cx, |room, cx| {
// room.most_active_project(cx).map(|(host, project)| {
// room.join_project(project, host, app_state.clone(), cx)
// })
// })
// if let Some(future) = future {
// future.await?;
// }
// Ok(Some(room))
});
}
let should_prompt = room.update(cx, |room, _| {
room.channel_id().is_some()
&& room.is_sharing_project()
&& room.remote_participants().len() > 0
});
if should_prompt && requesting_window.is_some() {
return cx.spawn(|this, mut cx| async move {
let answer = requesting_window.unwrap().update(&mut cx, |_, cx| {
cx.prompt(
PromptLevel::Warning,
"Leaving this call will unshare your current project.\nDo you want to switch channels?",
&["Yes, Join Channel", "Cancel"],
)
})?;
if answer.await? == 1 {
return Ok(None);
}
room.update(&mut cx, |room, cx| room.clear_state(cx))?;
this.update(&mut cx, |this, cx| {
this.join_channel(channel_id, requesting_window, cx)
})?
.await
});
}
if room.read(cx).channel_id().is_some() {
room.update(cx, |room, cx| room.clear_state(cx));
}
}
@ -512,24 +557,17 @@ pub fn report_call_event_for_channel(
pub struct Call {
active_call: Option<(Model<ActiveCall>, Vec<Subscription>)>,
parent_workspace: WeakView<Workspace>,
}
impl Call {
pub fn new(
parent_workspace: WeakView<Workspace>,
cx: &mut ViewContext<'_, Workspace>,
) -> Box<dyn CallHandler> {
pub fn new(cx: &mut ViewContext<'_, Workspace>) -> Box<dyn CallHandler> {
let mut active_call = None;
if cx.has_global::<Model<ActiveCall>>() {
let call = cx.global::<Model<ActiveCall>>().clone();
let subscriptions = vec![cx.subscribe(&call, Self::on_active_call_event)];
active_call = Some((call, subscriptions));
}
Box::new(Self {
active_call,
parent_workspace,
})
Box::new(Self { active_call })
}
fn on_active_call_event(
workspace: &mut Workspace,
@ -549,45 +587,10 @@ impl Call {
#[async_trait(?Send)]
impl CallHandler for Call {
fn shared_screen_for_peer(
&self,
peer_id: PeerId,
_pane: &View<Pane>,
cx: &mut ViewContext<Workspace>,
) -> Option<Box<dyn ItemHandle>> {
let (call, _) = self.active_call.as_ref()?;
let room = call.read(cx).room()?.read(cx);
let participant = room.remote_participant_for_peer_id(peer_id)?;
let _track = participant.video_tracks.values().next()?.clone();
let _user = participant.user.clone();
todo!();
// for item in pane.read(cx).items_of_type::<SharedScreen>() {
// if item.read(cx).peer_id == peer_id {
// return Box::new(Some(item));
// }
// }
// Some(Box::new(cx.build_view(|cx| {
// SharedScreen::new(&track, peer_id, user.clone(), cx)
// })))
}
fn room_id(&self, cx: &AppContext) -> Option<u64> {
Some(self.active_call.as_ref()?.0.read(cx).room()?.read(cx).id())
}
fn hang_up(&self, mut cx: AsyncWindowContext) -> Result<Task<Result<()>>> {
let Some((call, _)) = self.active_call.as_ref() else {
bail!("Cannot exit a call; not in a call");
};
call.update(&mut cx, |this, cx| this.hang_up(cx))
}
fn active_project(&self, cx: &AppContext) -> Option<WeakModel<Project>> {
ActiveCall::global(cx).read(cx).location().cloned()
}
fn peer_state(
&mut self,
leader_id: PeerId,
project: &Model<Project>,
cx: &mut ViewContext<Workspace>,
) -> Option<(bool, bool)> {
let (call, _) = self.active_call.as_ref()?;
@ -599,12 +602,7 @@ impl CallHandler for Call {
match participant.location {
ParticipantLocation::SharedProject { project_id } => {
leader_in_this_app = true;
leader_in_this_project = Some(project_id)
== self
.parent_workspace
.update(cx, |this, cx| this.project().read(cx).remote_id())
.log_err()
.flatten();
leader_in_this_project = Some(project_id) == project.read(cx).remote_id();
}
ParticipantLocation::UnsharedProject => {
leader_in_this_app = true;
@ -618,6 +616,134 @@ impl CallHandler for Call {
Some((leader_in_this_project, leader_in_this_app))
}
fn shared_screen_for_peer(
&self,
peer_id: PeerId,
pane: &View<Pane>,
cx: &mut ViewContext<Workspace>,
) -> Option<Box<dyn ItemHandle>> {
let (call, _) = self.active_call.as_ref()?;
let room = call.read(cx).room()?.read(cx);
let participant = room.remote_participant_for_peer_id(peer_id)?;
let track = participant.video_tracks.values().next()?.clone();
let user = participant.user.clone();
for item in pane.read(cx).items_of_type::<SharedScreen>() {
if item.read(cx).peer_id == peer_id {
return Some(Box::new(item));
}
}
Some(Box::new(cx.build_view(|cx| {
SharedScreen::new(&track, peer_id, user.clone(), cx)
})))
}
fn room_id(&self, cx: &AppContext) -> Option<u64> {
Some(self.active_call.as_ref()?.0.read(cx).room()?.read(cx).id())
}
fn hang_up(&self, cx: &mut AppContext) -> Task<Result<()>> {
let Some((call, _)) = self.active_call.as_ref() else {
return Task::ready(Err(anyhow!("Cannot exit a call; not in a call")));
};
call.update(cx, |this, cx| this.hang_up(cx))
}
fn active_project(&self, cx: &AppContext) -> Option<WeakModel<Project>> {
ActiveCall::global(cx).read(cx).location().cloned()
}
fn invite(
&mut self,
called_user_id: u64,
initial_project: Option<Model<Project>>,
cx: &mut AppContext,
) -> Task<Result<()>> {
ActiveCall::global(cx).update(cx, |this, cx| {
this.invite(called_user_id, initial_project, cx)
})
}
fn remote_participants(&self, cx: &AppContext) -> Option<Vec<(Arc<User>, PeerId)>> {
self.active_call
.as_ref()
.map(|call| {
call.0.read(cx).room().map(|room| {
room.read(cx)
.remote_participants()
.iter()
.map(|participant| {
(participant.1.user.clone(), participant.1.peer_id.clone())
})
.collect()
})
})
.flatten()
}
fn is_muted(&self, cx: &AppContext) -> Option<bool> {
self.active_call
.as_ref()
.map(|call| {
call.0
.read(cx)
.room()
.map(|room| room.read(cx).is_muted(cx))
})
.flatten()
}
fn toggle_mute(&self, cx: &mut AppContext) {
self.active_call.as_ref().map(|call| {
call.0.update(cx, |this, cx| {
this.room().map(|room| {
let room = room.clone();
cx.spawn(|_, mut cx| async move {
room.update(&mut cx, |this, cx| this.toggle_mute(cx))??
.await
})
.detach_and_log_err(cx);
})
})
});
}
fn toggle_screen_share(&self, cx: &mut AppContext) {
self.active_call.as_ref().map(|call| {
call.0.update(cx, |this, cx| {
this.room().map(|room| {
room.update(cx, |this, cx| {
if this.is_screen_sharing() {
this.unshare_screen(cx).log_err();
} else {
let t = this.share_screen(cx);
cx.spawn(move |_, _| async move {
t.await.log_err();
})
.detach();
}
})
})
})
});
}
fn toggle_deafen(&self, cx: &mut AppContext) {
self.active_call.as_ref().map(|call| {
call.0.update(cx, |this, cx| {
this.room().map(|room| {
room.update(cx, |this, cx| {
this.toggle_deafen(cx).log_err();
})
})
})
});
}
fn is_deafened(&self, cx: &AppContext) -> Option<bool> {
self.active_call
.as_ref()
.map(|call| {
call.0
.read(cx)
.room()
.map(|room| room.read(cx).is_deafened())
})
.flatten()
.flatten()
}
}
#[cfg(test)]

View File

@ -4,7 +4,7 @@ use client::{proto, User};
use collections::HashMap;
use gpui::WeakModel;
pub use live_kit_client::Frame;
use live_kit_client::{RemoteAudioTrack, RemoteVideoTrack};
pub(crate) use live_kit_client::{RemoteAudioTrack, RemoteVideoTrack};
use project::Project;
use std::sync::Arc;

View File

@ -21,7 +21,7 @@ use live_kit_client::{
};
use postage::{sink::Sink, stream::Stream, watch};
use project::Project;
use settings::Settings;
use settings::Settings as _;
use std::{future::Future, mem, sync::Arc, time::Duration};
use util::{post_inc, ResultExt, TryFutureExt};
@ -1267,7 +1267,6 @@ impl Room {
.ok_or_else(|| anyhow!("live-kit was not initialized"))?
.await
};
let publication = publish_track.await;
this.upgrade()
.ok_or_else(|| anyhow!("room was dropped"))?

View File

@ -0,0 +1,111 @@
use crate::participant::{Frame, RemoteVideoTrack};
use anyhow::Result;
use client::{proto::PeerId, User};
use futures::StreamExt;
use gpui::{
div, img, AppContext, Div, Element, EventEmitter, FocusHandle, Focusable, FocusableView,
InteractiveElement, ParentElement, Render, SharedString, Styled, Task, View, ViewContext,
VisualContext, WindowContext,
};
use std::sync::{Arc, Weak};
use ui::{h_stack, Icon, IconElement};
use workspace::{item::Item, ItemNavHistory, WorkspaceId};
pub enum Event {
Close,
}
pub struct SharedScreen {
track: Weak<RemoteVideoTrack>,
frame: Option<Frame>,
pub peer_id: PeerId,
user: Arc<User>,
nav_history: Option<ItemNavHistory>,
_maintain_frame: Task<Result<()>>,
focus: FocusHandle,
}
impl SharedScreen {
pub fn new(
track: &Arc<RemoteVideoTrack>,
peer_id: PeerId,
user: Arc<User>,
cx: &mut ViewContext<Self>,
) -> Self {
cx.focus_handle();
let mut frames = track.frames();
Self {
track: Arc::downgrade(track),
frame: None,
peer_id,
user,
nav_history: Default::default(),
_maintain_frame: cx.spawn(|this, mut cx| async move {
while let Some(frame) = frames.next().await {
this.update(&mut cx, |this, cx| {
this.frame = Some(frame);
cx.notify();
})?;
}
this.update(&mut cx, |_, cx| cx.emit(Event::Close))?;
Ok(())
}),
focus: cx.focus_handle(),
}
}
}
impl EventEmitter<Event> for SharedScreen {}
impl EventEmitter<workspace::item::ItemEvent> for SharedScreen {}
impl FocusableView for SharedScreen {
fn focus_handle(&self, _: &AppContext) -> FocusHandle {
self.focus.clone()
}
}
impl Render for SharedScreen {
type Element = Focusable<Div>;
fn render(&mut self, _: &mut ViewContext<Self>) -> Self::Element {
div().track_focus(&self.focus).size_full().children(
self.frame
.as_ref()
.map(|frame| img(frame.image()).size_full()),
)
}
}
impl Item for SharedScreen {
fn tab_tooltip_text(&self, _: &AppContext) -> Option<SharedString> {
Some(format!("{}'s screen", self.user.github_login).into())
}
fn deactivated(&mut self, cx: &mut ViewContext<Self>) {
if let Some(nav_history) = self.nav_history.as_mut() {
nav_history.push::<()>(None, cx);
}
}
fn tab_content(&self, _: Option<usize>, _: &WindowContext<'_>) -> gpui::AnyElement {
h_stack()
.gap_1()
.child(IconElement::new(Icon::Screen))
.child(SharedString::from(format!(
"{}'s screen",
self.user.github_login
)))
.into_any()
}
fn set_nav_history(&mut self, history: ItemNavHistory, _: &mut ViewContext<Self>) {
self.nav_history = Some(history);
}
fn clone_on_split(
&self,
_workspace_id: WorkspaceId,
cx: &mut ViewContext<Self>,
) -> Option<View<Self>> {
let track = self.track.upgrade()?;
Some(cx.build_view(|cx| Self::new(&track, self.peer_id, self.user.clone(), cx)))
}
}

View File

@ -551,7 +551,6 @@ impl Client {
F: 'static + Future<Output = Result<()>>,
{
let message_type_id = TypeId::of::<M>();
let mut state = self.state.write();
state
.models_by_message_type
@ -694,8 +693,8 @@ impl Client {
}
}
pub async fn has_keychain_credentials(&self, cx: &AsyncAppContext) -> bool {
read_credentials_from_keychain(cx).await.is_some()
pub fn has_keychain_credentials(&self, cx: &AsyncAppContext) -> bool {
read_credentials_from_keychain(cx).is_some()
}
#[async_recursion(?Send)]
@ -726,7 +725,7 @@ impl Client {
let mut read_from_keychain = false;
let mut credentials = self.state.read().credentials.clone();
if credentials.is_none() && try_keychain {
credentials = read_credentials_from_keychain(cx).await;
credentials = read_credentials_from_keychain(cx);
read_from_keychain = credentials.is_some();
}
if credentials.is_none() {
@ -1325,7 +1324,7 @@ impl Client {
}
}
async fn read_credentials_from_keychain(cx: &AsyncAppContext) -> Option<Credentials> {
fn read_credentials_from_keychain(cx: &AsyncAppContext) -> Option<Credentials> {
if IMPERSONATE_LOGIN.is_some() {
return None;
}

View File

@ -3941,7 +3941,7 @@ async fn test_collaborating_with_diagnostics(
// Ensure client B observes the new diagnostics.
project_b.read_with(cx_b, |project, cx| {
assert_eq!(
project.diagnostic_summaries(cx).collect::<Vec<_>>(),
project.diagnostic_summaries(false, cx).collect::<Vec<_>>(),
&[(
ProjectPath {
worktree_id,
@ -3961,14 +3961,14 @@ async fn test_collaborating_with_diagnostics(
let project_c = client_c.build_remote_project(project_id, cx_c).await;
let project_c_diagnostic_summaries =
Rc::new(RefCell::new(project_c.read_with(cx_c, |project, cx| {
project.diagnostic_summaries(cx).collect::<Vec<_>>()
project.diagnostic_summaries(false, cx).collect::<Vec<_>>()
})));
project_c.update(cx_c, |_, cx| {
let summaries = project_c_diagnostic_summaries.clone();
cx.subscribe(&project_c, {
move |p, _, event, cx| {
if let project::Event::DiskBasedDiagnosticsFinished { .. } = event {
*summaries.borrow_mut() = p.diagnostic_summaries(cx).collect();
*summaries.borrow_mut() = p.diagnostic_summaries(false, cx).collect();
}
}
})
@ -4018,7 +4018,7 @@ async fn test_collaborating_with_diagnostics(
deterministic.run_until_parked();
project_b.read_with(cx_b, |project, cx| {
assert_eq!(
project.diagnostic_summaries(cx).collect::<Vec<_>>(),
project.diagnostic_summaries(false, cx).collect::<Vec<_>>(),
[(
ProjectPath {
worktree_id,
@ -4034,7 +4034,7 @@ async fn test_collaborating_with_diagnostics(
});
project_c.read_with(cx_c, |project, cx| {
assert_eq!(
project.diagnostic_summaries(cx).collect::<Vec<_>>(),
project.diagnostic_summaries(false, cx).collect::<Vec<_>>(),
[(
ProjectPath {
worktree_id,
@ -4097,13 +4097,22 @@ async fn test_collaborating_with_diagnostics(
);
deterministic.run_until_parked();
project_a.read_with(cx_a, |project, cx| {
assert_eq!(project.diagnostic_summaries(cx).collect::<Vec<_>>(), [])
assert_eq!(
project.diagnostic_summaries(false, cx).collect::<Vec<_>>(),
[]
)
});
project_b.read_with(cx_b, |project, cx| {
assert_eq!(project.diagnostic_summaries(cx).collect::<Vec<_>>(), [])
assert_eq!(
project.diagnostic_summaries(false, cx).collect::<Vec<_>>(),
[]
)
});
project_c.read_with(cx_c, |project, cx| {
assert_eq!(project.diagnostic_summaries(cx).collect::<Vec<_>>(), [])
assert_eq!(
project.diagnostic_summaries(false, cx).collect::<Vec<_>>(),
[]
)
});
}

View File

@ -364,7 +364,8 @@ async fn test_joining_channel_ancestor_member(
let active_call_b = cx_b.read(ActiveCall::global);
assert!(active_call_b
.update(cx_b, |active_call, cx| active_call.join_channel(sub_id, cx))
.update(cx_b, |active_call, cx| active_call
.join_channel(sub_id, None, cx))
.await
.is_ok());
}
@ -394,7 +395,9 @@ async fn test_channel_room(
let active_call_b = cx_b.read(ActiveCall::global);
active_call_a
.update(cx_a, |active_call, cx| active_call.join_channel(zed_id, cx))
.update(cx_a, |active_call, cx| {
active_call.join_channel(zed_id, None, cx)
})
.await
.unwrap();
@ -442,7 +445,9 @@ async fn test_channel_room(
});
active_call_b
.update(cx_b, |active_call, cx| active_call.join_channel(zed_id, cx))
.update(cx_b, |active_call, cx| {
active_call.join_channel(zed_id, None, cx)
})
.await
.unwrap();
@ -559,12 +564,16 @@ async fn test_channel_room(
});
active_call_a
.update(cx_a, |active_call, cx| active_call.join_channel(zed_id, cx))
.update(cx_a, |active_call, cx| {
active_call.join_channel(zed_id, None, cx)
})
.await
.unwrap();
active_call_b
.update(cx_b, |active_call, cx| active_call.join_channel(zed_id, cx))
.update(cx_b, |active_call, cx| {
active_call.join_channel(zed_id, None, cx)
})
.await
.unwrap();
@ -608,7 +617,9 @@ async fn test_channel_jumping(executor: BackgroundExecutor, cx_a: &mut TestAppCo
let active_call_a = cx_a.read(ActiveCall::global);
active_call_a
.update(cx_a, |active_call, cx| active_call.join_channel(zed_id, cx))
.update(cx_a, |active_call, cx| {
active_call.join_channel(zed_id, None, cx)
})
.await
.unwrap();
@ -627,7 +638,7 @@ async fn test_channel_jumping(executor: BackgroundExecutor, cx_a: &mut TestAppCo
active_call_a
.update(cx_a, |active_call, cx| {
active_call.join_channel(rust_id, cx)
active_call.join_channel(rust_id, None, cx)
})
.await
.unwrap();
@ -793,7 +804,7 @@ async fn test_call_from_channel(
let active_call_b = cx_b.read(ActiveCall::global);
active_call_a
.update(cx_a, |call, cx| call.join_channel(channel_id, cx))
.update(cx_a, |call, cx| call.join_channel(channel_id, None, cx))
.await
.unwrap();
@ -1286,7 +1297,7 @@ async fn test_guest_access(
// Non-members should not be allowed to join
assert!(active_call_b
.update(cx_b, |call, cx| call.join_channel(channel_a, cx))
.update(cx_b, |call, cx| call.join_channel(channel_a, None, cx))
.await
.is_err());
@ -1308,7 +1319,7 @@ async fn test_guest_access(
// Client B joins channel A as a guest
active_call_b
.update(cx_b, |call, cx| call.join_channel(channel_a, cx))
.update(cx_b, |call, cx| call.join_channel(channel_a, None, cx))
.await
.unwrap();
@ -1341,7 +1352,7 @@ async fn test_guest_access(
assert_channels_list_shape(client_b.channel_store(), cx_b, &[]);
active_call_b
.update(cx_b, |call, cx| call.join_channel(channel_b, cx))
.update(cx_b, |call, cx| call.join_channel(channel_b, None, cx))
.await
.unwrap();
@ -1372,7 +1383,7 @@ async fn test_invite_access(
// should not be allowed to join
assert!(active_call_b
.update(cx_b, |call, cx| call.join_channel(channel_b_id, cx))
.update(cx_b, |call, cx| call.join_channel(channel_b_id, None, cx))
.await
.is_err());
@ -1390,7 +1401,7 @@ async fn test_invite_access(
.unwrap();
active_call_b
.update(cx_b, |call, cx| call.join_channel(channel_b_id, cx))
.update(cx_b, |call, cx| call.join_channel(channel_b_id, None, cx))
.await
.unwrap();

View File

@ -510,9 +510,10 @@ async fn test_joining_channels_and_calling_multiple_users_simultaneously(
// Simultaneously join channel 1 and then channel 2
active_call_a
.update(cx_a, |call, cx| call.join_channel(channel_1, cx))
.update(cx_a, |call, cx| call.join_channel(channel_1, None, cx))
.detach();
let join_channel_2 = active_call_a.update(cx_a, |call, cx| call.join_channel(channel_2, cx));
let join_channel_2 =
active_call_a.update(cx_a, |call, cx| call.join_channel(channel_2, None, cx));
join_channel_2.await.unwrap();
@ -538,7 +539,8 @@ async fn test_joining_channels_and_calling_multiple_users_simultaneously(
call.invite(client_c.user_id().unwrap(), None, cx)
});
let join_channel = active_call_a.update(cx_a, |call, cx| call.join_channel(channel_1, cx));
let join_channel =
active_call_a.update(cx_a, |call, cx| call.join_channel(channel_1, None, cx));
b_invite.await.unwrap();
c_invite.await.unwrap();
@ -567,7 +569,8 @@ async fn test_joining_channels_and_calling_multiple_users_simultaneously(
.unwrap();
// Simultaneously join channel 1 and call user B and user C from client A.
let join_channel = active_call_a.update(cx_a, |call, cx| call.join_channel(channel_1, cx));
let join_channel =
active_call_a.update(cx_a, |call, cx| call.join_channel(channel_1, None, cx));
let b_invite = active_call_a.update(cx_a, |call, cx| {
call.invite(client_b.user_id().unwrap(), None, cx)
@ -3685,7 +3688,7 @@ async fn test_collaborating_with_diagnostics(
project_b.read_with(cx_b, |project, cx| {
assert_eq!(
project.diagnostic_summaries(cx).collect::<Vec<_>>(),
project.diagnostic_summaries(false, cx).collect::<Vec<_>>(),
&[(
ProjectPath {
worktree_id,
@ -3705,14 +3708,14 @@ async fn test_collaborating_with_diagnostics(
let project_c = client_c.build_remote_project(project_id, cx_c).await;
let project_c_diagnostic_summaries =
Rc::new(RefCell::new(project_c.read_with(cx_c, |project, cx| {
project.diagnostic_summaries(cx).collect::<Vec<_>>()
project.diagnostic_summaries(false, cx).collect::<Vec<_>>()
})));
project_c.update(cx_c, |_, cx| {
let summaries = project_c_diagnostic_summaries.clone();
cx.subscribe(&project_c, {
move |p, _, event, cx| {
if let project::Event::DiskBasedDiagnosticsFinished { .. } = event {
*summaries.borrow_mut() = p.diagnostic_summaries(cx).collect();
*summaries.borrow_mut() = p.diagnostic_summaries(false, cx).collect();
}
}
})
@ -3763,7 +3766,7 @@ async fn test_collaborating_with_diagnostics(
project_b.read_with(cx_b, |project, cx| {
assert_eq!(
project.diagnostic_summaries(cx).collect::<Vec<_>>(),
project.diagnostic_summaries(false, cx).collect::<Vec<_>>(),
[(
ProjectPath {
worktree_id,
@ -3780,7 +3783,7 @@ async fn test_collaborating_with_diagnostics(
project_c.read_with(cx_c, |project, cx| {
assert_eq!(
project.diagnostic_summaries(cx).collect::<Vec<_>>(),
project.diagnostic_summaries(false, cx).collect::<Vec<_>>(),
[(
ProjectPath {
worktree_id,
@ -3841,15 +3844,24 @@ async fn test_collaborating_with_diagnostics(
executor.run_until_parked();
project_a.read_with(cx_a, |project, cx| {
assert_eq!(project.diagnostic_summaries(cx).collect::<Vec<_>>(), [])
assert_eq!(
project.diagnostic_summaries(false, cx).collect::<Vec<_>>(),
[]
)
});
project_b.read_with(cx_b, |project, cx| {
assert_eq!(project.diagnostic_summaries(cx).collect::<Vec<_>>(), [])
assert_eq!(
project.diagnostic_summaries(false, cx).collect::<Vec<_>>(),
[]
)
});
project_c.read_with(cx_c, |project, cx| {
assert_eq!(project.diagnostic_summaries(cx).collect::<Vec<_>>(), [])
assert_eq!(
project.diagnostic_summaries(false, cx).collect::<Vec<_>>(),
[]
)
});
}

View File

@ -221,7 +221,7 @@ impl TestServer {
fs: fs.clone(),
build_window_options: |_, _, _| Default::default(),
node_runtime: FakeNodeRuntime::new(),
call_factory: |_, _| Box::new(workspace::TestCallHandler),
call_factory: |_| Box::new(workspace::TestCallHandler),
});
cx.update(|cx| {

File diff suppressed because it is too large Load Diff

View File

@ -1,37 +1,34 @@
use client::{ContactRequestStatus, User, UserStore};
use gpui::{
elements::*, AppContext, Entity, ModelHandle, MouseState, Task, View, ViewContext, ViewHandle,
div, img, svg, AnyElement, AppContext, DismissEvent, Div, Entity, EventEmitter, FocusHandle,
FocusableView, Img, IntoElement, Model, ParentElement as _, Render, Styled, Task, View,
ViewContext, VisualContext, WeakView,
};
use picker::{Picker, PickerDelegate, PickerEvent};
use picker::{Picker, PickerDelegate};
use std::sync::Arc;
use util::TryFutureExt;
use workspace::Modal;
use theme::ActiveTheme as _;
use ui::{h_stack, v_stack, Label};
use util::{ResultExt as _, TryFutureExt};
pub fn init(cx: &mut AppContext) {
Picker::<ContactFinderDelegate>::init(cx);
cx.add_action(ContactFinder::dismiss)
//Picker::<ContactFinderDelegate>::init(cx);
//cx.add_action(ContactFinder::dismiss)
}
pub struct ContactFinder {
picker: ViewHandle<Picker<ContactFinderDelegate>>,
picker: View<Picker<ContactFinderDelegate>>,
has_focus: bool,
}
impl ContactFinder {
pub fn new(user_store: ModelHandle<UserStore>, cx: &mut ViewContext<Self>) -> Self {
let picker = cx.add_view(|cx| {
Picker::new(
ContactFinderDelegate {
user_store,
potential_contacts: Arc::from([]),
selected_index: 0,
},
cx,
)
.with_theme(|theme| theme.collab_panel.tabbed_modal.picker.clone())
});
cx.subscribe(&picker, |_, _, e, cx| cx.emit(*e)).detach();
pub fn new(user_store: Model<UserStore>, cx: &mut ViewContext<Self>) -> Self {
let delegate = ContactFinderDelegate {
parent: cx.view().downgrade(),
user_store,
potential_contacts: Arc::from([]),
selected_index: 0,
};
let picker = cx.build_view(|cx| Picker::new(delegate, cx));
Self {
picker,
@ -41,105 +38,72 @@ impl ContactFinder {
pub fn set_query(&mut self, query: String, cx: &mut ViewContext<Self>) {
self.picker.update(cx, |picker, cx| {
picker.set_query(query, cx);
// todo!()
// picker.set_query(query, cx);
});
}
fn dismiss(&mut self, _: &menu::Cancel, cx: &mut ViewContext<Self>) {
cx.emit(PickerEvent::Dismiss);
}
}
impl Entity for ContactFinder {
type Event = PickerEvent;
}
impl View for ContactFinder {
fn ui_name() -> &'static str {
"ContactFinder"
}
fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
let full_theme = &theme::current(cx);
let theme = &full_theme.collab_panel.tabbed_modal;
fn render_mode_button(
text: &'static str,
theme: &theme::TabbedModal,
_cx: &mut ViewContext<ContactFinder>,
) -> AnyElement<ContactFinder> {
let contained_text = &theme.tab_button.active_state().default;
Label::new(text, contained_text.text.clone())
.contained()
.with_style(contained_text.container.clone())
.into_any()
impl Render for ContactFinder {
fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
fn render_mode_button(text: &'static str) -> AnyElement {
Label::new(text).into_any_element()
}
Flex::column()
.with_child(
Flex::column()
.with_child(
Label::new("Contacts", theme.title.text.clone())
.contained()
.with_style(theme.title.container.clone()),
)
.with_child(Flex::row().with_children([render_mode_button(
"Invite new contacts",
&theme,
cx,
)]))
.expanded()
.contained()
.with_style(theme.header),
v_stack()
.child(
v_stack()
.child(Label::new("Contacts"))
.child(h_stack().children([render_mode_button("Invite new contacts")]))
.bg(cx.theme().colors().element_background),
)
.with_child(
ChildView::new(&self.picker, cx)
.contained()
.with_style(theme.body),
)
.constrained()
.with_max_height(theme.max_height)
.with_max_width(theme.max_width)
.contained()
.with_style(theme.modal)
.into_any()
.child(self.picker.clone())
.w_96()
}
fn focus_in(&mut self, _: gpui::AnyViewHandle, cx: &mut ViewContext<Self>) {
self.has_focus = true;
if cx.is_self_focused() {
cx.focus(&self.picker)
}
}
// fn focus_in(&mut self, _: gpui::AnyViewHandle, cx: &mut ViewContext<Self>) {
// self.has_focus = true;
// if cx.is_self_focused() {
// cx.focus(&self.picker)
// }
// }
fn focus_out(&mut self, _: gpui::AnyViewHandle, _: &mut ViewContext<Self>) {
self.has_focus = false;
}
// fn focus_out(&mut self, _: gpui::AnyViewHandle, _: &mut ViewContext<Self>) {
// self.has_focus = false;
// }
type Element = Div;
}
impl Modal for ContactFinder {
fn has_focus(&self) -> bool {
self.has_focus
}
// impl Modal for ContactFinder {
// fn has_focus(&self) -> bool {
// self.has_focus
// }
fn dismiss_on_event(event: &Self::Event) -> bool {
match event {
PickerEvent::Dismiss => true,
}
}
}
// fn dismiss_on_event(event: &Self::Event) -> bool {
// match event {
// PickerEvent::Dismiss => true,
// }
// }
// }
pub struct ContactFinderDelegate {
parent: WeakView<ContactFinder>,
potential_contacts: Arc<[Arc<User>]>,
user_store: ModelHandle<UserStore>,
user_store: Model<UserStore>,
selected_index: usize,
}
impl PickerDelegate for ContactFinderDelegate {
fn placeholder_text(&self) -> Arc<str> {
"Search collaborator by username...".into()
}
impl EventEmitter<DismissEvent> for ContactFinder {}
impl FocusableView for ContactFinder {
fn focus_handle(&self, cx: &AppContext) -> FocusHandle {
self.picker.focus_handle(cx)
}
}
impl PickerDelegate for ContactFinderDelegate {
type ListItem = Div;
fn match_count(&self) -> usize {
self.potential_contacts.len()
}
@ -152,6 +116,10 @@ impl PickerDelegate for ContactFinderDelegate {
self.selected_index = ix;
}
fn placeholder_text(&self) -> Arc<str> {
"Search collaborator by username...".into()
}
fn update_matches(&mut self, query: String, cx: &mut ViewContext<Picker<Self>>) -> Task<()> {
let search_users = self
.user_store
@ -161,7 +129,7 @@ impl PickerDelegate for ContactFinderDelegate {
async {
let potential_contacts = search_users.await?;
picker.update(&mut cx, |picker, cx| {
picker.delegate_mut().potential_contacts = potential_contacts.into();
picker.delegate.potential_contacts = potential_contacts.into();
cx.notify();
})?;
anyhow::Ok(())
@ -191,19 +159,18 @@ impl PickerDelegate for ContactFinderDelegate {
}
fn dismissed(&mut self, cx: &mut ViewContext<Picker<Self>>) {
cx.emit(PickerEvent::Dismiss);
//cx.emit(PickerEvent::Dismiss);
self.parent
.update(cx, |_, cx| cx.emit(DismissEvent))
.log_err();
}
fn render_match(
&self,
ix: usize,
mouse_state: &mut MouseState,
selected: bool,
cx: &gpui::AppContext,
) -> AnyElement<Picker<Self>> {
let full_theme = &theme::current(cx);
let theme = &full_theme.collab_panel.contact_finder;
let tabbed_modal = &full_theme.collab_panel.tabbed_modal;
cx: &mut ViewContext<Picker<Self>>,
) -> Option<Self::ListItem> {
let user = &self.potential_contacts[ix];
let request_status = self.user_store.read(cx).contact_request_status(user);
@ -214,48 +181,46 @@ impl PickerDelegate for ContactFinderDelegate {
ContactRequestStatus::RequestSent => Some("icons/x.svg"),
ContactRequestStatus::RequestAccepted => None,
};
let button_style = if self.user_store.read(cx).is_contact_request_pending(user) {
&theme.disabled_contact_button
} else {
&theme.contact_button
};
let style = tabbed_modal
.picker
.item
.in_state(selected)
.style_for(mouse_state);
Flex::row()
.with_children(user.avatar.clone().map(|avatar| {
Image::from_data(avatar)
.with_style(theme.contact_avatar)
.aligned()
.left()
}))
.with_child(
Label::new(user.github_login.clone(), style.label.clone())
.contained()
.with_style(theme.contact_username)
.aligned()
.left(),
)
.with_children(icon_path.map(|icon_path| {
Svg::new(icon_path)
.with_color(button_style.color)
.constrained()
.with_width(button_style.icon_width)
.aligned()
.contained()
.with_style(button_style.container)
.constrained()
.with_width(button_style.button_width)
.with_height(button_style.button_width)
.aligned()
.flex_float()
}))
.contained()
.with_style(style.container)
.constrained()
.with_height(tabbed_modal.row_height)
.into_any()
Some(
div()
.flex_1()
.justify_between()
.children(user.avatar.clone().map(|avatar| img(avatar)))
.child(Label::new(user.github_login.clone()))
.children(icon_path.map(|icon_path| svg().path(icon_path))),
)
// Flex::row()
// .with_children(user.avatar.clone().map(|avatar| {
// Image::from_data(avatar)
// .with_style(theme.contact_avatar)
// .aligned()
// .left()
// }))
// .with_child(
// Label::new(user.github_login.clone(), style.label.clone())
// .contained()
// .with_style(theme.contact_username)
// .aligned()
// .left(),
// )
// .with_children(icon_path.map(|icon_path| {
// Svg::new(icon_path)
// .with_color(button_style.color)
// .constrained()
// .with_width(button_style.icon_width)
// .aligned()
// .contained()
// .with_style(button_style.container)
// .constrained()
// .with_width(button_style.button_width)
// .with_height(button_style.button_width)
// .aligned()
// .flex_float()
// }))
// .contained()
// .with_style(style.container)
// .constrained()
// .with_height(tabbed_modal.row_height)
// .into_any()
}
}

View File

@ -31,14 +31,17 @@ use std::sync::Arc;
use call::ActiveCall;
use client::{Client, UserStore};
use gpui::{
div, px, rems, AppContext, Div, InteractiveElement, IntoElement, Model, ParentElement, Render,
Stateful, StatefulInteractiveElement, Styled, Subscription, ViewContext, VisualContext,
WeakView, WindowBounds,
div, px, rems, AppContext, Div, Element, InteractiveElement, IntoElement, Model, MouseButton,
ParentElement, Render, RenderOnce, Stateful, StatefulInteractiveElement, Styled, Subscription,
ViewContext, VisualContext, WeakView, WindowBounds,
};
use project::Project;
use theme::ActiveTheme;
use ui::{h_stack, Button, ButtonVariant, Color, KeyBinding, Label, Tooltip};
use workspace::Workspace;
use ui::{h_stack, prelude::*, Avatar, Button, ButtonStyle2, IconButton, KeyBinding, Tooltip};
use util::ResultExt;
use workspace::{notifications::NotifyResultExt, Workspace};
use crate::face_pile::FacePile;
// const MAX_PROJECT_NAME_LENGTH: usize = 40;
// const MAX_BRANCH_NAME_LENGTH: usize = 40;
@ -85,6 +88,41 @@ impl Render for CollabTitlebarItem {
type Element = Stateful<Div>;
fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
let is_in_room = self
.workspace
.update(cx, |this, cx| this.call_state().is_in_room(cx))
.unwrap_or_default();
let is_shared = is_in_room && self.project.read(cx).is_shared();
let current_user = self.user_store.read(cx).current_user();
let client = self.client.clone();
let users = self
.workspace
.update(cx, |this, cx| this.call_state().remote_participants(cx))
.log_err()
.flatten();
let mic_icon = if self
.workspace
.update(cx, |this, cx| this.call_state().is_muted(cx))
.log_err()
.flatten()
.unwrap_or_default()
{
ui::Icon::MicMute
} else {
ui::Icon::Mic
};
let speakers_icon = if self
.workspace
.update(cx, |this, cx| this.call_state().is_deafened(cx))
.log_err()
.flatten()
.unwrap_or_default()
{
ui::Icon::AudioOff
} else {
ui::Icon::AudioOn
};
let workspace = self.workspace.clone();
h_stack()
.id("titlebar")
.justify_between()
@ -115,8 +153,8 @@ impl Render for CollabTitlebarItem {
.border_color(gpui::red())
.id("project_owner_indicator")
.child(
Button::new("player")
.variant(ButtonVariant::Ghost)
Button::new("player", "player")
.style(ButtonStyle2::Subtle)
.color(Some(Color::Player(0))),
)
.tooltip(move |cx| Tooltip::text("Toggle following", cx)),
@ -127,7 +165,10 @@ impl Render for CollabTitlebarItem {
.border()
.border_color(gpui::red())
.id("titlebar_project_menu_button")
.child(Button::new("project_name").variant(ButtonVariant::Ghost))
.child(
Button::new("project_name", "project_name")
.style(ButtonStyle2::Subtle),
)
.tooltip(move |cx| Tooltip::text("Recent Projects", cx)),
)
// TODO - Add git menu
@ -137,8 +178,8 @@ impl Render for CollabTitlebarItem {
.border_color(gpui::red())
.id("titlebar_git_menu_button")
.child(
Button::new("branch_name")
.variant(ButtonVariant::Ghost)
Button::new("branch_name", "branch_name")
.style(ButtonStyle2::Subtle)
.color(Some(Color::Muted)),
)
.tooltip(move |cx| {
@ -155,8 +196,116 @@ impl Render for CollabTitlebarItem {
.into()
}),
),
) // self.titlebar_item
.child(h_stack().child(Label::new("Right side titlebar item")))
)
.when_some(
users.zip(current_user.clone()),
|this, (remote_participants, current_user)| {
let mut pile = FacePile::default();
pile.extend(
current_user
.avatar
.clone()
.map(|avatar| {
div().child(Avatar::data(avatar.clone())).into_any_element()
})
.into_iter()
.chain(remote_participants.into_iter().flat_map(|(user, peer_id)| {
user.avatar.as_ref().map(|avatar| {
div()
.child(
Avatar::data(avatar.clone()).into_element().into_any(),
)
.on_mouse_down(MouseButton::Left, {
let workspace = workspace.clone();
move |_, cx| {
workspace
.update(cx, |this, cx| {
this.open_shared_screen(peer_id, cx);
})
.log_err();
}
})
.into_any_element()
})
})),
);
this.child(pile.render(cx))
},
)
.child(div().flex_1())
.when(is_in_room, |this| {
this.child(
h_stack()
.child(
h_stack()
.child(Button::new(
"toggle_sharing",
if is_shared { "Unshare" } else { "Share" },
))
.child(IconButton::new("leave-call", ui::Icon::Exit).on_click({
let workspace = workspace.clone();
move |_, cx| {
workspace
.update(cx, |this, cx| {
this.call_state().hang_up(cx).detach();
})
.log_err();
}
})),
)
.child(
h_stack()
.child(IconButton::new("mute-microphone", mic_icon).on_click({
let workspace = workspace.clone();
move |_, cx| {
workspace
.update(cx, |this, cx| {
this.call_state().toggle_mute(cx);
})
.log_err();
}
}))
.child(IconButton::new("mute-sound", speakers_icon).on_click({
let workspace = workspace.clone();
move |_, cx| {
workspace
.update(cx, |this, cx| {
this.call_state().toggle_deafen(cx);
})
.log_err();
}
}))
.child(IconButton::new("screen-share", ui::Icon::Screen).on_click(
move |_, cx| {
workspace
.update(cx, |this, cx| {
this.call_state().toggle_screen_share(cx);
})
.log_err();
},
))
.pl_2(),
),
)
})
.map(|this| {
if let Some(user) = current_user {
this.when_some(user.avatar.clone(), |this, avatar| {
this.child(ui::Avatar::data(avatar))
})
} else {
this.child(Button::new("sign_in", "Sign in").on_click(move |_, cx| {
let client = client.clone();
cx.spawn(move |mut cx| async move {
client
.authenticate_and_connect(true, &cx)
.await
.notify_async_err(&mut cx);
})
.detach();
}))
}
})
}
}

View File

@ -7,11 +7,14 @@ pub mod notification_panel;
pub mod notifications;
mod panel_settings;
use std::sync::Arc;
use std::{rc::Rc, sync::Arc};
pub use collab_panel::CollabPanel;
pub use collab_titlebar_item::CollabTitlebarItem;
use gpui::AppContext;
use gpui::{
point, AppContext, GlobalPixels, Pixels, PlatformDisplay, Size, WindowBounds, WindowKind,
WindowOptions,
};
pub use panel_settings::{
ChatPanelSettings, CollaborationPanelSettings, NotificationPanelSettings,
};
@ -23,7 +26,7 @@ use workspace::AppState;
// [ToggleScreenSharing, ToggleMute, ToggleDeafen, LeaveCall]
// );
pub fn init(_app_state: &Arc<AppState>, cx: &mut AppContext) {
pub fn init(app_state: &Arc<AppState>, cx: &mut AppContext) {
CollaborationPanelSettings::register(cx);
ChatPanelSettings::register(cx);
NotificationPanelSettings::register(cx);
@ -32,7 +35,7 @@ pub fn init(_app_state: &Arc<AppState>, cx: &mut AppContext) {
collab_titlebar_item::init(cx);
collab_panel::init(cx);
// chat_panel::init(cx);
// notifications::init(&app_state, cx);
notifications::init(&app_state, cx);
// cx.add_global_action(toggle_screen_sharing);
// cx.add_global_action(toggle_mute);
@ -95,31 +98,36 @@ pub fn init(_app_state: &Arc<AppState>, cx: &mut AppContext) {
// }
// }
// fn notification_window_options(
// screen: Rc<dyn Screen>,
// window_size: Vector2F,
// ) -> WindowOptions<'static> {
// const NOTIFICATION_PADDING: f32 = 16.;
fn notification_window_options(
screen: Rc<dyn PlatformDisplay>,
window_size: Size<Pixels>,
) -> WindowOptions {
let notification_margin_width = GlobalPixels::from(16.);
let notification_margin_height = GlobalPixels::from(-0.) - GlobalPixels::from(48.);
// let screen_bounds = screen.content_bounds();
// WindowOptions {
// bounds: WindowBounds::Fixed(RectF::new(
// screen_bounds.upper_right()
// + vec2f(
// -NOTIFICATION_PADDING - window_size.x(),
// NOTIFICATION_PADDING,
// ),
// window_size,
// )),
// titlebar: None,
// center: false,
// focus: false,
// show: true,
// kind: WindowKind::PopUp,
// is_movable: false,
// screen: Some(screen),
// }
// }
let screen_bounds = screen.bounds();
let size: Size<GlobalPixels> = window_size.into();
// todo!() use content bounds instead of screen.bounds and get rid of magics in point's 2nd argument.
let bounds = gpui::Bounds::<GlobalPixels> {
origin: screen_bounds.upper_right()
- point(
size.width + notification_margin_width,
notification_margin_height,
),
size: window_size.into(),
};
WindowOptions {
bounds: WindowBounds::Fixed(bounds),
titlebar: None,
center: false,
focus: false,
show: true,
kind: WindowKind::PopUp,
is_movable: false,
display_id: Some(screen.id()),
}
}
// fn render_avatar<T: 'static>(
// avatar: Option<Arc<ImageData>>,

View File

@ -1,54 +1,48 @@
// use std::ops::Range;
use gpui::{
div, AnyElement, Div, IntoElement as _, ParentElement as _, RenderOnce, Styled, WindowContext,
};
// use gpui::{
// geometry::{
// rect::RectF,
// vector::{vec2f, Vector2F},
// },
// json::ToJson,
// serde_json::{self, json},
// AnyElement, Axis, Element, View, ViewContext,
// };
#[derive(Default)]
pub struct FacePile {
pub faces: Vec<AnyElement>,
}
// pub(crate) struct FacePile<V: View> {
// overlap: f32,
// faces: Vec<AnyElement<V>>,
// }
impl RenderOnce for FacePile {
type Rendered = Div;
// impl<V: View> FacePile<V> {
// pub fn new(overlap: f32) -> Self {
// Self {
// overlap,
// faces: Vec::new(),
// }
// }
// }
fn render(self, _: &mut WindowContext) -> Self::Rendered {
let player_count = self.faces.len();
let player_list = self.faces.into_iter().enumerate().map(|(ix, player)| {
let isnt_last = ix < player_count - 1;
// impl<V: View> Element<V> for FacePile<V> {
// type LayoutState = ();
// type PaintState = ();
div().when(isnt_last, |div| div.neg_mr_1()).child(player)
});
div().p_1().flex().items_center().children(player_list)
}
}
// impl Element for FacePile {
// type State = ();
// fn layout(
// &mut self,
// constraint: gpui::SizeConstraint,
// view: &mut V,
// cx: &mut ViewContext<V>,
// ) -> (Vector2F, Self::LayoutState) {
// debug_assert!(constraint.max_along(Axis::Horizontal) == f32::INFINITY);
// state: Option<Self::State>,
// cx: &mut WindowContext,
// ) -> (LayoutId, Self::State) {
// let mut width = 0.;
// let mut max_height = 0.;
// let mut faces = Vec::with_capacity(self.faces.len());
// for face in &mut self.faces {
// let layout = face.layout(constraint, view, cx);
// let layout = face.layout(cx);
// width += layout.x();
// max_height = f32::max(max_height, layout.y());
// faces.push(layout);
// }
// width -= self.overlap * self.faces.len().saturating_sub(1) as f32;
// (
// Vector2F::new(width, max_height.clamp(1., constraint.max.y())),
// (),
// )
// (cx.request_layout(&Style::default(), faces), ())
// // (
// // Vector2F::new(width, max_height.clamp(1., constraint.max.y())),
// // (),
// // ))
// }
// fn paint(
@ -77,37 +71,10 @@
// ()
// }
// fn rect_for_text_range(
// &self,
// _: Range<usize>,
// _: RectF,
// _: RectF,
// _: &Self::LayoutState,
// _: &Self::PaintState,
// _: &V,
// _: &ViewContext<V>,
// ) -> Option<RectF> {
// None
// }
// fn debug(
// &self,
// bounds: RectF,
// _: &Self::LayoutState,
// _: &Self::PaintState,
// _: &V,
// _: &ViewContext<V>,
// ) -> serde_json::Value {
// json!({
// "type": "FacePile",
// "bounds": bounds.to_json()
// })
// }
// }
// impl<V: View> Extend<AnyElement<V>> for FacePile<V> {
// fn extend<T: IntoIterator<Item = AnyElement<V>>>(&mut self, children: T) {
// self.faces.extend(children);
// }
// }
impl Extend<AnyElement> for FacePile {
fn extend<T: IntoIterator<Item = AnyElement>>(&mut self, children: T) {
self.faces.extend(children);
}
}

View File

@ -1,11 +1,11 @@
// use gpui::AppContext;
// use std::sync::Arc;
// use workspace::AppState;
use gpui::AppContext;
use std::sync::Arc;
use workspace::AppState;
// pub mod incoming_call_notification;
pub mod incoming_call_notification;
// pub mod project_shared_notification;
// pub fn init(app_state: &Arc<AppState>, cx: &mut AppContext) {
// incoming_call_notification::init(app_state, cx);
// project_shared_notification::init(app_state, cx);
// }
pub fn init(app_state: &Arc<AppState>, cx: &mut AppContext) {
incoming_call_notification::init(app_state, cx);
//project_shared_notification::init(app_state, cx);
}

View File

@ -1,14 +1,13 @@
use crate::notification_window_options;
use call::{ActiveCall, IncomingCall};
use client::proto;
use futures::StreamExt;
use gpui::{
elements::*,
geometry::vector::vec2f,
platform::{CursorStyle, MouseButton},
AnyElement, AppContext, Entity, View, ViewContext, WindowHandle,
div, px, red, AppContext, Div, Element, ParentElement, Render, RenderOnce, Styled, ViewContext,
VisualContext as _, WindowHandle,
};
use std::sync::{Arc, Weak};
use ui::prelude::*;
use ui::{h_stack, v_stack, Avatar, Button, Label};
use util::ResultExt;
use workspace::AppState;
@ -19,23 +18,44 @@ pub fn init(app_state: &Arc<AppState>, cx: &mut AppContext) {
let mut notification_windows: Vec<WindowHandle<IncomingCallNotification>> = Vec::new();
while let Some(incoming_call) = incoming_call.next().await {
for window in notification_windows.drain(..) {
window.remove(&mut cx);
window
.update(&mut cx, |_, cx| {
// todo!()
cx.remove_window();
})
.log_err();
}
if let Some(incoming_call) = incoming_call {
let window_size = cx.read(|cx| {
let theme = &theme::current(cx).incoming_call_notification;
vec2f(theme.window_width, theme.window_height)
});
let unique_screens = cx.update(|cx| cx.displays()).unwrap();
let window_size = gpui::Size {
width: px(380.),
height: px(64.),
};
for screen in cx.platform().screens() {
for window in unique_screens {
let options = notification_window_options(window, window_size);
let window = cx
.add_window(notification_window_options(screen, window_size), |_| {
IncomingCallNotification::new(incoming_call.clone(), app_state.clone())
});
.open_window(options, |cx| {
cx.build_view(|_| {
IncomingCallNotification::new(
incoming_call.clone(),
app_state.clone(),
)
})
})
.unwrap();
notification_windows.push(window);
}
// for screen in cx.platform().screens() {
// let window = cx
// .add_window(notification_window_options(screen, window_size), |_| {
// IncomingCallNotification::new(incoming_call.clone(), app_state.clone())
// });
// notification_windows.push(window);
// }
}
}
})
@ -47,167 +67,206 @@ struct RespondToCall {
accept: bool,
}
pub struct IncomingCallNotification {
struct IncomingCallNotificationState {
call: IncomingCall,
app_state: Weak<AppState>,
}
impl IncomingCallNotification {
pub struct IncomingCallNotification {
state: Arc<IncomingCallNotificationState>,
}
impl IncomingCallNotificationState {
pub fn new(call: IncomingCall, app_state: Weak<AppState>) -> Self {
Self { call, app_state }
}
fn respond(&mut self, accept: bool, cx: &mut ViewContext<Self>) {
fn respond(&self, accept: bool, cx: &mut AppContext) {
let active_call = ActiveCall::global(cx);
if accept {
let join = active_call.update(cx, |active_call, cx| active_call.accept_incoming(cx));
let caller_user_id = self.call.calling_user.id;
let initial_project_id = self.call.initial_project.as_ref().map(|project| project.id);
let app_state = self.app_state.clone();
cx.app_context()
.spawn(|mut cx| async move {
join.await?;
if let Some(project_id) = initial_project_id {
cx.update(|cx| {
if let Some(app_state) = app_state.upgrade() {
workspace::join_remote_project(
project_id,
caller_user_id,
app_state,
cx,
)
.detach_and_log_err(cx);
}
});
}
anyhow::Ok(())
})
.detach_and_log_err(cx);
let cx: &mut AppContext = cx;
cx.spawn(|cx| async move {
join.await?;
if let Some(_project_id) = initial_project_id {
cx.update(|_cx| {
if let Some(_app_state) = app_state.upgrade() {
// workspace::join_remote_project(
// project_id,
// caller_user_id,
// app_state,
// cx,
// )
// .detach_and_log_err(cx);
}
})
.log_err();
}
anyhow::Ok(())
})
.detach_and_log_err(cx);
} else {
active_call.update(cx, |active_call, cx| {
active_call.decline_incoming(cx).log_err();
});
}
}
fn render_caller(&self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
let theme = &theme::current(cx).incoming_call_notification;
let default_project = proto::ParticipantProject::default();
let initial_project = self
.call
.initial_project
.as_ref()
.unwrap_or(&default_project);
Flex::row()
.with_children(self.call.calling_user.avatar.clone().map(|avatar| {
Image::from_data(avatar)
.with_style(theme.caller_avatar)
.aligned()
}))
.with_child(
Flex::column()
.with_child(
Label::new(
self.call.calling_user.github_login.clone(),
theme.caller_username.text.clone(),
)
.contained()
.with_style(theme.caller_username.container),
)
.with_child(
Label::new(
format!(
"is sharing a project in Zed{}",
if initial_project.worktree_root_names.is_empty() {
""
} else {
":"
}
),
theme.caller_message.text.clone(),
)
.contained()
.with_style(theme.caller_message.container),
)
.with_children(if initial_project.worktree_root_names.is_empty() {
None
} else {
Some(
Label::new(
initial_project.worktree_root_names.join(", "),
theme.worktree_roots.text.clone(),
)
.contained()
.with_style(theme.worktree_roots.container),
)
})
.contained()
.with_style(theme.caller_metadata)
.aligned(),
)
.contained()
.with_style(theme.caller_container)
.flex(1., true)
.into_any()
}
fn render_buttons(&self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
enum Accept {}
enum Decline {}
let theme = theme::current(cx);
Flex::column()
.with_child(
MouseEventHandler::new::<Accept, _>(0, cx, |_, _| {
let theme = &theme.incoming_call_notification;
Label::new("Accept", theme.accept_button.text.clone())
.aligned()
.contained()
.with_style(theme.accept_button.container)
})
.with_cursor_style(CursorStyle::PointingHand)
.on_click(MouseButton::Left, |_, this, cx| {
this.respond(true, cx);
})
.flex(1., true),
)
.with_child(
MouseEventHandler::new::<Decline, _>(0, cx, |_, _| {
let theme = &theme.incoming_call_notification;
Label::new("Decline", theme.decline_button.text.clone())
.aligned()
.contained()
.with_style(theme.decline_button.container)
})
.with_cursor_style(CursorStyle::PointingHand)
.on_click(MouseButton::Left, |_, this, cx| {
this.respond(false, cx);
})
.flex(1., true),
)
.constrained()
.with_width(theme.incoming_call_notification.button_width)
.into_any()
}
}
impl Entity for IncomingCallNotification {
type Event = ();
}
impl View for IncomingCallNotification {
fn ui_name() -> &'static str {
"IncomingCallNotification"
impl IncomingCallNotification {
pub fn new(call: IncomingCall, app_state: Weak<AppState>) -> Self {
Self {
state: Arc::new(IncomingCallNotificationState::new(call, app_state)),
}
}
fn render_caller(&self, cx: &mut ViewContext<Self>) -> impl Element {
h_stack()
.children(
self.state
.call
.calling_user
.avatar
.as_ref()
.map(|avatar| Avatar::data(avatar.clone())),
)
.child(
v_stack()
.child(Label::new(format!(
"{} is sharing a project in Zed",
self.state.call.calling_user.github_login
)))
.child(self.render_buttons(cx)),
)
// let theme = &theme::current(cx).incoming_call_notification;
// let default_project = proto::ParticipantProject::default();
// let initial_project = self
// .call
// .initial_project
// .as_ref()
// .unwrap_or(&default_project);
// Flex::row()
// .with_children(self.call.calling_user.avatar.clone().map(|avatar| {
// Image::from_data(avatar)
// .with_style(theme.caller_avatar)
// .aligned()
// }))
// .with_child(
// Flex::column()
// .with_child(
// Label::new(
// self.call.calling_user.github_login.clone(),
// theme.caller_username.text.clone(),
// )
// .contained()
// .with_style(theme.caller_username.container),
// )
// .with_child(
// Label::new(
// format!(
// "is sharing a project in Zed{}",
// if initial_project.worktree_root_names.is_empty() {
// ""
// } else {
// ":"
// }
// ),
// theme.caller_message.text.clone(),
// )
// .contained()
// .with_style(theme.caller_message.container),
// )
// .with_children(if initial_project.worktree_root_names.is_empty() {
// None
// } else {
// Some(
// Label::new(
// initial_project.worktree_root_names.join(", "),
// theme.worktree_roots.text.clone(),
// )
// .contained()
// .with_style(theme.worktree_roots.container),
// )
// })
// .contained()
// .with_style(theme.caller_metadata)
// .aligned(),
// )
// .contained()
// .with_style(theme.caller_container)
// .flex(1., true)
// .into_any()
}
fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
let background = theme::current(cx).incoming_call_notification.background;
Flex::row()
.with_child(self.render_caller(cx))
.with_child(self.render_buttons(cx))
.contained()
.with_background_color(background)
.expanded()
.into_any()
fn render_buttons(&self, cx: &mut ViewContext<Self>) -> impl Element {
h_stack()
.child(
Button::new("accept", "Accept")
.render(cx)
// .bg(green())
.on_click({
let state = self.state.clone();
move |_, cx| state.respond(true, cx)
}),
)
.child(
Button::new("decline", "Decline")
.render(cx)
// .bg(red())
.on_click({
let state = self.state.clone();
move |_, cx| state.respond(false, cx)
}),
)
// enum Accept {}
// enum Decline {}
// let theme = theme::current(cx);
// Flex::column()
// .with_child(
// MouseEventHandler::new::<Accept, _>(0, cx, |_, _| {
// let theme = &theme.incoming_call_notification;
// Label::new("Accept", theme.accept_button.text.clone())
// .aligned()
// .contained()
// .with_style(theme.accept_button.container)
// })
// .with_cursor_style(CursorStyle::PointingHand)
// .on_click(MouseButton::Left, |_, this, cx| {
// this.respond(true, cx);
// })
// .flex(1., true),
// )
// .with_child(
// MouseEventHandler::new::<Decline, _>(0, cx, |_, _| {
// let theme = &theme.incoming_call_notification;
// Label::new("Decline", theme.decline_button.text.clone())
// .aligned()
// .contained()
// .with_style(theme.decline_button.container)
// })
// .with_cursor_style(CursorStyle::PointingHand)
// .on_click(MouseButton::Left, |_, this, cx| {
// this.respond(false, cx);
// })
// .flex(1., true),
// )
// .constrained()
// .with_width(theme.incoming_call_notification.button_width)
// .into_any()
}
}
impl Render for IncomingCallNotification {
type Element = Div;
fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
div().bg(red()).flex_none().child(self.render_caller(cx))
// Flex::row()
// .with_child()
// .with_child(self.render_buttons(cx))
// .contained()
// .with_background_color(background)
// .expanded()
// .into_any()
}
}

View File

@ -1,16 +1,17 @@
use collections::{CommandPaletteFilter, HashMap};
use fuzzy::{StringMatch, StringMatchCandidate};
use gpui::{
actions, div, prelude::*, Action, AppContext, Div, EventEmitter, FocusHandle, FocusableView,
Keystroke, Manager, ParentElement, Render, Styled, View, ViewContext, VisualContext, WeakView,
};
use picker::{Picker, PickerDelegate};
use std::{
cmp::{self, Reverse},
sync::Arc,
};
use theme::ActiveTheme;
use ui::{h_stack, v_stack, HighlightedLabel, KeyBinding, StyledExt};
use collections::{CommandPaletteFilter, HashMap};
use fuzzy::{StringMatch, StringMatchCandidate};
use gpui::{
actions, Action, AppContext, DismissEvent, Div, EventEmitter, FocusHandle, FocusableView,
Keystroke, ParentElement, Render, Styled, View, ViewContext, VisualContext, WeakView,
};
use picker::{Picker, PickerDelegate};
use ui::{h_stack, v_stack, HighlightedLabel, KeyBinding, ListItem};
use util::{
channel::{parse_zed_link, ReleaseChannel, RELEASE_CHANNEL},
ResultExt,
@ -68,7 +69,7 @@ impl CommandPalette {
}
}
impl EventEmitter<Manager> for CommandPalette {}
impl EventEmitter<DismissEvent> for CommandPalette {}
impl FocusableView for CommandPalette {
fn focus_handle(&self, cx: &AppContext) -> FocusHandle {
@ -80,7 +81,7 @@ impl Render for CommandPalette {
type Element = Div;
fn render(&mut self, _cx: &mut ViewContext<Self>) -> Self::Element {
v_stack().w_96().child(self.picker.clone())
v_stack().min_w_96().child(self.picker.clone())
}
}
@ -140,7 +141,7 @@ impl CommandPaletteDelegate {
}
impl PickerDelegate for CommandPaletteDelegate {
type ListItem = Div;
type ListItem = ListItem;
fn placeholder_text(&self) -> Arc<str> {
"Execute a command...".into()
@ -268,7 +269,7 @@ impl PickerDelegate for CommandPaletteDelegate {
fn dismissed(&mut self, cx: &mut ViewContext<Picker<Self>>) {
self.command_palette
.update(cx, |_, cx| cx.emit(Manager::Dismiss))
.update(cx, |_, cx| cx.emit(DismissEvent))
.log_err();
}
@ -293,32 +294,26 @@ impl PickerDelegate for CommandPaletteDelegate {
ix: usize,
selected: bool,
cx: &mut ViewContext<Picker<Self>>,
) -> Self::ListItem {
let colors = cx.theme().colors();
) -> Option<Self::ListItem> {
let Some(r#match) = self.matches.get(ix) else {
return div();
return None;
};
let Some(command) = self.commands.get(r#match.candidate_id) else {
return div();
return None;
};
div()
.px_1()
.text_color(colors.text)
.text_ui()
.bg(colors.ghost_element_background)
.rounded_md()
.when(selected, |this| this.bg(colors.ghost_element_selected))
.hover(|this| this.bg(colors.ghost_element_hover))
.child(
Some(
ListItem::new(ix).inset(true).selected(selected).child(
h_stack()
.w_full()
.justify_between()
.child(HighlightedLabel::new(
command.name.clone(),
r#match.positions.clone(),
))
.children(KeyBinding::for_action(&*command.action, cx)),
)
),
)
}
}

View File

@ -126,7 +126,7 @@ impl View for ProjectDiagnosticsEditor {
json!({
"project": json!({
"language_servers": project.language_server_statuses().collect::<Vec<_>>(),
"summary": project.diagnostic_summary(cx),
"summary": project.diagnostic_summary(false, cx),
}),
"summary": self.summary,
"paths_to_update": self.paths_to_update.iter().map(|(server_id, paths)|
@ -195,7 +195,7 @@ impl ProjectDiagnosticsEditor {
});
let project = project_handle.read(cx);
let summary = project.diagnostic_summary(cx);
let summary = project.diagnostic_summary(false, cx);
let mut this = Self {
project: project_handle,
summary,
@ -241,7 +241,7 @@ impl ProjectDiagnosticsEditor {
let mut new_summaries: HashMap<LanguageServerId, HashSet<ProjectPath>> = self
.project
.read(cx)
.diagnostic_summaries(cx)
.diagnostic_summaries(false, cx)
.fold(HashMap::default(), |mut summaries, (path, server_id, _)| {
summaries.entry(server_id).or_default().insert(path);
summaries
@ -320,7 +320,7 @@ impl ProjectDiagnosticsEditor {
.context("rechecking diagnostics for paths")?;
this.update(&mut cx, |this, cx| {
this.summary = this.project.read(cx).diagnostic_summary(cx);
this.summary = this.project.read(cx).diagnostic_summary(false, cx);
cx.emit(Event::TitleChanged);
})?;
anyhow::Ok(())

View File

@ -34,19 +34,19 @@ impl DiagnosticIndicator {
}
project::Event::DiskBasedDiagnosticsFinished { language_server_id }
| project::Event::LanguageServerRemoved(language_server_id) => {
this.summary = project.read(cx).diagnostic_summary(cx);
this.summary = project.read(cx).diagnostic_summary(false, cx);
this.in_progress_checks.remove(language_server_id);
cx.notify();
}
project::Event::DiagnosticsUpdated { .. } => {
this.summary = project.read(cx).diagnostic_summary(cx);
this.summary = project.read(cx).diagnostic_summary(false, cx);
cx.notify();
}
_ => {}
})
.detach();
Self {
summary: project.read(cx).diagnostic_summary(cx),
summary: project.read(cx).diagnostic_summary(false, cx),
in_progress_checks: project
.read(cx)
.language_servers_running_disk_based_diagnostics()

View File

@ -165,7 +165,7 @@ impl ProjectDiagnosticsEditor {
});
let project = project_handle.read(cx);
let summary = project.diagnostic_summary(cx);
let summary = project.diagnostic_summary(false, cx);
let mut this = Self {
project: project_handle,
summary,
@ -252,7 +252,7 @@ impl ProjectDiagnosticsEditor {
let mut new_summaries: HashMap<LanguageServerId, HashSet<ProjectPath>> = self
.project
.read(cx)
.diagnostic_summaries(cx)
.diagnostic_summaries(false, cx)
.fold(HashMap::default(), |mut summaries, (path, server_id, _)| {
summaries.entry(server_id).or_default().insert(path);
summaries
@ -332,7 +332,7 @@ impl ProjectDiagnosticsEditor {
.context("rechecking diagnostics for paths")?;
this.update(&mut cx, |this, cx| {
this.summary = this.project.read(cx).diagnostic_summary(cx);
this.summary = this.project.read(cx).diagnostic_summary(false, cx);
cx.emit(ItemEvent::UpdateTab);
cx.emit(ItemEvent::UpdateBreadcrumbs);
})?;

View File

@ -77,13 +77,13 @@ impl DiagnosticIndicator {
project::Event::DiskBasedDiagnosticsFinished { language_server_id }
| project::Event::LanguageServerRemoved(language_server_id) => {
this.summary = project.read(cx).diagnostic_summary(cx);
this.summary = project.read(cx).diagnostic_summary(false, cx);
this.in_progress_checks.remove(language_server_id);
cx.notify();
}
project::Event::DiagnosticsUpdated { .. } => {
this.summary = project.read(cx).diagnostic_summary(cx);
this.summary = project.read(cx).diagnostic_summary(false, cx);
cx.notify();
}
@ -92,7 +92,7 @@ impl DiagnosticIndicator {
.detach();
Self {
summary: project.read(cx).diagnostic_summary(cx),
summary: project.read(cx).diagnostic_summary(false, cx),
in_progress_checks: project
.read(cx)
.language_servers_running_disk_based_diagnostics()

View File

@ -1,5 +1,6 @@
use crate::ProjectDiagnosticsEditor;
use gpui::{div, Div, EventEmitter, ParentElement, Render, ViewContext, WeakView};
use ui::prelude::*;
use ui::{Icon, IconButton, Tooltip};
use workspace::{item::ItemHandle, ToolbarItemEvent, ToolbarItemLocation, ToolbarItemView};

View File

@ -40,11 +40,12 @@ use fuzzy::{StringMatch, StringMatchCandidate};
use git::diff_hunk_to_display;
use gpui::{
actions, div, point, prelude::*, px, relative, rems, size, uniform_list, Action, AnyElement,
AppContext, AsyncWindowContext, BackgroundExecutor, Bounds, ClipboardItem, Context, ElementId,
EventEmitter, FocusHandle, FocusableView, FontFeatures, FontStyle, FontWeight, HighlightStyle,
Hsla, InputHandler, InteractiveText, KeyContext, Model, MouseButton, ParentElement, Pixels,
Render, RenderOnce, SharedString, Styled, StyledText, Subscription, Task, TextRun, TextStyle,
UniformListScrollHandle, View, ViewContext, VisualContext, WeakView, WhiteSpace, WindowContext,
AppContext, AsyncWindowContext, BackgroundExecutor, Bounds, ClipboardItem, Context,
DispatchPhase, Div, ElementId, EventEmitter, FocusHandle, FocusableView, FontFeatures,
FontStyle, FontWeight, HighlightStyle, Hsla, InputHandler, InteractiveText, KeyContext, Model,
MouseButton, ParentElement, Pixels, Render, RenderOnce, SharedString, Styled, StyledText,
Subscription, Task, TextRun, TextStyle, UniformListScrollHandle, View, ViewContext,
VisualContext, WeakView, WhiteSpace, WindowContext,
};
use highlight_matching_bracket::refresh_matching_bracket_highlights;
use hover_popover::{hide_hover, HoverState};
@ -72,7 +73,7 @@ use ordered_float::OrderedFloat;
use parking_lot::{Mutex, RwLock};
use project::{FormatTrigger, Location, Project, ProjectPath, ProjectTransaction};
use rand::prelude::*;
use rpc::proto::*;
use rpc::proto::{self, *};
use scroll::{
autoscroll::Autoscroll, OngoingScroll, ScrollAnchor, ScrollManager, ScrollbarAutoHide,
};
@ -98,12 +99,13 @@ use text::{OffsetUtf16, Rope};
use theme::{
ActiveTheme, DiagnosticStyle, PlayerColor, SyntaxTheme, Theme, ThemeColors, ThemeSettings,
};
use ui::{h_stack, v_stack, HighlightedLabel, IconButton, Popover, StyledExt, Tooltip};
use ui::prelude::*;
use ui::{h_stack, v_stack, HighlightedLabel, IconButton, Popover, Tooltip};
use util::{post_inc, RangeExt, ResultExt, TryFutureExt};
use workspace::{
item::{ItemEvent, ItemHandle},
searchable::SearchEvent,
ItemNavHistory, SplitDirection, ViewId, Workspace,
ItemNavHistory, Pane, SplitDirection, ViewId, Workspace,
};
const CURSOR_BLINK_INTERVAL: Duration = Duration::from_millis(500);
@ -152,7 +154,6 @@ pub fn render_parsed_markdown(
}),
);
// todo!("add the ability to change cursor style for link ranges")
let mut links = Vec::new();
let mut link_ranges = Vec::new();
for (range, region) in parsed.region_ranges.iter().zip(&parsed.regions) {
@ -529,8 +530,6 @@ pub fn init(cx: &mut AppContext) {
// cx.register_action_type(Editor::context_menu_next);
// cx.register_action_type(Editor::context_menu_last);
hover_popover::init(cx);
workspace::register_project_item::<Editor>(cx);
workspace::register_followable_item::<Editor>(cx);
workspace::register_deserializable_item::<Editor>(cx);
@ -663,6 +662,7 @@ pub struct Editor {
pixel_position_of_newest_cursor: Option<gpui::Point<Pixels>>,
gutter_width: Pixels,
style: Option<EditorStyle>,
editor_actions: Vec<Box<dyn Fn(&mut ViewContext<Self>)>>,
}
pub struct EditorSnapshot {
@ -970,95 +970,94 @@ impl CompletionsMenu {
fn pre_resolve_completion_documentation(
&self,
_editor: &Editor,
_cx: &mut ViewContext<Editor>,
editor: &Editor,
cx: &mut ViewContext<Editor>,
) -> Option<Task<()>> {
// todo!("implementation below ");
None
let settings = EditorSettings::get_global(cx);
if !settings.show_completion_documentation {
return None;
}
let Some(project) = editor.project.clone() else {
return None;
};
let client = project.read(cx).client();
let language_registry = project.read(cx).languages().clone();
let is_remote = project.read(cx).is_remote();
let project_id = project.read(cx).remote_id();
let completions = self.completions.clone();
let completion_indices: Vec<_> = self.matches.iter().map(|m| m.candidate_id).collect();
Some(cx.spawn(move |this, mut cx| async move {
if is_remote {
let Some(project_id) = project_id else {
log::error!("Remote project without remote_id");
return;
};
for completion_index in completion_indices {
let completions_guard = completions.read();
let completion = &completions_guard[completion_index];
if completion.documentation.is_some() {
continue;
}
let server_id = completion.server_id;
let completion = completion.lsp_completion.clone();
drop(completions_guard);
Self::resolve_completion_documentation_remote(
project_id,
server_id,
completions.clone(),
completion_index,
completion,
client.clone(),
language_registry.clone(),
)
.await;
_ = this.update(&mut cx, |_, cx| cx.notify());
}
} else {
for completion_index in completion_indices {
let completions_guard = completions.read();
let completion = &completions_guard[completion_index];
if completion.documentation.is_some() {
continue;
}
let server_id = completion.server_id;
let completion = completion.lsp_completion.clone();
drop(completions_guard);
let server = project
.read_with(&mut cx, |project, _| {
project.language_server_for_id(server_id)
})
.ok()
.flatten();
let Some(server) = server else {
return;
};
Self::resolve_completion_documentation_local(
server,
completions.clone(),
completion_index,
completion,
language_registry.clone(),
)
.await;
_ = this.update(&mut cx, |_, cx| cx.notify());
}
}
}))
}
// {
// let settings = EditorSettings::get_global(cx);
// if !settings.show_completion_documentation {
// return None;
// }
// let Some(project) = editor.project.clone() else {
// return None;
// };
// let client = project.read(cx).client();
// let language_registry = project.read(cx).languages().clone();
// let is_remote = project.read(cx).is_remote();
// let project_id = project.read(cx).remote_id();
// let completions = self.completions.clone();
// let completion_indices: Vec<_> = self.matches.iter().map(|m| m.candidate_id).collect();
// Some(cx.spawn(move |this, mut cx| async move {
// if is_remote {
// let Some(project_id) = project_id else {
// log::error!("Remote project without remote_id");
// return;
// };
// for completion_index in completion_indices {
// let completions_guard = completions.read();
// let completion = &completions_guard[completion_index];
// if completion.documentation.is_some() {
// continue;
// }
// let server_id = completion.server_id;
// let completion = completion.lsp_completion.clone();
// drop(completions_guard);
// Self::resolve_completion_documentation_remote(
// project_id,
// server_id,
// completions.clone(),
// completion_index,
// completion,
// client.clone(),
// language_registry.clone(),
// )
// .await;
// _ = this.update(&mut cx, |_, cx| cx.notify());
// }
// } else {
// for completion_index in completion_indices {
// let completions_guard = completions.read();
// let completion = &completions_guard[completion_index];
// if completion.documentation.is_some() {
// continue;
// }
// let server_id = completion.server_id;
// let completion = completion.lsp_completion.clone();
// drop(completions_guard);
// let server = project.read_with(&mut cx, |project, _| {
// project.language_server_for_id(server_id)
// });
// let Some(server) = server else {
// return;
// };
// Self::resolve_completion_documentation_local(
// server,
// completions.clone(),
// completion_index,
// completion,
// language_registry.clone(),
// )
// .await;
// _ = this.update(&mut cx, |_, cx| cx.notify());
// }
// }
// }))
// }
fn attempt_resolve_selected_completion_documentation(
&mut self,
@ -1079,10 +1078,9 @@ impl CompletionsMenu {
let completions = self.completions.clone();
let completions_guard = completions.read();
let completion = &completions_guard[completion_index];
// todo!()
// if completion.documentation.is_some() {
// return;
// }
if completion.documentation.is_some() {
return;
}
let server_id = completion.server_id;
let completion = completion.lsp_completion.clone();
@ -1141,41 +1139,40 @@ impl CompletionsMenu {
client: Arc<Client>,
language_registry: Arc<LanguageRegistry>,
) {
// todo!()
// let request = proto::ResolveCompletionDocumentation {
// project_id,
// language_server_id: server_id.0 as u64,
// lsp_completion: serde_json::to_string(&completion).unwrap().into_bytes(),
// };
let request = proto::ResolveCompletionDocumentation {
project_id,
language_server_id: server_id.0 as u64,
lsp_completion: serde_json::to_string(&completion).unwrap().into_bytes(),
};
// let Some(response) = client
// .request(request)
// .await
// .context("completion documentation resolve proto request")
// .log_err()
// else {
// return;
// };
let Some(response) = client
.request(request)
.await
.context("completion documentation resolve proto request")
.log_err()
else {
return;
};
// if response.text.is_empty() {
// let mut completions = completions.write();
// let completion = &mut completions[completion_index];
// completion.documentation = Some(Documentation::Undocumented);
// }
if response.text.is_empty() {
let mut completions = completions.write();
let completion = &mut completions[completion_index];
completion.documentation = Some(Documentation::Undocumented);
}
// let documentation = if response.is_markdown {
// Documentation::MultiLineMarkdown(
// markdown::parse_markdown(&response.text, &language_registry, None).await,
// )
// } else if response.text.lines().count() <= 1 {
// Documentation::SingleLine(response.text)
// } else {
// Documentation::MultiLinePlainText(response.text)
// };
let documentation = if response.is_markdown {
Documentation::MultiLineMarkdown(
markdown::parse_markdown(&response.text, &language_registry, None).await,
)
} else if response.text.lines().count() <= 1 {
Documentation::SingleLine(response.text)
} else {
Documentation::MultiLinePlainText(response.text)
};
// let mut completions = completions.write();
// let completion = &mut completions[completion_index];
// completion.documentation = Some(documentation);
let mut completions = completions.write();
let completion = &mut completions[completion_index];
completion.documentation = Some(documentation);
}
async fn resolve_completion_documentation_local(
@ -1185,38 +1182,37 @@ impl CompletionsMenu {
completion: lsp::CompletionItem,
language_registry: Arc<LanguageRegistry>,
) {
// todo!()
// let can_resolve = server
// .capabilities()
// .completion_provider
// .as_ref()
// .and_then(|options| options.resolve_provider)
// .unwrap_or(false);
// if !can_resolve {
// return;
// }
let can_resolve = server
.capabilities()
.completion_provider
.as_ref()
.and_then(|options| options.resolve_provider)
.unwrap_or(false);
if !can_resolve {
return;
}
// let request = server.request::<lsp::request::ResolveCompletionItem>(completion);
// let Some(completion_item) = request.await.log_err() else {
// return;
// };
let request = server.request::<lsp::request::ResolveCompletionItem>(completion);
let Some(completion_item) = request.await.log_err() else {
return;
};
// if let Some(lsp_documentation) = completion_item.documentation {
// let documentation = language::prepare_completion_documentation(
// &lsp_documentation,
// &language_registry,
// None, // TODO: Try to reasonably work out which language the completion is for
// )
// .await;
if let Some(lsp_documentation) = completion_item.documentation {
let documentation = language::prepare_completion_documentation(
&lsp_documentation,
&language_registry,
None, // TODO: Try to reasonably work out which language the completion is for
)
.await;
// let mut completions = completions.write();
// let completion = &mut completions[completion_index];
// completion.documentation = Some(documentation);
// } else {
// let mut completions = completions.write();
// let completion = &mut completions[completion_index];
// completion.documentation = Some(Documentation::Undocumented);
// }
let mut completions = completions.write();
let completion = &mut completions[completion_index];
completion.documentation = Some(documentation);
} else {
let mut completions = completions.write();
let completion = &mut completions[completion_index];
completion.documentation = Some(Documentation::Undocumented);
}
}
fn visible(&self) -> bool {
@ -1272,6 +1268,13 @@ impl CompletionsMenu {
multiline_docs.map(|div| {
div.id("multiline_docs")
.max_h(max_height)
.flex_1()
.px_1p5()
.py_1()
.min_w(px(260.))
.max_w(px(640.))
.w(px(500.))
.text_ui()
.overflow_y_scroll()
// Prevent a mouse down on documentation from being propagated to the editor,
// because that would move the cursor.
@ -1322,13 +1325,18 @@ impl CompletionsMenu {
div()
.id(mat.candidate_id)
.min_w(px(300.))
.max_w(px(700.))
.min_w(px(220.))
.max_w(px(540.))
.whitespace_nowrap()
.overflow_hidden()
.bg(gpui::green())
.hover(|style| style.bg(gpui::blue()))
.when(item_ix == selected_item, |div| div.bg(gpui::red()))
.text_ui()
.px_1()
.rounded(px(4.))
.bg(cx.theme().colors().ghost_element_background)
.hover(|style| style.bg(cx.theme().colors().ghost_element_hover))
.when(item_ix == selected_item, |div| {
div.bg(cx.theme().colors().ghost_element_selected)
})
.on_mouse_down(
MouseButton::Left,
cx.listener(move |editor, event, cx| {
@ -1887,6 +1895,7 @@ impl Editor {
pixel_position_of_newest_cursor: None,
gutter_width: Default::default(),
style: None,
editor_actions: Default::default(),
_subscriptions: vec![
cx.observe(&buffer, Self::on_buffer_changed),
cx.subscribe(&buffer, Self::on_buffer_event),
@ -2018,10 +2027,14 @@ impl Editor {
&self.buffer
}
fn workspace(&self) -> Option<View<Workspace>> {
pub fn workspace(&self) -> Option<View<Workspace>> {
self.workspace.as_ref()?.0.upgrade()
}
pub fn pane(&self, cx: &AppContext) -> Option<View<Pane>> {
self.workspace()?.read(cx).pane_for(&self.handle.upgrade()?)
}
pub fn title<'a>(&self, cx: &'a AppContext) -> Cow<'a, str> {
self.buffer().read(cx).title(cx)
}
@ -4369,7 +4382,7 @@ impl Editor {
editor.fold_at(&FoldAt { buffer_row }, cx);
}
}))
.color(ui::Color::Muted)
.icon_color(ui::Color::Muted)
})
})
.flatten()
@ -9178,6 +9191,26 @@ impl Editor {
cx.emit(EditorEvent::Blurred);
cx.notify();
}
pub fn register_action<A: Action>(
&mut self,
listener: impl Fn(&A, &mut WindowContext) + 'static,
) -> &mut Self {
let listener = Arc::new(listener);
self.editor_actions.push(Box::new(move |cx| {
let view = cx.view().clone();
let cx = cx.window_context();
let listener = listener.clone();
cx.on_action(TypeId::of::<A>(), move |action, phase, cx| {
let action = action.downcast_ref().unwrap();
if phase == DispatchPhase::Bubble {
listener(action, cx)
}
})
}));
self
}
}
pub trait CollaborationHub {

View File

@ -5427,178 +5427,177 @@ async fn test_strip_whitespace_and_format_via_lsp(cx: &mut gpui::TestAppContext)
);
}
//todo!(completion)
// #[gpui::test]
// async fn test_completion(cx: &mut gpui::TestAppContext) {
// init_test(cx, |_| {});
#[gpui::test]
async fn test_completion(cx: &mut gpui::TestAppContext) {
init_test(cx, |_| {});
// let mut cx = EditorLspTestContext::new_rust(
// lsp::ServerCapabilities {
// completion_provider: Some(lsp::CompletionOptions {
// trigger_characters: Some(vec![".".to_string(), ":".to_string()]),
// resolve_provider: Some(true),
// ..Default::default()
// }),
// ..Default::default()
// },
// cx,
// )
// .await;
let mut cx = EditorLspTestContext::new_rust(
lsp::ServerCapabilities {
completion_provider: Some(lsp::CompletionOptions {
trigger_characters: Some(vec![".".to_string(), ":".to_string()]),
resolve_provider: Some(true),
..Default::default()
}),
..Default::default()
},
cx,
)
.await;
// cx.set_state(indoc! {"
// oneˇ
// two
// three
// "});
// cx.simulate_keystroke(".");
// handle_completion_request(
// &mut cx,
// indoc! {"
// one.|<>
// two
// three
// "},
// vec!["first_completion", "second_completion"],
// )
// .await;
// cx.condition(|editor, _| editor.context_menu_visible())
// .await;
// let apply_additional_edits = cx.update_editor(|editor, cx| {
// editor.context_menu_next(&Default::default(), cx);
// editor
// .confirm_completion(&ConfirmCompletion::default(), cx)
// .unwrap()
// });
// cx.assert_editor_state(indoc! {"
// one.second_completionˇ
// two
// three
// "});
cx.set_state(indoc! {"
oneˇ
two
three
"});
cx.simulate_keystroke(".");
handle_completion_request(
&mut cx,
indoc! {"
one.|<>
two
three
"},
vec!["first_completion", "second_completion"],
)
.await;
cx.condition(|editor, _| editor.context_menu_visible())
.await;
let apply_additional_edits = cx.update_editor(|editor, cx| {
editor.context_menu_next(&Default::default(), cx);
editor
.confirm_completion(&ConfirmCompletion::default(), cx)
.unwrap()
});
cx.assert_editor_state(indoc! {"
one.second_completionˇ
two
three
"});
// handle_resolve_completion_request(
// &mut cx,
// Some(vec![
// (
// //This overlaps with the primary completion edit which is
// //misbehavior from the LSP spec, test that we filter it out
// indoc! {"
// one.second_ˇcompletion
// two
// threeˇ
// "},
// "overlapping additional edit",
// ),
// (
// indoc! {"
// one.second_completion
// two
// threeˇ
// "},
// "\nadditional edit",
// ),
// ]),
// )
// .await;
// apply_additional_edits.await.unwrap();
// cx.assert_editor_state(indoc! {"
// one.second_completionˇ
// two
// three
// additional edit
// "});
handle_resolve_completion_request(
&mut cx,
Some(vec![
(
//This overlaps with the primary completion edit which is
//misbehavior from the LSP spec, test that we filter it out
indoc! {"
one.second_ˇcompletion
two
threeˇ
"},
"overlapping additional edit",
),
(
indoc! {"
one.second_completion
two
threeˇ
"},
"\nadditional edit",
),
]),
)
.await;
apply_additional_edits.await.unwrap();
cx.assert_editor_state(indoc! {"
one.second_completionˇ
two
three
additional edit
"});
// cx.set_state(indoc! {"
// one.second_completion
// twoˇ
// threeˇ
// additional edit
// "});
// cx.simulate_keystroke(" ");
// assert!(cx.editor(|e, _| e.context_menu.read().is_none()));
// cx.simulate_keystroke("s");
// assert!(cx.editor(|e, _| e.context_menu.read().is_none()));
cx.set_state(indoc! {"
one.second_completion
twoˇ
threeˇ
additional edit
"});
cx.simulate_keystroke(" ");
assert!(cx.editor(|e, _| e.context_menu.read().is_none()));
cx.simulate_keystroke("s");
assert!(cx.editor(|e, _| e.context_menu.read().is_none()));
// cx.assert_editor_state(indoc! {"
// one.second_completion
// two sˇ
// three sˇ
// additional edit
// "});
// handle_completion_request(
// &mut cx,
// indoc! {"
// one.second_completion
// two s
// three <s|>
// additional edit
// "},
// vec!["fourth_completion", "fifth_completion", "sixth_completion"],
// )
// .await;
// cx.condition(|editor, _| editor.context_menu_visible())
// .await;
cx.assert_editor_state(indoc! {"
one.second_completion
two
three
additional edit
"});
handle_completion_request(
&mut cx,
indoc! {"
one.second_completion
two s
three <s|>
additional edit
"},
vec!["fourth_completion", "fifth_completion", "sixth_completion"],
)
.await;
cx.condition(|editor, _| editor.context_menu_visible())
.await;
// cx.simulate_keystroke("i");
cx.simulate_keystroke("i");
// handle_completion_request(
// &mut cx,
// indoc! {"
// one.second_completion
// two si
// three <si|>
// additional edit
// "},
// vec!["fourth_completion", "fifth_completion", "sixth_completion"],
// )
// .await;
// cx.condition(|editor, _| editor.context_menu_visible())
// .await;
handle_completion_request(
&mut cx,
indoc! {"
one.second_completion
two si
three <si|>
additional edit
"},
vec!["fourth_completion", "fifth_completion", "sixth_completion"],
)
.await;
cx.condition(|editor, _| editor.context_menu_visible())
.await;
// let apply_additional_edits = cx.update_editor(|editor, cx| {
// editor
// .confirm_completion(&ConfirmCompletion::default(), cx)
// .unwrap()
// });
// cx.assert_editor_state(indoc! {"
// one.second_completion
// two sixth_completionˇ
// three sixth_completionˇ
// additional edit
// "});
let apply_additional_edits = cx.update_editor(|editor, cx| {
editor
.confirm_completion(&ConfirmCompletion::default(), cx)
.unwrap()
});
cx.assert_editor_state(indoc! {"
one.second_completion
two sixth_completionˇ
three sixth_completionˇ
additional edit
"});
// handle_resolve_completion_request(&mut cx, None).await;
// apply_additional_edits.await.unwrap();
handle_resolve_completion_request(&mut cx, None).await;
apply_additional_edits.await.unwrap();
// cx.update(|cx| {
// cx.update_global::<SettingsStore, _, _>(|settings, cx| {
// settings.update_user_settings::<EditorSettings>(cx, |settings| {
// settings.show_completions_on_input = Some(false);
// });
// })
// });
// cx.set_state("editorˇ");
// cx.simulate_keystroke(".");
// assert!(cx.editor(|e, _| e.context_menu.read().is_none()));
// cx.simulate_keystroke("c");
// cx.simulate_keystroke("l");
// cx.simulate_keystroke("o");
// cx.assert_editor_state("editor.cloˇ");
// assert!(cx.editor(|e, _| e.context_menu.read().is_none()));
// cx.update_editor(|editor, cx| {
// editor.show_completions(&ShowCompletions, cx);
// });
// handle_completion_request(&mut cx, "editor.<clo|>", vec!["close", "clobber"]).await;
// cx.condition(|editor, _| editor.context_menu_visible())
// .await;
// let apply_additional_edits = cx.update_editor(|editor, cx| {
// editor
// .confirm_completion(&ConfirmCompletion::default(), cx)
// .unwrap()
// });
// cx.assert_editor_state("editor.closeˇ");
// handle_resolve_completion_request(&mut cx, None).await;
// apply_additional_edits.await.unwrap();
// }
cx.update(|cx| {
cx.update_global::<SettingsStore, _>(|settings, cx| {
settings.update_user_settings::<EditorSettings>(cx, |settings| {
settings.show_completions_on_input = Some(false);
});
})
});
cx.set_state("editorˇ");
cx.simulate_keystroke(".");
assert!(cx.editor(|e, _| e.context_menu.read().is_none()));
cx.simulate_keystroke("c");
cx.simulate_keystroke("l");
cx.simulate_keystroke("o");
cx.assert_editor_state("editor.cloˇ");
assert!(cx.editor(|e, _| e.context_menu.read().is_none()));
cx.update_editor(|editor, cx| {
editor.show_completions(&ShowCompletions, cx);
});
handle_completion_request(&mut cx, "editor.<clo|>", vec!["close", "clobber"]).await;
cx.condition(|editor, _| editor.context_menu_visible())
.await;
let apply_additional_edits = cx.update_editor(|editor, cx| {
editor
.confirm_completion(&ConfirmCompletion::default(), cx)
.unwrap()
});
cx.assert_editor_state("editor.closeˇ");
handle_resolve_completion_request(&mut cx, None).await;
apply_additional_edits.await.unwrap();
}
#[gpui::test]
async fn test_toggle_comment(cx: &mut gpui::TestAppContext) {
@ -7803,197 +7802,196 @@ async fn test_language_server_restart_due_to_settings_change(cx: &mut gpui::Test
);
}
//todo!(completions)
// #[gpui::test]
// async fn test_completions_with_additional_edits(cx: &mut gpui::TestAppContext) {
// init_test(cx, |_| {});
#[gpui::test]
async fn test_completions_with_additional_edits(cx: &mut gpui::TestAppContext) {
init_test(cx, |_| {});
// let mut cx = EditorLspTestContext::new_rust(
// lsp::ServerCapabilities {
// completion_provider: Some(lsp::CompletionOptions {
// trigger_characters: Some(vec![".".to_string()]),
// resolve_provider: Some(true),
// ..Default::default()
// }),
// ..Default::default()
// },
// cx,
// )
// .await;
let mut cx = EditorLspTestContext::new_rust(
lsp::ServerCapabilities {
completion_provider: Some(lsp::CompletionOptions {
trigger_characters: Some(vec![".".to_string()]),
resolve_provider: Some(true),
..Default::default()
}),
..Default::default()
},
cx,
)
.await;
// cx.set_state(indoc! {"fn main() { let a = 2ˇ; }"});
// cx.simulate_keystroke(".");
// let completion_item = lsp::CompletionItem {
// label: "some".into(),
// kind: Some(lsp::CompletionItemKind::SNIPPET),
// detail: Some("Wrap the expression in an `Option::Some`".to_string()),
// documentation: Some(lsp::Documentation::MarkupContent(lsp::MarkupContent {
// kind: lsp::MarkupKind::Markdown,
// value: "```rust\nSome(2)\n```".to_string(),
// })),
// deprecated: Some(false),
// sort_text: Some("fffffff2".to_string()),
// filter_text: Some("some".to_string()),
// insert_text_format: Some(lsp::InsertTextFormat::SNIPPET),
// text_edit: Some(lsp::CompletionTextEdit::Edit(lsp::TextEdit {
// range: lsp::Range {
// start: lsp::Position {
// line: 0,
// character: 22,
// },
// end: lsp::Position {
// line: 0,
// character: 22,
// },
// },
// new_text: "Some(2)".to_string(),
// })),
// additional_text_edits: Some(vec![lsp::TextEdit {
// range: lsp::Range {
// start: lsp::Position {
// line: 0,
// character: 20,
// },
// end: lsp::Position {
// line: 0,
// character: 22,
// },
// },
// new_text: "".to_string(),
// }]),
// ..Default::default()
// };
cx.set_state(indoc! {"fn main() { let a = 2ˇ; }"});
cx.simulate_keystroke(".");
let completion_item = lsp::CompletionItem {
label: "some".into(),
kind: Some(lsp::CompletionItemKind::SNIPPET),
detail: Some("Wrap the expression in an `Option::Some`".to_string()),
documentation: Some(lsp::Documentation::MarkupContent(lsp::MarkupContent {
kind: lsp::MarkupKind::Markdown,
value: "```rust\nSome(2)\n```".to_string(),
})),
deprecated: Some(false),
sort_text: Some("fffffff2".to_string()),
filter_text: Some("some".to_string()),
insert_text_format: Some(lsp::InsertTextFormat::SNIPPET),
text_edit: Some(lsp::CompletionTextEdit::Edit(lsp::TextEdit {
range: lsp::Range {
start: lsp::Position {
line: 0,
character: 22,
},
end: lsp::Position {
line: 0,
character: 22,
},
},
new_text: "Some(2)".to_string(),
})),
additional_text_edits: Some(vec![lsp::TextEdit {
range: lsp::Range {
start: lsp::Position {
line: 0,
character: 20,
},
end: lsp::Position {
line: 0,
character: 22,
},
},
new_text: "".to_string(),
}]),
..Default::default()
};
// let closure_completion_item = completion_item.clone();
// let mut request = cx.handle_request::<lsp::request::Completion, _, _>(move |_, _, _| {
// let task_completion_item = closure_completion_item.clone();
// async move {
// Ok(Some(lsp::CompletionResponse::Array(vec![
// task_completion_item,
// ])))
// }
// });
let closure_completion_item = completion_item.clone();
let mut request = cx.handle_request::<lsp::request::Completion, _, _>(move |_, _, _| {
let task_completion_item = closure_completion_item.clone();
async move {
Ok(Some(lsp::CompletionResponse::Array(vec![
task_completion_item,
])))
}
});
// request.next().await;
request.next().await;
// cx.condition(|editor, _| editor.context_menu_visible())
// .await;
// let apply_additional_edits = cx.update_editor(|editor, cx| {
// editor
// .confirm_completion(&ConfirmCompletion::default(), cx)
// .unwrap()
// });
// cx.assert_editor_state(indoc! {"fn main() { let a = 2.Some(2)ˇ; }"});
cx.condition(|editor, _| editor.context_menu_visible())
.await;
let apply_additional_edits = cx.update_editor(|editor, cx| {
editor
.confirm_completion(&ConfirmCompletion::default(), cx)
.unwrap()
});
cx.assert_editor_state(indoc! {"fn main() { let a = 2.Some(2)ˇ; }"});
// cx.handle_request::<lsp::request::ResolveCompletionItem, _, _>(move |_, _, _| {
// let task_completion_item = completion_item.clone();
// async move { Ok(task_completion_item) }
// })
// .next()
// .await
// .unwrap();
// apply_additional_edits.await.unwrap();
// cx.assert_editor_state(indoc! {"fn main() { let a = Some(2)ˇ; }"});
// }
cx.handle_request::<lsp::request::ResolveCompletionItem, _, _>(move |_, _, _| {
let task_completion_item = completion_item.clone();
async move { Ok(task_completion_item) }
})
.next()
.await
.unwrap();
apply_additional_edits.await.unwrap();
cx.assert_editor_state(indoc! {"fn main() { let a = Some(2)ˇ; }"});
}
// #[gpui::test]
// async fn test_completions_in_languages_with_extra_word_characters(cx: &mut gpui::TestAppContext) {
// init_test(cx, |_| {});
#[gpui::test]
async fn test_completions_in_languages_with_extra_word_characters(cx: &mut gpui::TestAppContext) {
init_test(cx, |_| {});
// let mut cx = EditorLspTestContext::new(
// Language::new(
// LanguageConfig {
// path_suffixes: vec!["jsx".into()],
// overrides: [(
// "element".into(),
// LanguageConfigOverride {
// word_characters: Override::Set(['-'].into_iter().collect()),
// ..Default::default()
// },
// )]
// .into_iter()
// .collect(),
// ..Default::default()
// },
// Some(tree_sitter_typescript::language_tsx()),
// )
// .with_override_query("(jsx_self_closing_element) @element")
// .unwrap(),
// lsp::ServerCapabilities {
// completion_provider: Some(lsp::CompletionOptions {
// trigger_characters: Some(vec![":".to_string()]),
// ..Default::default()
// }),
// ..Default::default()
// },
// cx,
// )
// .await;
let mut cx = EditorLspTestContext::new(
Language::new(
LanguageConfig {
path_suffixes: vec!["jsx".into()],
overrides: [(
"element".into(),
LanguageConfigOverride {
word_characters: Override::Set(['-'].into_iter().collect()),
..Default::default()
},
)]
.into_iter()
.collect(),
..Default::default()
},
Some(tree_sitter_typescript::language_tsx()),
)
.with_override_query("(jsx_self_closing_element) @element")
.unwrap(),
lsp::ServerCapabilities {
completion_provider: Some(lsp::CompletionOptions {
trigger_characters: Some(vec![":".to_string()]),
..Default::default()
}),
..Default::default()
},
cx,
)
.await;
// cx.lsp
// .handle_request::<lsp::request::Completion, _, _>(move |_, _| async move {
// Ok(Some(lsp::CompletionResponse::Array(vec![
// lsp::CompletionItem {
// label: "bg-blue".into(),
// ..Default::default()
// },
// lsp::CompletionItem {
// label: "bg-red".into(),
// ..Default::default()
// },
// lsp::CompletionItem {
// label: "bg-yellow".into(),
// ..Default::default()
// },
// ])))
// });
cx.lsp
.handle_request::<lsp::request::Completion, _, _>(move |_, _| async move {
Ok(Some(lsp::CompletionResponse::Array(vec![
lsp::CompletionItem {
label: "bg-blue".into(),
..Default::default()
},
lsp::CompletionItem {
label: "bg-red".into(),
..Default::default()
},
lsp::CompletionItem {
label: "bg-yellow".into(),
..Default::default()
},
])))
});
// cx.set_state(r#"<p class="bgˇ" />"#);
cx.set_state(r#"<p class="bgˇ" />"#);
// // Trigger completion when typing a dash, because the dash is an extra
// // word character in the 'element' scope, which contains the cursor.
// cx.simulate_keystroke("-");
// cx.executor().run_until_parked();
// cx.update_editor(|editor, _| {
// if let Some(ContextMenu::Completions(menu)) = editor.context_menu.read().as_ref() {
// assert_eq!(
// menu.matches.iter().map(|m| &m.string).collect::<Vec<_>>(),
// &["bg-red", "bg-blue", "bg-yellow"]
// );
// } else {
// panic!("expected completion menu to be open");
// }
// });
// Trigger completion when typing a dash, because the dash is an extra
// word character in the 'element' scope, which contains the cursor.
cx.simulate_keystroke("-");
cx.executor().run_until_parked();
cx.update_editor(|editor, _| {
if let Some(ContextMenu::Completions(menu)) = editor.context_menu.read().as_ref() {
assert_eq!(
menu.matches.iter().map(|m| &m.string).collect::<Vec<_>>(),
&["bg-red", "bg-blue", "bg-yellow"]
);
} else {
panic!("expected completion menu to be open");
}
});
// cx.simulate_keystroke("l");
// cx.executor().run_until_parked();
// cx.update_editor(|editor, _| {
// if let Some(ContextMenu::Completions(menu)) = editor.context_menu.read().as_ref() {
// assert_eq!(
// menu.matches.iter().map(|m| &m.string).collect::<Vec<_>>(),
// &["bg-blue", "bg-yellow"]
// );
// } else {
// panic!("expected completion menu to be open");
// }
// });
cx.simulate_keystroke("l");
cx.executor().run_until_parked();
cx.update_editor(|editor, _| {
if let Some(ContextMenu::Completions(menu)) = editor.context_menu.read().as_ref() {
assert_eq!(
menu.matches.iter().map(|m| &m.string).collect::<Vec<_>>(),
&["bg-blue", "bg-yellow"]
);
} else {
panic!("expected completion menu to be open");
}
});
// // When filtering completions, consider the character after the '-' to
// // be the start of a subword.
// cx.set_state(r#"<p class="yelˇ" />"#);
// cx.simulate_keystroke("l");
// cx.executor().run_until_parked();
// cx.update_editor(|editor, _| {
// if let Some(ContextMenu::Completions(menu)) = editor.context_menu.read().as_ref() {
// assert_eq!(
// menu.matches.iter().map(|m| &m.string).collect::<Vec<_>>(),
// &["bg-yellow"]
// );
// } else {
// panic!("expected completion menu to be open");
// }
// });
// }
// When filtering completions, consider the character after the '-' to
// be the start of a subword.
cx.set_state(r#"<p class="yelˇ" />"#);
cx.simulate_keystroke("l");
cx.executor().run_until_parked();
cx.update_editor(|editor, _| {
if let Some(ContextMenu::Completions(menu)) = editor.context_menu.read().as_ref() {
assert_eq!(
menu.matches.iter().map(|m| &m.string).collect::<Vec<_>>(),
&["bg-yellow"]
);
} else {
panic!("expected completion menu to be open");
}
});
}
#[gpui::test]
async fn test_document_format_with_prettier(cx: &mut gpui::TestAppContext) {

View File

@ -5,7 +5,9 @@ use crate::{
},
editor_settings::ShowScrollbar,
git::{diff_hunk_to_display, DisplayDiffHunk},
hover_popover::hover_at,
hover_popover::{
self, hover_at, HOVER_POPOVER_GAP, MIN_POPOVER_CHARACTER_WIDTH, MIN_POPOVER_LINE_HEIGHT,
},
link_go_to_definition::{
go_to_fetched_definition, go_to_fetched_type_definition, update_go_to_definition_link,
update_inlay_link_and_hover_points, GoToDefinitionTrigger,
@ -20,10 +22,11 @@ use collections::{BTreeMap, HashMap};
use gpui::{
div, point, px, relative, size, transparent_black, Action, AnyElement, AvailableSpace,
BorrowWindow, Bounds, ContentMask, Corners, DispatchPhase, Edges, Element, ElementId,
ElementInputHandler, Entity, EntityId, Hsla, InteractiveElement, IntoElement, LineLayout,
MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, ParentElement, Pixels, RenderOnce,
ScrollWheelEvent, ShapedLine, SharedString, Size, StatefulInteractiveElement, Style, Styled,
TextRun, TextStyle, View, ViewContext, WeakView, WindowContext, WrappedLine,
ElementInputHandler, Entity, EntityId, Hsla, InteractiveBounds, InteractiveElement,
IntoElement, LineLayout, MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent,
ParentElement, Pixels, RenderOnce, ScrollWheelEvent, ShapedLine, SharedString, Size,
StackingOrder, StatefulInteractiveElement, Style, Styled, TextRun, TextStyle, View,
ViewContext, WeakView, WindowContext, WrappedLine,
};
use itertools::Itertools;
use language::language_settings::ShowWhitespaceSetting;
@ -45,6 +48,7 @@ use std::{
};
use sum_tree::Bias;
use theme::{ActiveTheme, PlayerColor};
use ui::prelude::*;
use ui::{h_stack, IconButton, Tooltip};
use util::ResultExt;
use workspace::item::Item;
@ -126,6 +130,11 @@ impl EditorElement {
fn register_actions(&self, cx: &mut WindowContext) {
let view = &self.editor;
self.editor.update(cx, |editor, cx| {
for action in editor.editor_actions.iter() {
(action)(cx)
}
});
register_action(view, cx, Editor::move_left);
register_action(view, cx, Editor::move_right);
register_action(view, cx, Editor::move_down);
@ -257,6 +266,7 @@ impl EditorElement {
// on_action(cx, Editor::open_excerpts); todo!()
register_action(view, cx, Editor::toggle_soft_wrap);
register_action(view, cx, Editor::toggle_inlay_hints);
register_action(view, cx, hover_popover::hover);
register_action(view, cx, Editor::reveal_in_finder);
register_action(view, cx, Editor::copy_path);
register_action(view, cx, Editor::copy_relative_path);
@ -308,6 +318,7 @@ impl EditorElement {
position_map: &PositionMap,
text_bounds: Bounds<Pixels>,
gutter_bounds: Bounds<Pixels>,
stacking_order: &StackingOrder,
cx: &mut ViewContext<Editor>,
) -> bool {
let mut click_count = event.click_count;
@ -318,6 +329,9 @@ impl EditorElement {
} else if !text_bounds.contains_point(&event.position) {
return false;
}
if !cx.was_top_layer(&event.position, stacking_order) {
return false;
}
let point_for_position = position_map.point_for_position(text_bounds, event.position);
let position = point_for_position.previous_valid;
@ -376,6 +390,7 @@ impl EditorElement {
event: &MouseUpEvent,
position_map: &PositionMap,
text_bounds: Bounds<Pixels>,
stacking_order: &StackingOrder,
cx: &mut ViewContext<Editor>,
) -> bool {
let end_selection = editor.has_pending_selection();
@ -388,6 +403,7 @@ impl EditorElement {
if !pending_nonempty_selections
&& event.modifiers.command
&& text_bounds.contains_point(&event.position)
&& cx.was_top_layer(&event.position, stacking_order)
{
let point = position_map.point_for_position(text_bounds, event.position);
let could_be_inlay = point.as_valid().is_none();
@ -410,6 +426,7 @@ impl EditorElement {
position_map: &PositionMap,
text_bounds: Bounds<Pixels>,
gutter_bounds: Bounds<Pixels>,
stacking_order: &StackingOrder,
cx: &mut ViewContext<Editor>,
) -> bool {
let modifiers = event.modifiers;
@ -449,10 +466,12 @@ impl EditorElement {
let text_hovered = text_bounds.contains_point(&event.position);
let gutter_hovered = gutter_bounds.contains_point(&event.position);
let was_top = cx.was_top_layer(&event.position, stacking_order);
editor.set_gutter_hovered(gutter_hovered, cx);
// Don't trigger hover popover if mouse is hovering over context menu
if text_hovered {
if text_hovered && was_top {
let point_for_position = position_map.point_for_position(text_bounds, event.position);
match point_for_position.as_valid() {
@ -482,7 +501,7 @@ impl EditorElement {
} else {
update_go_to_definition_link(editor, None, modifiers.command, modifiers.shift, cx);
hover_at(editor, None, cx);
gutter_hovered
gutter_hovered && was_top
}
}
@ -490,10 +509,10 @@ impl EditorElement {
editor: &mut Editor,
event: &ScrollWheelEvent,
position_map: &PositionMap,
bounds: Bounds<Pixels>,
bounds: &InteractiveBounds,
cx: &mut ViewContext<Editor>,
) -> bool {
if !bounds.contains_point(&event.position) {
if !bounds.visibly_contains(&event.position, cx) {
return false;
}
@ -1024,8 +1043,8 @@ impl EditorElement {
}
});
if let Some((position, mut context_menu)) = layout.context_menu.take() {
cx.with_z_index(1, |cx| {
cx.with_z_index(1, |cx| {
if let Some((position, mut context_menu)) = layout.context_menu.take() {
let available_space =
size(AvailableSpace::MinContent, AvailableSpace::MinContent);
let context_menu_size = context_menu.measure(available_space, cx);
@ -1052,81 +1071,74 @@ impl EditorElement {
list_origin.y -= layout.position_map.line_height + list_height;
}
context_menu.draw(list_origin, available_space, cx);
})
}
cx.break_content_mask(|cx| {
context_menu.draw(list_origin, available_space, cx)
});
}
// if let Some((position, hover_popovers)) = layout.hover_popovers.as_mut() {
// cx.scene().push_stacking_context(None, None);
if let Some((position, mut hover_popovers)) = layout.hover_popovers.take() {
let available_space =
size(AvailableSpace::MinContent, AvailableSpace::MinContent);
// // This is safe because we check on layout whether the required row is available
// let hovered_row_layout =
// &layout.position_map.line_layouts[(position.row() - start_row) as usize].line;
// This is safe because we check on layout whether the required row is available
let hovered_row_layout = &layout.position_map.line_layouts
[(position.row() - start_row) as usize]
.line;
// // Minimum required size: Take the first popover, and add 1.5 times the minimum popover
// // height. This is the size we will use to decide whether to render popovers above or below
// // the hovered line.
// let first_size = hover_popovers[0].size();
// let height_to_reserve = first_size.y
// + 1.5 * MIN_POPOVER_LINE_HEIGHT as f32 * layout.position_map.line_height;
// Minimum required size: Take the first popover, and add 1.5 times the minimum popover
// height. This is the size we will use to decide whether to render popovers above or below
// the hovered line.
let first_size = hover_popovers[0].measure(available_space, cx);
let height_to_reserve = first_size.height
+ 1.5 * MIN_POPOVER_LINE_HEIGHT * layout.position_map.line_height;
// // Compute Hovered Point
// let x = hovered_row_layout.x_for_index(position.column() as usize) - scroll_left;
// let y = position.row() as f32 * layout.position_map.line_height - scroll_top;
// let hovered_point = content_origin + point(x, y);
// Compute Hovered Point
let x = hovered_row_layout.x_for_index(position.column() as usize)
- layout.position_map.scroll_position.x;
let y = position.row() as f32 * layout.position_map.line_height
- layout.position_map.scroll_position.y;
let hovered_point = content_origin + point(x, y);
// if hovered_point.y - height_to_reserve > 0.0 {
// // There is enough space above. Render popovers above the hovered point
// let mut current_y = hovered_point.y;
// for hover_popover in hover_popovers {
// let size = hover_popover.size();
// let mut popover_origin = point(hovered_point.x, current_y - size.y);
if hovered_point.y - height_to_reserve > Pixels::ZERO {
// There is enough space above. Render popovers above the hovered point
let mut current_y = hovered_point.y;
for mut hover_popover in hover_popovers {
let size = hover_popover.measure(available_space, cx);
let mut popover_origin =
point(hovered_point.x, current_y - size.height);
// let x_out_of_bounds = bounds.max_x - (popover_origin.x + size.x);
// if x_out_of_bounds < 0.0 {
// popover_origin.set_x(popover_origin.x + x_out_of_bounds);
// }
let x_out_of_bounds =
text_bounds.upper_right().x - (popover_origin.x + size.width);
if x_out_of_bounds < Pixels::ZERO {
popover_origin.x = popover_origin.x + x_out_of_bounds;
}
// hover_popover.paint(
// popover_origin,
// Bounds::<Pixels>::from_points(
// gpui::Point::<Pixels>::zero(),
// point(f32::MAX, f32::MAX),
// ), // Let content bleed outside of editor
// editor,
// cx,
// );
cx.break_content_mask(|cx| {
hover_popover.draw(popover_origin, available_space, cx)
});
// current_y = popover_origin.y - HOVER_POPOVER_GAP;
// }
// } else {
// // There is not enough space above. Render popovers below the hovered point
// let mut current_y = hovered_point.y + layout.position_map.line_height;
// for hover_popover in hover_popovers {
// let size = hover_popover.size();
// let mut popover_origin = point(hovered_point.x, current_y);
current_y = popover_origin.y - HOVER_POPOVER_GAP;
}
} else {
// There is not enough space above. Render popovers below the hovered point
let mut current_y = hovered_point.y + layout.position_map.line_height;
for mut hover_popover in hover_popovers {
let size = hover_popover.measure(available_space, cx);
let mut popover_origin = point(hovered_point.x, current_y);
// let x_out_of_bounds = bounds.max_x - (popover_origin.x + size.x);
// if x_out_of_bounds < 0.0 {
// popover_origin.set_x(popover_origin.x + x_out_of_bounds);
// }
let x_out_of_bounds =
text_bounds.upper_right().x - (popover_origin.x + size.width);
if x_out_of_bounds < Pixels::ZERO {
popover_origin.x = popover_origin.x + x_out_of_bounds;
}
// hover_popover.paint(
// popover_origin,
// Bounds::<Pixels>::from_points(
// gpui::Point::<Pixels>::zero(),
// point(f32::MAX, f32::MAX),
// ), // Let content bleed outside of editor
// editor,
// cx,
// );
hover_popover.draw(popover_origin, available_space, cx);
// current_y = popover_origin.y + size.y + HOVER_POPOVER_GAP;
// }
// }
// cx.scene().pop_stacking_context();
// }
current_y = popover_origin.y + size.height + HOVER_POPOVER_GAP;
}
}
}
})
},
)
}
@ -1992,15 +2004,23 @@ impl EditorElement {
}
let visible_rows = start_row..start_row + line_layouts.len() as u32;
// todo!("hover")
// let mut hover = editor.hover_state.render(
// &snapshot,
// &style,
// visible_rows,
// editor.workspace.as_ref().map(|(w, _)| w.clone()),
// cx,
// );
// let mode = editor.mode;
let max_size = size(
(120. * em_width) // Default size
.min(bounds.size.width / 2.) // Shrink to half of the editor width
.max(MIN_POPOVER_CHARACTER_WIDTH * em_width), // Apply minimum width of 20 characters
(16. * line_height) // Default size
.min(bounds.size.height / 2.) // Shrink to half of the editor height
.max(MIN_POPOVER_LINE_HEIGHT * line_height), // Apply minimum height of 4 lines
);
let mut hover = editor.hover_state.render(
&snapshot,
&style,
visible_rows,
max_size,
editor.workspace.as_ref().map(|(w, _)| w.clone()),
cx,
);
let mut fold_indicators = cx.with_element_id(Some("gutter_fold_indicators"), |cx| {
editor.render_fold_indicators(
@ -2013,27 +2033,6 @@ impl EditorElement {
)
});
// todo!("hover popovers")
// if let Some((_, hover_popovers)) = hover.as_mut() {
// for hover_popover in hover_popovers.iter_mut() {
// hover_popover.layout(
// SizeConstraint {
// min: gpui::Point::<Pixels>::zero(),
// max: point(
// (120. * em_width) // Default size
// .min(size.x / 2.) // Shrink to half of the editor width
// .max(MIN_POPOVER_CHARACTER_WIDTH * em_width), // Apply minimum width of 20 characters
// (16. * line_height) // Default size
// .min(size.y / 2.) // Shrink to half of the editor height
// .max(MIN_POPOVER_LINE_HEIGHT * line_height), // Apply minimum height of 4 lines
// ),
// },
// editor,
// cx,
// );
// }
// }
let invisible_symbol_font_size = font_size / 2.;
let tab_invisible = cx
.text_system()
@ -2102,7 +2101,7 @@ impl EditorElement {
fold_indicators,
tab_invisible,
space_invisible,
// hover_popovers: hover,
hover_popovers: hover,
}
})
}
@ -2294,10 +2293,15 @@ impl EditorElement {
cx: &mut WindowContext,
) {
let content_origin = text_bounds.origin + point(layout.gutter_margin, Pixels::ZERO);
let interactive_bounds = InteractiveBounds {
bounds: bounds.intersect(&cx.content_mask().bounds),
stacking_order: cx.stacking_order().clone(),
};
cx.on_mouse_event({
let position_map = layout.position_map.clone();
let editor = self.editor.clone();
let interactive_bounds = interactive_bounds.clone();
move |event: &ScrollWheelEvent, phase, cx| {
if phase != DispatchPhase::Bubble {
@ -2305,7 +2309,7 @@ impl EditorElement {
}
let should_cancel = editor.update(cx, |editor, cx| {
Self::scroll(editor, event, &position_map, bounds, cx)
Self::scroll(editor, event, &position_map, &interactive_bounds, cx)
});
if should_cancel {
cx.stop_propagation();
@ -2316,6 +2320,7 @@ impl EditorElement {
cx.on_mouse_event({
let position_map = layout.position_map.clone();
let editor = self.editor.clone();
let stacking_order = cx.stacking_order().clone();
move |event: &MouseDownEvent, phase, cx| {
if phase != DispatchPhase::Bubble {
@ -2323,7 +2328,15 @@ impl EditorElement {
}
let should_cancel = editor.update(cx, |editor, cx| {
Self::mouse_down(editor, event, &position_map, text_bounds, gutter_bounds, cx)
Self::mouse_down(
editor,
event,
&position_map,
text_bounds,
gutter_bounds,
&stacking_order,
cx,
)
});
if should_cancel {
@ -2335,9 +2348,18 @@ impl EditorElement {
cx.on_mouse_event({
let position_map = layout.position_map.clone();
let editor = self.editor.clone();
let stacking_order = cx.stacking_order().clone();
move |event: &MouseUpEvent, phase, cx| {
let should_cancel = editor.update(cx, |editor, cx| {
Self::mouse_up(editor, event, &position_map, text_bounds, cx)
Self::mouse_up(
editor,
event,
&position_map,
text_bounds,
&stacking_order,
cx,
)
});
if should_cancel {
@ -2363,13 +2385,23 @@ impl EditorElement {
cx.on_mouse_event({
let position_map = layout.position_map.clone();
let editor = self.editor.clone();
let stacking_order = cx.stacking_order().clone();
move |event: &MouseMoveEvent, phase, cx| {
if phase != DispatchPhase::Bubble {
return;
}
let stop_propogating = editor.update(cx, |editor, cx| {
Self::mouse_moved(editor, event, &position_map, text_bounds, gutter_bounds, cx)
Self::mouse_moved(
editor,
event,
&position_map,
text_bounds,
gutter_bounds,
&stacking_order,
cx,
)
});
if stop_propogating {
@ -2629,9 +2661,11 @@ impl Element for EditorElement {
// We call with_z_index to establish a new stacking context.
cx.with_z_index(0, |cx| {
cx.with_content_mask(Some(ContentMask { bounds }), |cx| {
// Paint mouse listeners first, so any elements we paint on top of the editor
// Paint mouse listeners at z-index 0 so any elements we paint on top of the editor
// take precedence.
self.paint_mouse_listeners(bounds, gutter_bounds, text_bounds, &layout, cx);
cx.with_z_index(0, |cx| {
self.paint_mouse_listeners(bounds, gutter_bounds, text_bounds, &layout, cx);
});
let input_handler = ElementInputHandler::new(bounds, self.editor.clone(), cx);
cx.handle_input(&focus_handle, input_handler);
@ -3287,7 +3321,7 @@ pub struct LayoutState {
max_row: u32,
context_menu: Option<(DisplayPoint, AnyElement)>,
code_actions_indicator: Option<CodeActionsIndicator>,
// hover_popovers: Option<(DisplayPoint, Vec<AnyElement>)>,
hover_popovers: Option<(DisplayPoint, Vec<AnyElement>)>,
fold_indicators: Vec<Option<IconButton>>,
tab_invisible: ShapedLine,
space_invisible: ShapedLine,
@ -4085,7 +4119,7 @@ fn scale_horizontal_mouse_autoscroll_delta(delta: Pixels) -> f32 {
// }
// }
fn register_action<T: Action>(
pub fn register_action<T: Action>(
view: &View<Editor>,
cx: &mut WindowContext,
listener: impl Fn(&mut Editor, &T, &mut ViewContext<Editor>) + 'static,

File diff suppressed because it is too large Load Diff

View File

@ -5,7 +5,7 @@ use crate::{
Anchor, DisplayPoint, Editor, EditorSnapshot, GoToDefinition, GoToTypeDefinition, InlayId,
SelectPhase,
};
use gpui::{Task, ViewContext};
use gpui::{px, Task, ViewContext};
use language::{Bias, ToOffset};
use lsp::LanguageServerId;
use project::{
@ -13,6 +13,7 @@ use project::{
ResolveState,
};
use std::ops::Range;
use theme::ActiveTheme as _;
use util::TryFutureExt;
#[derive(Debug, Default)]
@ -485,40 +486,45 @@ pub fn show_link_definition(
});
if any_definition_does_not_contain_current_location {
// todo!()
// // Highlight symbol using theme link definition highlight style
// let style = theme::current(cx).editor.link_definition;
// let highlight_range =
// symbol_range.unwrap_or_else(|| match &trigger_point {
// TriggerPoint::Text(trigger_anchor) => {
// let snapshot = &snapshot.buffer_snapshot;
// // If no symbol range returned from language server, use the surrounding word.
// let (offset_range, _) =
// snapshot.surrounding_word(*trigger_anchor);
// RangeInEditor::Text(
// snapshot.anchor_before(offset_range.start)
// ..snapshot.anchor_after(offset_range.end),
// )
// }
// TriggerPoint::InlayHint(highlight, _, _) => {
// RangeInEditor::Inlay(highlight.clone())
// }
// });
let style = gpui::HighlightStyle {
underline: Some(gpui::UnderlineStyle {
thickness: px(1.),
..Default::default()
}),
color: Some(gpui::red()),
..Default::default()
};
let highlight_range =
symbol_range.unwrap_or_else(|| match &trigger_point {
TriggerPoint::Text(trigger_anchor) => {
let snapshot = &snapshot.buffer_snapshot;
// If no symbol range returned from language server, use the surrounding word.
let (offset_range, _) =
snapshot.surrounding_word(*trigger_anchor);
RangeInEditor::Text(
snapshot.anchor_before(offset_range.start)
..snapshot.anchor_after(offset_range.end),
)
}
TriggerPoint::InlayHint(highlight, _, _) => {
RangeInEditor::Inlay(highlight.clone())
}
});
// match highlight_range {
// RangeInEditor::Text(text_range) => this
// .highlight_text::<LinkGoToDefinitionState>(
// vec![text_range],
// style,
// cx,
// ),
// RangeInEditor::Inlay(highlight) => this
// .highlight_inlays::<LinkGoToDefinitionState>(
// vec![highlight],
// style,
// cx,
// ),
// }
match highlight_range {
RangeInEditor::Text(text_range) => this
.highlight_text::<LinkGoToDefinitionState>(
vec![text_range],
style,
cx,
),
RangeInEditor::Inlay(highlight) => this
.highlight_inlays::<LinkGoToDefinitionState>(
vec![highlight],
style,
cx,
),
}
} else {
hide_link_definition(this, cx);
}

View File

@ -595,31 +595,32 @@ impl<'a> MutableSelectionsCollection<'a> {
self.select(selections)
}
pub fn select_anchor_ranges<I: IntoIterator<Item = Range<Anchor>>>(&mut self, ranges: I) {
todo!()
// let buffer = self.buffer.read(self.cx).snapshot(self.cx);
// let selections = ranges
// .into_iter()
// .map(|range| {
// let mut start = range.start;
// let mut end = range.end;
// let reversed = if start.cmp(&end, &buffer).is_gt() {
// mem::swap(&mut start, &mut end);
// true
// } else {
// false
// };
// Selection {
// id: post_inc(&mut self.collection.next_selection_id),
// start,
// end,
// reversed,
// goal: SelectionGoal::None,
// }
// })
// .collect::<Vec<_>>();
// self.select_anchors(selections)
pub fn select_anchor_ranges<I>(&mut self, ranges: I)
where
I: IntoIterator<Item = Range<Anchor>>,
{
let buffer = self.buffer.read(self.cx).snapshot(self.cx);
let selections = ranges
.into_iter()
.map(|range| {
let mut start = range.start;
let mut end = range.end;
let reversed = if start.cmp(&end, &buffer).is_gt() {
mem::swap(&mut start, &mut end);
true
} else {
false
};
Selection {
id: post_inc(&mut self.collection.next_selection_id),
start,
end,
reversed,
goal: SelectionGoal::None,
}
})
.collect::<Vec<_>>();
self.select_anchors(selections)
}
pub fn new_selection_id(&mut self) -> usize {

View File

@ -27,7 +27,7 @@ pub fn marked_display_snapshot(
let (unmarked_text, markers) = marked_text_offsets(text);
let font = cx.text_style().font();
let font_size: Pixels = 14.into();
let font_size: Pixels = 14usize.into();
let buffer = MultiBuffer::build_simple(&unmarked_text, cx);
let display_map = cx.build_model(|cx| DisplayMap::new(buffer, font, font_size, None, 1, 1, cx));

View File

@ -30,11 +30,11 @@ pub trait FeatureFlagViewExt<V: 'static> {
impl<V> FeatureFlagViewExt<V> for ViewContext<'_, V>
where
V: 'static + Send + Sync,
V: 'static,
{
fn observe_flag<T: FeatureFlag, F>(&mut self, callback: F) -> Subscription
where
F: Fn(bool, &mut V, &mut ViewContext<V>) + Send + Sync + 'static,
F: Fn(bool, &mut V, &mut ViewContext<V>) + 'static,
{
self.observe_global::<FeatureFlags>(move |v, cx| {
let feature_flags = cx.global::<FeatureFlags>();

View File

@ -518,6 +518,7 @@ impl PickerDelegate for FileFinderDelegate {
}
fn update_matches(&mut self, raw_query: String, cx: &mut ViewContext<FileFinder>) -> Task<()> {
let raw_query = raw_query.trim();
if raw_query.is_empty() {
let project = self.project.read(cx);
self.latest_search_id = post_inc(&mut self.search_count);
@ -539,7 +540,6 @@ impl PickerDelegate for FileFinderDelegate {
cx.notify();
Task::ready(())
} else {
let raw_query = &raw_query;
let query = PathLikeWithPosition::parse_str(raw_query, |path_like_str| {
Ok::<_, std::convert::Infallible>(FileSearchQuery {
raw_query: raw_query.to_owned(),
@ -735,6 +735,7 @@ mod tests {
cx.dispatch_action(window.into(), Toggle);
let finder = cx.read(|cx| workspace.read(cx).modal::<FileFinder>().unwrap());
finder
.update(cx, |finder, cx| {
finder.delegate_mut().update_matches("bna".to_string(), cx)
@ -743,7 +744,6 @@ mod tests {
finder.read_with(cx, |finder, _| {
assert_eq!(finder.delegate().matches.len(), 2);
});
let active_pane = cx.read(|cx| workspace.read(cx).active_pane().clone());
cx.dispatch_action(window.into(), SelectNext);
cx.dispatch_action(window.into(), Confirm);
@ -762,6 +762,49 @@ mod tests {
"bandana"
);
});
for bandana_query in [
"bandana",
" bandana",
"bandana ",
" bandana ",
" ndan ",
" band ",
] {
finder
.update(cx, |finder, cx| {
finder
.delegate_mut()
.update_matches(bandana_query.to_string(), cx)
})
.await;
finder.read_with(cx, |finder, _| {
assert_eq!(
finder.delegate().matches.len(),
1,
"Wrong number of matches for bandana query '{bandana_query}'"
);
});
let active_pane = cx.read(|cx| workspace.read(cx).active_pane().clone());
cx.dispatch_action(window.into(), SelectNext);
cx.dispatch_action(window.into(), Confirm);
active_pane
.condition(cx, |pane, _| pane.active_item().is_some())
.await;
cx.read(|cx| {
let active_item = active_pane.read(cx).active_item().unwrap();
assert_eq!(
active_item
.as_any()
.downcast_ref::<Editor>()
.unwrap()
.read(cx)
.title(cx),
"bandana",
"Wrong match for bandana query '{bandana_query}'"
);
});
}
}
#[gpui::test]

View File

@ -2,9 +2,8 @@ use collections::HashMap;
use editor::{scroll::autoscroll::Autoscroll, Bias, Editor};
use fuzzy::{CharBag, PathMatch, PathMatchCandidate};
use gpui::{
actions, div, AppContext, Div, EventEmitter, FocusHandle, FocusableView, InteractiveElement,
IntoElement, Manager, Model, ParentElement, Render, Styled, Task, View, ViewContext,
VisualContext, WeakView,
actions, AppContext, DismissEvent, Div, EventEmitter, FocusHandle, FocusableView, Model,
ParentElement, Render, Styled, Task, View, ViewContext, VisualContext, WeakView,
};
use picker::{Picker, PickerDelegate};
use project::{PathMatchCandidateSet, Project, ProjectPath, WorktreeId};
@ -16,8 +15,7 @@ use std::{
},
};
use text::Point;
use theme::ActiveTheme;
use ui::{v_stack, HighlightedLabel, StyledExt};
use ui::{v_stack, HighlightedLabel, ListItem};
use util::{paths::PathLikeWithPosition, post_inc, ResultExt};
use workspace::Workspace;
@ -111,7 +109,7 @@ impl FileFinder {
}
}
impl EventEmitter<Manager> for FileFinder {}
impl EventEmitter<DismissEvent> for FileFinder {}
impl FocusableView for FileFinder {
fn focus_handle(&self, cx: &AppContext) -> FocusHandle {
self.picker.focus_handle(cx)
@ -530,7 +528,7 @@ impl FileFinderDelegate {
}
impl PickerDelegate for FileFinderDelegate {
type ListItem = Div;
type ListItem = ListItem;
fn placeholder_text(&self) -> Arc<str> {
"Search project files...".into()
@ -554,6 +552,7 @@ impl PickerDelegate for FileFinderDelegate {
raw_query: String,
cx: &mut ViewContext<Picker<Self>>,
) -> Task<()> {
let raw_query = raw_query.trim();
if raw_query.is_empty() {
let project = self.project.read(cx);
self.latest_search_id = post_inc(&mut self.search_count);
@ -575,7 +574,6 @@ impl PickerDelegate for FileFinderDelegate {
cx.notify();
Task::ready(())
} else {
let raw_query = &raw_query;
let query = PathLikeWithPosition::parse_str(raw_query, |path_like_str| {
Ok::<_, std::convert::Infallible>(FileSearchQuery {
raw_query: raw_query.to_owned(),
@ -689,9 +687,7 @@ impl PickerDelegate for FileFinderDelegate {
.log_err();
}
}
finder
.update(&mut cx, |_, cx| cx.emit(Manager::Dismiss))
.ok()?;
finder.update(&mut cx, |_, cx| cx.emit(DismissEvent)).ok()?;
Some(())
})
@ -702,7 +698,7 @@ impl PickerDelegate for FileFinderDelegate {
fn dismissed(&mut self, cx: &mut ViewContext<Picker<FileFinderDelegate>>) {
self.file_finder
.update(cx, |_, cx| cx.emit(Manager::Dismiss))
.update(cx, |_, cx| cx.emit(DismissEvent))
.log_err();
}
@ -711,30 +707,22 @@ impl PickerDelegate for FileFinderDelegate {
ix: usize,
selected: bool,
cx: &mut ViewContext<Picker<Self>>,
) -> Self::ListItem {
) -> Option<Self::ListItem> {
let path_match = self
.matches
.get(ix)
.expect("Invalid matches state: no element for index {ix}");
let theme = cx.theme();
let colors = theme.colors();
let (file_name, file_name_positions, full_path, full_path_positions) =
self.labels_for_match(path_match, cx, ix);
div()
.px_1()
.text_color(colors.text)
.text_ui()
.bg(colors.ghost_element_background)
.rounded_md()
.when(selected, |this| this.bg(colors.ghost_element_selected))
.hover(|this| this.bg(colors.ghost_element_hover))
.child(
Some(
ListItem::new(ix).inset(true).selected(selected).child(
v_stack()
.child(HighlightedLabel::new(file_name, file_name_positions))
.child(HighlightedLabel::new(full_path, full_path_positions)),
)
),
)
}
}
@ -778,18 +766,49 @@ mod tests {
let (picker, workspace, cx) = build_find_picker(project, cx);
cx.simulate_input("bna");
picker.update(cx, |picker, _| {
assert_eq!(picker.delegate.matches.len(), 2);
});
cx.dispatch_action(SelectNext);
cx.dispatch_action(Confirm);
cx.read(|cx| {
let active_editor = workspace.read(cx).active_item_as::<Editor>(cx).unwrap();
assert_eq!(active_editor.read(cx).title(cx), "bandana");
});
for bandana_query in [
"bandana",
" bandana",
"bandana ",
" bandana ",
" ndan ",
" band ",
] {
picker
.update(cx, |picker, cx| {
picker
.delegate
.update_matches(bandana_query.to_string(), cx)
})
.await;
picker.update(cx, |picker, _| {
assert_eq!(
picker.delegate.matches.len(),
1,
"Wrong number of matches for bandana query '{bandana_query}'"
);
});
cx.dispatch_action(SelectNext);
cx.dispatch_action(Confirm);
cx.read(|cx| {
let active_editor = workspace.read(cx).active_item_as::<Editor>(cx).unwrap();
assert_eq!(
active_editor.read(cx).title(cx),
"bandana",
"Wrong match for bandana query '{bandana_query}'"
);
});
}
}
#[gpui::test]

View File

@ -1,13 +1,13 @@
use editor::{display_map::ToDisplayPoint, scroll::autoscroll::Autoscroll, Editor};
use gpui::{
actions, div, prelude::*, AppContext, Div, EventEmitter, FocusHandle, FocusableView, Manager,
Render, SharedString, Styled, Subscription, View, ViewContext, VisualContext, WindowContext,
actions, div, prelude::*, AppContext, DismissEvent, Div, EventEmitter, FocusHandle,
FocusableView, Render, SharedString, Styled, Subscription, View, ViewContext, VisualContext,
WindowContext,
};
use text::{Bias, Point};
use theme::ActiveTheme;
use ui::{h_stack, v_stack, Color, Label, StyledExt};
use util::paths::FILE_ROW_COLUMN_DELIMITER;
use workspace::Workspace;
actions!(Toggle);
@ -25,22 +25,24 @@ pub struct GoToLine {
impl FocusableView for GoToLine {
fn focus_handle(&self, cx: &AppContext) -> FocusHandle {
self.active_editor.focus_handle(cx)
self.line_editor.focus_handle(cx)
}
}
impl EventEmitter<Manager> for GoToLine {}
impl EventEmitter<DismissEvent> for GoToLine {}
impl GoToLine {
fn register(workspace: &mut Workspace, _: &mut ViewContext<Workspace>) {
workspace.register_action(|workspace, _: &Toggle, cx| {
let Some(editor) = workspace
.active_item(cx)
.and_then(|active_item| active_item.downcast::<Editor>())
else {
fn register(editor: &mut Editor, cx: &mut ViewContext<Editor>) {
let handle = cx.view().downgrade();
editor.register_action(move |_: &Toggle, cx| {
let Some(editor) = handle.upgrade() else {
return;
};
workspace.toggle_modal(cx, move |cx| GoToLine::new(editor, cx));
let Some(workspace) = editor.read(cx).workspace() else {
return;
};
workspace.update(cx, |workspace, cx| {
workspace.toggle_modal(cx, move |cx| GoToLine::new(editor, cx));
})
});
}
@ -88,7 +90,7 @@ impl GoToLine {
) {
match event {
// todo!() this isn't working...
editor::EditorEvent::Blurred => cx.emit(Manager::Dismiss),
editor::EditorEvent::Blurred => cx.emit(DismissEvent),
editor::EditorEvent::BufferEdited { .. } => self.highlight_current_line(cx),
_ => {}
}
@ -123,7 +125,7 @@ impl GoToLine {
}
fn cancel(&mut self, _: &menu::Cancel, cx: &mut ViewContext<Self>) {
cx.emit(Manager::Dismiss);
cx.emit(DismissEvent);
}
fn confirm(&mut self, _: &menu::Confirm, cx: &mut ViewContext<Self>) {
@ -140,7 +142,7 @@ impl GoToLine {
self.prev_scroll_position.take();
}
cx.emit(Manager::Dismiss);
cx.emit(DismissEvent);
}
}

View File

@ -65,6 +65,8 @@ fn generate_shader_bindings() -> PathBuf {
"MonochromeSprite".into(),
"PolychromeSprite".into(),
"PathSprite".into(),
"SurfaceInputIndex".into(),
"SurfaceBounds".into(),
]);
config.no_includes = true;
config.enumeration.prefix_with_name = true;

View File

@ -162,6 +162,7 @@ macro_rules! actions {
( $name:ident ) => {
#[derive(::std::cmp::PartialEq, ::std::clone::Clone, ::std::default::Default, gpui::serde_derive::Deserialize, gpui::Action)]
#[serde(crate = "gpui::serde")]
pub struct $name;
};

View File

@ -520,6 +520,10 @@ impl AppContext {
self.platform.should_auto_hide_scrollbars()
}
pub fn restart(&self) {
self.platform.restart()
}
pub(crate) fn push_effect(&mut self, effect: Effect) {
match &effect {
Effect::Notify { emitter } => {
@ -580,7 +584,7 @@ impl AppContext {
.windows
.iter()
.filter_map(|(_, window)| {
let window = window.as_ref().unwrap();
let window = window.as_ref()?;
if window.dirty {
Some(window.handle.clone())
} else {
@ -1049,7 +1053,9 @@ impl Context for AppContext {
let root_view = window.root_view.clone().unwrap();
let result = update(root_view, &mut WindowContext::new(cx, &mut window));
if !window.removed {
if window.removed {
cx.windows.remove(handle.id);
} else {
cx.windows
.get_mut(handle.id)
.ok_or_else(|| anyhow!("window not found"))?

View File

@ -1,7 +1,7 @@
use crate::{
AnyView, AnyWindowHandle, AppCell, AppContext, BackgroundExecutor, Context, FocusableView,
ForegroundExecutor, Manager, Model, ModelContext, Render, Result, Task, View, ViewContext,
VisualContext, WindowContext, WindowHandle,
AnyView, AnyWindowHandle, AppCell, AppContext, BackgroundExecutor, Context, DismissEvent,
FocusableView, ForegroundExecutor, Model, ModelContext, Render, Result, Task, View,
ViewContext, VisualContext, WindowContext, WindowHandle,
};
use anyhow::{anyhow, Context as _};
use derive_more::{Deref, DerefMut};
@ -325,8 +325,7 @@ impl VisualContext for AsyncWindowContext {
where
V: crate::ManagedView,
{
self.window.update(self, |_, cx| {
view.update(cx, |_, cx| cx.emit(Manager::Dismiss))
})
self.window
.update(self, |_, cx| view.update(cx, |_, cx| cx.emit(DismissEvent)))
}
}

View File

@ -611,7 +611,7 @@ impl<'a> VisualContext for VisualTestContext<'a> {
{
self.window
.update(self.cx, |_, cx| {
view.update(cx, |_, cx| cx.emit(crate::Manager::Dismiss))
view.update(cx, |_, cx| cx.emit(crate::DismissEvent))
})
.unwrap()
}

View File

@ -111,7 +111,7 @@ pub struct Component<C> {
pub struct CompositeElementState<C: RenderOnce> {
rendered_element: Option<<C::Rendered as IntoElement>::Element>,
rendered_element_state: <<C::Rendered as IntoElement>::Element as Element>::State,
rendered_element_state: Option<<<C::Rendered as IntoElement>::Element as Element>::State>,
}
impl<C> Component<C> {
@ -131,20 +131,40 @@ impl<C: RenderOnce> Element for Component<C> {
cx: &mut WindowContext,
) -> (LayoutId, Self::State) {
let mut element = self.component.take().unwrap().render(cx).into_element();
let (layout_id, state) = element.layout(state.map(|s| s.rendered_element_state), cx);
let state = CompositeElementState {
rendered_element: Some(element),
rendered_element_state: state,
};
(layout_id, state)
if let Some(element_id) = element.element_id() {
let layout_id =
cx.with_element_state(element_id, |state, cx| element.layout(state, cx));
let state = CompositeElementState {
rendered_element: Some(element),
rendered_element_state: None,
};
(layout_id, state)
} else {
let (layout_id, state) =
element.layout(state.and_then(|s| s.rendered_element_state), cx);
let state = CompositeElementState {
rendered_element: Some(element),
rendered_element_state: Some(state),
};
(layout_id, state)
}
}
fn paint(self, bounds: Bounds<Pixels>, state: &mut Self::State, cx: &mut WindowContext) {
state
.rendered_element
.take()
.unwrap()
.paint(bounds, &mut state.rendered_element_state, cx);
let element = state.rendered_element.take().unwrap();
if let Some(element_id) = element.element_id() {
cx.with_element_state(element_id, |element_state, cx| {
let mut element_state = element_state.unwrap();
element.paint(bounds, &mut element_state, cx);
((), element_state)
});
} else {
element.paint(
bounds,
&mut state.rendered_element_state.as_mut().unwrap(),
cx,
);
}
}
}

View File

@ -3,7 +3,8 @@ use crate::{
BorrowWindow, Bounds, ClickEvent, DispatchPhase, Element, ElementId, FocusEvent, FocusHandle,
IntoElement, KeyContext, KeyDownEvent, KeyUpEvent, LayoutId, MouseButton, MouseDownEvent,
MouseMoveEvent, MouseUpEvent, ParentElement, Pixels, Point, Render, ScrollWheelEvent,
SharedString, Size, Style, StyleRefinement, Styled, Task, View, Visibility, WindowContext,
SharedString, Size, StackingOrder, Style, StyleRefinement, Styled, Task, View, Visibility,
WindowContext,
};
use collections::HashMap;
use refineable::Refineable;
@ -11,6 +12,7 @@ use smallvec::SmallVec;
use std::{
any::{Any, TypeId},
cell::RefCell,
cmp::Ordering,
fmt::Debug,
mem,
rc::Rc,
@ -84,7 +86,7 @@ pub trait InteractiveElement: Sized + Element {
move |event, bounds, phase, cx| {
if phase == DispatchPhase::Bubble
&& event.button == button
&& bounds.contains_point(&event.position)
&& bounds.visibly_contains(&event.position, cx)
{
(listener)(event, cx)
}
@ -99,7 +101,7 @@ pub trait InteractiveElement: Sized + Element {
) -> Self {
self.interactivity().mouse_down_listeners.push(Box::new(
move |event, bounds, phase, cx| {
if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
if phase == DispatchPhase::Bubble && bounds.visibly_contains(&event.position, cx) {
(listener)(event, cx)
}
},
@ -117,7 +119,7 @@ pub trait InteractiveElement: Sized + Element {
.push(Box::new(move |event, bounds, phase, cx| {
if phase == DispatchPhase::Bubble
&& event.button == button
&& bounds.contains_point(&event.position)
&& bounds.visibly_contains(&event.position, cx)
{
(listener)(event, cx)
}
@ -132,7 +134,7 @@ pub trait InteractiveElement: Sized + Element {
self.interactivity()
.mouse_up_listeners
.push(Box::new(move |event, bounds, phase, cx| {
if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
if phase == DispatchPhase::Bubble && bounds.visibly_contains(&event.position, cx) {
(listener)(event, cx)
}
}));
@ -145,7 +147,8 @@ pub trait InteractiveElement: Sized + Element {
) -> Self {
self.interactivity().mouse_down_listeners.push(Box::new(
move |event, bounds, phase, cx| {
if phase == DispatchPhase::Capture && !bounds.contains_point(&event.position) {
if phase == DispatchPhase::Capture && !bounds.visibly_contains(&event.position, cx)
{
(listener)(event, cx)
}
},
@ -163,7 +166,7 @@ pub trait InteractiveElement: Sized + Element {
.push(Box::new(move |event, bounds, phase, cx| {
if phase == DispatchPhase::Capture
&& event.button == button
&& !bounds.contains_point(&event.position)
&& !bounds.visibly_contains(&event.position, cx)
{
(listener)(event, cx);
}
@ -177,7 +180,7 @@ pub trait InteractiveElement: Sized + Element {
) -> Self {
self.interactivity().mouse_move_listeners.push(Box::new(
move |event, bounds, phase, cx| {
if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
if phase == DispatchPhase::Bubble && bounds.visibly_contains(&event.position, cx) {
(listener)(event, cx);
}
},
@ -191,7 +194,7 @@ pub trait InteractiveElement: Sized + Element {
) -> Self {
self.interactivity().scroll_wheel_listeners.push(Box::new(
move |event, bounds, phase, cx| {
if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
if phase == DispatchPhase::Bubble && bounds.visibly_contains(&event.position, cx) {
(listener)(event, cx);
}
},
@ -355,6 +358,11 @@ pub trait StatefulInteractiveElement: InteractiveElement {
self
}
fn track_scroll(mut self, scroll_handle: &ScrollHandle) -> Self {
self.interactivity().scroll_handle = Some(scroll_handle.clone());
self
}
fn active(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self
where
Self: Sized,
@ -526,15 +534,15 @@ pub type FocusListeners = SmallVec<[FocusListener; 2]>;
pub type FocusListener = Box<dyn Fn(&FocusHandle, &FocusEvent, &mut WindowContext) + 'static>;
pub type MouseDownListener =
Box<dyn Fn(&MouseDownEvent, &Bounds<Pixels>, DispatchPhase, &mut WindowContext) + 'static>;
Box<dyn Fn(&MouseDownEvent, &InteractiveBounds, DispatchPhase, &mut WindowContext) + 'static>;
pub type MouseUpListener =
Box<dyn Fn(&MouseUpEvent, &Bounds<Pixels>, DispatchPhase, &mut WindowContext) + 'static>;
Box<dyn Fn(&MouseUpEvent, &InteractiveBounds, DispatchPhase, &mut WindowContext) + 'static>;
pub type MouseMoveListener =
Box<dyn Fn(&MouseMoveEvent, &Bounds<Pixels>, DispatchPhase, &mut WindowContext) + 'static>;
Box<dyn Fn(&MouseMoveEvent, &InteractiveBounds, DispatchPhase, &mut WindowContext) + 'static>;
pub type ScrollWheelListener =
Box<dyn Fn(&ScrollWheelEvent, &Bounds<Pixels>, DispatchPhase, &mut WindowContext) + 'static>;
Box<dyn Fn(&ScrollWheelEvent, &InteractiveBounds, DispatchPhase, &mut WindowContext) + 'static>;
pub type ClickListener = Box<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>;
@ -624,6 +632,26 @@ impl Element for Div {
let mut child_max = Point::default();
let content_size = if element_state.child_layout_ids.is_empty() {
bounds.size
} else if let Some(scroll_handle) = self.interactivity.scroll_handle.as_ref() {
let mut state = scroll_handle.0.borrow_mut();
state.child_bounds = Vec::with_capacity(element_state.child_layout_ids.len());
state.bounds = bounds;
let requested = state.requested_scroll_top.take();
for (ix, child_layout_id) in element_state.child_layout_ids.iter().enumerate() {
let child_bounds = cx.layout_bounds(*child_layout_id);
child_min = child_min.min(&child_bounds.origin);
child_max = child_max.max(&child_bounds.lower_right());
state.child_bounds.push(child_bounds);
if let Some(requested) = requested.as_ref() {
if requested.0 == ix {
*state.offset.borrow_mut() =
bounds.origin - (child_bounds.origin - point(px(0.), requested.1));
}
}
}
(child_max - child_min).into()
} else {
for child_layout_id in &element_state.child_layout_ids {
let child_bounds = cx.layout_bounds(*child_layout_id);
@ -694,6 +722,7 @@ pub struct Interactivity {
pub key_context: KeyContext,
pub focusable: bool,
pub tracked_focus_handle: Option<FocusHandle>,
pub scroll_handle: Option<ScrollHandle>,
pub focus_listeners: FocusListeners,
pub group: Option<SharedString>,
pub base_style: StyleRefinement,
@ -719,6 +748,18 @@ pub struct Interactivity {
pub tooltip_builder: Option<TooltipBuilder>,
}
#[derive(Clone)]
pub struct InteractiveBounds {
pub bounds: Bounds<Pixels>,
pub stacking_order: StackingOrder,
}
impl InteractiveBounds {
pub fn visibly_contains(&self, point: &Point<Pixels>, cx: &WindowContext) -> bool {
self.bounds.contains_point(point) && cx.was_top_layer(&point, &self.stacking_order)
}
}
impl Interactivity {
pub fn layout(
&mut self,
@ -740,6 +781,10 @@ impl Interactivity {
});
}
if let Some(scroll_handle) = self.scroll_handle.as_ref() {
element_state.scroll_offset = Some(scroll_handle.0.borrow().offset.clone());
}
let style = self.compute_style(None, &mut element_state, cx);
let layout_id = f(style, cx);
(layout_id, element_state)
@ -755,34 +800,52 @@ impl Interactivity {
) {
let style = self.compute_style(Some(bounds), element_state, cx);
if style
.background
.as_ref()
.is_some_and(|fill| fill.color().is_some_and(|color| !color.is_transparent()))
{
cx.with_z_index(style.z_index.unwrap_or(0), |cx| cx.add_opaque_layer(bounds))
}
let interactive_bounds = Rc::new(InteractiveBounds {
bounds: bounds.intersect(&cx.content_mask().bounds),
stacking_order: cx.stacking_order().clone(),
});
if let Some(mouse_cursor) = style.mouse_cursor {
let hovered = bounds.contains_point(&cx.mouse_position());
let mouse_position = &cx.mouse_position();
let hovered = interactive_bounds.visibly_contains(mouse_position, cx);
if hovered {
cx.set_cursor_style(mouse_cursor);
}
}
for listener in self.mouse_down_listeners.drain(..) {
let interactive_bounds = interactive_bounds.clone();
cx.on_mouse_event(move |event: &MouseDownEvent, phase, cx| {
listener(event, &bounds, phase, cx);
listener(event, &*interactive_bounds, phase, cx);
})
}
for listener in self.mouse_up_listeners.drain(..) {
let interactive_bounds = interactive_bounds.clone();
cx.on_mouse_event(move |event: &MouseUpEvent, phase, cx| {
listener(event, &bounds, phase, cx);
listener(event, &*interactive_bounds, phase, cx);
})
}
for listener in self.mouse_move_listeners.drain(..) {
let interactive_bounds = interactive_bounds.clone();
cx.on_mouse_event(move |event: &MouseMoveEvent, phase, cx| {
listener(event, &bounds, phase, cx);
listener(event, &*interactive_bounds, phase, cx);
})
}
for listener in self.scroll_wheel_listeners.drain(..) {
let interactive_bounds = interactive_bounds.clone();
cx.on_mouse_event(move |event: &ScrollWheelEvent, phase, cx| {
listener(event, &bounds, phase, cx);
listener(event, &*interactive_bounds, phase, cx);
})
}
@ -803,8 +866,9 @@ impl Interactivity {
}
if self.hover_style.is_some()
|| (cx.active_drag.is_some() && !self.drag_over_styles.is_empty())
|| cx.active_drag.is_some() && !self.drag_over_styles.is_empty()
{
let bounds = bounds.intersect(&cx.content_mask().bounds);
let hovered = bounds.contains_point(&cx.mouse_position());
cx.on_mouse_event(move |event: &MouseMoveEvent, phase, cx| {
if phase == DispatchPhase::Capture {
@ -817,8 +881,11 @@ impl Interactivity {
if cx.active_drag.is_some() {
let drop_listeners = mem::take(&mut self.drop_listeners);
let interactive_bounds = interactive_bounds.clone();
cx.on_mouse_event(move |event: &MouseUpEvent, phase, cx| {
if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
if phase == DispatchPhase::Bubble
&& interactive_bounds.visibly_contains(&event.position, &cx)
{
if let Some(drag_state_type) =
cx.active_drag.as_ref().map(|drag| drag.view.entity_type())
{
@ -847,6 +914,7 @@ impl Interactivity {
if let Some(mouse_down) = mouse_down {
if let Some(drag_listener) = drag_listener {
let active_state = element_state.clicked_state.clone();
let interactive_bounds = interactive_bounds.clone();
cx.on_mouse_event(move |event: &MouseMoveEvent, phase, cx| {
if cx.active_drag.is_some() {
@ -854,7 +922,7 @@ impl Interactivity {
cx.notify();
}
} else if phase == DispatchPhase::Bubble
&& bounds.contains_point(&event.position)
&& interactive_bounds.visibly_contains(&event.position, cx)
&& (event.position - mouse_down.position).magnitude() > DRAG_THRESHOLD
{
*active_state.borrow_mut() = ElementClickedState::default();
@ -867,8 +935,11 @@ impl Interactivity {
});
}
let interactive_bounds = interactive_bounds.clone();
cx.on_mouse_event(move |event: &MouseUpEvent, phase, cx| {
if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
if phase == DispatchPhase::Bubble
&& interactive_bounds.visibly_contains(&event.position, cx)
{
let mouse_click = ClickEvent {
down: mouse_down.clone(),
up: event.clone(),
@ -881,8 +952,11 @@ impl Interactivity {
cx.notify();
});
} else {
let interactive_bounds = interactive_bounds.clone();
cx.on_mouse_event(move |event: &MouseDownEvent, phase, cx| {
if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
if phase == DispatchPhase::Bubble
&& interactive_bounds.visibly_contains(&event.position, cx)
{
*pending_mouse_down.borrow_mut() = Some(event.clone());
cx.notify();
}
@ -893,13 +967,14 @@ impl Interactivity {
if let Some(hover_listener) = self.hover_listener.take() {
let was_hovered = element_state.hover_state.clone();
let has_mouse_down = element_state.pending_mouse_down.clone();
let interactive_bounds = interactive_bounds.clone();
cx.on_mouse_event(move |event: &MouseMoveEvent, phase, cx| {
if phase != DispatchPhase::Bubble {
return;
}
let is_hovered =
bounds.contains_point(&event.position) && has_mouse_down.borrow().is_none();
let is_hovered = interactive_bounds.visibly_contains(&event.position, cx)
&& has_mouse_down.borrow().is_none();
let mut was_hovered = was_hovered.borrow_mut();
if is_hovered != was_hovered.clone() {
@ -914,14 +989,15 @@ impl Interactivity {
if let Some(tooltip_builder) = self.tooltip_builder.take() {
let active_tooltip = element_state.active_tooltip.clone();
let pending_mouse_down = element_state.pending_mouse_down.clone();
let interactive_bounds = interactive_bounds.clone();
cx.on_mouse_event(move |event: &MouseMoveEvent, phase, cx| {
if phase != DispatchPhase::Bubble {
return;
}
let is_hovered =
bounds.contains_point(&event.position) && pending_mouse_down.borrow().is_none();
let is_hovered = interactive_bounds.visibly_contains(&event.position, cx)
&& pending_mouse_down.borrow().is_none();
if !is_hovered {
active_tooltip.borrow_mut().take();
return;
@ -979,11 +1055,12 @@ impl Interactivity {
.group_active_style
.as_ref()
.and_then(|group_active| GroupBounds::get(&group_active.group, cx));
let interactive_bounds = interactive_bounds.clone();
cx.on_mouse_event(move |down: &MouseDownEvent, phase, cx| {
if phase == DispatchPhase::Bubble {
let group = active_group_bounds
.map_or(false, |bounds| bounds.contains_point(&down.position));
let element = bounds.contains_point(&down.position);
let element = interactive_bounds.visibly_contains(&down.position, cx);
if group || element {
*active_state.borrow_mut() = ElementClickedState { group, element };
cx.notify();
@ -1000,9 +1077,12 @@ impl Interactivity {
.clone();
let line_height = cx.line_height();
let scroll_max = (content_size - bounds.size).max(&Size::default());
let interactive_bounds = interactive_bounds.clone();
cx.on_mouse_event(move |event: &ScrollWheelEvent, phase, cx| {
if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
if phase == DispatchPhase::Bubble
&& interactive_bounds.visibly_contains(&event.position, cx)
{
let mut scroll_offset = scroll_offset.borrow_mut();
let old_scroll_offset = *scroll_offset;
let delta = event.delta.pixel_delta(line_height);
@ -1093,19 +1173,22 @@ impl Interactivity {
let mouse_position = cx.mouse_position();
if let Some(group_hover) = self.group_hover_style.as_ref() {
if let Some(group_bounds) = GroupBounds::get(&group_hover.group, cx) {
if group_bounds.contains_point(&mouse_position) {
if group_bounds.contains_point(&mouse_position)
&& cx.was_top_layer(&mouse_position, cx.stacking_order())
{
style.refine(&group_hover.style);
}
}
}
// if self.hover_style.is_some() {
if bounds.contains_point(&mouse_position) {
// eprintln!("div hovered {bounds:?} {mouse_position:?}");
style.refine(&self.hover_style);
} else {
// eprintln!("div NOT hovered {bounds:?} {mouse_position:?}");
if self.hover_style.is_some() {
if bounds
.intersect(&cx.content_mask().bounds)
.contains_point(&mouse_position)
&& cx.was_top_layer(&mouse_position, cx.stacking_order())
{
style.refine(&self.hover_style);
}
}
// }
if let Some(drag) = cx.active_drag.take() {
for (state_type, group_drag_style) in &self.group_drag_over_styles {
@ -1120,7 +1203,9 @@ impl Interactivity {
for (state_type, drag_over_style) in &self.drag_over_styles {
if *state_type == drag.view.entity_type()
&& bounds.contains_point(&mouse_position)
&& bounds
.intersect(&cx.content_mask().bounds)
.contains_point(&mouse_position)
{
style.refine(drag_over_style);
}
@ -1152,6 +1237,7 @@ impl Default for Interactivity {
key_context: KeyContext::default(),
focusable: false,
tracked_focus_handle: None,
scroll_handle: None,
focus_listeners: SmallVec::default(),
// scroll_offset: Point::default(),
group: None,
@ -1375,3 +1461,83 @@ where
self.element.children_mut()
}
}
#[derive(Default)]
struct ScrollHandleState {
// not great to have the nested rc's...
offset: Rc<RefCell<Point<Pixels>>>,
bounds: Bounds<Pixels>,
child_bounds: Vec<Bounds<Pixels>>,
requested_scroll_top: Option<(usize, Pixels)>,
}
#[derive(Clone)]
pub struct ScrollHandle(Rc<RefCell<ScrollHandleState>>);
impl ScrollHandle {
pub fn new() -> Self {
Self(Rc::default())
}
pub fn offset(&self) -> Point<Pixels> {
self.0.borrow().offset.borrow().clone()
}
pub fn top_item(&self) -> usize {
let state = self.0.borrow();
let top = state.bounds.top() - state.offset.borrow().y;
match state.child_bounds.binary_search_by(|bounds| {
if top < bounds.top() {
Ordering::Greater
} else if top > bounds.bottom() {
Ordering::Less
} else {
Ordering::Equal
}
}) {
Ok(ix) => ix,
Err(ix) => ix.min(state.child_bounds.len().saturating_sub(1)),
}
}
pub fn bounds_for_item(&self, ix: usize) -> Option<Bounds<Pixels>> {
self.0.borrow().child_bounds.get(ix).cloned()
}
/// scroll_to_item scrolls the minimal amount to ensure that the item is
/// fully visible
pub fn scroll_to_item(&self, ix: usize) {
let state = self.0.borrow();
let Some(bounds) = state.child_bounds.get(ix) else {
return;
};
let scroll_offset = state.offset.borrow().y;
if bounds.top() + scroll_offset < state.bounds.top() {
state.offset.borrow_mut().y = state.bounds.top() - bounds.top();
} else if bounds.bottom() + scroll_offset > state.bounds.bottom() {
state.offset.borrow_mut().y = state.bounds.bottom() - bounds.bottom();
}
}
pub fn logical_scroll_top(&self) -> (usize, Pixels) {
let ix = self.top_item();
let state = self.0.borrow();
if let Some(child_bounds) = state.child_bounds.get(ix) {
(
ix,
child_bounds.top() + state.offset.borrow().y - state.bounds.top(),
)
} else {
(ix, px(0.))
}
}
pub fn set_logical_scroll_top(&self, ix: usize, px: Pixels) {
self.0.borrow_mut().requested_scroll_top = Some((ix, px));
}
}

View File

@ -1,30 +1,67 @@
use std::sync::Arc;
use crate::{
Bounds, Element, InteractiveElement, InteractiveElementState, Interactivity, IntoElement,
LayoutId, Pixels, SharedString, StyleRefinement, Styled, WindowContext,
point, size, Bounds, DevicePixels, Element, ImageData, InteractiveElement,
InteractiveElementState, Interactivity, IntoElement, LayoutId, Pixels, SharedString, Size,
StyleRefinement, Styled, WindowContext,
};
use futures::FutureExt;
use media::core_video::CVImageBuffer;
use util::ResultExt;
#[derive(Clone, Debug)]
pub enum ImageSource {
/// Image content will be loaded from provided URI at render time.
Uri(SharedString),
Data(Arc<ImageData>),
Surface(CVImageBuffer),
}
impl From<SharedString> for ImageSource {
fn from(value: SharedString) -> Self {
Self::Uri(value)
}
}
impl From<&'static str> for ImageSource {
fn from(uri: &'static str) -> Self {
Self::Uri(uri.into())
}
}
impl From<String> for ImageSource {
fn from(uri: String) -> Self {
Self::Uri(uri.into())
}
}
impl From<Arc<ImageData>> for ImageSource {
fn from(value: Arc<ImageData>) -> Self {
Self::Data(value)
}
}
impl From<CVImageBuffer> for ImageSource {
fn from(value: CVImageBuffer) -> Self {
Self::Surface(value)
}
}
pub struct Img {
interactivity: Interactivity,
uri: Option<SharedString>,
source: ImageSource,
grayscale: bool,
}
pub fn img() -> Img {
pub fn img(source: impl Into<ImageSource>) -> Img {
Img {
interactivity: Interactivity::default(),
uri: None,
source: source.into(),
grayscale: false,
}
}
impl Img {
pub fn uri(mut self, uri: impl Into<SharedString>) -> Self {
self.uri = Some(uri.into());
self
}
pub fn grayscale(mut self, grayscale: bool) -> Self {
self.grayscale = grayscale;
self
@ -39,9 +76,8 @@ impl Element for Img {
element_state: Option<Self::State>,
cx: &mut WindowContext,
) -> (LayoutId, Self::State) {
self.interactivity.layout(element_state, cx, |style, cx| {
cx.request_layout(&style, None)
})
self.interactivity
.layout(element_state, cx, |style, cx| cx.request_layout(&style, []))
}
fn paint(
@ -56,31 +92,43 @@ impl Element for Img {
element_state,
cx,
|style, _scroll_offset, cx| {
let corner_radii = style.corner_radii;
if let Some(uri) = self.uri.clone() {
// eprintln!(">>> image_cache.get({uri}");
let image_future = cx.image_cache.get(uri.clone());
// eprintln!("<<< image_cache.get({uri}");
if let Some(data) = image_future
.clone()
.now_or_never()
.and_then(|result| result.ok())
{
let corner_radii = corner_radii.to_pixels(bounds.size, cx.rem_size());
cx.with_z_index(1, |cx| {
cx.paint_image(bounds, corner_radii, data, self.grayscale)
.log_err()
});
} else {
cx.spawn(|mut cx| async move {
if image_future.await.ok().is_some() {
cx.on_next_frame(|cx| cx.notify());
let corner_radii = style.corner_radii.to_pixels(bounds.size, cx.rem_size());
cx.with_z_index(1, |cx| {
match self.source {
ImageSource::Uri(uri) => {
let image_future = cx.image_cache.get(uri.clone());
if let Some(data) = image_future
.clone()
.now_or_never()
.and_then(|result| result.ok())
{
let new_bounds = preserve_aspect_ratio(bounds, data.size());
cx.paint_image(new_bounds, corner_radii, data, self.grayscale)
.log_err();
} else {
cx.spawn(|mut cx| async move {
if image_future.await.ok().is_some() {
cx.on_next_frame(|cx| cx.notify());
}
})
.detach();
}
})
.detach()
}
}
}
ImageSource::Data(data) => {
let new_bounds = preserve_aspect_ratio(bounds, data.size());
cx.paint_image(new_bounds, corner_radii, data, self.grayscale)
.log_err();
}
ImageSource::Surface(surface) => {
let size = size(surface.width().into(), surface.height().into());
let new_bounds = preserve_aspect_ratio(bounds, size);
// TODO: Add support for corner_radii and grayscale.
cx.paint_surface(new_bounds, surface);
}
};
});
},
)
}
@ -109,3 +157,29 @@ impl InteractiveElement for Img {
&mut self.interactivity
}
}
fn preserve_aspect_ratio(bounds: Bounds<Pixels>, image_size: Size<DevicePixels>) -> Bounds<Pixels> {
let image_size = image_size.map(|dimension| Pixels::from(u32::from(dimension)));
let image_ratio = image_size.width / image_size.height;
let bounds_ratio = bounds.size.width / bounds.size.height;
let new_size = if bounds_ratio > image_ratio {
size(
image_size.width * (bounds.size.height / image_size.height),
bounds.size.height,
)
} else {
size(
bounds.size.width,
image_size.height * (bounds.size.width / image_size.width),
)
};
Bounds {
origin: point(
bounds.origin.x + (bounds.size.width - new_size.width) / 2.0,
bounds.origin.y + (bounds.size.height - new_size.height) / 2.0,
),
size: new_size,
}
}

View File

@ -144,9 +144,11 @@ impl Element for Overlay {
}
cx.with_element_offset(desired.origin - bounds.origin, |cx| {
for child in self.children {
child.paint(cx);
}
cx.break_content_mask(|cx| {
for child in self.children {
child.paint(cx);
}
})
})
}
}

View File

@ -287,7 +287,9 @@ impl TextState {
pub struct InteractiveText {
element_id: ElementId,
text: StyledText,
click_listener: Option<Box<dyn Fn(InteractiveTextClickEvent, &mut WindowContext<'_>)>>,
click_listener:
Option<Box<dyn Fn(&[Range<usize>], InteractiveTextClickEvent, &mut WindowContext<'_>)>>,
clickable_ranges: Vec<Range<usize>>,
}
struct InteractiveTextClickEvent {
@ -306,6 +308,7 @@ impl InteractiveText {
element_id: id.into(),
text,
click_listener: None,
clickable_ranges: Vec::new(),
}
}
@ -314,7 +317,7 @@ impl InteractiveText {
ranges: Vec<Range<usize>>,
listener: impl Fn(usize, &mut WindowContext<'_>) + 'static,
) -> Self {
self.click_listener = Some(Box::new(move |event, cx| {
self.click_listener = Some(Box::new(move |ranges, event, cx| {
for (range_ix, range) in ranges.iter().enumerate() {
if range.contains(&event.mouse_down_index) && range.contains(&event.mouse_up_index)
{
@ -322,6 +325,7 @@ impl InteractiveText {
}
}
}));
self.clickable_ranges = ranges;
self
}
}
@ -356,6 +360,19 @@ impl Element for InteractiveText {
fn paint(self, bounds: Bounds<Pixels>, state: &mut Self::State, cx: &mut WindowContext) {
if let Some(click_listener) = self.click_listener {
if let Some(ix) = state
.text_state
.index_for_position(bounds, cx.mouse_position())
{
if self
.clickable_ranges
.iter()
.any(|range| range.contains(&ix))
{
cx.set_cursor_style(crate::CursorStyle::PointingHand)
}
}
let text_state = state.text_state.clone();
let mouse_down = state.mouse_down_index.clone();
if let Some(mouse_down_index) = mouse_down.get() {
@ -365,6 +382,7 @@ impl Element for InteractiveText {
text_state.index_for_position(bounds, event.position)
{
click_listener(
&self.clickable_ranges,
InteractiveTextClickEvent {
mouse_down_index,
mouse_up_index,

View File

@ -9,7 +9,7 @@ use taffy::style::Overflow;
/// uniform_list provides lazy rendering for a set of items that are of uniform height.
/// When rendered into a container with overflow-y: hidden and a fixed (or max) height,
/// uniform_list will only render the visibile subset of items.
/// uniform_list will only render the visible subset of items.
pub fn uniform_list<I, R, V>(
view: View<V>,
id: I,
@ -173,7 +173,7 @@ impl Element for UniformList {
let item_size = element_state.item_size;
let content_size = Size {
width: padded_bounds.size.width,
height: item_size.height * self.item_count,
height: item_size.height * self.item_count + padding.top + padding.bottom,
};
let shared_scroll_offset = element_state
@ -221,9 +221,7 @@ impl Element for UniformList {
let items = (self.render_items)(visible_range.clone(), cx);
cx.with_z_index(1, |cx| {
let content_mask = ContentMask {
bounds: padded_bounds,
};
let content_mask = ContentMask { bounds };
cx.with_content_mask(Some(content_mask), |cx| {
for (item, ix) in items.into_iter().zip(visible_range) {
let item_origin = padded_bounds.origin

View File

@ -740,7 +740,7 @@ impl<T> Copy for Corners<T> where T: Copy + Clone + Default + Debug {}
Deserialize,
)]
#[repr(transparent)]
pub struct Pixels(pub(crate) f32);
pub struct Pixels(pub f32);
impl std::ops::Div for Pixels {
type Output = f32;
@ -905,6 +905,12 @@ impl From<Pixels> for usize {
}
}
impl From<usize> for Pixels {
fn from(pixels: usize) -> Self {
Pixels(pixels as f32)
}
}
#[derive(
Add, AddAssign, Clone, Copy, Default, Div, Eq, Hash, Ord, PartialEq, PartialOrd, Sub, SubAssign,
)]
@ -959,6 +965,18 @@ impl From<u64> for DevicePixels {
}
}
impl From<DevicePixels> for usize {
fn from(device_pixels: DevicePixels) -> Self {
device_pixels.0 as usize
}
}
impl From<usize> for DevicePixels {
fn from(device_pixels: usize) -> Self {
DevicePixels(device_pixels as i32)
}
}
#[derive(Clone, Copy, Default, Add, AddAssign, Sub, SubAssign, Div, PartialEq, PartialOrd)]
#[repr(transparent)]
pub struct ScaledPixels(pub(crate) f32);
@ -1034,7 +1052,7 @@ impl sqlez::bindable::Bind for GlobalPixels {
}
#[derive(Clone, Copy, Default, Add, Sub, Mul, Div, Neg)]
pub struct Rems(f32);
pub struct Rems(pub f32);
impl Mul<Pixels> for Rems {
type Output = Pixels;

View File

@ -1,7 +1,7 @@
use crate::{
point, size, AtlasTextureId, AtlasTextureKind, AtlasTile, Bounds, ContentMask, DevicePixels,
Hsla, MetalAtlas, MonochromeSprite, Path, PathId, PathVertex, PolychromeSprite, PrimitiveBatch,
Quad, ScaledPixels, Scene, Shadow, Size, Underline,
Quad, ScaledPixels, Scene, Shadow, Size, Surface, Underline,
};
use cocoa::{
base::{NO, YES},
@ -9,6 +9,9 @@ use cocoa::{
quartzcore::AutoresizingMask,
};
use collections::HashMap;
use core_foundation::base::TCFType;
use foreign_types::ForeignType;
use media::core_video::CVMetalTextureCache;
use metal::{CommandQueue, MTLPixelFormat, MTLResourceOptions, NSRange};
use objc::{self, msg_send, sel, sel_impl};
use smallvec::SmallVec;
@ -27,9 +30,11 @@ pub(crate) struct MetalRenderer {
underlines_pipeline_state: metal::RenderPipelineState,
monochrome_sprites_pipeline_state: metal::RenderPipelineState,
polychrome_sprites_pipeline_state: metal::RenderPipelineState,
surfaces_pipeline_state: metal::RenderPipelineState,
unit_vertices: metal::Buffer,
instances: metal::Buffer,
sprite_atlas: Arc<MetalAtlas>,
core_video_texture_cache: CVMetalTextureCache,
}
impl MetalRenderer {
@ -143,6 +148,14 @@ impl MetalRenderer {
"polychrome_sprite_fragment",
MTLPixelFormat::BGRA8Unorm,
);
let surfaces_pipeline_state = build_pipeline_state(
&device,
&library,
"surfaces",
"surface_vertex",
"surface_fragment",
MTLPixelFormat::BGRA8Unorm,
);
let command_queue = device.new_command_queue();
let sprite_atlas = Arc::new(MetalAtlas::new(device.clone()));
@ -157,9 +170,11 @@ impl MetalRenderer {
underlines_pipeline_state,
monochrome_sprites_pipeline_state,
polychrome_sprites_pipeline_state,
surfaces_pipeline_state,
unit_vertices,
instances,
sprite_atlas,
core_video_texture_cache: CVMetalTextureCache::new(device.as_ptr()).unwrap(),
}
}
@ -268,6 +283,14 @@ impl MetalRenderer {
command_encoder,
);
}
PrimitiveBatch::Surfaces(surfaces) => {
self.draw_surfaces(
surfaces,
&mut instance_offset,
viewport_size,
command_encoder,
);
}
}
}
@ -793,6 +816,102 @@ impl MetalRenderer {
);
*offset = next_offset;
}
fn draw_surfaces(
&mut self,
surfaces: &[Surface],
offset: &mut usize,
viewport_size: Size<DevicePixels>,
command_encoder: &metal::RenderCommandEncoderRef,
) {
command_encoder.set_render_pipeline_state(&self.surfaces_pipeline_state);
command_encoder.set_vertex_buffer(
SurfaceInputIndex::Vertices as u64,
Some(&self.unit_vertices),
0,
);
command_encoder.set_vertex_bytes(
SurfaceInputIndex::ViewportSize as u64,
mem::size_of_val(&viewport_size) as u64,
&viewport_size as *const Size<DevicePixels> as *const _,
);
for surface in surfaces {
let texture_size = size(
DevicePixels::from(surface.image_buffer.width() as i32),
DevicePixels::from(surface.image_buffer.height() as i32),
);
assert_eq!(
surface.image_buffer.pixel_format_type(),
media::core_video::kCVPixelFormatType_420YpCbCr8BiPlanarFullRange
);
let y_texture = self
.core_video_texture_cache
.create_texture_from_image(
surface.image_buffer.as_concrete_TypeRef(),
ptr::null(),
MTLPixelFormat::R8Unorm,
surface.image_buffer.plane_width(0),
surface.image_buffer.plane_height(0),
0,
)
.unwrap();
let cb_cr_texture = self
.core_video_texture_cache
.create_texture_from_image(
surface.image_buffer.as_concrete_TypeRef(),
ptr::null(),
MTLPixelFormat::RG8Unorm,
surface.image_buffer.plane_width(1),
surface.image_buffer.plane_height(1),
1,
)
.unwrap();
align_offset(offset);
let next_offset = *offset + mem::size_of::<Surface>();
assert!(
next_offset <= INSTANCE_BUFFER_SIZE,
"instance buffer exhausted"
);
command_encoder.set_vertex_buffer(
SurfaceInputIndex::Surfaces as u64,
Some(&self.instances),
*offset as u64,
);
command_encoder.set_vertex_bytes(
SurfaceInputIndex::TextureSize as u64,
mem::size_of_val(&texture_size) as u64,
&texture_size as *const Size<DevicePixels> as *const _,
);
command_encoder.set_fragment_texture(
SurfaceInputIndex::YTexture as u64,
Some(y_texture.as_texture_ref()),
);
command_encoder.set_fragment_texture(
SurfaceInputIndex::CbCrTexture as u64,
Some(cb_cr_texture.as_texture_ref()),
);
unsafe {
let buffer_contents =
(self.instances.contents() as *mut u8).add(*offset) as *mut SurfaceBounds;
ptr::write(
buffer_contents,
SurfaceBounds {
bounds: surface.bounds,
content_mask: surface.content_mask.clone(),
},
);
}
command_encoder.draw_primitives(metal::MTLPrimitiveType::Triangle, 0, 6);
*offset = next_offset;
}
}
}
fn build_pipeline_state(
@ -898,6 +1017,16 @@ enum SpriteInputIndex {
AtlasTexture = 4,
}
#[repr(C)]
enum SurfaceInputIndex {
Vertices = 0,
Surfaces = 1,
ViewportSize = 2,
TextureSize = 3,
YTexture = 4,
CbCrTexture = 5,
}
#[repr(C)]
enum PathRasterizationInputIndex {
Vertices = 0,
@ -911,3 +1040,10 @@ pub struct PathSprite {
pub color: Hsla,
pub tile: AtlasTile,
}
#[derive(Clone, Debug, Eq, PartialEq)]
#[repr(C)]
pub struct SurfaceBounds {
pub bounds: Bounds<ScaledPixels>,
pub content_mask: ContentMask<ScaledPixels>,
}

View File

@ -469,6 +469,58 @@ fragment float4 path_sprite_fragment(
return color;
}
struct SurfaceVertexOutput {
float4 position [[position]];
float2 texture_position;
float clip_distance [[clip_distance]][4];
};
struct SurfaceFragmentInput {
float4 position [[position]];
float2 texture_position;
};
vertex SurfaceVertexOutput surface_vertex(
uint unit_vertex_id [[vertex_id]], uint surface_id [[instance_id]],
constant float2 *unit_vertices [[buffer(SurfaceInputIndex_Vertices)]],
constant SurfaceBounds *surfaces [[buffer(SurfaceInputIndex_Surfaces)]],
constant Size_DevicePixels *viewport_size
[[buffer(SurfaceInputIndex_ViewportSize)]],
constant Size_DevicePixels *texture_size
[[buffer(SurfaceInputIndex_TextureSize)]]) {
float2 unit_vertex = unit_vertices[unit_vertex_id];
SurfaceBounds surface = surfaces[surface_id];
float4 device_position =
to_device_position(unit_vertex, surface.bounds, viewport_size);
float4 clip_distance = distance_from_clip_rect(unit_vertex, surface.bounds,
surface.content_mask.bounds);
// We are going to copy the whole texture, so the texture position corresponds
// to the current vertex of the unit triangle.
float2 texture_position = unit_vertex;
return SurfaceVertexOutput{
device_position,
texture_position,
{clip_distance.x, clip_distance.y, clip_distance.z, clip_distance.w}};
}
fragment float4 surface_fragment(SurfaceFragmentInput input [[stage_in]],
texture2d<float> y_texture
[[texture(SurfaceInputIndex_YTexture)]],
texture2d<float> cb_cr_texture
[[texture(SurfaceInputIndex_CbCrTexture)]]) {
constexpr sampler texture_sampler(mag_filter::linear, min_filter::linear);
const float4x4 ycbcrToRGBTransform =
float4x4(float4(+1.0000f, +1.0000f, +1.0000f, +0.0000f),
float4(+0.0000f, -0.3441f, +1.7720f, +0.0000f),
float4(+1.4020f, -0.7141f, +0.0000f, +0.0000f),
float4(-0.7010f, +0.5291f, -0.8860f, +1.0000f));
float4 ycbcr = float4(
y_texture.sample(texture_sampler, input.texture_position).r,
cb_cr_texture.sample(texture_sampler, input.texture_position).rg, 1.0);
return ycbcrToRGBTransform * ycbcr;
}
float4 hsla_to_rgba(Hsla hsla) {
float h = hsla.h * 6.0; // Now, it's an angle but scaled in [0, 6) range
float s = hsla.s;

View File

@ -683,6 +683,9 @@ impl Drop for MacWindow {
this.executor
.spawn(async move {
unsafe {
// todo!() this panic()s when you click the red close button
// unless should_close returns false.
// (luckliy in zed it always returns false)
window.close();
}
})

View File

@ -25,6 +25,7 @@ pub(crate) struct SceneBuilder {
underlines: Vec<Underline>,
monochrome_sprites: Vec<MonochromeSprite>,
polychrome_sprites: Vec<PolychromeSprite>,
surfaces: Vec<Surface>,
}
impl Default for SceneBuilder {
@ -38,6 +39,7 @@ impl Default for SceneBuilder {
underlines: Vec::new(),
monochrome_sprites: Vec::new(),
polychrome_sprites: Vec::new(),
surfaces: Vec::new(),
}
}
}
@ -120,6 +122,7 @@ impl SceneBuilder {
(PrimitiveKind::PolychromeSprite, ix) => {
self.polychrome_sprites[ix].order = draw_order as DrawOrder
}
(PrimitiveKind::Surface, ix) => self.surfaces[ix].order = draw_order as DrawOrder,
}
}
@ -129,6 +132,7 @@ impl SceneBuilder {
self.underlines.sort_unstable();
self.monochrome_sprites.sort_unstable();
self.polychrome_sprites.sort_unstable();
self.surfaces.sort_unstable();
Scene {
shadows: mem::take(&mut self.shadows),
@ -137,6 +141,7 @@ impl SceneBuilder {
underlines: mem::take(&mut self.underlines),
monochrome_sprites: mem::take(&mut self.monochrome_sprites),
polychrome_sprites: mem::take(&mut self.polychrome_sprites),
surfaces: mem::take(&mut self.surfaces),
}
}
@ -185,6 +190,10 @@ impl SceneBuilder {
sprite.order = layer_id;
self.polychrome_sprites.push(sprite);
}
Primitive::Surface(mut surface) => {
surface.order = layer_id;
self.surfaces.push(surface);
}
}
}
}
@ -196,6 +205,7 @@ pub(crate) struct Scene {
pub underlines: Vec<Underline>,
pub monochrome_sprites: Vec<MonochromeSprite>,
pub polychrome_sprites: Vec<PolychromeSprite>,
pub surfaces: Vec<Surface>,
}
impl Scene {
@ -224,6 +234,9 @@ impl Scene {
polychrome_sprites: &self.polychrome_sprites,
polychrome_sprites_start: 0,
polychrome_sprites_iter: self.polychrome_sprites.iter().peekable(),
surfaces: &self.surfaces,
surfaces_start: 0,
surfaces_iter: self.surfaces.iter().peekable(),
}
}
}
@ -247,6 +260,9 @@ struct BatchIterator<'a> {
polychrome_sprites: &'a [PolychromeSprite],
polychrome_sprites_start: usize,
polychrome_sprites_iter: Peekable<slice::Iter<'a, PolychromeSprite>>,
surfaces: &'a [Surface],
surfaces_start: usize,
surfaces_iter: Peekable<slice::Iter<'a, Surface>>,
}
impl<'a> Iterator for BatchIterator<'a> {
@ -272,6 +288,10 @@ impl<'a> Iterator for BatchIterator<'a> {
self.polychrome_sprites_iter.peek().map(|s| s.order),
PrimitiveKind::PolychromeSprite,
),
(
self.surfaces_iter.peek().map(|s| s.order),
PrimitiveKind::Surface,
),
];
orders_and_kinds.sort_by_key(|(order, kind)| (order.unwrap_or(u32::MAX), *kind));
@ -378,6 +398,21 @@ impl<'a> Iterator for BatchIterator<'a> {
sprites: &self.polychrome_sprites[sprites_start..sprites_end],
})
}
PrimitiveKind::Surface => {
let surfaces_start = self.surfaces_start;
let mut surfaces_end = surfaces_start;
while self
.surfaces_iter
.next_if(|surface| surface.order <= max_order)
.is_some()
{
surfaces_end += 1;
}
self.surfaces_start = surfaces_end;
Some(PrimitiveBatch::Surfaces(
&self.surfaces[surfaces_start..surfaces_end],
))
}
}
}
}
@ -391,6 +426,7 @@ pub enum PrimitiveKind {
Underline,
MonochromeSprite,
PolychromeSprite,
Surface,
}
pub enum Primitive {
@ -400,6 +436,7 @@ pub enum Primitive {
Underline(Underline),
MonochromeSprite(MonochromeSprite),
PolychromeSprite(PolychromeSprite),
Surface(Surface),
}
impl Primitive {
@ -411,6 +448,7 @@ impl Primitive {
Primitive::Underline(underline) => &underline.bounds,
Primitive::MonochromeSprite(sprite) => &sprite.bounds,
Primitive::PolychromeSprite(sprite) => &sprite.bounds,
Primitive::Surface(surface) => &surface.bounds,
}
}
@ -422,6 +460,7 @@ impl Primitive {
Primitive::Underline(underline) => &underline.content_mask,
Primitive::MonochromeSprite(sprite) => &sprite.content_mask,
Primitive::PolychromeSprite(sprite) => &sprite.content_mask,
Primitive::Surface(surface) => &surface.content_mask,
}
}
}
@ -440,6 +479,7 @@ pub(crate) enum PrimitiveBatch<'a> {
texture_id: AtlasTextureId,
sprites: &'a [PolychromeSprite],
},
Surfaces(&'a [Surface]),
}
#[derive(Default, Debug, Clone, Eq, PartialEq)]
@ -593,6 +633,32 @@ impl From<PolychromeSprite> for Primitive {
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Surface {
pub order: u32,
pub bounds: Bounds<ScaledPixels>,
pub content_mask: ContentMask<ScaledPixels>,
pub image_buffer: media::core_video::CVImageBuffer,
}
impl Ord for Surface {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.order.cmp(&other.order)
}
}
impl PartialOrd for Surface {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl From<Surface> for Primitive {
fn from(surface: Surface) -> Self {
Primitive::Surface(surface)
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub(crate) struct PathId(pub(crate) usize);

View File

@ -8,8 +8,8 @@ use crate::{
MouseUpEvent, Path, Pixels, PlatformAtlas, PlatformDisplay, PlatformInputHandler,
PlatformWindow, Point, PolychromeSprite, PromptLevel, Quad, Render, RenderGlyphParams,
RenderImageParams, RenderSvgParams, ScaledPixels, SceneBuilder, Shadow, SharedString, Size,
Style, SubscriberSet, Subscription, TaffyLayoutEngine, Task, Underline, UnderlineStyle, View,
VisualContext, WeakView, WindowBounds, WindowOptions, SUBPIXEL_VARIANTS,
Style, SubscriberSet, Subscription, Surface, TaffyLayoutEngine, Task, Underline,
UnderlineStyle, View, VisualContext, WeakView, WindowBounds, WindowOptions, SUBPIXEL_VARIANTS,
};
use anyhow::{anyhow, Context as _, Result};
use collections::HashMap;
@ -18,6 +18,7 @@ use futures::{
channel::{mpsc, oneshot},
StreamExt,
};
use media::core_video::CVImageBuffer;
use parking_lot::RwLock;
use slotmap::SlotMap;
use smallvec::SmallVec;
@ -39,8 +40,8 @@ use util::ResultExt;
/// A global stacking order, which is created by stacking successive z-index values.
/// Each z-index will always be interpreted in the context of its parent z-index.
#[derive(Deref, DerefMut, Ord, PartialOrd, Eq, PartialEq, Clone, Default)]
pub(crate) struct StackingOrder(pub(crate) SmallVec<[u32; 16]>);
#[derive(Deref, DerefMut, Ord, PartialOrd, Eq, PartialEq, Clone, Default, Debug)]
pub struct StackingOrder(pub(crate) SmallVec<[u32; 16]>);
/// Represents the two different phases when dispatching events.
#[derive(Default, Copy, Clone, Debug, Eq, PartialEq)]
@ -193,13 +194,11 @@ pub trait FocusableView: 'static + Render {
/// ManagedView is a view (like a Modal, Popover, Menu, etc.)
/// where the lifecycle of the view is handled by another view.
pub trait ManagedView: FocusableView + EventEmitter<Manager> {}
pub trait ManagedView: FocusableView + EventEmitter<DismissEvent> {}
impl<M: FocusableView + EventEmitter<Manager>> ManagedView for M {}
impl<M: FocusableView + EventEmitter<DismissEvent>> ManagedView for M {}
pub enum Manager {
Dismiss,
}
pub struct DismissEvent;
// Holds the state for a specific window.
pub struct Window {
@ -243,7 +242,8 @@ pub(crate) struct Frame {
pub(crate) dispatch_tree: DispatchTree,
pub(crate) focus_listeners: Vec<AnyFocusListener>,
pub(crate) scene_builder: SceneBuilder,
z_index_stack: StackingOrder,
pub(crate) depth_map: Vec<(StackingOrder, Bounds<Pixels>)>,
pub(crate) z_index_stack: StackingOrder,
content_mask_stack: Vec<ContentMask<Pixels>>,
element_offset_stack: Vec<Point<Pixels>>,
}
@ -257,6 +257,7 @@ impl Frame {
focus_listeners: Vec::new(),
scene_builder: SceneBuilder::default(),
z_index_stack: StackingOrder::default(),
depth_map: Default::default(),
content_mask_stack: Vec::new(),
element_offset_stack: Vec::new(),
}
@ -806,6 +807,32 @@ impl<'a> WindowContext<'a> {
result
}
/// Called during painting to track which z-index is on top at each pixel position
pub fn add_opaque_layer(&mut self, bounds: Bounds<Pixels>) {
let stacking_order = self.window.current_frame.z_index_stack.clone();
let depth_map = &mut self.window.current_frame.depth_map;
match depth_map.binary_search_by(|(level, _)| stacking_order.cmp(&level)) {
Ok(i) | Err(i) => depth_map.insert(i, (stacking_order, bounds)),
}
}
/// Returns true if the top-most opaque layer painted over this point was part of the
/// same layer as the given stacking order.
pub fn was_top_layer(&self, point: &Point<Pixels>, level: &StackingOrder) -> bool {
for (stack, bounds) in self.window.previous_frame.depth_map.iter() {
if bounds.contains_point(point) {
return level.starts_with(stack) || stack.starts_with(level);
}
}
false
}
/// Called during painting to get the current stacking order.
pub fn stacking_order(&self) -> &StackingOrder {
&self.window.current_frame.z_index_stack
}
/// Paint one or more drop shadows into the scene for the current frame at the current z-index.
pub fn paint_shadows(
&mut self,
@ -1090,6 +1117,23 @@ impl<'a> WindowContext<'a> {
Ok(())
}
/// Paint a surface into the scene for the current frame at the current z-index.
pub fn paint_surface(&mut self, bounds: Bounds<Pixels>, image_buffer: CVImageBuffer) {
let scale_factor = self.scale_factor();
let bounds = bounds.scale(scale_factor);
let content_mask = self.content_mask().scale(scale_factor);
let window = &mut *self.window;
window.current_frame.scene_builder.insert(
&window.current_frame.z_index_stack,
Surface {
order: 0,
bounds,
content_mask,
image_buffer,
},
);
}
/// Draw pixels to the display for this window based on the contents of its scene.
pub(crate) fn draw(&mut self) {
let root_view = self.window.root_view.take().unwrap();
@ -1153,6 +1197,7 @@ impl<'a> WindowContext<'a> {
frame.mouse_listeners.values_mut().for_each(Vec::clear);
frame.focus_listeners.clear();
frame.dispatch_tree.clear();
frame.depth_map.clear();
}
/// Dispatch a mouse or keyboard event on the window.
@ -1453,13 +1498,15 @@ impl<'a> WindowContext<'a> {
}
}
pub fn constructor_for<V: Render, R>(
pub fn handler_for<V: Render>(
&self,
view: &View<V>,
f: impl Fn(&mut V, &mut ViewContext<V>) -> R + 'static,
) -> impl Fn(&mut WindowContext) -> R + 'static {
let view = view.clone();
move |cx: &mut WindowContext| view.update(cx, |view, cx| f(view, cx))
f: impl Fn(&mut V, &mut ViewContext<V>) + 'static,
) -> impl Fn(&mut WindowContext) {
let view = view.downgrade();
move |cx: &mut WindowContext| {
view.update(cx, |view, cx| f(view, cx)).ok();
}
}
//========== ELEMENT RELATED FUNCTIONS ===========
@ -1517,6 +1564,13 @@ impl<'a> WindowContext<'a> {
.set_input_handler(Box::new(input_handler));
}
}
pub fn on_window_should_close(&mut self, f: impl Fn(&mut WindowContext) -> bool + 'static) {
let mut this = self.to_async();
self.window
.platform_window
.on_should_close(Box::new(move || this.update(|_, cx| f(cx)).unwrap_or(true)))
}
}
impl Context for WindowContext<'_> {
@ -1663,7 +1717,7 @@ impl VisualContext for WindowContext<'_> {
where
V: ManagedView,
{
self.update_view(view, |_, cx| cx.emit(Manager::Dismiss))
self.update_view(view, |_, cx| cx.emit(DismissEvent))
}
}
@ -1752,6 +1806,24 @@ pub trait BorrowWindow: BorrowMut<Window> + BorrowMut<AppContext> {
}
}
/// Invoke the given function with the content mask reset to that
/// of the window.
fn break_content_mask<R>(&mut self, f: impl FnOnce(&mut Self) -> R) -> R {
let mask = ContentMask {
bounds: Bounds {
origin: Point::default(),
size: self.window().viewport_size,
},
};
self.window_mut()
.current_frame
.content_mask_stack
.push(mask);
let result = f(self);
self.window_mut().current_frame.content_mask_stack.pop();
result
}
/// Update the global element offset relative to the current offset. This is used to implement
/// scrolling.
fn with_element_offset<R>(
@ -1885,23 +1957,6 @@ pub trait BorrowWindow: BorrowMut<Window> + BorrowMut<AppContext> {
})
}
/// Like `with_element_state`, but for situations where the element_id is optional. If the
/// id is `None`, no state will be retrieved or stored.
fn with_optional_element_state<S, R>(
&mut self,
element_id: Option<ElementId>,
f: impl FnOnce(Option<S>, &mut Self) -> (R, S),
) -> R
where
S: 'static,
{
if let Some(element_id) = element_id {
self.with_element_state(element_id, f)
} else {
f(None, self).0
}
}
/// Obtain the current content mask.
fn content_mask(&self) -> ContentMask<Pixels> {
self.window()
@ -2349,7 +2404,7 @@ impl<'a, V: 'static> ViewContext<'a, V> {
where
V: ManagedView,
{
self.defer(|_, cx| cx.emit(Manager::Dismiss))
self.defer(|_, cx| cx.emit(DismissEvent))
}
pub fn listener<E>(
@ -2545,7 +2600,7 @@ impl<V: 'static + Render> WindowHandle<V> {
cx.read_window(self, |root_view, _cx| root_view.clone())
}
pub fn is_active(&self, cx: &WindowContext) -> Option<bool> {
pub fn is_active(&self, cx: &AppContext) -> Option<bool> {
cx.windows
.get(self.id)
.and_then(|window| window.as_ref().map(|window| window.active))

View File

@ -11,7 +11,7 @@ pub struct HighlightId(pub u32);
const DEFAULT_SYNTAX_HIGHLIGHT_ID: HighlightId = HighlightId(u32::MAX);
impl HighlightMap {
pub fn new(capture_names: &[String], theme: &SyntaxTheme) -> Self {
pub fn new(capture_names: &[&str], theme: &SyntaxTheme) -> Self {
// For each capture name in the highlight query, find the longest
// key in the theme's syntax styles that matches all of the
// dot-separated components of the capture name.
@ -98,9 +98,9 @@ mod tests {
);
let capture_names = &[
"function.special".to_string(),
"function.async.rust".to_string(),
"variable.builtin.self".to_string(),
"function.special",
"function.async.rust",
"variable.builtin.self",
];
let map = HighlightMap::new(capture_names, &theme);

View File

@ -197,8 +197,12 @@ impl CachedLspAdapter {
self.adapter.code_action_kinds()
}
pub fn workspace_configuration(&self, cx: &mut AppContext) -> BoxFuture<'static, Value> {
self.adapter.workspace_configuration(cx)
pub fn workspace_configuration(
&self,
workspace_root: &Path,
cx: &mut AppContext,
) -> BoxFuture<'static, Value> {
self.adapter.workspace_configuration(workspace_root, cx)
}
pub fn process_diagnostics(&self, params: &mut lsp::PublishDiagnosticsParams) {
@ -312,7 +316,7 @@ pub trait LspAdapter: 'static + Send + Sync {
None
}
fn workspace_configuration(&self, _: &mut AppContext) -> BoxFuture<'static, Value> {
fn workspace_configuration(&self, _: &Path, _: &mut AppContext) -> BoxFuture<'static, Value> {
futures::future::ready(serde_json::json!({})).boxed()
}
@ -1383,7 +1387,7 @@ impl Language {
let query = Query::new(self.grammar_mut().ts_language, source)?;
let mut override_configs_by_id = HashMap::default();
for (ix, name) in query.capture_names().iter().enumerate() {
for (ix, name) in query.capture_names().iter().copied().enumerate() {
if !name.starts_with('_') {
let value = self.config.overrides.remove(name).unwrap_or_default();
for server_name in &value.opt_into_language_servers {
@ -1396,7 +1400,7 @@ impl Language {
}
}
override_configs_by_id.insert(ix as u32, (name.clone(), value));
override_configs_by_id.insert(ix as u32, (name.into(), value));
}
}

View File

@ -1300,7 +1300,7 @@ fn assert_capture_ranges(
.collect::<Vec<_>>();
for capture in captures {
let name = &queries[capture.grammar_index].capture_names()[capture.index as usize];
if highlight_query_capture_names.contains(&name.as_str()) {
if highlight_query_capture_names.contains(&name) {
actual_ranges.push(capture.node.byte_range());
}
}

View File

@ -11,7 +11,7 @@ pub struct HighlightId(pub u32);
const DEFAULT_SYNTAX_HIGHLIGHT_ID: HighlightId = HighlightId(u32::MAX);
impl HighlightMap {
pub fn new(capture_names: &[String], theme: &SyntaxTheme) -> Self {
pub fn new(capture_names: &[&str], theme: &SyntaxTheme) -> Self {
// For each capture name in the highlight query, find the longest
// key in the theme's syntax styles that matches all of the
// dot-separated components of the capture name.
@ -100,9 +100,9 @@ mod tests {
};
let capture_names = &[
"function.special".to_string(),
"function.async.rust".to_string(),
"variable.builtin.self".to_string(),
"function.special",
"function.async.rust",
"variable.builtin.self",
];
let map = HighlightMap::new(capture_names, &theme);

View File

@ -200,8 +200,12 @@ impl CachedLspAdapter {
self.adapter.code_action_kinds()
}
pub fn workspace_configuration(&self, cx: &mut AppContext) -> BoxFuture<'static, Value> {
self.adapter.workspace_configuration(cx)
pub fn workspace_configuration(
&self,
workspace_root: &Path,
cx: &mut AppContext,
) -> BoxFuture<'static, Value> {
self.adapter.workspace_configuration(workspace_root, cx)
}
pub fn process_diagnostics(&self, params: &mut lsp::PublishDiagnosticsParams) {
@ -315,7 +319,7 @@ pub trait LspAdapter: 'static + Send + Sync {
None
}
fn workspace_configuration(&self, _: &mut AppContext) -> BoxFuture<'static, Value> {
fn workspace_configuration(&self, _: &Path, _: &mut AppContext) -> BoxFuture<'static, Value> {
futures::future::ready(serde_json::json!({})).boxed()
}
@ -1391,7 +1395,7 @@ impl Language {
let mut override_configs_by_id = HashMap::default();
for (ix, name) in query.capture_names().iter().enumerate() {
if !name.starts_with('_') {
let value = self.config.overrides.remove(name).unwrap_or_default();
let value = self.config.overrides.remove(*name).unwrap_or_default();
for server_name in &value.opt_into_language_servers {
if !self
.config
@ -1402,7 +1406,7 @@ impl Language {
}
}
override_configs_by_id.insert(ix as u32, (name.clone(), value));
override_configs_by_id.insert(ix as u32, (name.to_string(), value));
}
}

View File

@ -1300,7 +1300,7 @@ fn assert_capture_ranges(
.collect::<Vec<_>>();
for capture in captures {
let name = &queries[capture.grammar_index].capture_names()[capture.index as usize];
if highlight_query_capture_names.contains(&name.as_str()) {
if highlight_query_capture_names.contains(&name) {
actual_ranges.push(capture.node.byte_range());
}
}

View File

@ -429,8 +429,8 @@ impl LanguageServer {
let root_uri = Url::from_file_path(&self.root_path).unwrap();
#[allow(deprecated)]
let params = InitializeParams {
process_id: Default::default(),
root_path: Default::default(),
process_id: None,
root_path: None,
root_uri: Some(root_uri.clone()),
initialization_options: options,
capabilities: ClientCapabilities {
@ -451,12 +451,15 @@ impl LanguageServer {
inlay_hint: Some(InlayHintWorkspaceClientCapabilities {
refresh_support: Some(true),
}),
diagnostic: Some(DiagnosticWorkspaceClientCapabilities {
refresh_support: None,
}),
..Default::default()
}),
text_document: Some(TextDocumentClientCapabilities {
definition: Some(GotoCapability {
link_support: Some(true),
..Default::default()
dynamic_registration: None,
}),
code_action: Some(CodeActionClientCapabilities {
code_action_literal_support: Some(CodeActionLiteralSupport {
@ -501,7 +504,7 @@ impl LanguageServer {
}),
hover: Some(HoverClientCapabilities {
content_format: Some(vec![MarkupKind::Markdown]),
..Default::default()
dynamic_registration: None,
}),
inlay_hint: Some(InlayHintClientCapabilities {
resolve_support: Some(InlayHintResolveClientCapabilities {
@ -515,6 +518,20 @@ impl LanguageServer {
}),
dynamic_registration: Some(false),
}),
publish_diagnostics: Some(PublishDiagnosticsClientCapabilities {
related_information: Some(true),
..Default::default()
}),
formatting: Some(DynamicRegistrationClientCapabilities {
dynamic_registration: None,
}),
on_type_formatting: Some(DynamicRegistrationClientCapabilities {
dynamic_registration: None,
}),
diagnostic: Some(DiagnosticClientCapabilities {
related_document_support: Some(true),
dynamic_registration: None,
}),
..Default::default()
}),
experimental: Some(json!({
@ -524,15 +541,15 @@ impl LanguageServer {
work_done_progress: Some(true),
..Default::default()
}),
..Default::default()
general: None,
},
trace: Default::default(),
trace: None,
workspace_folders: Some(vec![WorkspaceFolder {
uri: root_uri,
name: Default::default(),
}]),
client_info: Default::default(),
locale: Default::default(),
client_info: None,
locale: None,
};
let response = self.request::<request::Initialize>(params).await?;

View File

@ -434,8 +434,8 @@ impl LanguageServer {
let root_uri = Url::from_file_path(&self.root_path).unwrap();
#[allow(deprecated)]
let params = InitializeParams {
process_id: Default::default(),
root_path: Default::default(),
process_id: None,
root_path: None,
root_uri: Some(root_uri.clone()),
initialization_options: options,
capabilities: ClientCapabilities {
@ -456,12 +456,15 @@ impl LanguageServer {
inlay_hint: Some(InlayHintWorkspaceClientCapabilities {
refresh_support: Some(true),
}),
diagnostic: Some(DiagnosticWorkspaceClientCapabilities {
refresh_support: None,
}),
..Default::default()
}),
text_document: Some(TextDocumentClientCapabilities {
definition: Some(GotoCapability {
link_support: Some(true),
..Default::default()
dynamic_registration: None,
}),
code_action: Some(CodeActionClientCapabilities {
code_action_literal_support: Some(CodeActionLiteralSupport {
@ -503,7 +506,7 @@ impl LanguageServer {
}),
hover: Some(HoverClientCapabilities {
content_format: Some(vec![MarkupKind::Markdown]),
..Default::default()
dynamic_registration: None,
}),
inlay_hint: Some(InlayHintClientCapabilities {
resolve_support: Some(InlayHintResolveClientCapabilities {
@ -517,6 +520,20 @@ impl LanguageServer {
}),
dynamic_registration: Some(false),
}),
publish_diagnostics: Some(PublishDiagnosticsClientCapabilities {
related_information: Some(true),
..Default::default()
}),
formatting: Some(DynamicRegistrationClientCapabilities {
dynamic_registration: None,
}),
on_type_formatting: Some(DynamicRegistrationClientCapabilities {
dynamic_registration: None,
}),
diagnostic: Some(DiagnosticClientCapabilities {
related_document_support: Some(true),
dynamic_registration: None,
}),
..Default::default()
}),
experimental: Some(json!({
@ -526,15 +543,15 @@ impl LanguageServer {
work_done_progress: Some(true),
..Default::default()
}),
..Default::default()
general: None,
},
trace: Default::default(),
trace: None,
workspace_folders: Some(vec![WorkspaceFolder {
uri: root_uri,
name: Default::default(),
}]),
client_info: Default::default(),
locale: Default::default(),
client_info: None,
locale: None,
};
let response = self.request::<request::Initialize>(params).await?;

View File

@ -73,6 +73,7 @@ impl RealNodeRuntime {
let npm_file = node_dir.join("bin/npm");
let result = Command::new(&node_binary)
.env_clear()
.arg(npm_file)
.arg("--version")
.stdin(Stdio::null())
@ -149,6 +150,7 @@ impl NodeRuntime for RealNodeRuntime {
}
let mut command = Command::new(node_binary);
command.env_clear();
command.env("PATH", env_path);
command.arg(npm_file).arg(subcommand);
command.args(["--cache".into(), installation_path.join("cache")]);
@ -200,11 +202,11 @@ impl NodeRuntime for RealNodeRuntime {
&[
name,
"--json",
"-fetch-retry-mintimeout",
"--fetch-retry-mintimeout",
"2000",
"-fetch-retry-maxtimeout",
"--fetch-retry-maxtimeout",
"5000",
"-fetch-timeout",
"--fetch-timeout",
"5000",
],
)
@ -229,11 +231,11 @@ impl NodeRuntime for RealNodeRuntime {
let mut arguments: Vec<_> = packages.iter().map(|p| p.as_str()).collect();
arguments.extend_from_slice(&[
"-fetch-retry-mintimeout",
"--fetch-retry-mintimeout",
"2000",
"-fetch-retry-maxtimeout",
"--fetch-retry-maxtimeout",
"5000",
"-fetch-timeout",
"--fetch-timeout",
"5000",
]);

View File

@ -1,7 +1,8 @@
use editor::Editor;
use gpui::{
div, prelude::*, uniform_list, AppContext, Div, FocusHandle, FocusableView, MouseButton,
MouseDownEvent, Render, Task, UniformListScrollHandle, View, ViewContext, WindowContext,
div, prelude::*, uniform_list, AnyElement, AppContext, Div, FocusHandle, FocusableView,
MouseButton, MouseDownEvent, Render, Task, UniformListScrollHandle, View, ViewContext,
WindowContext,
};
use std::{cmp, sync::Arc};
use ui::{prelude::*, v_stack, Color, Divider, Label};
@ -16,7 +17,6 @@ pub struct Picker<D: PickerDelegate> {
pub trait PickerDelegate: Sized + 'static {
type ListItem: IntoElement;
fn match_count(&self) -> usize;
fn selected_index(&self) -> usize;
fn set_selected_index(&mut self, ix: usize, cx: &mut ViewContext<Picker<Self>>);
@ -32,7 +32,7 @@ pub trait PickerDelegate: Sized + 'static {
ix: usize,
selected: bool,
cx: &mut ViewContext<Picker<Self>>,
) -> Self::ListItem;
) -> Option<Self::ListItem>;
}
impl<D: PickerDelegate> FocusableView for Picker<D> {
@ -205,7 +205,6 @@ impl<D: PickerDelegate> Render for Picker<D> {
.when(self.delegate.match_count() > 0, |el| {
el.child(
v_stack()
.p_1()
.grow()
.child(
uniform_list(
@ -229,7 +228,7 @@ impl<D: PickerDelegate> Render for Picker<D> {
)
}),
)
.child(picker.delegate.render_match(
.children(picker.delegate.render_match(
ix,
ix == selected_index,
cx,
@ -239,7 +238,8 @@ impl<D: PickerDelegate> Render for Picker<D> {
}
},
)
.track_scroll(self.scroll_handle.clone()),
.track_scroll(self.scroll_handle.clone())
.p_1()
)
.max_h_72()
.overflow_hidden(),
@ -256,3 +256,22 @@ impl<D: PickerDelegate> Render for Picker<D> {
})
}
}
pub fn simple_picker_match(
selected: bool,
cx: &mut WindowContext,
children: impl FnOnce(&mut WindowContext) -> AnyElement,
) -> AnyElement {
let colors = cx.theme().colors();
div()
.px_1()
.text_color(colors.text)
.text_ui()
.bg(colors.ghost_element_background)
.rounded_md()
.when(selected, |this| this.bg(colors.ghost_element_selected))
.hover(|this| this.bg(colors.ghost_element_hover))
.child((children)(cx))
.into_any()
}

View File

@ -13,12 +13,14 @@ use node_runtime::NodeRuntime;
use serde::{Deserialize, Serialize};
use util::paths::{PathMatcher, DEFAULT_PRETTIER_DIR};
#[derive(Clone)]
pub enum Prettier {
Real(RealPrettier),
#[cfg(any(test, feature = "test-support"))]
Test(TestPrettier),
}
#[derive(Clone)]
pub struct RealPrettier {
default: bool,
prettier_dir: PathBuf,
@ -26,11 +28,13 @@ pub struct RealPrettier {
}
#[cfg(any(test, feature = "test-support"))]
#[derive(Clone)]
pub struct TestPrettier {
prettier_dir: PathBuf,
default: bool,
}
pub const FAIL_THRESHOLD: usize = 4;
pub const PRETTIER_SERVER_FILE: &str = "prettier_server.js";
pub const PRETTIER_SERVER_JS: &str = include_str!("./prettier_server.js");
const PRETTIER_PACKAGE_NAME: &str = "prettier";

View File

@ -153,7 +153,10 @@ async function handleMessage(message, prettier) {
const { method, id, params } = message;
if (method === undefined) {
throw new Error(`Message method is undefined: ${JSON.stringify(message)}`);
} else if (method == "initialized") {
return;
}
if (id === undefined) {
throw new Error(`Message id is undefined: ${JSON.stringify(message)}`);
}

View File

@ -13,12 +13,14 @@ use std::{
};
use util::paths::{PathMatcher, DEFAULT_PRETTIER_DIR};
#[derive(Clone)]
pub enum Prettier {
Real(RealPrettier),
#[cfg(any(test, feature = "test-support"))]
Test(TestPrettier),
}
#[derive(Clone)]
pub struct RealPrettier {
default: bool,
prettier_dir: PathBuf,
@ -26,11 +28,13 @@ pub struct RealPrettier {
}
#[cfg(any(test, feature = "test-support"))]
#[derive(Clone)]
pub struct TestPrettier {
prettier_dir: PathBuf,
default: bool,
}
pub const FAIL_THRESHOLD: usize = 4;
pub const PRETTIER_SERVER_FILE: &str = "prettier_server.js";
pub const PRETTIER_SERVER_JS: &str = include_str!("./prettier_server.js");
const PRETTIER_PACKAGE_NAME: &str = "prettier";

View File

@ -153,7 +153,10 @@ async function handleMessage(message, prettier) {
const { method, id, params } = message;
if (method === undefined) {
throw new Error(`Message method is undefined: ${JSON.stringify(message)}`);
} else if (method == "initialized") {
return;
}
if (id === undefined) {
throw new Error(`Message id is undefined: ${JSON.stringify(message)}`);
}

View File

@ -0,0 +1,758 @@
use std::{
ops::ControlFlow,
path::{Path, PathBuf},
sync::Arc,
};
use anyhow::Context;
use collections::HashSet;
use fs::Fs;
use futures::{
future::{self, Shared},
FutureExt,
};
use gpui::{AsyncAppContext, ModelContext, ModelHandle, Task};
use language::{
language_settings::{Formatter, LanguageSettings},
Buffer, Language, LanguageServerName, LocalFile,
};
use lsp::LanguageServerId;
use node_runtime::NodeRuntime;
use prettier::Prettier;
use util::{paths::DEFAULT_PRETTIER_DIR, ResultExt, TryFutureExt};
use crate::{
Event, File, FormatOperation, PathChange, Project, ProjectEntryId, Worktree, WorktreeId,
};
pub fn prettier_plugins_for_language(
language: &Language,
language_settings: &LanguageSettings,
) -> Option<HashSet<&'static str>> {
match &language_settings.formatter {
Formatter::Prettier { .. } | Formatter::Auto => {}
Formatter::LanguageServer | Formatter::External { .. } => return None,
};
let mut prettier_plugins = None;
if language.prettier_parser_name().is_some() {
prettier_plugins
.get_or_insert_with(|| HashSet::default())
.extend(
language
.lsp_adapters()
.iter()
.flat_map(|adapter| adapter.prettier_plugins()),
)
}
prettier_plugins
}
pub(super) async fn format_with_prettier(
project: &ModelHandle<Project>,
buffer: &ModelHandle<Buffer>,
cx: &mut AsyncAppContext,
) -> Option<FormatOperation> {
if let Some((prettier_path, prettier_task)) = project
.update(cx, |project, cx| {
project.prettier_instance_for_buffer(buffer, cx)
})
.await
{
match prettier_task.await {
Ok(prettier) => {
let buffer_path = buffer.update(cx, |buffer, cx| {
File::from_dyn(buffer.file()).map(|file| file.abs_path(cx))
});
match prettier.format(buffer, buffer_path, cx).await {
Ok(new_diff) => return Some(FormatOperation::Prettier(new_diff)),
Err(e) => {
log::error!(
"Prettier instance from {prettier_path:?} failed to format a buffer: {e:#}"
);
}
}
}
Err(e) => project.update(cx, |project, _| {
let instance_to_update = match prettier_path {
Some(prettier_path) => {
log::error!(
"Prettier instance from path {prettier_path:?} failed to spawn: {e:#}"
);
project.prettier_instances.get_mut(&prettier_path)
}
None => {
log::error!("Default prettier instance failed to spawn: {e:#}");
match &mut project.default_prettier.prettier {
PrettierInstallation::NotInstalled { .. } => None,
PrettierInstallation::Installed(instance) => Some(instance),
}
}
};
if let Some(instance) = instance_to_update {
instance.attempt += 1;
instance.prettier = None;
}
}),
}
}
None
}
pub struct DefaultPrettier {
prettier: PrettierInstallation,
installed_plugins: HashSet<&'static str>,
}
pub enum PrettierInstallation {
NotInstalled {
attempts: usize,
installation_task: Option<Shared<Task<Result<(), Arc<anyhow::Error>>>>>,
not_installed_plugins: HashSet<&'static str>,
},
Installed(PrettierInstance),
}
pub type PrettierTask = Shared<Task<Result<Arc<Prettier>, Arc<anyhow::Error>>>>;
#[derive(Clone)]
pub struct PrettierInstance {
attempt: usize,
prettier: Option<PrettierTask>,
}
impl Default for DefaultPrettier {
fn default() -> Self {
Self {
prettier: PrettierInstallation::NotInstalled {
attempts: 0,
installation_task: None,
not_installed_plugins: HashSet::default(),
},
installed_plugins: HashSet::default(),
}
}
}
impl DefaultPrettier {
pub fn instance(&self) -> Option<&PrettierInstance> {
if let PrettierInstallation::Installed(instance) = &self.prettier {
Some(instance)
} else {
None
}
}
pub fn prettier_task(
&mut self,
node: &Arc<dyn NodeRuntime>,
worktree_id: Option<WorktreeId>,
cx: &mut ModelContext<'_, Project>,
) -> Option<Task<anyhow::Result<PrettierTask>>> {
match &mut self.prettier {
PrettierInstallation::NotInstalled { .. } => {
Some(start_default_prettier(Arc::clone(node), worktree_id, cx))
}
PrettierInstallation::Installed(existing_instance) => {
existing_instance.prettier_task(node, None, worktree_id, cx)
}
}
}
}
impl PrettierInstance {
pub fn prettier_task(
&mut self,
node: &Arc<dyn NodeRuntime>,
prettier_dir: Option<&Path>,
worktree_id: Option<WorktreeId>,
cx: &mut ModelContext<'_, Project>,
) -> Option<Task<anyhow::Result<PrettierTask>>> {
if self.attempt > prettier::FAIL_THRESHOLD {
match prettier_dir {
Some(prettier_dir) => log::warn!(
"Prettier from path {prettier_dir:?} exceeded launch threshold, not starting"
),
None => log::warn!("Default prettier exceeded launch threshold, not starting"),
}
return None;
}
Some(match &self.prettier {
Some(prettier_task) => Task::ready(Ok(prettier_task.clone())),
None => match prettier_dir {
Some(prettier_dir) => {
let new_task = start_prettier(
Arc::clone(node),
prettier_dir.to_path_buf(),
worktree_id,
cx,
);
self.attempt += 1;
self.prettier = Some(new_task.clone());
Task::ready(Ok(new_task))
}
None => {
self.attempt += 1;
let node = Arc::clone(node);
cx.spawn(|project, mut cx| async move {
project
.update(&mut cx, |_, cx| {
start_default_prettier(node, worktree_id, cx)
})
.await
})
}
},
})
}
}
fn start_default_prettier(
node: Arc<dyn NodeRuntime>,
worktree_id: Option<WorktreeId>,
cx: &mut ModelContext<'_, Project>,
) -> Task<anyhow::Result<PrettierTask>> {
cx.spawn(|project, mut cx| async move {
loop {
let installation_task = project.update(&mut cx, |project, _| {
match &project.default_prettier.prettier {
PrettierInstallation::NotInstalled {
installation_task, ..
} => ControlFlow::Continue(installation_task.clone()),
PrettierInstallation::Installed(default_prettier) => {
ControlFlow::Break(default_prettier.clone())
}
}
});
match installation_task {
ControlFlow::Continue(None) => {
anyhow::bail!("Default prettier is not installed and cannot be started")
}
ControlFlow::Continue(Some(installation_task)) => {
log::info!("Waiting for default prettier to install");
if let Err(e) = installation_task.await {
project.update(&mut cx, |project, _| {
if let PrettierInstallation::NotInstalled {
installation_task,
attempts,
..
} = &mut project.default_prettier.prettier
{
*installation_task = None;
*attempts += 1;
}
});
anyhow::bail!(
"Cannot start default prettier due to its installation failure: {e:#}"
);
}
let new_default_prettier = project.update(&mut cx, |project, cx| {
let new_default_prettier =
start_prettier(node, DEFAULT_PRETTIER_DIR.clone(), worktree_id, cx);
project.default_prettier.prettier =
PrettierInstallation::Installed(PrettierInstance {
attempt: 0,
prettier: Some(new_default_prettier.clone()),
});
new_default_prettier
});
return Ok(new_default_prettier);
}
ControlFlow::Break(instance) => match instance.prettier {
Some(instance) => return Ok(instance),
None => {
let new_default_prettier = project.update(&mut cx, |project, cx| {
let new_default_prettier =
start_prettier(node, DEFAULT_PRETTIER_DIR.clone(), worktree_id, cx);
project.default_prettier.prettier =
PrettierInstallation::Installed(PrettierInstance {
attempt: instance.attempt + 1,
prettier: Some(new_default_prettier.clone()),
});
new_default_prettier
});
return Ok(new_default_prettier);
}
},
}
}
})
}
fn start_prettier(
node: Arc<dyn NodeRuntime>,
prettier_dir: PathBuf,
worktree_id: Option<WorktreeId>,
cx: &mut ModelContext<'_, Project>,
) -> PrettierTask {
cx.spawn(|project, mut cx| async move {
log::info!("Starting prettier at path {prettier_dir:?}");
let new_server_id = project.update(&mut cx, |project, _| {
project.languages.next_language_server_id()
});
let new_prettier = Prettier::start(new_server_id, prettier_dir, node, cx.clone())
.await
.context("default prettier spawn")
.map(Arc::new)
.map_err(Arc::new)?;
register_new_prettier(&project, &new_prettier, worktree_id, new_server_id, &mut cx);
Ok(new_prettier)
})
.shared()
}
fn register_new_prettier(
project: &ModelHandle<Project>,
prettier: &Prettier,
worktree_id: Option<WorktreeId>,
new_server_id: LanguageServerId,
cx: &mut AsyncAppContext,
) {
let prettier_dir = prettier.prettier_dir();
let is_default = prettier.is_default();
if is_default {
log::info!("Started default prettier in {prettier_dir:?}");
} else {
log::info!("Started prettier in {prettier_dir:?}");
}
if let Some(prettier_server) = prettier.server() {
project.update(cx, |project, cx| {
let name = if is_default {
LanguageServerName(Arc::from("prettier (default)"))
} else {
let worktree_path = worktree_id
.and_then(|id| project.worktree_for_id(id, cx))
.map(|worktree| worktree.update(cx, |worktree, _| worktree.abs_path()));
let name = match worktree_path {
Some(worktree_path) => {
if prettier_dir == worktree_path.as_ref() {
let name = prettier_dir
.file_name()
.and_then(|name| name.to_str())
.unwrap_or_default();
format!("prettier ({name})")
} else {
let dir_to_display = prettier_dir
.strip_prefix(worktree_path.as_ref())
.ok()
.unwrap_or(prettier_dir);
format!("prettier ({})", dir_to_display.display())
}
}
None => format!("prettier ({})", prettier_dir.display()),
};
LanguageServerName(Arc::from(name))
};
project
.supplementary_language_servers
.insert(new_server_id, (name, Arc::clone(prettier_server)));
cx.emit(Event::LanguageServerAdded(new_server_id));
});
}
}
async fn install_prettier_packages(
plugins_to_install: HashSet<&'static str>,
node: Arc<dyn NodeRuntime>,
) -> anyhow::Result<()> {
let packages_to_versions =
future::try_join_all(plugins_to_install.iter().chain(Some(&"prettier")).map(
|package_name| async {
let returned_package_name = package_name.to_string();
let latest_version = node
.npm_package_latest_version(package_name)
.await
.with_context(|| {
format!("fetching latest npm version for package {returned_package_name}")
})?;
anyhow::Ok((returned_package_name, latest_version))
},
))
.await
.context("fetching latest npm versions")?;
log::info!("Fetching default prettier and plugins: {packages_to_versions:?}");
let borrowed_packages = packages_to_versions
.iter()
.map(|(package, version)| (package.as_str(), version.as_str()))
.collect::<Vec<_>>();
node.npm_install_packages(DEFAULT_PRETTIER_DIR.as_path(), &borrowed_packages)
.await
.context("fetching formatter packages")?;
anyhow::Ok(())
}
async fn save_prettier_server_file(fs: &dyn Fs) -> Result<(), anyhow::Error> {
let prettier_wrapper_path = DEFAULT_PRETTIER_DIR.join(prettier::PRETTIER_SERVER_FILE);
fs.save(
&prettier_wrapper_path,
&text::Rope::from(prettier::PRETTIER_SERVER_JS),
text::LineEnding::Unix,
)
.await
.with_context(|| {
format!(
"writing {} file at {prettier_wrapper_path:?}",
prettier::PRETTIER_SERVER_FILE
)
})?;
Ok(())
}
impl Project {
pub fn update_prettier_settings(
&self,
worktree: &ModelHandle<Worktree>,
changes: &[(Arc<Path>, ProjectEntryId, PathChange)],
cx: &mut ModelContext<'_, Project>,
) {
let prettier_config_files = Prettier::CONFIG_FILE_NAMES
.iter()
.map(Path::new)
.collect::<HashSet<_>>();
let prettier_config_file_changed = changes
.iter()
.filter(|(_, _, change)| !matches!(change, PathChange::Loaded))
.filter(|(path, _, _)| {
!path
.components()
.any(|component| component.as_os_str().to_string_lossy() == "node_modules")
})
.find(|(path, _, _)| prettier_config_files.contains(path.as_ref()));
let current_worktree_id = worktree.read(cx).id();
if let Some((config_path, _, _)) = prettier_config_file_changed {
log::info!(
"Prettier config file {config_path:?} changed, reloading prettier instances for worktree {current_worktree_id}"
);
let prettiers_to_reload =
self.prettiers_per_worktree
.get(&current_worktree_id)
.iter()
.flat_map(|prettier_paths| prettier_paths.iter())
.flatten()
.filter_map(|prettier_path| {
Some((
current_worktree_id,
Some(prettier_path.clone()),
self.prettier_instances.get(prettier_path)?.clone(),
))
})
.chain(self.default_prettier.instance().map(|default_prettier| {
(current_worktree_id, None, default_prettier.clone())
}))
.collect::<Vec<_>>();
cx.background()
.spawn(async move {
let _: Vec<()> = future::join_all(prettiers_to_reload.into_iter().map(|(worktree_id, prettier_path, prettier_instance)| {
async move {
if let Some(instance) = prettier_instance.prettier {
match instance.await {
Ok(prettier) => {
prettier.clear_cache().log_err().await;
},
Err(e) => {
match prettier_path {
Some(prettier_path) => log::error!(
"Failed to clear prettier {prettier_path:?} cache for worktree {worktree_id:?} on prettier settings update: {e:#}"
),
None => log::error!(
"Failed to clear default prettier cache for worktree {worktree_id:?} on prettier settings update: {e:#}"
),
}
},
}
}
}
}))
.await;
})
.detach();
}
}
fn prettier_instance_for_buffer(
&mut self,
buffer: &ModelHandle<Buffer>,
cx: &mut ModelContext<Self>,
) -> Task<Option<(Option<PathBuf>, PrettierTask)>> {
let buffer = buffer.read(cx);
let buffer_file = buffer.file();
let Some(buffer_language) = buffer.language() else {
return Task::ready(None);
};
if buffer_language.prettier_parser_name().is_none() {
return Task::ready(None);
}
if self.is_local() {
let Some(node) = self.node.as_ref().map(Arc::clone) else {
return Task::ready(None);
};
match File::from_dyn(buffer_file).map(|file| (file.worktree_id(cx), file.abs_path(cx)))
{
Some((worktree_id, buffer_path)) => {
let fs = Arc::clone(&self.fs);
let installed_prettiers = self.prettier_instances.keys().cloned().collect();
return cx.spawn(|project, mut cx| async move {
match cx
.background()
.spawn(async move {
Prettier::locate_prettier_installation(
fs.as_ref(),
&installed_prettiers,
&buffer_path,
)
.await
})
.await
{
Ok(ControlFlow::Break(())) => {
return None;
}
Ok(ControlFlow::Continue(None)) => {
let default_instance = project.update(&mut cx, |project, cx| {
project
.prettiers_per_worktree
.entry(worktree_id)
.or_default()
.insert(None);
project.default_prettier.prettier_task(
&node,
Some(worktree_id),
cx,
)
});
Some((None, default_instance?.log_err().await?))
}
Ok(ControlFlow::Continue(Some(prettier_dir))) => {
project.update(&mut cx, |project, _| {
project
.prettiers_per_worktree
.entry(worktree_id)
.or_default()
.insert(Some(prettier_dir.clone()))
});
if let Some(prettier_task) =
project.update(&mut cx, |project, cx| {
project.prettier_instances.get_mut(&prettier_dir).map(
|existing_instance| {
existing_instance.prettier_task(
&node,
Some(&prettier_dir),
Some(worktree_id),
cx,
)
},
)
})
{
log::debug!(
"Found already started prettier in {prettier_dir:?}"
);
return Some((
Some(prettier_dir),
prettier_task?.await.log_err()?,
));
}
log::info!("Found prettier in {prettier_dir:?}, starting.");
let new_prettier_task = project.update(&mut cx, |project, cx| {
let new_prettier_task = start_prettier(
node,
prettier_dir.clone(),
Some(worktree_id),
cx,
);
project.prettier_instances.insert(
prettier_dir.clone(),
PrettierInstance {
attempt: 0,
prettier: Some(new_prettier_task.clone()),
},
);
new_prettier_task
});
Some((Some(prettier_dir), new_prettier_task))
}
Err(e) => {
log::error!("Failed to determine prettier path for buffer: {e:#}");
return None;
}
}
});
}
None => {
let new_task = self.default_prettier.prettier_task(&node, None, cx);
return cx
.spawn(|_, _| async move { Some((None, new_task?.log_err().await?)) });
}
}
} else {
return Task::ready(None);
}
}
#[cfg(any(test, feature = "test-support"))]
pub fn install_default_prettier(
&mut self,
_worktree: Option<WorktreeId>,
plugins: HashSet<&'static str>,
_cx: &mut ModelContext<Self>,
) {
// suppress unused code warnings
let _ = install_prettier_packages;
let _ = save_prettier_server_file;
self.default_prettier.installed_plugins.extend(plugins);
self.default_prettier.prettier = PrettierInstallation::Installed(PrettierInstance {
attempt: 0,
prettier: None,
});
}
#[cfg(not(any(test, feature = "test-support")))]
pub fn install_default_prettier(
&mut self,
worktree: Option<WorktreeId>,
mut new_plugins: HashSet<&'static str>,
cx: &mut ModelContext<Self>,
) {
let Some(node) = self.node.as_ref().cloned() else {
return;
};
log::info!("Initializing default prettier with plugins {new_plugins:?}");
let fs = Arc::clone(&self.fs);
let locate_prettier_installation = match worktree.and_then(|worktree_id| {
self.worktree_for_id(worktree_id, cx)
.map(|worktree| worktree.read(cx).abs_path())
}) {
Some(locate_from) => {
let installed_prettiers = self.prettier_instances.keys().cloned().collect();
cx.background().spawn(async move {
Prettier::locate_prettier_installation(
fs.as_ref(),
&installed_prettiers,
locate_from.as_ref(),
)
.await
})
}
None => Task::ready(Ok(ControlFlow::Continue(None))),
};
new_plugins.retain(|plugin| !self.default_prettier.installed_plugins.contains(plugin));
let mut installation_attempt = 0;
let previous_installation_task = match &mut self.default_prettier.prettier {
PrettierInstallation::NotInstalled {
installation_task,
attempts,
not_installed_plugins,
} => {
installation_attempt = *attempts;
if installation_attempt > prettier::FAIL_THRESHOLD {
*installation_task = None;
log::warn!(
"Default prettier installation had failed {installation_attempt} times, not attempting again",
);
return;
}
new_plugins.extend(not_installed_plugins.iter());
installation_task.clone()
}
PrettierInstallation::Installed { .. } => {
if new_plugins.is_empty() {
return;
}
None
}
};
let plugins_to_install = new_plugins.clone();
let fs = Arc::clone(&self.fs);
let new_installation_task = cx
.spawn(|project, mut cx| async move {
match locate_prettier_installation
.await
.context("locate prettier installation")
.map_err(Arc::new)?
{
ControlFlow::Break(()) => return Ok(()),
ControlFlow::Continue(prettier_path) => {
if prettier_path.is_some() {
new_plugins.clear();
}
let mut needs_install = false;
if let Some(previous_installation_task) = previous_installation_task {
if let Err(e) = previous_installation_task.await {
log::error!("Failed to install default prettier: {e:#}");
project.update(&mut cx, |project, _| {
if let PrettierInstallation::NotInstalled { attempts, not_installed_plugins, .. } = &mut project.default_prettier.prettier {
*attempts += 1;
new_plugins.extend(not_installed_plugins.iter());
installation_attempt = *attempts;
needs_install = true;
};
});
}
};
if installation_attempt > prettier::FAIL_THRESHOLD {
project.update(&mut cx, |project, _| {
if let PrettierInstallation::NotInstalled { installation_task, .. } = &mut project.default_prettier.prettier {
*installation_task = None;
};
});
log::warn!(
"Default prettier installation had failed {installation_attempt} times, not attempting again",
);
return Ok(());
}
project.update(&mut cx, |project, _| {
new_plugins.retain(|plugin| {
!project.default_prettier.installed_plugins.contains(plugin)
});
if let PrettierInstallation::NotInstalled { not_installed_plugins, .. } = &mut project.default_prettier.prettier {
not_installed_plugins.retain(|plugin| {
!project.default_prettier.installed_plugins.contains(plugin)
});
not_installed_plugins.extend(new_plugins.iter());
}
needs_install |= !new_plugins.is_empty();
});
if needs_install {
let installed_plugins = new_plugins.clone();
cx.background()
.spawn(async move {
save_prettier_server_file(fs.as_ref()).await?;
install_prettier_packages(new_plugins, node).await
})
.await
.context("prettier & plugins install")
.map_err(Arc::new)?;
log::info!("Initialized prettier with plugins: {installed_plugins:?}");
project.update(&mut cx, |project, _| {
project.default_prettier.prettier =
PrettierInstallation::Installed(PrettierInstance {
attempt: 0,
prettier: None,
});
project.default_prettier
.installed_plugins
.extend(installed_plugins);
});
}
}
}
Ok(())
})
.shared();
self.default_prettier.prettier = PrettierInstallation::NotInstalled {
attempts: installation_attempt,
installation_task: Some(new_installation_task),
not_installed_plugins: plugins_to_install,
};
}
}

View File

@ -1,5 +1,6 @@
mod ignore;
mod lsp_command;
mod prettier_support;
pub mod project_settings;
pub mod search;
pub mod terminals;
@ -20,7 +21,7 @@ use futures::{
mpsc::{self, UnboundedReceiver},
oneshot,
},
future::{self, try_join_all, Shared},
future::{try_join_all, Shared},
stream::FuturesUnordered,
AsyncWriteExt, Future, FutureExt, StreamExt, TryFutureExt,
};
@ -31,9 +32,7 @@ use gpui::{
};
use itertools::Itertools;
use language::{
language_settings::{
language_settings, FormatOnSave, Formatter, InlayHintKind, LanguageSettings,
},
language_settings::{language_settings, FormatOnSave, Formatter, InlayHintKind},
point_to_lsp,
proto::{
deserialize_anchor, deserialize_fingerprint, deserialize_line_ending, deserialize_version,
@ -54,7 +53,7 @@ use lsp_command::*;
use node_runtime::NodeRuntime;
use parking_lot::Mutex;
use postage::watch;
use prettier::Prettier;
use prettier_support::{DefaultPrettier, PrettierInstance};
use project_settings::{LspSettings, ProjectSettings};
use rand::prelude::*;
use search::SearchQuery;
@ -72,7 +71,7 @@ use std::{
hash::Hash,
mem,
num::NonZeroU32,
ops::{ControlFlow, Range},
ops::Range,
path::{self, Component, Path, PathBuf},
process::Stdio,
str,
@ -85,11 +84,8 @@ use std::{
use terminals::Terminals;
use text::Anchor;
use util::{
debug_panic, defer,
http::HttpClient,
merge_json_value_into,
paths::{DEFAULT_PRETTIER_DIR, LOCAL_SETTINGS_RELATIVE_PATH},
post_inc, ResultExt, TryFutureExt as _,
debug_panic, defer, http::HttpClient, merge_json_value_into,
paths::LOCAL_SETTINGS_RELATIVE_PATH, post_inc, ResultExt, TryFutureExt as _,
};
pub use fs::*;
@ -168,16 +164,9 @@ pub struct Project {
copilot_log_subscription: Option<lsp::Subscription>,
current_lsp_settings: HashMap<Arc<str>, LspSettings>,
node: Option<Arc<dyn NodeRuntime>>,
default_prettier: Option<DefaultPrettier>,
default_prettier: DefaultPrettier,
prettiers_per_worktree: HashMap<WorktreeId, HashSet<Option<PathBuf>>>,
prettier_instances: HashMap<PathBuf, Shared<Task<Result<Arc<Prettier>, Arc<anyhow::Error>>>>>,
}
struct DefaultPrettier {
instance: Option<Shared<Task<Result<Arc<Prettier>, Arc<anyhow::Error>>>>>,
installation_process: Option<Shared<Task<Result<(), Arc<anyhow::Error>>>>>,
#[cfg(not(any(test, feature = "test-support")))]
installed_plugins: HashSet<&'static str>,
prettier_instances: HashMap<PathBuf, PrettierInstance>,
}
struct DelayedDebounced {
@ -542,6 +531,14 @@ struct ProjectLspAdapterDelegate {
http_client: Arc<dyn HttpClient>,
}
// Currently, formatting operations are represented differently depending on
// whether they come from a language server or an external command.
enum FormatOperation {
Lsp(Vec<(Range<Anchor>, String)>),
External(Diff),
Prettier(Diff),
}
impl FormatTrigger {
fn from_proto(value: i32) -> FormatTrigger {
match value {
@ -690,7 +687,7 @@ impl Project {
copilot_log_subscription: None,
current_lsp_settings: settings::get::<ProjectSettings>(cx).lsp.clone(),
node: Some(node),
default_prettier: None,
default_prettier: DefaultPrettier::default(),
prettiers_per_worktree: HashMap::default(),
prettier_instances: HashMap::default(),
}
@ -791,7 +788,7 @@ impl Project {
copilot_log_subscription: None,
current_lsp_settings: settings::get::<ProjectSettings>(cx).lsp.clone(),
node: None,
default_prettier: None,
default_prettier: DefaultPrettier::default(),
prettiers_per_worktree: HashMap::default(),
prettier_instances: HashMap::default(),
};
@ -928,8 +925,19 @@ impl Project {
.detach();
}
let mut prettier_plugins_by_worktree = HashMap::default();
for (worktree, language, settings) in language_formatters_to_check {
self.install_default_formatters(worktree, &language, &settings, cx);
if let Some(plugins) =
prettier_support::prettier_plugins_for_language(&language, &settings)
{
prettier_plugins_by_worktree
.entry(worktree)
.or_insert_with(|| HashSet::default())
.extend(plugins);
}
}
for (worktree, prettier_plugins) in prettier_plugins_by_worktree {
self.install_default_prettier(worktree, prettier_plugins, cx);
}
// Start all the newly-enabled language servers.
@ -2633,8 +2641,9 @@ impl Project {
});
for (adapter, server) in servers {
let workspace_config =
cx.update(|cx| adapter.workspace_configuration(cx)).await;
let workspace_config = cx
.update(|cx| adapter.workspace_configuration(server.root_path(), cx))
.await;
server
.notify::<lsp::notification::DidChangeConfiguration>(
lsp::DidChangeConfigurationParams {
@ -2685,8 +2694,11 @@ impl Project {
let settings = language_settings(Some(&new_language), buffer_file.as_ref(), cx).clone();
let buffer_file = File::from_dyn(buffer_file.as_ref());
let worktree = buffer_file.as_ref().map(|f| f.worktree_id(cx));
self.install_default_formatters(worktree, &new_language, &settings, cx);
if let Some(prettier_plugins) =
prettier_support::prettier_plugins_for_language(&new_language, &settings)
{
self.install_default_prettier(worktree, prettier_plugins, cx);
};
if let Some(file) = buffer_file {
let worktree = file.worktree.clone();
if let Some(tree) = worktree.read(cx).as_local() {
@ -2742,7 +2754,7 @@ impl Project {
stderr_capture.clone(),
language.clone(),
adapter.clone(),
worktree_path,
Arc::clone(&worktree_path),
ProjectLspAdapterDelegate::new(self, cx),
cx,
) {
@ -2765,6 +2777,7 @@ impl Project {
cx.spawn_weak(|this, mut cx| async move {
let result = Self::setup_and_insert_language_server(
this,
&worktree_path,
override_options,
pending_server,
adapter.clone(),
@ -2880,6 +2893,7 @@ impl Project {
async fn setup_and_insert_language_server(
this: WeakModelHandle<Self>,
worktree_path: &Path,
override_initialization_options: Option<serde_json::Value>,
pending_server: PendingLanguageServer,
adapter: Arc<CachedLspAdapter>,
@ -2892,6 +2906,7 @@ impl Project {
this,
override_initialization_options,
pending_server,
worktree_path,
adapter.clone(),
server_id,
cx,
@ -2921,11 +2936,14 @@ impl Project {
this: WeakModelHandle<Self>,
override_options: Option<serde_json::Value>,
pending_server: PendingLanguageServer,
worktree_path: &Path,
adapter: Arc<CachedLspAdapter>,
server_id: LanguageServerId,
cx: &mut AsyncAppContext,
) -> Result<Arc<LanguageServer>> {
let workspace_config = cx.update(|cx| adapter.workspace_configuration(cx)).await;
let workspace_config = cx
.update(|cx| adapter.workspace_configuration(worktree_path, cx))
.await;
let language_server = pending_server.task.await?;
language_server
@ -2953,11 +2971,14 @@ impl Project {
language_server
.on_request::<lsp::request::WorkspaceConfiguration, _, _>({
let adapter = adapter.clone();
let worktree_path = worktree_path.to_path_buf();
move |params, mut cx| {
let adapter = adapter.clone();
let worktree_path = worktree_path.clone();
async move {
let workspace_config =
cx.update(|cx| adapter.workspace_configuration(cx)).await;
let workspace_config = cx
.update(|cx| adapter.workspace_configuration(&worktree_path, cx))
.await;
Ok(params
.items
.into_iter()
@ -4073,8 +4094,6 @@ impl Project {
let remove_trailing_whitespace = settings.remove_trailing_whitespace_on_save;
let ensure_final_newline = settings.ensure_final_newline_on_save;
let format_on_save = settings.format_on_save.clone();
let formatter = settings.formatter.clone();
let tab_size = settings.tab_size;
// First, format buffer's whitespace according to the settings.
@ -4099,18 +4118,10 @@ impl Project {
buffer.end_transaction(cx)
});
// Currently, formatting operations are represented differently depending on
// whether they come from a language server or an external command.
enum FormatOperation {
Lsp(Vec<(Range<Anchor>, String)>),
External(Diff),
Prettier(Diff),
}
// Apply language-specific formatting using either a language server
// or external command.
let mut format_operation = None;
match (formatter, format_on_save) {
match (&settings.formatter, &settings.format_on_save) {
(_, FormatOnSave::Off) if trigger == FormatTrigger::Save => {}
(Formatter::LanguageServer, FormatOnSave::On | FormatOnSave::Off)
@ -4155,46 +4166,11 @@ impl Project {
}
}
(Formatter::Auto, FormatOnSave::On | FormatOnSave::Off) => {
if let Some((prettier_path, prettier_task)) = project
.update(&mut cx, |project, cx| {
project.prettier_instance_for_buffer(buffer, cx)
}).await {
match prettier_task.await
{
Ok(prettier) => {
let buffer_path = buffer.update(&mut cx, |buffer, cx| {
File::from_dyn(buffer.file()).map(|file| file.abs_path(cx))
});
format_operation = Some(FormatOperation::Prettier(
prettier
.format(buffer, buffer_path, &cx)
.await
.context("formatting via prettier")?,
));
}
Err(e) => {
project.update(&mut cx, |project, _| {
match &prettier_path {
Some(prettier_path) => {
project.prettier_instances.remove(prettier_path);
},
None => {
if let Some(default_prettier) = project.default_prettier.as_mut() {
default_prettier.instance = None;
}
},
}
});
match &prettier_path {
Some(prettier_path) => {
log::error!("Failed to create prettier instance from {prettier_path:?} for buffer during autoformatting: {e:#}");
},
None => {
log::error!("Failed to create default prettier instance for buffer during autoformatting: {e:#}");
},
}
}
}
if let Some(new_operation) =
prettier_support::format_with_prettier(&project, buffer, &mut cx)
.await
{
format_operation = Some(new_operation);
} else if let Some((language_server, buffer_abs_path)) =
language_server.as_ref().zip(buffer_abs_path.as_ref())
{
@ -4212,48 +4188,13 @@ impl Project {
));
}
}
(Formatter::Prettier { .. }, FormatOnSave::On | FormatOnSave::Off) => {
if let Some((prettier_path, prettier_task)) = project
.update(&mut cx, |project, cx| {
project.prettier_instance_for_buffer(buffer, cx)
}).await {
match prettier_task.await
{
Ok(prettier) => {
let buffer_path = buffer.update(&mut cx, |buffer, cx| {
File::from_dyn(buffer.file()).map(|file| file.abs_path(cx))
});
format_operation = Some(FormatOperation::Prettier(
prettier
.format(buffer, buffer_path, &cx)
.await
.context("formatting via prettier")?,
));
}
Err(e) => {
project.update(&mut cx, |project, _| {
match &prettier_path {
Some(prettier_path) => {
project.prettier_instances.remove(prettier_path);
},
None => {
if let Some(default_prettier) = project.default_prettier.as_mut() {
default_prettier.instance = None;
}
},
}
});
match &prettier_path {
Some(prettier_path) => {
log::error!("Failed to create prettier instance from {prettier_path:?} for buffer during autoformatting: {e:#}");
},
None => {
log::error!("Failed to create default prettier instance for buffer during autoformatting: {e:#}");
},
}
}
}
}
(Formatter::Prettier, FormatOnSave::On | FormatOnSave::Off) => {
if let Some(new_operation) =
prettier_support::format_with_prettier(&project, buffer, &mut cx)
.await
{
format_operation = Some(new_operation);
}
}
};
@ -6566,85 +6507,6 @@ impl Project {
.detach();
}
fn update_prettier_settings(
&self,
worktree: &ModelHandle<Worktree>,
changes: &[(Arc<Path>, ProjectEntryId, PathChange)],
cx: &mut ModelContext<'_, Project>,
) {
let prettier_config_files = Prettier::CONFIG_FILE_NAMES
.iter()
.map(Path::new)
.collect::<HashSet<_>>();
let prettier_config_file_changed = changes
.iter()
.filter(|(_, _, change)| !matches!(change, PathChange::Loaded))
.filter(|(path, _, _)| {
!path
.components()
.any(|component| component.as_os_str().to_string_lossy() == "node_modules")
})
.find(|(path, _, _)| prettier_config_files.contains(path.as_ref()));
let current_worktree_id = worktree.read(cx).id();
if let Some((config_path, _, _)) = prettier_config_file_changed {
log::info!(
"Prettier config file {config_path:?} changed, reloading prettier instances for worktree {current_worktree_id}"
);
let prettiers_to_reload = self
.prettiers_per_worktree
.get(&current_worktree_id)
.iter()
.flat_map(|prettier_paths| prettier_paths.iter())
.flatten()
.filter_map(|prettier_path| {
Some((
current_worktree_id,
Some(prettier_path.clone()),
self.prettier_instances.get(prettier_path)?.clone(),
))
})
.chain(self.default_prettier.iter().filter_map(|default_prettier| {
Some((
current_worktree_id,
None,
default_prettier.instance.clone()?,
))
}))
.collect::<Vec<_>>();
cx.background()
.spawn(async move {
for task_result in future::join_all(prettiers_to_reload.into_iter().map(|(worktree_id, prettier_path, prettier_task)| {
async move {
prettier_task.await?
.clear_cache()
.await
.with_context(|| {
match prettier_path {
Some(prettier_path) => format!(
"clearing prettier {prettier_path:?} cache for worktree {worktree_id:?} on prettier settings update"
),
None => format!(
"clearing default prettier cache for worktree {worktree_id:?} on prettier settings update"
),
}
})
.map_err(Arc::new)
}
}))
.await
{
if let Err(e) = task_result {
log::error!("Failed to clear cache for prettier: {e:#}");
}
}
})
.detach();
}
}
pub fn set_active_path(&mut self, entry: Option<ProjectPath>, cx: &mut ModelContext<Self>) {
let new_active_entry = entry.and_then(|project_path| {
let worktree = self.worktree_for_id(project_path.worktree_id, cx)?;
@ -6671,9 +6533,15 @@ impl Project {
})
}
pub fn diagnostic_summary(&self, cx: &AppContext) -> DiagnosticSummary {
pub fn diagnostic_summary(&self, include_ignored: bool, cx: &AppContext) -> DiagnosticSummary {
let mut summary = DiagnosticSummary::default();
for (_, _, path_summary) in self.diagnostic_summaries(cx) {
for (_, _, path_summary) in
self.diagnostic_summaries(include_ignored, cx)
.filter(|(path, _, _)| {
let worktree = self.entry_for_path(&path, cx).map(|entry| entry.is_ignored);
include_ignored || worktree == Some(false)
})
{
summary.error_count += path_summary.error_count;
summary.warning_count += path_summary.warning_count;
}
@ -6682,6 +6550,7 @@ impl Project {
pub fn diagnostic_summaries<'a>(
&'a self,
include_ignored: bool,
cx: &'a AppContext,
) -> impl Iterator<Item = (ProjectPath, LanguageServerId, DiagnosticSummary)> + 'a {
self.visible_worktrees(cx).flat_map(move |worktree| {
@ -6692,6 +6561,10 @@ impl Project {
.map(move |(path, server_id, summary)| {
(ProjectPath { worktree_id, path }, server_id, summary)
})
.filter(move |(path, _, _)| {
let worktree = self.entry_for_path(&path, cx).map(|entry| entry.is_ignored);
include_ignored || worktree == Some(false)
})
})
}
@ -8536,446 +8409,6 @@ impl Project {
Vec::new()
}
}
fn prettier_instance_for_buffer(
&mut self,
buffer: &ModelHandle<Buffer>,
cx: &mut ModelContext<Self>,
) -> Task<
Option<(
Option<PathBuf>,
Shared<Task<Result<Arc<Prettier>, Arc<anyhow::Error>>>>,
)>,
> {
let buffer = buffer.read(cx);
let buffer_file = buffer.file();
let Some(buffer_language) = buffer.language() else {
return Task::ready(None);
};
if buffer_language.prettier_parser_name().is_none() {
return Task::ready(None);
}
if self.is_local() {
let Some(node) = self.node.as_ref().map(Arc::clone) else {
return Task::ready(None);
};
match File::from_dyn(buffer_file).map(|file| (file.worktree_id(cx), file.abs_path(cx)))
{
Some((worktree_id, buffer_path)) => {
let fs = Arc::clone(&self.fs);
let installed_prettiers = self.prettier_instances.keys().cloned().collect();
return cx.spawn(|project, mut cx| async move {
match cx
.background()
.spawn(async move {
Prettier::locate_prettier_installation(
fs.as_ref(),
&installed_prettiers,
&buffer_path,
)
.await
})
.await
{
Ok(ControlFlow::Break(())) => {
return None;
}
Ok(ControlFlow::Continue(None)) => {
let started_default_prettier =
project.update(&mut cx, |project, _| {
project
.prettiers_per_worktree
.entry(worktree_id)
.or_default()
.insert(None);
project.default_prettier.as_ref().and_then(
|default_prettier| default_prettier.instance.clone(),
)
});
match started_default_prettier {
Some(old_task) => return Some((None, old_task)),
None => {
let new_default_prettier = project
.update(&mut cx, |_, cx| {
start_default_prettier(node, Some(worktree_id), cx)
})
.await;
return Some((None, new_default_prettier));
}
}
}
Ok(ControlFlow::Continue(Some(prettier_dir))) => {
project.update(&mut cx, |project, _| {
project
.prettiers_per_worktree
.entry(worktree_id)
.or_default()
.insert(Some(prettier_dir.clone()))
});
if let Some(existing_prettier) =
project.update(&mut cx, |project, _| {
project.prettier_instances.get(&prettier_dir).cloned()
})
{
log::debug!(
"Found already started prettier in {prettier_dir:?}"
);
return Some((Some(prettier_dir), existing_prettier));
}
log::info!("Found prettier in {prettier_dir:?}, starting.");
let new_prettier_task = project.update(&mut cx, |project, cx| {
let new_prettier_task = start_prettier(
node,
prettier_dir.clone(),
Some(worktree_id),
cx,
);
project
.prettier_instances
.insert(prettier_dir.clone(), new_prettier_task.clone());
new_prettier_task
});
Some((Some(prettier_dir), new_prettier_task))
}
Err(e) => {
return Some((
None,
Task::ready(Err(Arc::new(
e.context("determining prettier path"),
)))
.shared(),
));
}
}
});
}
None => {
let started_default_prettier = self
.default_prettier
.as_ref()
.and_then(|default_prettier| default_prettier.instance.clone());
match started_default_prettier {
Some(old_task) => return Task::ready(Some((None, old_task))),
None => {
let new_task = start_default_prettier(node, None, cx);
return cx.spawn(|_, _| async move { Some((None, new_task.await)) });
}
}
}
}
} else if self.remote_id().is_some() {
return Task::ready(None);
} else {
Task::ready(Some((
None,
Task::ready(Err(Arc::new(anyhow!("project does not have a remote id")))).shared(),
)))
}
}
#[cfg(any(test, feature = "test-support"))]
fn install_default_formatters(
&mut self,
_worktree: Option<WorktreeId>,
_new_language: &Language,
_language_settings: &LanguageSettings,
_cx: &mut ModelContext<Self>,
) {
}
#[cfg(not(any(test, feature = "test-support")))]
fn install_default_formatters(
&mut self,
worktree: Option<WorktreeId>,
new_language: &Language,
language_settings: &LanguageSettings,
cx: &mut ModelContext<Self>,
) {
match &language_settings.formatter {
Formatter::Prettier { .. } | Formatter::Auto => {}
Formatter::LanguageServer | Formatter::External { .. } => return,
};
let Some(node) = self.node.as_ref().cloned() else {
return;
};
let mut prettier_plugins = None;
if new_language.prettier_parser_name().is_some() {
prettier_plugins
.get_or_insert_with(|| HashSet::<&'static str>::default())
.extend(
new_language
.lsp_adapters()
.iter()
.flat_map(|adapter| adapter.prettier_plugins()),
)
}
let Some(prettier_plugins) = prettier_plugins else {
return;
};
let fs = Arc::clone(&self.fs);
let locate_prettier_installation = match worktree.and_then(|worktree_id| {
self.worktree_for_id(worktree_id, cx)
.map(|worktree| worktree.read(cx).abs_path())
}) {
Some(locate_from) => {
let installed_prettiers = self.prettier_instances.keys().cloned().collect();
cx.background().spawn(async move {
Prettier::locate_prettier_installation(
fs.as_ref(),
&installed_prettiers,
locate_from.as_ref(),
)
.await
})
}
None => Task::ready(Ok(ControlFlow::Break(()))),
};
let mut plugins_to_install = prettier_plugins;
let previous_installation_process =
if let Some(default_prettier) = &mut self.default_prettier {
plugins_to_install
.retain(|plugin| !default_prettier.installed_plugins.contains(plugin));
if plugins_to_install.is_empty() {
return;
}
default_prettier.installation_process.clone()
} else {
None
};
let fs = Arc::clone(&self.fs);
let default_prettier = self
.default_prettier
.get_or_insert_with(|| DefaultPrettier {
instance: None,
installation_process: None,
installed_plugins: HashSet::default(),
});
default_prettier.installation_process = Some(
cx.spawn(|this, mut cx| async move {
match locate_prettier_installation
.await
.context("locate prettier installation")
.map_err(Arc::new)?
{
ControlFlow::Break(()) => return Ok(()),
ControlFlow::Continue(Some(_non_default_prettier)) => return Ok(()),
ControlFlow::Continue(None) => {
let mut needs_install = match previous_installation_process {
Some(previous_installation_process) => {
previous_installation_process.await.is_err()
}
None => true,
};
this.update(&mut cx, |this, _| {
if let Some(default_prettier) = &mut this.default_prettier {
plugins_to_install.retain(|plugin| {
!default_prettier.installed_plugins.contains(plugin)
});
needs_install |= !plugins_to_install.is_empty();
}
});
if needs_install {
let installed_plugins = plugins_to_install.clone();
cx.background()
.spawn(async move {
install_default_prettier(plugins_to_install, node, fs).await
})
.await
.context("prettier & plugins install")
.map_err(Arc::new)?;
this.update(&mut cx, |this, _| {
let default_prettier =
this.default_prettier
.get_or_insert_with(|| DefaultPrettier {
instance: None,
installation_process: Some(
Task::ready(Ok(())).shared(),
),
installed_plugins: HashSet::default(),
});
default_prettier.instance = None;
default_prettier.installed_plugins.extend(installed_plugins);
});
}
}
}
Ok(())
})
.shared(),
);
}
}
fn start_default_prettier(
node: Arc<dyn NodeRuntime>,
worktree_id: Option<WorktreeId>,
cx: &mut ModelContext<'_, Project>,
) -> Task<Shared<Task<Result<Arc<Prettier>, Arc<anyhow::Error>>>>> {
cx.spawn(|project, mut cx| async move {
loop {
let default_prettier_installing = project.update(&mut cx, |project, _| {
project
.default_prettier
.as_ref()
.and_then(|default_prettier| default_prettier.installation_process.clone())
});
match default_prettier_installing {
Some(installation_task) => {
if installation_task.await.is_ok() {
break;
}
}
None => break,
}
}
project.update(&mut cx, |project, cx| {
match project
.default_prettier
.as_mut()
.and_then(|default_prettier| default_prettier.instance.as_mut())
{
Some(default_prettier) => default_prettier.clone(),
None => {
let new_default_prettier =
start_prettier(node, DEFAULT_PRETTIER_DIR.clone(), worktree_id, cx);
project
.default_prettier
.get_or_insert_with(|| DefaultPrettier {
instance: None,
installation_process: None,
#[cfg(not(any(test, feature = "test-support")))]
installed_plugins: HashSet::default(),
})
.instance = Some(new_default_prettier.clone());
new_default_prettier
}
}
})
})
}
fn start_prettier(
node: Arc<dyn NodeRuntime>,
prettier_dir: PathBuf,
worktree_id: Option<WorktreeId>,
cx: &mut ModelContext<'_, Project>,
) -> Shared<Task<Result<Arc<Prettier>, Arc<anyhow::Error>>>> {
cx.spawn(|project, mut cx| async move {
let new_server_id = project.update(&mut cx, |project, _| {
project.languages.next_language_server_id()
});
let new_prettier = Prettier::start(new_server_id, prettier_dir, node, cx.clone())
.await
.context("default prettier spawn")
.map(Arc::new)
.map_err(Arc::new)?;
register_new_prettier(&project, &new_prettier, worktree_id, new_server_id, &mut cx);
Ok(new_prettier)
})
.shared()
}
fn register_new_prettier(
project: &ModelHandle<Project>,
prettier: &Prettier,
worktree_id: Option<WorktreeId>,
new_server_id: LanguageServerId,
cx: &mut AsyncAppContext,
) {
let prettier_dir = prettier.prettier_dir();
let is_default = prettier.is_default();
if is_default {
log::info!("Started default prettier in {prettier_dir:?}");
} else {
log::info!("Started prettier in {prettier_dir:?}");
}
if let Some(prettier_server) = prettier.server() {
project.update(cx, |project, cx| {
let name = if is_default {
LanguageServerName(Arc::from("prettier (default)"))
} else {
let worktree_path = worktree_id
.and_then(|id| project.worktree_for_id(id, cx))
.map(|worktree| worktree.update(cx, |worktree, _| worktree.abs_path()));
let name = match worktree_path {
Some(worktree_path) => {
if prettier_dir == worktree_path.as_ref() {
let name = prettier_dir
.file_name()
.and_then(|name| name.to_str())
.unwrap_or_default();
format!("prettier ({name})")
} else {
let dir_to_display = prettier_dir
.strip_prefix(worktree_path.as_ref())
.ok()
.unwrap_or(prettier_dir);
format!("prettier ({})", dir_to_display.display())
}
}
None => format!("prettier ({})", prettier_dir.display()),
};
LanguageServerName(Arc::from(name))
};
project
.supplementary_language_servers
.insert(new_server_id, (name, Arc::clone(prettier_server)));
cx.emit(Event::LanguageServerAdded(new_server_id));
});
}
}
#[cfg(not(any(test, feature = "test-support")))]
async fn install_default_prettier(
plugins_to_install: HashSet<&'static str>,
node: Arc<dyn NodeRuntime>,
fs: Arc<dyn Fs>,
) -> anyhow::Result<()> {
let prettier_wrapper_path = DEFAULT_PRETTIER_DIR.join(prettier::PRETTIER_SERVER_FILE);
// method creates parent directory if it doesn't exist
fs.save(
&prettier_wrapper_path,
&text::Rope::from(prettier::PRETTIER_SERVER_JS),
text::LineEnding::Unix,
)
.await
.with_context(|| {
format!(
"writing {} file at {prettier_wrapper_path:?}",
prettier::PRETTIER_SERVER_FILE
)
})?;
let packages_to_versions =
future::try_join_all(plugins_to_install.iter().chain(Some(&"prettier")).map(
|package_name| async {
let returned_package_name = package_name.to_string();
let latest_version = node
.npm_package_latest_version(package_name)
.await
.with_context(|| {
format!("fetching latest npm version for package {returned_package_name}")
})?;
anyhow::Ok((returned_package_name, latest_version))
},
))
.await
.context("fetching latest npm versions")?;
log::info!("Fetching default prettier and plugins: {packages_to_versions:?}");
let borrowed_packages = packages_to_versions
.iter()
.map(|(package, version)| (package.as_str(), version.as_str()))
.collect::<Vec<_>>();
node.npm_install_packages(DEFAULT_PRETTIER_DIR.as_path(), &borrowed_packages)
.await
.context("fetching formatter packages")?;
anyhow::Ok(())
}
fn subscribe_for_copilot_events(

View File

@ -806,7 +806,7 @@ async fn test_single_file_worktrees_diagnostics(cx: &mut gpui::TestAppContext) {
}
#[gpui::test]
async fn test_hidden_worktrees_diagnostics(cx: &mut gpui::TestAppContext) {
async fn test_omitted_diagnostics(cx: &mut gpui::TestAppContext) {
init_test(cx);
let fs = FakeFs::new(cx.background());
@ -814,7 +814,12 @@ async fn test_hidden_worktrees_diagnostics(cx: &mut gpui::TestAppContext) {
"/root",
json!({
"dir": {
".git": {
"HEAD": "ref: refs/heads/main",
},
".gitignore": "b.rs",
"a.rs": "let a = 1;",
"b.rs": "let b = 2;",
},
"other.rs": "let b = c;"
}),
@ -822,6 +827,13 @@ async fn test_hidden_worktrees_diagnostics(cx: &mut gpui::TestAppContext) {
.await;
let project = Project::test(fs, ["/root/dir".as_ref()], cx).await;
let (worktree, _) = project
.update(cx, |project, cx| {
project.find_or_create_local_worktree("/root/dir", true, cx)
})
.await
.unwrap();
let main_worktree_id = worktree.read_with(cx, |tree, _| tree.id());
let (worktree, _) = project
.update(cx, |project, cx| {
@ -829,12 +841,30 @@ async fn test_hidden_worktrees_diagnostics(cx: &mut gpui::TestAppContext) {
})
.await
.unwrap();
let worktree_id = worktree.read_with(cx, |tree, _| tree.id());
let other_worktree_id = worktree.read_with(cx, |tree, _| tree.id());
let server_id = LanguageServerId(0);
project.update(cx, |project, cx| {
project
.update_diagnostics(
LanguageServerId(0),
server_id,
lsp::PublishDiagnosticsParams {
uri: Url::from_file_path("/root/dir/b.rs").unwrap(),
version: None,
diagnostics: vec![lsp::Diagnostic {
range: lsp::Range::new(lsp::Position::new(0, 4), lsp::Position::new(0, 5)),
severity: Some(lsp::DiagnosticSeverity::ERROR),
message: "unused variable 'b'".to_string(),
..Default::default()
}],
},
&[],
cx,
)
.unwrap();
project
.update_diagnostics(
server_id,
lsp::PublishDiagnosticsParams {
uri: Url::from_file_path("/root/other.rs").unwrap(),
version: None,
@ -851,11 +881,34 @@ async fn test_hidden_worktrees_diagnostics(cx: &mut gpui::TestAppContext) {
.unwrap();
});
let buffer = project
.update(cx, |project, cx| project.open_buffer((worktree_id, ""), cx))
let main_ignored_buffer = project
.update(cx, |project, cx| {
project.open_buffer((main_worktree_id, "b.rs"), cx)
})
.await
.unwrap();
buffer.read_with(cx, |buffer, _| {
main_ignored_buffer.read_with(cx, |buffer, _| {
let chunks = chunks_with_diagnostics(buffer, 0..buffer.len());
assert_eq!(
chunks
.iter()
.map(|(s, d)| (s.as_str(), *d))
.collect::<Vec<_>>(),
&[
("let ", None),
("b", Some(DiagnosticSeverity::ERROR)),
(" = 2;", None),
],
"Gigitnored buffers should still get in-buffer diagnostics",
);
});
let other_buffer = project
.update(cx, |project, cx| {
project.open_buffer((other_worktree_id, ""), cx)
})
.await
.unwrap();
other_buffer.read_with(cx, |buffer, _| {
let chunks = chunks_with_diagnostics(buffer, 0..buffer.len());
assert_eq!(
chunks
@ -866,13 +919,29 @@ async fn test_hidden_worktrees_diagnostics(cx: &mut gpui::TestAppContext) {
("let b = ", None),
("c", Some(DiagnosticSeverity::ERROR)),
(";", None),
]
],
"Buffers from hidden projects should still get in-buffer diagnostics"
);
});
project.read_with(cx, |project, cx| {
assert_eq!(project.diagnostic_summaries(cx).next(), None);
assert_eq!(project.diagnostic_summary(cx).error_count, 0);
assert_eq!(project.diagnostic_summaries(false, cx).next(), None);
assert_eq!(
project.diagnostic_summaries(true, cx).collect::<Vec<_>>(),
vec![(
ProjectPath {
worktree_id: main_worktree_id,
path: Arc::from(Path::new("b.rs")),
},
server_id,
DiagnosticSummary {
error_count: 1,
warning_count: 0,
}
)]
);
assert_eq!(project.diagnostic_summary(false, cx).error_count, 0);
assert_eq!(project.diagnostic_summary(true, cx).error_count, 1);
});
}
@ -1145,7 +1214,7 @@ async fn test_restarting_server_with_diagnostics_published(cx: &mut gpui::TestAp
});
project.read_with(cx, |project, cx| {
assert_eq!(
project.diagnostic_summary(cx),
project.diagnostic_summary(false, cx),
DiagnosticSummary {
error_count: 1,
warning_count: 0,
@ -1171,7 +1240,7 @@ async fn test_restarting_server_with_diagnostics_published(cx: &mut gpui::TestAp
});
project.read_with(cx, |project, cx| {
assert_eq!(
project.diagnostic_summary(cx),
project.diagnostic_summary(false, cx),
DiagnosticSummary {
error_count: 0,
warning_count: 0,
@ -1763,7 +1832,7 @@ async fn test_diagnostics_from_multiple_language_servers(cx: &mut gpui::TestAppC
.unwrap();
assert_eq!(
project.diagnostic_summary(cx),
project.diagnostic_summary(false, cx),
DiagnosticSummary {
error_count: 2,
warning_count: 0,

View File

@ -717,8 +717,9 @@ async fn location_links_from_lsp(
})?
.await?;
buffer.update(&mut cx, |origin_buffer, cx| {
cx.update(|cx| {
let origin_location = origin_range.map(|origin_range| {
let origin_buffer = buffer.read(cx);
let origin_start =
origin_buffer.clip_point_utf16(point_from_lsp(origin_range.start), Bias::Left);
let origin_end =

View File

@ -0,0 +1,772 @@
use std::{
ops::ControlFlow,
path::{Path, PathBuf},
sync::Arc,
};
use anyhow::Context;
use collections::HashSet;
use fs::Fs;
use futures::{
future::{self, Shared},
FutureExt,
};
use gpui::{AsyncAppContext, Model, ModelContext, Task, WeakModel};
use language::{
language_settings::{Formatter, LanguageSettings},
Buffer, Language, LanguageServerName, LocalFile,
};
use lsp::LanguageServerId;
use node_runtime::NodeRuntime;
use prettier::Prettier;
use util::{paths::DEFAULT_PRETTIER_DIR, ResultExt, TryFutureExt};
use crate::{
Event, File, FormatOperation, PathChange, Project, ProjectEntryId, Worktree, WorktreeId,
};
pub fn prettier_plugins_for_language(
language: &Language,
language_settings: &LanguageSettings,
) -> Option<HashSet<&'static str>> {
match &language_settings.formatter {
Formatter::Prettier { .. } | Formatter::Auto => {}
Formatter::LanguageServer | Formatter::External { .. } => return None,
};
let mut prettier_plugins = None;
if language.prettier_parser_name().is_some() {
prettier_plugins
.get_or_insert_with(|| HashSet::default())
.extend(
language
.lsp_adapters()
.iter()
.flat_map(|adapter| adapter.prettier_plugins()),
)
}
prettier_plugins
}
pub(super) async fn format_with_prettier(
project: &WeakModel<Project>,
buffer: &Model<Buffer>,
cx: &mut AsyncAppContext,
) -> Option<FormatOperation> {
if let Some((prettier_path, prettier_task)) = project
.update(cx, |project, cx| {
project.prettier_instance_for_buffer(buffer, cx)
})
.ok()?
.await
{
match prettier_task.await {
Ok(prettier) => {
let buffer_path = buffer
.update(cx, |buffer, cx| {
File::from_dyn(buffer.file()).map(|file| file.abs_path(cx))
})
.ok()?;
match prettier.format(buffer, buffer_path, cx).await {
Ok(new_diff) => return Some(FormatOperation::Prettier(new_diff)),
Err(e) => {
log::error!(
"Prettier instance from {prettier_path:?} failed to format a buffer: {e:#}"
);
}
}
}
Err(e) => project
.update(cx, |project, _| {
let instance_to_update = match prettier_path {
Some(prettier_path) => {
log::error!(
"Prettier instance from path {prettier_path:?} failed to spawn: {e:#}"
);
project.prettier_instances.get_mut(&prettier_path)
}
None => {
log::error!("Default prettier instance failed to spawn: {e:#}");
match &mut project.default_prettier.prettier {
PrettierInstallation::NotInstalled { .. } => None,
PrettierInstallation::Installed(instance) => Some(instance),
}
}
};
if let Some(instance) = instance_to_update {
instance.attempt += 1;
instance.prettier = None;
}
})
.ok()?,
}
}
None
}
pub struct DefaultPrettier {
prettier: PrettierInstallation,
installed_plugins: HashSet<&'static str>,
}
pub enum PrettierInstallation {
NotInstalled {
attempts: usize,
installation_task: Option<Shared<Task<Result<(), Arc<anyhow::Error>>>>>,
not_installed_plugins: HashSet<&'static str>,
},
Installed(PrettierInstance),
}
pub type PrettierTask = Shared<Task<Result<Arc<Prettier>, Arc<anyhow::Error>>>>;
#[derive(Clone)]
pub struct PrettierInstance {
attempt: usize,
prettier: Option<PrettierTask>,
}
impl Default for DefaultPrettier {
fn default() -> Self {
Self {
prettier: PrettierInstallation::NotInstalled {
attempts: 0,
installation_task: None,
not_installed_plugins: HashSet::default(),
},
installed_plugins: HashSet::default(),
}
}
}
impl DefaultPrettier {
pub fn instance(&self) -> Option<&PrettierInstance> {
if let PrettierInstallation::Installed(instance) = &self.prettier {
Some(instance)
} else {
None
}
}
pub fn prettier_task(
&mut self,
node: &Arc<dyn NodeRuntime>,
worktree_id: Option<WorktreeId>,
cx: &mut ModelContext<'_, Project>,
) -> Option<Task<anyhow::Result<PrettierTask>>> {
match &mut self.prettier {
PrettierInstallation::NotInstalled { .. } => {
Some(start_default_prettier(Arc::clone(node), worktree_id, cx))
}
PrettierInstallation::Installed(existing_instance) => {
existing_instance.prettier_task(node, None, worktree_id, cx)
}
}
}
}
impl PrettierInstance {
pub fn prettier_task(
&mut self,
node: &Arc<dyn NodeRuntime>,
prettier_dir: Option<&Path>,
worktree_id: Option<WorktreeId>,
cx: &mut ModelContext<'_, Project>,
) -> Option<Task<anyhow::Result<PrettierTask>>> {
if self.attempt > prettier::FAIL_THRESHOLD {
match prettier_dir {
Some(prettier_dir) => log::warn!(
"Prettier from path {prettier_dir:?} exceeded launch threshold, not starting"
),
None => log::warn!("Default prettier exceeded launch threshold, not starting"),
}
return None;
}
Some(match &self.prettier {
Some(prettier_task) => Task::ready(Ok(prettier_task.clone())),
None => match prettier_dir {
Some(prettier_dir) => {
let new_task = start_prettier(
Arc::clone(node),
prettier_dir.to_path_buf(),
worktree_id,
cx,
);
self.attempt += 1;
self.prettier = Some(new_task.clone());
Task::ready(Ok(new_task))
}
None => {
self.attempt += 1;
let node = Arc::clone(node);
cx.spawn(|project, mut cx| async move {
project
.update(&mut cx, |_, cx| {
start_default_prettier(node, worktree_id, cx)
})?
.await
})
}
},
})
}
}
fn start_default_prettier(
node: Arc<dyn NodeRuntime>,
worktree_id: Option<WorktreeId>,
cx: &mut ModelContext<'_, Project>,
) -> Task<anyhow::Result<PrettierTask>> {
cx.spawn(|project, mut cx| async move {
loop {
let installation_task = project.update(&mut cx, |project, _| {
match &project.default_prettier.prettier {
PrettierInstallation::NotInstalled {
installation_task, ..
} => ControlFlow::Continue(installation_task.clone()),
PrettierInstallation::Installed(default_prettier) => {
ControlFlow::Break(default_prettier.clone())
}
}
})?;
match installation_task {
ControlFlow::Continue(None) => {
anyhow::bail!("Default prettier is not installed and cannot be started")
}
ControlFlow::Continue(Some(installation_task)) => {
log::info!("Waiting for default prettier to install");
if let Err(e) = installation_task.await {
project.update(&mut cx, |project, _| {
if let PrettierInstallation::NotInstalled {
installation_task,
attempts,
..
} = &mut project.default_prettier.prettier
{
*installation_task = None;
*attempts += 1;
}
})?;
anyhow::bail!(
"Cannot start default prettier due to its installation failure: {e:#}"
);
}
let new_default_prettier = project.update(&mut cx, |project, cx| {
let new_default_prettier =
start_prettier(node, DEFAULT_PRETTIER_DIR.clone(), worktree_id, cx);
project.default_prettier.prettier =
PrettierInstallation::Installed(PrettierInstance {
attempt: 0,
prettier: Some(new_default_prettier.clone()),
});
new_default_prettier
})?;
return Ok(new_default_prettier);
}
ControlFlow::Break(instance) => match instance.prettier {
Some(instance) => return Ok(instance),
None => {
let new_default_prettier = project.update(&mut cx, |project, cx| {
let new_default_prettier =
start_prettier(node, DEFAULT_PRETTIER_DIR.clone(), worktree_id, cx);
project.default_prettier.prettier =
PrettierInstallation::Installed(PrettierInstance {
attempt: instance.attempt + 1,
prettier: Some(new_default_prettier.clone()),
});
new_default_prettier
})?;
return Ok(new_default_prettier);
}
},
}
}
})
}
fn start_prettier(
node: Arc<dyn NodeRuntime>,
prettier_dir: PathBuf,
worktree_id: Option<WorktreeId>,
cx: &mut ModelContext<'_, Project>,
) -> PrettierTask {
cx.spawn(|project, mut cx| async move {
log::info!("Starting prettier at path {prettier_dir:?}");
let new_server_id = project.update(&mut cx, |project, _| {
project.languages.next_language_server_id()
})?;
let new_prettier = Prettier::start(new_server_id, prettier_dir, node, cx.clone())
.await
.context("default prettier spawn")
.map(Arc::new)
.map_err(Arc::new)?;
register_new_prettier(&project, &new_prettier, worktree_id, new_server_id, &mut cx);
Ok(new_prettier)
})
.shared()
}
fn register_new_prettier(
project: &WeakModel<Project>,
prettier: &Prettier,
worktree_id: Option<WorktreeId>,
new_server_id: LanguageServerId,
cx: &mut AsyncAppContext,
) {
let prettier_dir = prettier.prettier_dir();
let is_default = prettier.is_default();
if is_default {
log::info!("Started default prettier in {prettier_dir:?}");
} else {
log::info!("Started prettier in {prettier_dir:?}");
}
if let Some(prettier_server) = prettier.server() {
project
.update(cx, |project, cx| {
let name = if is_default {
LanguageServerName(Arc::from("prettier (default)"))
} else {
let worktree_path = worktree_id
.and_then(|id| project.worktree_for_id(id, cx))
.map(|worktree| worktree.update(cx, |worktree, _| worktree.abs_path()));
let name = match worktree_path {
Some(worktree_path) => {
if prettier_dir == worktree_path.as_ref() {
let name = prettier_dir
.file_name()
.and_then(|name| name.to_str())
.unwrap_or_default();
format!("prettier ({name})")
} else {
let dir_to_display = prettier_dir
.strip_prefix(worktree_path.as_ref())
.ok()
.unwrap_or(prettier_dir);
format!("prettier ({})", dir_to_display.display())
}
}
None => format!("prettier ({})", prettier_dir.display()),
};
LanguageServerName(Arc::from(name))
};
project
.supplementary_language_servers
.insert(new_server_id, (name, Arc::clone(prettier_server)));
cx.emit(Event::LanguageServerAdded(new_server_id));
})
.ok();
}
}
async fn install_prettier_packages(
plugins_to_install: HashSet<&'static str>,
node: Arc<dyn NodeRuntime>,
) -> anyhow::Result<()> {
let packages_to_versions =
future::try_join_all(plugins_to_install.iter().chain(Some(&"prettier")).map(
|package_name| async {
let returned_package_name = package_name.to_string();
let latest_version = node
.npm_package_latest_version(package_name)
.await
.with_context(|| {
format!("fetching latest npm version for package {returned_package_name}")
})?;
anyhow::Ok((returned_package_name, latest_version))
},
))
.await
.context("fetching latest npm versions")?;
log::info!("Fetching default prettier and plugins: {packages_to_versions:?}");
let borrowed_packages = packages_to_versions
.iter()
.map(|(package, version)| (package.as_str(), version.as_str()))
.collect::<Vec<_>>();
node.npm_install_packages(DEFAULT_PRETTIER_DIR.as_path(), &borrowed_packages)
.await
.context("fetching formatter packages")?;
anyhow::Ok(())
}
async fn save_prettier_server_file(fs: &dyn Fs) -> Result<(), anyhow::Error> {
let prettier_wrapper_path = DEFAULT_PRETTIER_DIR.join(prettier::PRETTIER_SERVER_FILE);
fs.save(
&prettier_wrapper_path,
&text::Rope::from(prettier::PRETTIER_SERVER_JS),
text::LineEnding::Unix,
)
.await
.with_context(|| {
format!(
"writing {} file at {prettier_wrapper_path:?}",
prettier::PRETTIER_SERVER_FILE
)
})?;
Ok(())
}
impl Project {
pub fn update_prettier_settings(
&self,
worktree: &Model<Worktree>,
changes: &[(Arc<Path>, ProjectEntryId, PathChange)],
cx: &mut ModelContext<'_, Project>,
) {
let prettier_config_files = Prettier::CONFIG_FILE_NAMES
.iter()
.map(Path::new)
.collect::<HashSet<_>>();
let prettier_config_file_changed = changes
.iter()
.filter(|(_, _, change)| !matches!(change, PathChange::Loaded))
.filter(|(path, _, _)| {
!path
.components()
.any(|component| component.as_os_str().to_string_lossy() == "node_modules")
})
.find(|(path, _, _)| prettier_config_files.contains(path.as_ref()));
let current_worktree_id = worktree.read(cx).id();
if let Some((config_path, _, _)) = prettier_config_file_changed {
log::info!(
"Prettier config file {config_path:?} changed, reloading prettier instances for worktree {current_worktree_id}"
);
let prettiers_to_reload =
self.prettiers_per_worktree
.get(&current_worktree_id)
.iter()
.flat_map(|prettier_paths| prettier_paths.iter())
.flatten()
.filter_map(|prettier_path| {
Some((
current_worktree_id,
Some(prettier_path.clone()),
self.prettier_instances.get(prettier_path)?.clone(),
))
})
.chain(self.default_prettier.instance().map(|default_prettier| {
(current_worktree_id, None, default_prettier.clone())
}))
.collect::<Vec<_>>();
cx.background_executor()
.spawn(async move {
let _: Vec<()> = future::join_all(prettiers_to_reload.into_iter().map(|(worktree_id, prettier_path, prettier_instance)| {
async move {
if let Some(instance) = prettier_instance.prettier {
match instance.await {
Ok(prettier) => {
prettier.clear_cache().log_err().await;
},
Err(e) => {
match prettier_path {
Some(prettier_path) => log::error!(
"Failed to clear prettier {prettier_path:?} cache for worktree {worktree_id:?} on prettier settings update: {e:#}"
),
None => log::error!(
"Failed to clear default prettier cache for worktree {worktree_id:?} on prettier settings update: {e:#}"
),
}
},
}
}
}
}))
.await;
})
.detach();
}
}
fn prettier_instance_for_buffer(
&mut self,
buffer: &Model<Buffer>,
cx: &mut ModelContext<Self>,
) -> Task<Option<(Option<PathBuf>, PrettierTask)>> {
let buffer = buffer.read(cx);
let buffer_file = buffer.file();
let Some(buffer_language) = buffer.language() else {
return Task::ready(None);
};
if buffer_language.prettier_parser_name().is_none() {
return Task::ready(None);
}
if self.is_local() {
let Some(node) = self.node.as_ref().map(Arc::clone) else {
return Task::ready(None);
};
match File::from_dyn(buffer_file).map(|file| (file.worktree_id(cx), file.abs_path(cx)))
{
Some((worktree_id, buffer_path)) => {
let fs = Arc::clone(&self.fs);
let installed_prettiers = self.prettier_instances.keys().cloned().collect();
return cx.spawn(|project, mut cx| async move {
match cx
.background_executor()
.spawn(async move {
Prettier::locate_prettier_installation(
fs.as_ref(),
&installed_prettiers,
&buffer_path,
)
.await
})
.await
{
Ok(ControlFlow::Break(())) => {
return None;
}
Ok(ControlFlow::Continue(None)) => {
let default_instance = project
.update(&mut cx, |project, cx| {
project
.prettiers_per_worktree
.entry(worktree_id)
.or_default()
.insert(None);
project.default_prettier.prettier_task(
&node,
Some(worktree_id),
cx,
)
})
.ok()?;
Some((None, default_instance?.log_err().await?))
}
Ok(ControlFlow::Continue(Some(prettier_dir))) => {
project
.update(&mut cx, |project, _| {
project
.prettiers_per_worktree
.entry(worktree_id)
.or_default()
.insert(Some(prettier_dir.clone()))
})
.ok()?;
if let Some(prettier_task) = project
.update(&mut cx, |project, cx| {
project.prettier_instances.get_mut(&prettier_dir).map(
|existing_instance| {
existing_instance.prettier_task(
&node,
Some(&prettier_dir),
Some(worktree_id),
cx,
)
},
)
})
.ok()?
{
log::debug!(
"Found already started prettier in {prettier_dir:?}"
);
return Some((
Some(prettier_dir),
prettier_task?.await.log_err()?,
));
}
log::info!("Found prettier in {prettier_dir:?}, starting.");
let new_prettier_task = project
.update(&mut cx, |project, cx| {
let new_prettier_task = start_prettier(
node,
prettier_dir.clone(),
Some(worktree_id),
cx,
);
project.prettier_instances.insert(
prettier_dir.clone(),
PrettierInstance {
attempt: 0,
prettier: Some(new_prettier_task.clone()),
},
);
new_prettier_task
})
.ok()?;
Some((Some(prettier_dir), new_prettier_task))
}
Err(e) => {
log::error!("Failed to determine prettier path for buffer: {e:#}");
return None;
}
}
});
}
None => {
let new_task = self.default_prettier.prettier_task(&node, None, cx);
return cx
.spawn(|_, _| async move { Some((None, new_task?.log_err().await?)) });
}
}
} else {
return Task::ready(None);
}
}
#[cfg(any(test, feature = "test-support"))]
pub fn install_default_prettier(
&mut self,
_worktree: Option<WorktreeId>,
plugins: HashSet<&'static str>,
_cx: &mut ModelContext<Self>,
) {
// suppress unused code warnings
let _ = install_prettier_packages;
let _ = save_prettier_server_file;
self.default_prettier.installed_plugins.extend(plugins);
self.default_prettier.prettier = PrettierInstallation::Installed(PrettierInstance {
attempt: 0,
prettier: None,
});
}
#[cfg(not(any(test, feature = "test-support")))]
pub fn install_default_prettier(
&mut self,
worktree: Option<WorktreeId>,
mut new_plugins: HashSet<&'static str>,
cx: &mut ModelContext<Self>,
) {
let Some(node) = self.node.as_ref().cloned() else {
return;
};
log::info!("Initializing default prettier with plugins {new_plugins:?}");
let fs = Arc::clone(&self.fs);
let locate_prettier_installation = match worktree.and_then(|worktree_id| {
self.worktree_for_id(worktree_id, cx)
.map(|worktree| worktree.read(cx).abs_path())
}) {
Some(locate_from) => {
let installed_prettiers = self.prettier_instances.keys().cloned().collect();
cx.background_executor().spawn(async move {
Prettier::locate_prettier_installation(
fs.as_ref(),
&installed_prettiers,
locate_from.as_ref(),
)
.await
})
}
None => Task::ready(Ok(ControlFlow::Continue(None))),
};
new_plugins.retain(|plugin| !self.default_prettier.installed_plugins.contains(plugin));
let mut installation_attempt = 0;
let previous_installation_task = match &mut self.default_prettier.prettier {
PrettierInstallation::NotInstalled {
installation_task,
attempts,
not_installed_plugins,
} => {
installation_attempt = *attempts;
if installation_attempt > prettier::FAIL_THRESHOLD {
*installation_task = None;
log::warn!(
"Default prettier installation had failed {installation_attempt} times, not attempting again",
);
return;
}
new_plugins.extend(not_installed_plugins.iter());
installation_task.clone()
}
PrettierInstallation::Installed { .. } => {
if new_plugins.is_empty() {
return;
}
None
}
};
let plugins_to_install = new_plugins.clone();
let fs = Arc::clone(&self.fs);
let new_installation_task = cx
.spawn(|project, mut cx| async move {
match locate_prettier_installation
.await
.context("locate prettier installation")
.map_err(Arc::new)?
{
ControlFlow::Break(()) => return Ok(()),
ControlFlow::Continue(prettier_path) => {
if prettier_path.is_some() {
new_plugins.clear();
}
let mut needs_install = false;
if let Some(previous_installation_task) = previous_installation_task {
if let Err(e) = previous_installation_task.await {
log::error!("Failed to install default prettier: {e:#}");
project.update(&mut cx, |project, _| {
if let PrettierInstallation::NotInstalled { attempts, not_installed_plugins, .. } = &mut project.default_prettier.prettier {
*attempts += 1;
new_plugins.extend(not_installed_plugins.iter());
installation_attempt = *attempts;
needs_install = true;
};
})?;
}
};
if installation_attempt > prettier::FAIL_THRESHOLD {
project.update(&mut cx, |project, _| {
if let PrettierInstallation::NotInstalled { installation_task, .. } = &mut project.default_prettier.prettier {
*installation_task = None;
};
})?;
log::warn!(
"Default prettier installation had failed {installation_attempt} times, not attempting again",
);
return Ok(());
}
project.update(&mut cx, |project, _| {
new_plugins.retain(|plugin| {
!project.default_prettier.installed_plugins.contains(plugin)
});
if let PrettierInstallation::NotInstalled { not_installed_plugins, .. } = &mut project.default_prettier.prettier {
not_installed_plugins.retain(|plugin| {
!project.default_prettier.installed_plugins.contains(plugin)
});
not_installed_plugins.extend(new_plugins.iter());
}
needs_install |= !new_plugins.is_empty();
})?;
if needs_install {
let installed_plugins = new_plugins.clone();
cx.background_executor()
.spawn(async move {
save_prettier_server_file(fs.as_ref()).await?;
install_prettier_packages(new_plugins, node).await
})
.await
.context("prettier & plugins install")
.map_err(Arc::new)?;
log::info!("Initialized prettier with plugins: {installed_plugins:?}");
project.update(&mut cx, |project, _| {
project.default_prettier.prettier =
PrettierInstallation::Installed(PrettierInstance {
attempt: 0,
prettier: None,
});
project.default_prettier
.installed_plugins
.extend(installed_plugins);
})?;
}
}
}
Ok(())
})
.shared();
self.default_prettier.prettier = PrettierInstallation::NotInstalled {
attempts: installation_attempt,
installation_task: Some(new_installation_task),
not_installed_plugins: plugins_to_install,
};
}
}

View File

@ -1,5 +1,6 @@
mod ignore;
mod lsp_command;
mod prettier_support;
pub mod project_settings;
pub mod search;
pub mod terminals;
@ -20,7 +21,7 @@ use futures::{
mpsc::{self, UnboundedReceiver},
oneshot,
},
future::{self, try_join_all, Shared},
future::{try_join_all, Shared},
stream::FuturesUnordered,
AsyncWriteExt, Future, FutureExt, StreamExt, TryFutureExt,
};
@ -31,9 +32,7 @@ use gpui::{
};
use itertools::Itertools;
use language::{
language_settings::{
language_settings, FormatOnSave, Formatter, InlayHintKind, LanguageSettings,
},
language_settings::{language_settings, FormatOnSave, Formatter, InlayHintKind},
point_to_lsp,
proto::{
deserialize_anchor, deserialize_fingerprint, deserialize_line_ending, deserialize_version,
@ -54,7 +53,7 @@ use lsp_command::*;
use node_runtime::NodeRuntime;
use parking_lot::Mutex;
use postage::watch;
use prettier::Prettier;
use prettier_support::{DefaultPrettier, PrettierInstance};
use project_settings::{LspSettings, ProjectSettings};
use rand::prelude::*;
use search::SearchQuery;
@ -70,7 +69,7 @@ use std::{
hash::Hash,
mem,
num::NonZeroU32,
ops::{ControlFlow, Range},
ops::Range,
path::{self, Component, Path, PathBuf},
process::Stdio,
str,
@ -83,11 +82,8 @@ use std::{
use terminals::Terminals;
use text::Anchor;
use util::{
debug_panic, defer,
http::HttpClient,
merge_json_value_into,
paths::{DEFAULT_PRETTIER_DIR, LOCAL_SETTINGS_RELATIVE_PATH},
post_inc, ResultExt, TryFutureExt as _,
debug_panic, defer, http::HttpClient, merge_json_value_into,
paths::LOCAL_SETTINGS_RELATIVE_PATH, post_inc, ResultExt, TryFutureExt as _,
};
pub use fs::*;
@ -166,16 +162,9 @@ pub struct Project {
copilot_log_subscription: Option<lsp::Subscription>,
current_lsp_settings: HashMap<Arc<str>, LspSettings>,
node: Option<Arc<dyn NodeRuntime>>,
default_prettier: Option<DefaultPrettier>,
default_prettier: DefaultPrettier,
prettiers_per_worktree: HashMap<WorktreeId, HashSet<Option<PathBuf>>>,
prettier_instances: HashMap<PathBuf, Shared<Task<Result<Arc<Prettier>, Arc<anyhow::Error>>>>>,
}
struct DefaultPrettier {
instance: Option<Shared<Task<Result<Arc<Prettier>, Arc<anyhow::Error>>>>>,
installation_process: Option<Shared<Task<Result<(), Arc<anyhow::Error>>>>>,
#[cfg(not(any(test, feature = "test-support")))]
installed_plugins: HashSet<&'static str>,
prettier_instances: HashMap<PathBuf, PrettierInstance>,
}
struct DelayedDebounced {
@ -540,6 +529,14 @@ struct ProjectLspAdapterDelegate {
http_client: Arc<dyn HttpClient>,
}
// Currently, formatting operations are represented differently depending on
// whether they come from a language server or an external command.
enum FormatOperation {
Lsp(Vec<(Range<Anchor>, String)>),
External(Diff),
Prettier(Diff),
}
impl FormatTrigger {
fn from_proto(value: i32) -> FormatTrigger {
match value {
@ -689,7 +686,7 @@ impl Project {
copilot_log_subscription: None,
current_lsp_settings: ProjectSettings::get_global(cx).lsp.clone(),
node: Some(node),
default_prettier: None,
default_prettier: DefaultPrettier::default(),
prettiers_per_worktree: HashMap::default(),
prettier_instances: HashMap::default(),
}
@ -792,7 +789,7 @@ impl Project {
copilot_log_subscription: None,
current_lsp_settings: ProjectSettings::get_global(cx).lsp.clone(),
node: None,
default_prettier: None,
default_prettier: DefaultPrettier::default(),
prettiers_per_worktree: HashMap::default(),
prettier_instances: HashMap::default(),
};
@ -965,8 +962,19 @@ impl Project {
.detach();
}
let mut prettier_plugins_by_worktree = HashMap::default();
for (worktree, language, settings) in language_formatters_to_check {
self.install_default_formatters(worktree, &language, &settings, cx);
if let Some(plugins) =
prettier_support::prettier_plugins_for_language(&language, &settings)
{
prettier_plugins_by_worktree
.entry(worktree)
.or_insert_with(|| HashSet::default())
.extend(plugins);
}
}
for (worktree, prettier_plugins) in prettier_plugins_by_worktree {
self.install_default_prettier(worktree, prettier_plugins, cx);
}
// Start all the newly-enabled language servers.
@ -2669,8 +2677,9 @@ impl Project {
})?;
for (adapter, server) in servers {
let workspace_config =
cx.update(|cx| adapter.workspace_configuration(cx))?.await;
let workspace_config = cx
.update(|cx| adapter.workspace_configuration(server.root_path(), cx))?
.await;
server
.notify::<lsp::notification::DidChangeConfiguration>(
lsp::DidChangeConfigurationParams {
@ -2722,8 +2731,11 @@ impl Project {
let settings = language_settings(Some(&new_language), buffer_file.as_ref(), cx).clone();
let buffer_file = File::from_dyn(buffer_file.as_ref());
let worktree = buffer_file.as_ref().map(|f| f.worktree_id(cx));
self.install_default_formatters(worktree, &new_language, &settings, cx);
if let Some(prettier_plugins) =
prettier_support::prettier_plugins_for_language(&new_language, &settings)
{
self.install_default_prettier(worktree, prettier_plugins, cx);
};
if let Some(file) = buffer_file {
let worktree = file.worktree.clone();
if let Some(tree) = worktree.read(cx).as_local() {
@ -2779,7 +2791,7 @@ impl Project {
stderr_capture.clone(),
language.clone(),
adapter.clone(),
worktree_path,
Arc::clone(&worktree_path),
ProjectLspAdapterDelegate::new(self, cx),
cx,
) {
@ -2811,6 +2823,7 @@ impl Project {
cx.spawn(move |this, mut cx| async move {
let result = Self::setup_and_insert_language_server(
this.clone(),
&worktree_path,
initialization_options,
pending_server,
adapter.clone(),
@ -2931,6 +2944,7 @@ impl Project {
async fn setup_and_insert_language_server(
this: WeakModel<Self>,
worktree_path: &Path,
initialization_options: Option<serde_json::Value>,
pending_server: PendingLanguageServer,
adapter: Arc<CachedLspAdapter>,
@ -2943,6 +2957,7 @@ impl Project {
this.clone(),
initialization_options,
pending_server,
worktree_path,
adapter.clone(),
server_id,
cx,
@ -2972,11 +2987,14 @@ impl Project {
this: WeakModel<Self>,
initialization_options: Option<serde_json::Value>,
pending_server: PendingLanguageServer,
worktree_path: &Path,
adapter: Arc<CachedLspAdapter>,
server_id: LanguageServerId,
cx: &mut AsyncAppContext,
) -> Result<Arc<LanguageServer>> {
let workspace_config = cx.update(|cx| adapter.workspace_configuration(cx))?.await;
let workspace_config = cx
.update(|cx| adapter.workspace_configuration(worktree_path, cx))?
.await;
let language_server = pending_server.task.await?;
language_server
@ -3005,11 +3023,14 @@ impl Project {
language_server
.on_request::<lsp::request::WorkspaceConfiguration, _, _>({
let adapter = adapter.clone();
let worktree_path = worktree_path.to_path_buf();
move |params, cx| {
let adapter = adapter.clone();
let worktree_path = worktree_path.clone();
async move {
let workspace_config =
cx.update(|cx| adapter.workspace_configuration(cx))?.await;
let workspace_config = cx
.update(|cx| adapter.workspace_configuration(&worktree_path, cx))?
.await;
Ok(params
.items
.into_iter()
@ -4126,7 +4147,8 @@ impl Project {
this.buffers_being_formatted
.remove(&buffer.read(cx).remote_id());
}
}).ok();
})
.ok();
}
});
@ -4138,8 +4160,6 @@ impl Project {
let remove_trailing_whitespace = settings.remove_trailing_whitespace_on_save;
let ensure_final_newline = settings.ensure_final_newline_on_save;
let format_on_save = settings.format_on_save.clone();
let formatter = settings.formatter.clone();
let tab_size = settings.tab_size;
// First, format buffer's whitespace according to the settings.
@ -4164,18 +4184,10 @@ impl Project {
buffer.end_transaction(cx)
})?;
// Currently, formatting operations are represented differently depending on
// whether they come from a language server or an external command.
enum FormatOperation {
Lsp(Vec<(Range<Anchor>, String)>),
External(Diff),
Prettier(Diff),
}
// Apply language-specific formatting using either a language server
// or external command.
let mut format_operation = None;
match (formatter, format_on_save) {
match (&settings.formatter, &settings.format_on_save) {
(_, FormatOnSave::Off) if trigger == FormatTrigger::Save => {}
(Formatter::LanguageServer, FormatOnSave::On | FormatOnSave::Off)
@ -4220,46 +4232,11 @@ impl Project {
}
}
(Formatter::Auto, FormatOnSave::On | FormatOnSave::Off) => {
if let Some((prettier_path, prettier_task)) = project
.update(&mut cx, |project, cx| {
project.prettier_instance_for_buffer(buffer, cx)
})?.await {
match prettier_task.await
{
Ok(prettier) => {
let buffer_path = buffer.update(&mut cx, |buffer, cx| {
File::from_dyn(buffer.file()).map(|file| file.abs_path(cx))
})?;
format_operation = Some(FormatOperation::Prettier(
prettier
.format(buffer, buffer_path, &mut cx)
.await
.context("formatting via prettier")?,
));
}
Err(e) => {
project.update(&mut cx, |project, _| {
match &prettier_path {
Some(prettier_path) => {
project.prettier_instances.remove(prettier_path);
},
None => {
if let Some(default_prettier) = project.default_prettier.as_mut() {
default_prettier.instance = None;
}
},
}
})?;
match &prettier_path {
Some(prettier_path) => {
log::error!("Failed to create prettier instance from {prettier_path:?} for buffer during autoformatting: {e:#}");
},
None => {
log::error!("Failed to create default prettier instance for buffer during autoformatting: {e:#}");
},
}
}
}
if let Some(new_operation) =
prettier_support::format_with_prettier(&project, buffer, &mut cx)
.await
{
format_operation = Some(new_operation);
} else if let Some((language_server, buffer_abs_path)) =
language_server.as_ref().zip(buffer_abs_path.as_ref())
{
@ -4277,48 +4254,13 @@ impl Project {
));
}
}
(Formatter::Prettier { .. }, FormatOnSave::On | FormatOnSave::Off) => {
if let Some((prettier_path, prettier_task)) = project
.update(&mut cx, |project, cx| {
project.prettier_instance_for_buffer(buffer, cx)
})?.await {
match prettier_task.await
{
Ok(prettier) => {
let buffer_path = buffer.update(&mut cx, |buffer, cx| {
File::from_dyn(buffer.file()).map(|file| file.abs_path(cx))
})?;
format_operation = Some(FormatOperation::Prettier(
prettier
.format(buffer, buffer_path, &mut cx)
.await
.context("formatting via prettier")?,
));
}
Err(e) => {
project.update(&mut cx, |project, _| {
match &prettier_path {
Some(prettier_path) => {
project.prettier_instances.remove(prettier_path);
},
None => {
if let Some(default_prettier) = project.default_prettier.as_mut() {
default_prettier.instance = None;
}
},
}
})?;
match &prettier_path {
Some(prettier_path) => {
log::error!("Failed to create prettier instance from {prettier_path:?} for buffer during autoformatting: {e:#}");
},
None => {
log::error!("Failed to create default prettier instance for buffer during autoformatting: {e:#}");
},
}
}
}
}
(Formatter::Prettier, FormatOnSave::On | FormatOnSave::Off) => {
if let Some(new_operation) =
prettier_support::format_with_prettier(&project, buffer, &mut cx)
.await
{
format_operation = Some(new_operation);
}
}
};
@ -6638,84 +6580,6 @@ impl Project {
.detach();
}
fn update_prettier_settings(
&self,
worktree: &Model<Worktree>,
changes: &[(Arc<Path>, ProjectEntryId, PathChange)],
cx: &mut ModelContext<'_, Project>,
) {
let prettier_config_files = Prettier::CONFIG_FILE_NAMES
.iter()
.map(Path::new)
.collect::<HashSet<_>>();
let prettier_config_file_changed = changes
.iter()
.filter(|(_, _, change)| !matches!(change, PathChange::Loaded))
.filter(|(path, _, _)| {
!path
.components()
.any(|component| component.as_os_str().to_string_lossy() == "node_modules")
})
.find(|(path, _, _)| prettier_config_files.contains(path.as_ref()));
let current_worktree_id = worktree.read(cx).id();
if let Some((config_path, _, _)) = prettier_config_file_changed {
log::info!(
"Prettier config file {config_path:?} changed, reloading prettier instances for worktree {current_worktree_id}"
);
let prettiers_to_reload = self
.prettiers_per_worktree
.get(&current_worktree_id)
.iter()
.flat_map(|prettier_paths| prettier_paths.iter())
.flatten()
.filter_map(|prettier_path| {
Some((
current_worktree_id,
Some(prettier_path.clone()),
self.prettier_instances.get(prettier_path)?.clone(),
))
})
.chain(self.default_prettier.iter().filter_map(|default_prettier| {
Some((
current_worktree_id,
None,
default_prettier.instance.clone()?,
))
}))
.collect::<Vec<_>>();
cx.background_executor()
.spawn(async move {
for task_result in future::join_all(prettiers_to_reload.into_iter().map(|(worktree_id, prettier_path, prettier_task)| {
async move {
prettier_task.await?
.clear_cache()
.await
.with_context(|| {
match prettier_path {
Some(prettier_path) => format!(
"clearing prettier {prettier_path:?} cache for worktree {worktree_id:?} on prettier settings update"
),
None => format!(
"clearing default prettier cache for worktree {worktree_id:?} on prettier settings update"
),
}
})
.map_err(Arc::new)
}
}))
.await
{
if let Err(e) = task_result {
log::error!("Failed to clear cache for prettier: {e:#}");
}
}
})
.detach();
}
}
pub fn set_active_path(&mut self, entry: Option<ProjectPath>, cx: &mut ModelContext<Self>) {
let new_active_entry = entry.and_then(|project_path| {
let worktree = self.worktree_for_id(project_path.worktree_id, cx)?;
@ -6742,9 +6606,15 @@ impl Project {
})
}
pub fn diagnostic_summary(&self, cx: &AppContext) -> DiagnosticSummary {
pub fn diagnostic_summary(&self, include_ignored: bool, cx: &AppContext) -> DiagnosticSummary {
let mut summary = DiagnosticSummary::default();
for (_, _, path_summary) in self.diagnostic_summaries(cx) {
for (_, _, path_summary) in
self.diagnostic_summaries(include_ignored, cx)
.filter(|(path, _, _)| {
let worktree = self.entry_for_path(&path, cx).map(|entry| entry.is_ignored);
include_ignored || worktree == Some(false)
})
{
summary.error_count += path_summary.error_count;
summary.warning_count += path_summary.warning_count;
}
@ -6753,17 +6623,23 @@ impl Project {
pub fn diagnostic_summaries<'a>(
&'a self,
include_ignored: bool,
cx: &'a AppContext,
) -> impl Iterator<Item = (ProjectPath, LanguageServerId, DiagnosticSummary)> + 'a {
self.visible_worktrees(cx).flat_map(move |worktree| {
let worktree = worktree.read(cx);
let worktree_id = worktree.id();
worktree
.diagnostic_summaries()
.map(move |(path, server_id, summary)| {
(ProjectPath { worktree_id, path }, server_id, summary)
})
})
self.visible_worktrees(cx)
.flat_map(move |worktree| {
let worktree = worktree.read(cx);
let worktree_id = worktree.id();
worktree
.diagnostic_summaries()
.map(move |(path, server_id, summary)| {
(ProjectPath { worktree_id, path }, server_id, summary)
})
})
.filter(move |(path, _, _)| {
let worktree = self.entry_for_path(&path, cx).map(|entry| entry.is_ignored);
include_ignored || worktree == Some(false)
})
}
pub fn disk_based_diagnostics_started(
@ -8579,486 +8455,6 @@ impl Project {
Vec::new()
}
}
fn prettier_instance_for_buffer(
&mut self,
buffer: &Model<Buffer>,
cx: &mut ModelContext<Self>,
) -> Task<
Option<(
Option<PathBuf>,
Shared<Task<Result<Arc<Prettier>, Arc<anyhow::Error>>>>,
)>,
> {
let buffer = buffer.read(cx);
let buffer_file = buffer.file();
let Some(buffer_language) = buffer.language() else {
return Task::ready(None);
};
if buffer_language.prettier_parser_name().is_none() {
return Task::ready(None);
}
if self.is_local() {
let Some(node) = self.node.as_ref().map(Arc::clone) else {
return Task::ready(None);
};
match File::from_dyn(buffer_file).map(|file| (file.worktree_id(cx), file.abs_path(cx)))
{
Some((worktree_id, buffer_path)) => {
let fs = Arc::clone(&self.fs);
let installed_prettiers = self.prettier_instances.keys().cloned().collect();
return cx.spawn(|project, mut cx| async move {
match cx
.background_executor()
.spawn(async move {
Prettier::locate_prettier_installation(
fs.as_ref(),
&installed_prettiers,
&buffer_path,
)
.await
})
.await
{
Ok(ControlFlow::Break(())) => {
return None;
}
Ok(ControlFlow::Continue(None)) => {
match project.update(&mut cx, |project, _| {
project
.prettiers_per_worktree
.entry(worktree_id)
.or_default()
.insert(None);
project.default_prettier.as_ref().and_then(
|default_prettier| default_prettier.instance.clone(),
)
}) {
Ok(Some(old_task)) => Some((None, old_task)),
Ok(None) => {
match project.update(&mut cx, |_, cx| {
start_default_prettier(node, Some(worktree_id), cx)
}) {
Ok(new_default_prettier) => {
return Some((None, new_default_prettier.await))
}
Err(e) => {
Some((
None,
Task::ready(Err(Arc::new(e.context("project is gone during default prettier startup"))))
.shared(),
))
}
}
}
Err(e) => Some((None, Task::ready(Err(Arc::new(e.context("project is gone during default prettier checks"))))
.shared())),
}
}
Ok(ControlFlow::Continue(Some(prettier_dir))) => {
match project.update(&mut cx, |project, _| {
project
.prettiers_per_worktree
.entry(worktree_id)
.or_default()
.insert(Some(prettier_dir.clone()));
project.prettier_instances.get(&prettier_dir).cloned()
}) {
Ok(Some(existing_prettier)) => {
log::debug!(
"Found already started prettier in {prettier_dir:?}"
);
return Some((Some(prettier_dir), existing_prettier));
}
Err(e) => {
return Some((
Some(prettier_dir),
Task::ready(Err(Arc::new(e.context("project is gone during custom prettier checks"))))
.shared(),
))
}
_ => {},
}
log::info!("Found prettier in {prettier_dir:?}, starting.");
let new_prettier_task =
match project.update(&mut cx, |project, cx| {
let new_prettier_task = start_prettier(
node,
prettier_dir.clone(),
Some(worktree_id),
cx,
);
project.prettier_instances.insert(
prettier_dir.clone(),
new_prettier_task.clone(),
);
new_prettier_task
}) {
Ok(task) => task,
Err(e) => return Some((
Some(prettier_dir),
Task::ready(Err(Arc::new(e.context("project is gone during custom prettier startup"))))
.shared()
)),
};
Some((Some(prettier_dir), new_prettier_task))
}
Err(e) => {
return Some((
None,
Task::ready(Err(Arc::new(
e.context("determining prettier path"),
)))
.shared(),
));
}
}
});
}
None => {
let started_default_prettier = self
.default_prettier
.as_ref()
.and_then(|default_prettier| default_prettier.instance.clone());
match started_default_prettier {
Some(old_task) => return Task::ready(Some((None, old_task))),
None => {
let new_task = start_default_prettier(node, None, cx);
return cx.spawn(|_, _| async move { Some((None, new_task.await)) });
}
}
}
}
} else if self.remote_id().is_some() {
return Task::ready(None);
} else {
Task::ready(Some((
None,
Task::ready(Err(Arc::new(anyhow!("project does not have a remote id")))).shared(),
)))
}
}
#[cfg(any(test, feature = "test-support"))]
fn install_default_formatters(
&mut self,
_: Option<WorktreeId>,
_: &Language,
_: &LanguageSettings,
_: &mut ModelContext<Self>,
) {
}
#[cfg(not(any(test, feature = "test-support")))]
fn install_default_formatters(
&mut self,
worktree: Option<WorktreeId>,
new_language: &Language,
language_settings: &LanguageSettings,
cx: &mut ModelContext<Self>,
) {
match &language_settings.formatter {
Formatter::Prettier { .. } | Formatter::Auto => {}
Formatter::LanguageServer | Formatter::External { .. } => return,
};
let Some(node) = self.node.as_ref().cloned() else {
return;
};
let mut prettier_plugins = None;
if new_language.prettier_parser_name().is_some() {
prettier_plugins
.get_or_insert_with(|| HashSet::<&'static str>::default())
.extend(
new_language
.lsp_adapters()
.iter()
.flat_map(|adapter| adapter.prettier_plugins()),
)
}
let Some(prettier_plugins) = prettier_plugins else {
return;
};
let fs = Arc::clone(&self.fs);
let locate_prettier_installation = match worktree.and_then(|worktree_id| {
self.worktree_for_id(worktree_id, cx)
.map(|worktree| worktree.read(cx).abs_path())
}) {
Some(locate_from) => {
let installed_prettiers = self.prettier_instances.keys().cloned().collect();
cx.background_executor().spawn(async move {
Prettier::locate_prettier_installation(
fs.as_ref(),
&installed_prettiers,
locate_from.as_ref(),
)
.await
})
}
None => Task::ready(Ok(ControlFlow::Break(()))),
};
let mut plugins_to_install = prettier_plugins;
let previous_installation_process =
if let Some(default_prettier) = &mut self.default_prettier {
plugins_to_install
.retain(|plugin| !default_prettier.installed_plugins.contains(plugin));
if plugins_to_install.is_empty() {
return;
}
default_prettier.installation_process.clone()
} else {
None
};
let fs = Arc::clone(&self.fs);
let default_prettier = self
.default_prettier
.get_or_insert_with(|| DefaultPrettier {
instance: None,
installation_process: None,
installed_plugins: HashSet::default(),
});
default_prettier.installation_process = Some(
cx.spawn(|this, mut cx| async move {
match locate_prettier_installation
.await
.context("locate prettier installation")
.map_err(Arc::new)?
{
ControlFlow::Break(()) => return Ok(()),
ControlFlow::Continue(Some(_non_default_prettier)) => return Ok(()),
ControlFlow::Continue(None) => {
let mut needs_install = match previous_installation_process {
Some(previous_installation_process) => {
previous_installation_process.await.is_err()
}
None => true,
};
this.update(&mut cx, |this, _| {
if let Some(default_prettier) = &mut this.default_prettier {
plugins_to_install.retain(|plugin| {
!default_prettier.installed_plugins.contains(plugin)
});
needs_install |= !plugins_to_install.is_empty();
}
})?;
if needs_install {
let installed_plugins = plugins_to_install.clone();
cx.background_executor()
.spawn(async move {
install_default_prettier(plugins_to_install, node, fs).await
})
.await
.context("prettier & plugins install")
.map_err(Arc::new)?;
this.update(&mut cx, |this, _| {
let default_prettier =
this.default_prettier
.get_or_insert_with(|| DefaultPrettier {
instance: None,
installation_process: Some(
Task::ready(Ok(())).shared(),
),
installed_plugins: HashSet::default(),
});
default_prettier.instance = None;
default_prettier.installed_plugins.extend(installed_plugins);
})?;
}
}
}
Ok(())
})
.shared(),
);
}
}
fn start_default_prettier(
node: Arc<dyn NodeRuntime>,
worktree_id: Option<WorktreeId>,
cx: &mut ModelContext<'_, Project>,
) -> Task<Shared<Task<Result<Arc<Prettier>, Arc<anyhow::Error>>>>> {
cx.spawn(|project, mut cx| async move {
loop {
let default_prettier_installing = match project.update(&mut cx, |project, _| {
project
.default_prettier
.as_ref()
.and_then(|default_prettier| default_prettier.installation_process.clone())
}) {
Ok(installation) => installation,
Err(e) => {
return Task::ready(Err(Arc::new(
e.context("project is gone during default prettier installation"),
)))
.shared()
}
};
match default_prettier_installing {
Some(installation_task) => {
if installation_task.await.is_ok() {
break;
}
}
None => break,
}
}
match project.update(&mut cx, |project, cx| {
match project
.default_prettier
.as_mut()
.and_then(|default_prettier| default_prettier.instance.as_mut())
{
Some(default_prettier) => default_prettier.clone(),
None => {
let new_default_prettier =
start_prettier(node, DEFAULT_PRETTIER_DIR.clone(), worktree_id, cx);
project
.default_prettier
.get_or_insert_with(|| DefaultPrettier {
instance: None,
installation_process: None,
#[cfg(not(any(test, feature = "test-support")))]
installed_plugins: HashSet::default(),
})
.instance = Some(new_default_prettier.clone());
new_default_prettier
}
}
}) {
Ok(task) => task,
Err(e) => Task::ready(Err(Arc::new(
e.context("project is gone during default prettier startup"),
)))
.shared(),
}
})
}
fn start_prettier(
node: Arc<dyn NodeRuntime>,
prettier_dir: PathBuf,
worktree_id: Option<WorktreeId>,
cx: &mut ModelContext<'_, Project>,
) -> Shared<Task<Result<Arc<Prettier>, Arc<anyhow::Error>>>> {
cx.spawn(|project, mut cx| async move {
let new_server_id = project.update(&mut cx, |project, _| {
project.languages.next_language_server_id()
})?;
let new_prettier = Prettier::start(new_server_id, prettier_dir, node, cx.clone())
.await
.context("default prettier spawn")
.map(Arc::new)
.map_err(Arc::new)?;
register_new_prettier(&project, &new_prettier, worktree_id, new_server_id, &mut cx);
Ok(new_prettier)
})
.shared()
}
fn register_new_prettier(
project: &WeakModel<Project>,
prettier: &Prettier,
worktree_id: Option<WorktreeId>,
new_server_id: LanguageServerId,
cx: &mut AsyncAppContext,
) {
let prettier_dir = prettier.prettier_dir();
let is_default = prettier.is_default();
if is_default {
log::info!("Started default prettier in {prettier_dir:?}");
} else {
log::info!("Started prettier in {prettier_dir:?}");
}
if let Some(prettier_server) = prettier.server() {
project
.update(cx, |project, cx| {
let name = if is_default {
LanguageServerName(Arc::from("prettier (default)"))
} else {
let worktree_path = worktree_id
.and_then(|id| project.worktree_for_id(id, cx))
.map(|worktree| worktree.update(cx, |worktree, _| worktree.abs_path()));
let name = match worktree_path {
Some(worktree_path) => {
if prettier_dir == worktree_path.as_ref() {
let name = prettier_dir
.file_name()
.and_then(|name| name.to_str())
.unwrap_or_default();
format!("prettier ({name})")
} else {
let dir_to_display = prettier_dir
.strip_prefix(worktree_path.as_ref())
.ok()
.unwrap_or(prettier_dir);
format!("prettier ({})", dir_to_display.display())
}
}
None => format!("prettier ({})", prettier_dir.display()),
};
LanguageServerName(Arc::from(name))
};
project
.supplementary_language_servers
.insert(new_server_id, (name, Arc::clone(prettier_server)));
cx.emit(Event::LanguageServerAdded(new_server_id));
})
.ok();
}
}
#[cfg(not(any(test, feature = "test-support")))]
async fn install_default_prettier(
plugins_to_install: HashSet<&'static str>,
node: Arc<dyn NodeRuntime>,
fs: Arc<dyn Fs>,
) -> anyhow::Result<()> {
let prettier_wrapper_path = DEFAULT_PRETTIER_DIR.join(prettier::PRETTIER_SERVER_FILE);
// method creates parent directory if it doesn't exist
fs.save(
&prettier_wrapper_path,
&text::Rope::from(prettier::PRETTIER_SERVER_JS),
text::LineEnding::Unix,
)
.await
.with_context(|| {
format!(
"writing {} file at {prettier_wrapper_path:?}",
prettier::PRETTIER_SERVER_FILE
)
})?;
let packages_to_versions =
future::try_join_all(plugins_to_install.iter().chain(Some(&"prettier")).map(
|package_name| async {
let returned_package_name = package_name.to_string();
let latest_version = node
.npm_package_latest_version(package_name)
.await
.with_context(|| {
format!("fetching latest npm version for package {returned_package_name}")
})?;
anyhow::Ok((returned_package_name, latest_version))
},
))
.await
.context("fetching latest npm versions")?;
log::info!("Fetching default prettier and plugins: {packages_to_versions:?}");
let borrowed_packages = packages_to_versions
.iter()
.map(|(package, version)| (package.as_str(), version.as_str()))
.collect::<Vec<_>>();
node.npm_install_packages(DEFAULT_PRETTIER_DIR.as_path(), &borrowed_packages)
.await
.context("fetching formatter packages")?;
anyhow::Ok(())
}
fn subscribe_for_copilot_events(

View File

@ -823,7 +823,7 @@ async fn test_single_file_worktrees_diagnostics(cx: &mut gpui::TestAppContext) {
}
#[gpui::test]
async fn test_hidden_worktrees_diagnostics(cx: &mut gpui::TestAppContext) {
async fn test_omitted_diagnostics(cx: &mut gpui::TestAppContext) {
init_test(cx);
let fs = FakeFs::new(cx.executor());
@ -831,7 +831,12 @@ async fn test_hidden_worktrees_diagnostics(cx: &mut gpui::TestAppContext) {
"/root",
json!({
"dir": {
".git": {
"HEAD": "ref: refs/heads/main",
},
".gitignore": "b.rs",
"a.rs": "let a = 1;",
"b.rs": "let b = 2;",
},
"other.rs": "let b = c;"
}),
@ -839,6 +844,13 @@ async fn test_hidden_worktrees_diagnostics(cx: &mut gpui::TestAppContext) {
.await;
let project = Project::test(fs, ["/root/dir".as_ref()], cx).await;
let (worktree, _) = project
.update(cx, |project, cx| {
project.find_or_create_local_worktree("/root/dir", true, cx)
})
.await
.unwrap();
let main_worktree_id = worktree.read_with(cx, |tree, _| tree.id());
let (worktree, _) = project
.update(cx, |project, cx| {
@ -846,12 +858,30 @@ async fn test_hidden_worktrees_diagnostics(cx: &mut gpui::TestAppContext) {
})
.await
.unwrap();
let worktree_id = worktree.update(cx, |tree, _| tree.id());
let other_worktree_id = worktree.update(cx, |tree, _| tree.id());
let server_id = LanguageServerId(0);
project.update(cx, |project, cx| {
project
.update_diagnostics(
LanguageServerId(0),
server_id,
lsp::PublishDiagnosticsParams {
uri: Url::from_file_path("/root/dir/b.rs").unwrap(),
version: None,
diagnostics: vec![lsp::Diagnostic {
range: lsp::Range::new(lsp::Position::new(0, 4), lsp::Position::new(0, 5)),
severity: Some(lsp::DiagnosticSeverity::ERROR),
message: "unused variable 'b'".to_string(),
..Default::default()
}],
},
&[],
cx,
)
.unwrap();
project
.update_diagnostics(
server_id,
lsp::PublishDiagnosticsParams {
uri: Url::from_file_path("/root/other.rs").unwrap(),
version: None,
@ -868,11 +898,34 @@ async fn test_hidden_worktrees_diagnostics(cx: &mut gpui::TestAppContext) {
.unwrap();
});
let buffer = project
.update(cx, |project, cx| project.open_buffer((worktree_id, ""), cx))
let main_ignored_buffer = project
.update(cx, |project, cx| {
project.open_buffer((main_worktree_id, "b.rs"), cx)
})
.await
.unwrap();
buffer.update(cx, |buffer, _| {
main_ignored_buffer.update(cx, |buffer, _| {
let chunks = chunks_with_diagnostics(buffer, 0..buffer.len());
assert_eq!(
chunks
.iter()
.map(|(s, d)| (s.as_str(), *d))
.collect::<Vec<_>>(),
&[
("let ", None),
("b", Some(DiagnosticSeverity::ERROR)),
(" = 2;", None),
],
"Gigitnored buffers should still get in-buffer diagnostics",
);
});
let other_buffer = project
.update(cx, |project, cx| {
project.open_buffer((other_worktree_id, ""), cx)
})
.await
.unwrap();
other_buffer.update(cx, |buffer, _| {
let chunks = chunks_with_diagnostics(buffer, 0..buffer.len());
assert_eq!(
chunks
@ -883,13 +936,29 @@ async fn test_hidden_worktrees_diagnostics(cx: &mut gpui::TestAppContext) {
("let b = ", None),
("c", Some(DiagnosticSeverity::ERROR)),
(";", None),
]
],
"Buffers from hidden projects should still get in-buffer diagnostics"
);
});
project.update(cx, |project, cx| {
assert_eq!(project.diagnostic_summaries(cx).next(), None);
assert_eq!(project.diagnostic_summary(cx).error_count, 0);
assert_eq!(project.diagnostic_summaries(false, cx).next(), None);
assert_eq!(
project.diagnostic_summaries(true, cx).collect::<Vec<_>>(),
vec![(
ProjectPath {
worktree_id: main_worktree_id,
path: Arc::from(Path::new("b.rs")),
},
server_id,
DiagnosticSummary {
error_count: 1,
warning_count: 0,
}
)]
);
assert_eq!(project.diagnostic_summary(false, cx).error_count, 0);
assert_eq!(project.diagnostic_summary(true, cx).error_count, 1);
});
}
@ -1162,7 +1231,7 @@ async fn test_restarting_server_with_diagnostics_published(cx: &mut gpui::TestAp
});
project.update(cx, |project, cx| {
assert_eq!(
project.diagnostic_summary(cx),
project.diagnostic_summary(false, cx),
DiagnosticSummary {
error_count: 1,
warning_count: 0,
@ -1188,7 +1257,7 @@ async fn test_restarting_server_with_diagnostics_published(cx: &mut gpui::TestAp
});
project.update(cx, |project, cx| {
assert_eq!(
project.diagnostic_summary(cx),
project.diagnostic_summary(false, cx),
DiagnosticSummary {
error_count: 0,
warning_count: 0,
@ -1777,7 +1846,7 @@ async fn test_diagnostics_from_multiple_language_servers(cx: &mut gpui::TestAppC
.unwrap();
assert_eq!(
project.diagnostic_summary(cx),
project.diagnostic_summary(false, cx),
DiagnosticSummary {
error_count: 2,
warning_count: 0,

View File

@ -41,56 +41,47 @@ impl FileAssociations {
})
}
pub fn get_icon(path: &Path, cx: &AppContext) -> Arc<str> {
pub fn get_icon(path: &Path, cx: &AppContext) -> Option<Arc<str>> {
let this = cx.has_global::<Self>().then(|| cx.global::<Self>())?;
// FIXME: Associate a type with the languages and have the file's langauge
// override these associations
maybe!({
let this = cx.has_global::<Self>().then(|| cx.global::<Self>())?;
let suffix = path.icon_suffix()?;
// FIXME: Associate a type with the languages and have the file's langauge
// override these associations
maybe!({
let suffix = path.icon_suffix()?;
this.suffixes
.get(suffix)
.and_then(|type_str| this.types.get(type_str))
.map(|type_config| type_config.icon.clone())
})
.or_else(|| this.types.get("default").map(|config| config.icon.clone()))
})
.unwrap_or_else(|| Arc::from("".to_string()))
}
pub fn get_folder_icon(expanded: bool, cx: &AppContext) -> Arc<str> {
maybe!({
let this = cx.has_global::<Self>().then(|| cx.global::<Self>())?;
let key = if expanded {
EXPANDED_DIRECTORY_TYPE
} else {
COLLAPSED_DIRECTORY_TYPE
};
this.types
.get(key)
this.suffixes
.get(suffix)
.and_then(|type_str| this.types.get(type_str))
.map(|type_config| type_config.icon.clone())
})
.unwrap_or_else(|| Arc::from("".to_string()))
.or_else(|| this.types.get("default").map(|config| config.icon.clone()))
}
pub fn get_chevron_icon(expanded: bool, cx: &AppContext) -> Arc<str> {
maybe!({
let this = cx.has_global::<Self>().then(|| cx.global::<Self>())?;
pub fn get_folder_icon(expanded: bool, cx: &AppContext) -> Option<Arc<str>> {
let this = cx.has_global::<Self>().then(|| cx.global::<Self>())?;
let key = if expanded {
EXPANDED_CHEVRON_TYPE
} else {
COLLAPSED_CHEVRON_TYPE
};
let key = if expanded {
EXPANDED_DIRECTORY_TYPE
} else {
COLLAPSED_DIRECTORY_TYPE
};
this.types
.get(key)
.map(|type_config| type_config.icon.clone())
})
.unwrap_or_else(|| Arc::from("".to_string()))
this.types
.get(key)
.map(|type_config| type_config.icon.clone())
}
pub fn get_chevron_icon(expanded: bool, cx: &AppContext) -> Option<Arc<str>> {
let this = cx.has_global::<Self>().then(|| cx.global::<Self>())?;
let key = if expanded {
EXPANDED_CHEVRON_TYPE
} else {
COLLAPSED_CHEVRON_TYPE
};
this.types
.get(key)
.map(|type_config| type_config.icon.clone())
}
}

View File

@ -8,10 +8,10 @@ use file_associations::FileAssociations;
use anyhow::{anyhow, Result};
use gpui::{
actions, div, px, uniform_list, Action, AppContext, AssetSource, AsyncWindowContext,
ClipboardItem, Div, EventEmitter, FocusHandle, Focusable, FocusableView, InteractiveElement,
IntoElement, Model, MouseButton, MouseDownEvent, ParentElement, Pixels, Point, PromptLevel,
Render, Stateful, StatefulInteractiveElement, Styled, Task, UniformListScrollHandle, View,
actions, div, overlay, px, uniform_list, Action, AppContext, AssetSource, AsyncWindowContext,
ClipboardItem, DismissEvent, Div, EventEmitter, FocusHandle, Focusable, FocusableView,
InteractiveElement, Model, MouseButton, MouseDownEvent, ParentElement, Pixels, Point,
PromptLevel, Render, Stateful, Styled, Subscription, Task, UniformListScrollHandle, View,
ViewContext, VisualContext as _, WeakView, WindowContext,
};
use menu::{Confirm, SelectNext, SelectPrev};
@ -30,7 +30,7 @@ use std::{
sync::Arc,
};
use theme::ActiveTheme as _;
use ui::{h_stack, v_stack, IconElement, Label};
use ui::{v_stack, ContextMenu, IconElement, Label, ListItem};
use unicase::UniCase;
use util::{maybe, ResultExt, TryFutureExt};
use workspace::{
@ -50,6 +50,7 @@ pub struct ProjectPanel {
last_worktree_root_id: Option<ProjectEntryId>,
expanded_dir_ids: HashMap<WorktreeId, Vec<ProjectEntryId>>,
selection: Option<Selection>,
context_menu: Option<(View<ContextMenu>, Point<Pixels>, Subscription)>,
edit_state: Option<EditState>,
filename_editor: View<Editor>,
clipboard_entry: Option<ClipboardEntry>,
@ -232,6 +233,7 @@ impl ProjectPanel {
expanded_dir_ids: Default::default(),
selection: None,
edit_state: None,
context_menu: None,
filename_editor,
clipboard_entry: None,
// context_menu: cx.add_view(|cx| ContextMenu::new(view_id, cx)),
@ -367,80 +369,93 @@ impl ProjectPanel {
fn deploy_context_menu(
&mut self,
_position: Point<Pixels>,
_entry_id: ProjectEntryId,
_cx: &mut ViewContext<Self>,
position: Point<Pixels>,
entry_id: ProjectEntryId,
cx: &mut ViewContext<Self>,
) {
// todo!()
// let project = self.project.read(cx);
let this = cx.view().clone();
let project = self.project.read(cx);
// let worktree_id = if let Some(id) = project.worktree_id_for_entry(entry_id, cx) {
// id
// } else {
// return;
// };
let worktree_id = if let Some(id) = project.worktree_id_for_entry(entry_id, cx) {
id
} else {
return;
};
// self.selection = Some(Selection {
// worktree_id,
// entry_id,
// });
self.selection = Some(Selection {
worktree_id,
entry_id,
});
// let mut menu_entries = Vec::new();
// if let Some((worktree, entry)) = self.selected_entry(cx) {
// let is_root = Some(entry) == worktree.root_entry();
// if !project.is_remote() {
// menu_entries.push(ContextMenuItem::action(
// "Add Folder to Project",
// workspace::AddFolderToProject,
// ));
// if is_root {
// let project = self.project.clone();
// menu_entries.push(ContextMenuItem::handler("Remove from Project", move |cx| {
// project.update(cx, |project, cx| project.remove_worktree(worktree_id, cx));
// }));
// }
// }
// menu_entries.push(ContextMenuItem::action("New File", NewFile));
// menu_entries.push(ContextMenuItem::action("New Folder", NewDirectory));
// menu_entries.push(ContextMenuItem::Separator);
// menu_entries.push(ContextMenuItem::action("Cut", Cut));
// menu_entries.push(ContextMenuItem::action("Copy", Copy));
// if let Some(clipboard_entry) = self.clipboard_entry {
// if clipboard_entry.worktree_id() == worktree.id() {
// menu_entries.push(ContextMenuItem::action("Paste", Paste));
// }
// }
// menu_entries.push(ContextMenuItem::Separator);
// menu_entries.push(ContextMenuItem::action("Copy Path", CopyPath));
// menu_entries.push(ContextMenuItem::action(
// "Copy Relative Path",
// CopyRelativePath,
// ));
if let Some((worktree, entry)) = self.selected_entry(cx) {
let is_root = Some(entry) == worktree.root_entry();
let is_dir = entry.is_dir();
let worktree_id = worktree.id();
let is_local = project.is_local();
// if entry.is_dir() {
// menu_entries.push(ContextMenuItem::Separator);
// }
// menu_entries.push(ContextMenuItem::action("Reveal in Finder", RevealInFinder));
// if entry.is_dir() {
// menu_entries.push(ContextMenuItem::action("Open in Terminal", OpenInTerminal));
// menu_entries.push(ContextMenuItem::action(
// "Search Inside",
// NewSearchInDirectory,
// ));
// }
let context_menu = ContextMenu::build(cx, |mut menu, cx| {
if is_local {
menu = menu.action(
"Add Folder to Project",
Box::new(workspace::AddFolderToProject),
cx,
);
if is_root {
menu = menu.entry(
"Remove from Project",
cx.handler_for(&this, move |this, cx| {
this.project.update(cx, |project, cx| {
project.remove_worktree(worktree_id, cx)
});
}),
);
}
}
// menu_entries.push(ContextMenuItem::Separator);
// menu_entries.push(ContextMenuItem::action("Rename", Rename));
// if !is_root {
// menu_entries.push(ContextMenuItem::action("Delete", Delete));
// }
// }
menu = menu
.action("New File", Box::new(NewFile), cx)
.action("New Folder", Box::new(NewDirectory), cx)
.separator()
.action("Cut", Box::new(Cut), cx)
.action("Copy", Box::new(Copy), cx);
// // self.context_menu.update(cx, |menu, cx| {
// // menu.show(position, AnchorCorner::TopLeft, menu_entries, cx);
// // });
if let Some(clipboard_entry) = self.clipboard_entry {
if clipboard_entry.worktree_id() == worktree_id {
menu = menu.action("Paste", Box::new(Paste), cx);
}
}
// cx.notify();
menu = menu
.separator()
.action("Copy Path", Box::new(CopyPath), cx)
.action("Copy Relative Path", Box::new(CopyRelativePath), cx)
.separator()
.action("Reveal in Finder", Box::new(RevealInFinder), cx);
if is_dir {
menu = menu
.action("Open in Terminal", Box::new(OpenInTerminal), cx)
.action("Search Inside", Box::new(NewSearchInDirectory), cx)
}
menu = menu.separator().action("Rename", Box::new(Rename), cx);
if !is_root {
menu = menu.action("Delete", Box::new(Delete), cx);
}
menu
});
cx.focus_view(&context_menu);
let subscription = cx.subscribe(&context_menu, |this, _, _: &DismissEvent, cx| {
this.context_menu.take();
cx.notify();
});
self.context_menu = Some((context_menu, position, subscription));
}
cx.notify();
}
fn expand_selected_entry(&mut self, _: &ExpandSelectedEntry, cx: &mut ViewContext<Self>) {
@ -1268,16 +1283,16 @@ impl ProjectPanel {
let icon = match entry.kind {
EntryKind::File(_) => {
if show_file_icons {
Some(FileAssociations::get_icon(&entry.path, cx))
FileAssociations::get_icon(&entry.path, cx)
} else {
None
}
}
_ => {
if show_folder_icons {
Some(FileAssociations::get_folder_icon(is_expanded, cx))
FileAssociations::get_folder_icon(is_expanded, cx)
} else {
Some(FileAssociations::get_chevron_icon(is_expanded, cx))
FileAssociations::get_chevron_icon(is_expanded, cx)
}
}
};
@ -1334,13 +1349,19 @@ impl ProjectPanel {
}
}
fn render_entry_visual_element(
details: &EntryDetails,
editor: Option<&View<Editor>>,
padding: Pixels,
fn render_entry(
&self,
entry_id: ProjectEntryId,
details: EntryDetails,
// dragged_entry_destination: &mut Option<Arc<Path>>,
cx: &mut ViewContext<Self>,
) -> Div {
) -> ListItem {
let kind = details.kind;
let settings = ProjectPanelSettings::get_global(cx);
let show_editor = details.is_editing && !details.is_processing;
let is_selected = self
.selection
.map_or(false, |selection| selection.entry_id == entry_id);
let theme = cx.theme();
let filename_text_color = details
@ -1353,15 +1374,18 @@ impl ProjectPanel {
})
.unwrap_or(theme.status().info);
h_stack()
ListItem::new(entry_id.to_proto() as usize)
.indent_level(details.depth)
.indent_step_size(px(settings.indent_size))
.selected(is_selected)
.child(if let Some(icon) = &details.icon {
div().child(IconElement::from_path(icon.to_string()))
} else {
div()
})
.child(
if let (Some(editor), true) = (editor, show_editor) {
div().w_full().child(editor.clone())
if let (Some(editor), true) = (Some(&self.filename_editor), show_editor) {
div().h_full().w_full().child(editor.clone())
} else {
div()
.text_color(filename_text_color)
@ -1369,34 +1393,10 @@ impl ProjectPanel {
}
.ml_1(),
)
.pl(padding)
}
fn render_entry(
&self,
entry_id: ProjectEntryId,
details: EntryDetails,
// dragged_entry_destination: &mut Option<Arc<Path>>,
cx: &mut ViewContext<Self>,
) -> Stateful<Div> {
let kind = details.kind;
let settings = ProjectPanelSettings::get_global(cx);
const INDENT_SIZE: Pixels = px(16.0);
let padding = INDENT_SIZE + details.depth as f32 * px(settings.indent_size);
let show_editor = details.is_editing && !details.is_processing;
let is_selected = self
.selection
.map_or(false, |selection| selection.entry_id == entry_id);
Self::render_entry_visual_element(&details, Some(&self.filename_editor), padding, cx)
.id(entry_id.to_proto() as usize)
.w_full()
.cursor_pointer()
.when(is_selected, |this| {
this.bg(cx.theme().colors().element_selected)
})
.hover(|style| style.bg(cx.theme().colors().element_hover))
.on_click(cx.listener(move |this, event: &gpui::ClickEvent, cx| {
if event.down.button == MouseButton::Right {
return;
}
if !show_editor {
if kind.is_dir() {
this.toggle_expanded(entry_id, cx);
@ -1409,12 +1409,9 @@ impl ProjectPanel {
}
}
}))
.on_mouse_down(
MouseButton::Right,
cx.listener(move |this, event: &MouseDownEvent, cx| {
this.deploy_context_menu(event.position, entry_id, cx);
}),
)
.on_secondary_mouse_down(cx.listener(move |this, event: &MouseDownEvent, cx| {
this.deploy_context_menu(event.position, entry_id, cx);
}))
// .on_drop::<ProjectEntryId>(|this, event, cx| {
// this.move_entry(
// *dragged_entry,
@ -1436,6 +1433,7 @@ impl Render for ProjectPanel {
div()
.id("project-panel")
.size_full()
.relative()
.key_context("ProjectPanel")
.on_action(cx.listener(Self::select_next))
.on_action(cx.listener(Self::select_prev))
@ -1479,6 +1477,12 @@ impl Render for ProjectPanel {
.size_full()
.track_scroll(self.list.clone()),
)
.children(self.context_menu.as_ref().map(|(menu, position, _)| {
overlay()
.position(*position)
.anchor(gpui::AnchorCorner::TopLeft)
.child(menu.clone())
}))
} else {
v_stack()
.id("empty-project_panel")

View File

@ -7,23 +7,23 @@ use crate::{
ToggleCaseSensitive, ToggleReplace, ToggleWholeWord,
};
use collections::HashMap;
use editor::Editor;
use editor::{Editor, EditorMode};
use futures::channel::oneshot;
use gpui::{
actions, div, red, Action, AppContext, Div, EventEmitter, InteractiveElement as _, IntoElement,
ParentElement as _, Render, Styled, Subscription, Task, View, ViewContext, VisualContext as _,
WindowContext,
WeakView, WindowContext,
};
use project::search::SearchQuery;
use serde::Deserialize;
use std::{any::Any, sync::Arc};
use ui::{h_stack, ButtonGroup, Icon, IconButton, IconElement};
use ui::{h_stack, Icon, IconButton, IconElement};
use util::ResultExt;
use workspace::{
item::ItemHandle,
searchable::{Direction, SearchEvent, SearchableItemHandle, WeakSearchableItemHandle},
ToolbarItemLocation, ToolbarItemView, Workspace,
ToolbarItemLocation, ToolbarItemView,
};
#[derive(PartialEq, Clone, Deserialize, Default, Action)]
@ -38,7 +38,7 @@ pub enum Event {
}
pub fn init(cx: &mut AppContext) {
cx.observe_new_views(|workspace: &mut Workspace, _| BufferSearchBar::register(workspace))
cx.observe_new_views(|editor: &mut Editor, cx| BufferSearchBar::register(editor, cx))
.detach();
}
@ -187,6 +187,7 @@ impl Render for BufferSearchBar {
})
.on_action(cx.listener(Self::previous_history_query))
.on_action(cx.listener(Self::next_history_query))
.on_action(cx.listener(Self::dismiss))
.w_full()
.p_1()
.child(
@ -213,10 +214,11 @@ impl Render for BufferSearchBar {
.child(
h_stack()
.flex_none()
.child(ButtonGroup::new(vec![
search_button_for_mode(SearchMode::Text),
search_button_for_mode(SearchMode::Regex),
]))
.child(
h_stack()
.child(search_button_for_mode(SearchMode::Text))
.child(search_button_for_mode(SearchMode::Regex)),
)
.when(supported_options.replacement, |this| {
this.child(super::toggle_replace_button(self.replace_enabled))
}),
@ -294,9 +296,19 @@ impl ToolbarItemView for BufferSearchBar {
}
impl BufferSearchBar {
pub fn register(workspace: &mut Workspace) {
workspace.register_action(|workspace, a: &Deploy, cx| {
workspace.active_pane().update(cx, |this, cx| {
pub fn register(editor: &mut Editor, cx: &mut ViewContext<Editor>) {
if editor.mode() != EditorMode::Full {
return;
};
let handle = cx.view().downgrade();
editor.register_action(move |a: &Deploy, cx| {
let Some(pane) = handle.upgrade().and_then(|editor| editor.read(cx).pane(cx)) else {
return;
};
pane.update(cx, |this, cx| {
this.toolbar().update(cx, |this, cx| {
if let Some(search_bar) = this.item_of_type::<BufferSearchBar>() {
search_bar.update(cx, |this, cx| {
@ -316,11 +328,16 @@ impl BufferSearchBar {
});
});
fn register_action<A: Action>(
workspace: &mut Workspace,
editor: &mut Editor,
handle: WeakView<Editor>,
update: fn(&mut BufferSearchBar, &A, &mut ViewContext<BufferSearchBar>),
) {
workspace.register_action(move |workspace, action: &A, cx| {
workspace.active_pane().update(cx, move |this, cx| {
editor.register_action(move |action: &A, cx| {
let Some(pane) = handle.upgrade().and_then(|editor| editor.read(cx).pane(cx))
else {
return;
};
pane.update(cx, move |this, cx| {
this.toolbar().update(cx, move |this, cx| {
if let Some(search_bar) = this.item_of_type::<BufferSearchBar>() {
search_bar.update(cx, move |this, cx| update(this, action, cx));
@ -331,49 +348,76 @@ impl BufferSearchBar {
});
}
register_action(workspace, |this, action: &ToggleCaseSensitive, cx| {
if this.supported_options().case {
this.toggle_case_sensitive(action, cx);
}
});
register_action(workspace, |this, action: &ToggleWholeWord, cx| {
if this.supported_options().word {
this.toggle_whole_word(action, cx);
}
});
register_action(workspace, |this, action: &ToggleReplace, cx| {
if this.supported_options().replacement {
this.toggle_replace(action, cx);
}
});
register_action(workspace, |this, _: &ActivateRegexMode, cx| {
let handle = cx.view().downgrade();
register_action(
editor,
handle.clone(),
|this, action: &ToggleCaseSensitive, cx| {
if this.supported_options().case {
this.toggle_case_sensitive(action, cx);
}
},
);
register_action(
editor,
handle.clone(),
|this, action: &ToggleWholeWord, cx| {
if this.supported_options().word {
this.toggle_whole_word(action, cx);
}
},
);
register_action(
editor,
handle.clone(),
|this, action: &ToggleReplace, cx| {
if this.supported_options().replacement {
this.toggle_replace(action, cx);
}
},
);
register_action(editor, handle.clone(), |this, _: &ActivateRegexMode, cx| {
if this.supported_options().regex {
this.activate_search_mode(SearchMode::Regex, cx);
}
});
register_action(workspace, |this, _: &ActivateTextMode, cx| {
register_action(editor, handle.clone(), |this, _: &ActivateTextMode, cx| {
this.activate_search_mode(SearchMode::Text, cx);
});
register_action(workspace, |this, action: &CycleMode, cx| {
register_action(editor, handle.clone(), |this, action: &CycleMode, cx| {
if this.supported_options().regex {
// If regex is not supported then search has just one mode (text) - in that case there's no point in supporting
// cycling.
this.cycle_mode(action, cx)
}
});
register_action(workspace, |this, action: &SelectNextMatch, cx| {
this.select_next_match(action, cx);
});
register_action(workspace, |this, action: &SelectPrevMatch, cx| {
this.select_prev_match(action, cx);
});
register_action(workspace, |this, action: &SelectAllMatches, cx| {
this.select_all_matches(action, cx);
});
register_action(workspace, |this, _: &editor::Cancel, cx| {
register_action(
editor,
handle.clone(),
|this, action: &SelectNextMatch, cx| {
this.select_next_match(action, cx);
},
);
register_action(
editor,
handle.clone(),
|this, action: &SelectPrevMatch, cx| {
this.select_prev_match(action, cx);
},
);
register_action(
editor,
handle.clone(),
|this, action: &SelectAllMatches, cx| {
this.select_all_matches(action, cx);
},
);
register_action(editor, handle.clone(), |this, _: &editor::Cancel, cx| {
if !this.dismissed {
this.dismiss(&Dismiss, cx);
return;
}
cx.propagate();
});
}
pub fn new(cx: &mut ViewContext<Self>) -> Self {
@ -543,8 +587,7 @@ impl BufferSearchBar {
// let style = theme.search.action_button.clone();
IconButton::new(0, ui::Icon::SelectAll)
.on_click(|_, cx| cx.dispatch_action(Box::new(SelectAllMatches)))
IconButton::new(0, ui::Icon::SelectAll).action(Box::new(SelectAllMatches))
}
pub fn activate_search_mode(&mut self, mode: SearchMode, cx: &mut ViewContext<Self>) {

View File

@ -3,7 +3,8 @@ pub use buffer_search::BufferSearchBar;
use gpui::{actions, Action, AppContext, IntoElement};
pub use mode::SearchMode;
use project::search::SearchQuery;
use ui::ButtonVariant;
use ui::prelude::*;
use ui::{ButtonStyle2, Icon, IconButton};
//pub use project_search::{ProjectSearchBar, ProjectSearchView};
// use theme::components::{
// action_button::Button, svg::Svg, ComponentExt, IconButtonStyle, ToggleIconButtonStyle,
@ -83,35 +84,35 @@ impl SearchOptions {
}
pub fn as_button(&self, active: bool) -> impl IntoElement {
ui::IconButton::new(0, self.icon())
IconButton::new(0, self.icon())
.on_click({
let action = self.to_toggle_action();
move |_, cx| {
cx.dispatch_action(action.boxed_clone());
}
})
.variant(ui::ButtonVariant::Ghost)
.when(active, |button| button.variant(ButtonVariant::Filled))
.style(ButtonStyle2::Subtle)
.when(active, |button| button.style(ButtonStyle2::Filled))
}
}
fn toggle_replace_button(active: bool) -> impl IntoElement {
// todo: add toggle_replace button
ui::IconButton::new(0, ui::Icon::Replace)
IconButton::new(0, Icon::Replace)
.on_click(|_, cx| {
cx.dispatch_action(Box::new(ToggleReplace));
cx.notify();
})
.variant(ui::ButtonVariant::Ghost)
.when(active, |button| button.variant(ButtonVariant::Filled))
.style(ButtonStyle2::Subtle)
.when(active, |button| button.style(ButtonStyle2::Filled))
}
fn render_replace_button(
action: impl Action + 'static + Send + Sync,
icon: ui::Icon,
icon: Icon,
) -> impl IntoElement {
// todo: add tooltip
ui::IconButton::new(0, icon).on_click(move |_, cx| {
IconButton::new(0, icon).on_click(move |_, cx| {
cx.dispatch_action(action.boxed_clone());
})
}

View File

@ -1,12 +1,13 @@
use gpui::{IntoElement, MouseDownEvent, WindowContext};
use ui::{Button, ButtonVariant, IconButton};
use gpui::{ClickEvent, IntoElement, WindowContext};
use ui::prelude::*;
use ui::{Button, IconButton};
use crate::mode::SearchMode;
pub(super) fn render_nav_button(
icon: ui::Icon,
_active: bool,
on_click: impl Fn(&MouseDownEvent, &mut WindowContext) + 'static,
on_click: impl Fn(&ClickEvent, &mut WindowContext) + 'static,
) -> impl IntoElement {
// let tooltip_style = cx.theme().tooltip.clone();
// let cursor_style = if active {
@ -21,15 +22,9 @@ pub(super) fn render_nav_button(
pub(crate) fn render_search_mode_button(
mode: SearchMode,
is_active: bool,
on_click: impl Fn(&MouseDownEvent, &mut WindowContext) + 'static,
on_click: impl Fn(&ClickEvent, &mut WindowContext) + 'static,
) -> Button {
let button_variant = if is_active {
ButtonVariant::Filled
} else {
ButtonVariant::Ghost
};
Button::new(mode.label())
Button::new(mode.label(), mode.label())
.selected(is_active)
.on_click(on_click)
.variant(button_variant)
}

View File

@ -1659,13 +1659,13 @@ fn elixir_lang() -> Arc<Language> {
target: (identifier) @name)
operator: "when")
])
(#match? @name "^(def|defp|defdelegate|defguard|defguardp|defmacro|defmacrop|defn|defnp)$")) @item
(#any-match? @name "^(def|defp|defdelegate|defguard|defguardp|defmacro|defmacrop|defn|defnp)$")) @item
)
(call
target: (identifier) @name
(arguments (alias) @name)
(#match? @name "^(defmodule|defprotocol)$")) @item
(#any-match? @name "^(defmodule|defprotocol)$")) @item
"#,
)
.unwrap(),

View File

@ -2,7 +2,7 @@ use gpui::{
actions, div, prelude::*, Div, FocusHandle, Focusable, KeyBinding, Render, Stateful, View,
WindowContext,
};
use theme2::ActiveTheme;
use ui::prelude::*;
actions!(ActionA, ActionB, ActionC);
@ -33,7 +33,6 @@ impl Render for FocusStory {
let theme = cx.theme();
let color_1 = theme.status().created;
let color_2 = theme.status().modified;
let color_3 = theme.status().deleted;
let color_4 = theme.status().conflict;
let color_5 = theme.status().ignored;
let color_6 = theme.status().renamed;
@ -42,10 +41,10 @@ impl Render for FocusStory {
.id("parent")
.focusable()
.key_context("parent")
.on_action(cx.listener(|_, action: &ActionA, cx| {
.on_action(cx.listener(|_, _action: &ActionA, _cx| {
println!("Action A dispatched on parent");
}))
.on_action(cx.listener(|_, action: &ActionB, cx| {
.on_action(cx.listener(|_, _action: &ActionB, _cx| {
println!("Action B dispatched on parent");
}))
.on_focus(cx.listener(|_, _, _| println!("Parent focused")))
@ -61,7 +60,7 @@ impl Render for FocusStory {
div()
.track_focus(&self.child_1_focus)
.key_context("child-1")
.on_action(cx.listener(|_, action: &ActionB, cx| {
.on_action(cx.listener(|_, _action: &ActionB, _cx| {
println!("Action B dispatched on child 1 during");
}))
.w_full()
@ -83,7 +82,7 @@ impl Render for FocusStory {
div()
.track_focus(&self.child_2_focus)
.key_context("child-2")
.on_action(cx.listener(|_, action: &ActionC, cx| {
.on_action(cx.listener(|_, _action: &ActionC, _cx| {
println!("Action C dispatched on child 2");
}))
.w_full()

View File

@ -9,7 +9,7 @@ pub struct KitchenSinkStory;
impl KitchenSinkStory {
pub fn view(cx: &mut WindowContext) -> View<Self> {
cx.build_view(|cx| Self)
cx.build_view(|_cx| Self)
}
}

View File

@ -4,7 +4,8 @@ use gpui::{
};
use picker::{Picker, PickerDelegate};
use std::sync::Arc;
use theme2::ActiveTheme;
use ui::prelude::*;
use ui::{Label, ListItem};
pub struct PickerStory {
picker: View<Picker<Delegate>>,
@ -36,7 +37,7 @@ impl Delegate {
}
impl PickerDelegate for Delegate {
type ListItem = Div;
type ListItem = ListItem;
fn match_count(&self) -> usize {
self.candidates.len()
@ -50,26 +51,20 @@ impl PickerDelegate for Delegate {
&self,
ix: usize,
selected: bool,
cx: &mut gpui::ViewContext<Picker<Self>>,
) -> Self::ListItem {
let colors = cx.theme().colors();
_cx: &mut gpui::ViewContext<Picker<Self>>,
) -> Option<Self::ListItem> {
let Some(candidate_ix) = self.matches.get(ix) else {
return div();
return None;
};
// TASK: Make StringMatchCandidate::string a SharedString
let candidate = SharedString::from(self.candidates[*candidate_ix].string.clone());
div()
.text_color(colors.text)
.when(selected, |s| {
s.border_l_10().border_color(colors.terminal_ansi_yellow)
})
.hover(|style| {
style
.bg(colors.element_active)
.text_color(colors.text_accent)
})
.child(candidate)
Some(
ListItem::new(ix)
.inset(true)
.selected(selected)
.child(Label::new(candidate)),
)
}
fn selected_index(&self) -> usize {
@ -81,7 +76,7 @@ impl PickerDelegate for Delegate {
cx.notify();
}
fn confirm(&mut self, secondary: bool, cx: &mut gpui::ViewContext<Picker<Self>>) {
fn confirm(&mut self, secondary: bool, _cx: &mut gpui::ViewContext<Picker<Self>>) {
let candidate_ix = self.matches[self.selected_ix];
let candidate = self.candidates[candidate_ix].string.clone();

View File

@ -1,12 +1,12 @@
use gpui::{div, prelude::*, px, Div, Render, SharedString, Stateful, Styled, View, WindowContext};
use theme2::ActiveTheme;
use ui::prelude::*;
use ui::Tooltip;
pub struct ScrollStory;
impl ScrollStory {
pub fn view(cx: &mut WindowContext) -> View<ScrollStory> {
cx.build_view(|cx| ScrollStory)
cx.build_view(|_cx| ScrollStory)
}
}

View File

@ -8,7 +8,7 @@ pub struct TextStory;
impl TextStory {
pub fn view(cx: &mut WindowContext) -> View<Self> {
cx.build_view(|cx| Self)
cx.build_view(|_cx| Self)
}
}
@ -66,7 +66,7 @@ impl Render for TextStory {
}),
]),
)
.on_click(vec![2..4, 1..3, 7..9], |range_ix, cx| {
.on_click(vec![2..4, 1..3, 7..9], |range_ix, _cx| {
println!("Clicked range {range_ix}");
})
)

View File

@ -9,7 +9,7 @@ pub struct ZIndexStory;
impl Render for ZIndexStory {
type Element = Div;
fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
fn render(&mut self, _cx: &mut ViewContext<Self>) -> Self::Element {
Story::container().child(Story::title("z-index")).child(
div()
.flex()
@ -84,7 +84,7 @@ struct ZIndexExample {
impl RenderOnce for ZIndexExample {
type Rendered = Div;
fn render(self, cx: &mut WindowContext) -> Self::Rendered {
fn render(self, _cx: &mut WindowContext) -> Self::Rendered {
div()
.relative()
.size_full()

View File

@ -8,7 +8,6 @@ use clap::ValueEnum;
use gpui::{AnyView, VisualContext};
use strum::{EnumIter, EnumString, IntoEnumIterator};
use ui::prelude::*;
use ui::{AvatarStory, ButtonStory, IconStory, InputStory, LabelStory};
#[derive(Debug, PartialEq, Eq, Clone, Copy, strum::Display, EnumString, EnumIter)]
#[strum(serialize_all = "snake_case")]
@ -17,11 +16,14 @@ pub enum ComponentStory {
Button,
Checkbox,
ContextMenu,
Disclosure,
Focus,
Icon,
Input,
IconButton,
Keybinding,
Label,
List,
ListItem,
Scroll,
Text,
ZIndex,
@ -31,15 +33,18 @@ pub enum ComponentStory {
impl ComponentStory {
pub fn story(&self, cx: &mut WindowContext) -> AnyView {
match self {
Self::Avatar => cx.build_view(|_| AvatarStory).into(),
Self::Button => cx.build_view(|_| ButtonStory).into(),
Self::Avatar => cx.build_view(|_| ui::AvatarStory).into(),
Self::Button => cx.build_view(|_| ui::ButtonStory).into(),
Self::Checkbox => cx.build_view(|_| ui::CheckboxStory).into(),
Self::ContextMenu => cx.build_view(|_| ui::ContextMenuStory).into(),
Self::Disclosure => cx.build_view(|_| ui::DisclosureStory).into(),
Self::Focus => FocusStory::view(cx).into(),
Self::Icon => cx.build_view(|_| IconStory).into(),
Self::Input => cx.build_view(|_| InputStory).into(),
Self::Icon => cx.build_view(|_| ui::IconStory).into(),
Self::IconButton => cx.build_view(|_| ui::IconButtonStory).into(),
Self::Keybinding => cx.build_view(|_| ui::KeybindingStory).into(),
Self::Label => cx.build_view(|_| LabelStory).into(),
Self::Label => cx.build_view(|_| ui::LabelStory).into(),
Self::List => cx.build_view(|_| ui::ListStory).into(),
Self::ListItem => cx.build_view(|_| ui::ListItemStory).into(),
Self::Scroll => ScrollStory::view(cx).into(),
Self::Text => TextStory::view(cx).into(),
Self::ZIndex => cx.build_view(|_| ZIndexStory).into(),

Some files were not shown because too many files have changed in this diff Show More