Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

Merge branch 'main' into copilot-disabled-globs

Max Brunsfeld9d41f83b1b15
diff --cc Cargo.lock
index d1f5631f6f1,54b2868acfe..e2b29c7aeb5
--- a/Cargo.lock
+++ b/Cargo.lock
@@@ -2156,6 -2155,6 +2157,7 @@@ dependencies =
"serde",
"serde_derive",
"settings",
++ "smallvec",
"sysinfo",
"theme",
"tree-sitter-markdown",
diff --cc crates/copilot_button/src/copilot_button.rs
index 93adc95efd1,832bdaf3dae..aa93af4d4c1
--- a/crates/copilot_button/src/copilot_button.rs
+++ b/crates/copilot_button/src/copilot_button.rs
@@@ -1,19 -1,18 +1,19 @@@
+use anyhow::Result;
use context_menu::{ContextMenu, ContextMenuItem};
- use copilot::{Copilot, Reinstall, SignOut, Status};
+ use copilot::{Copilot, SignOut, Status};
-use editor::Editor;
+use editor::{scroll::autoscroll::Autoscroll, Editor};
use gpui::{
elements::*,
platform::{CursorStyle, MouseButton},
- AnyElement, AppContext, Element, Entity, MouseState, Subscription, View, ViewContext,
- ViewHandle, WindowContext,
+ AnyElement, AppContext, AsyncAppContext, Element, Entity, MouseState, Subscription, View,
+ ViewContext, ViewHandle, WeakViewHandle, WindowContext,
};
use settings::{settings_file::SettingsFile, Settings};
-use std::sync::Arc;
-use util::ResultExt;
+use std::{path::Path, sync::Arc};
+use util::{paths, ResultExt};
use workspace::{
- item::ItemHandle, notifications::simple_message_notification::OsOpen, StatusItemView, Toast,
- Workspace,
+ create_and_open_local_file, item::ItemHandle,
- notifications::simple_message_notification::OsOpen, AppState, StatusItemView, Toast, Workspace,
++ notifications::simple_message_notification::OsOpen, StatusItemView, Toast, Workspace,
};
const COPILOT_SETTINGS_URL: &str = "https://github.com/settings/copilot";
@@@ -195,38 -200,7 +203,31 @@@ impl CopilotButton
));
}
- let globally_enabled = cx.global::<Settings>().show_copilot_suggestions(None);
+ if let Some(path) = self.path.as_ref() {
+ let path_enabled = settings.copilot_enabled_for_path(path);
- let app_state = Arc::downgrade(&self.app_state);
+ let path = path.clone();
+ menu_options.push(ContextMenuItem::handler(
+ format!(
+ "{} Suggestions for This Path",
+ if path_enabled { "Hide" } else { "Show" }
+ ),
+ move |cx| {
- if let Some((workspace, app_state)) = cx
- .root_view()
- .clone()
- .downcast::<Workspace>()
- .zip(app_state.upgrade())
- {
++ if let Some(workspace) = cx.root_view().clone().downcast::<Workspace>() {
+ let workspace = workspace.downgrade();
+ cx.spawn(|_, cx| {
+ configure_disabled_globs(
+ workspace,
- app_state,
+ path_enabled.then_some(path.clone()),
+ cx,
+ )
+ })
+ .detach_and_log_err(cx);
+ }
+ },
+ ));
+ }
+
+ let globally_enabled = cx.global::<Settings>().features.copilot;
menu_options.push(ContextMenuItem::handler(
if globally_enabled {
"Hide Suggestions for All Files"
@@@ -300,63 -270,8 +301,62 @@@ impl StatusItemView for CopilotButton
}
}
+async fn configure_disabled_globs(
+ workspace: WeakViewHandle<Workspace>,
- app_state: Arc<AppState>,
+ path_to_disable: Option<Arc<Path>>,
+ mut cx: AsyncAppContext,
+) -> Result<()> {
+ let settings_editor = workspace
+ .update(&mut cx, |_, cx| {
- create_and_open_local_file(&paths::SETTINGS, app_state, cx, || {
++ create_and_open_local_file(&paths::SETTINGS, cx, || {
+ Settings::initial_user_settings_content(&assets::Assets)
+ .as_ref()
+ .into()
+ })
+ })?
+ .await?
+ .downcast::<Editor>()
+ .unwrap();
+
+ settings_editor.downgrade().update(&mut cx, |item, cx| {
+ let text = item.buffer().read(cx).snapshot(cx).text();
+
+ let edits = SettingsFile::update_unsaved(&text, cx, |file| {
+ let copilot = file.copilot.get_or_insert_with(Default::default);
+ let globs = copilot.disabled_globs.get_or_insert_with(|| {
+ cx.global::<Settings>()
+ .copilot
+ .disabled_globs
+ .clone()
+ .iter()
+ .map(|glob| glob.as_str().to_string())
+ .collect::<Vec<_>>()
+ });
+
+ if let Some(path_to_disable) = &path_to_disable {
+ globs.push(path_to_disable.to_string_lossy().into_owned());
+ } else {
+ globs.clear();
+ }
+ });
+
+ if !edits.is_empty() {
+ item.change_selections(Some(Autoscroll::newest()), cx, |selections| {
+ selections.select_ranges(edits.iter().map(|e| e.0.clone()));
+ });
+
+ // When *enabling* a path, don't actually perform an edit, just select the range.
+ if path_to_disable.is_some() {
+ item.edit(edits.iter().cloned(), cx);
+ }
+ }
+ })?;
+
+ anyhow::Ok(())
+}
+
fn toggle_copilot_globally(cx: &mut AppContext) {
- let show_copilot_suggestions = cx.global::<Settings>().show_copilot_suggestions(None);
+ let show_copilot_suggestions = cx.global::<Settings>().show_copilot_suggestions(None, None);
SettingsFile::update(cx, move |file_contents| {
file_contents.editor.show_copilot_suggestions = Some((!show_copilot_suggestions).into())
});
diff --cc crates/editor/src/editor.rs
index 69eb8170ac6,df2ca6f43d8..be57596584e
--- a/crates/editor/src/editor.rs
+++ b/crates/editor/src/editor.rs
@@@ -51,8 -52,8 +52,8 @@@ use itertools::Itertools
pub use language::{char_kind, CharKind};
use language::{
AutoindentMode, BracketPair, Buffer, CodeAction, CodeLabel, Completion, CursorShape,
-- Diagnostic, DiagnosticSeverity, IndentKind, IndentSize, Language, OffsetRangeExt, OffsetUtf16,
-- Point, Selection, SelectionGoal, TransactionId,
++ Diagnostic, DiagnosticSeverity, File, IndentKind, IndentSize, Language, OffsetRangeExt,
++ OffsetUtf16, Point, Selection, SelectionGoal, TransactionId,
};
use link_go_to_definition::{
hide_link_definition, show_link_definition, LinkDefinitionKind, LinkGoToDefinitionState,
@@@ -1356,6 -1378,6 +1378,10 @@@ impl Editor
self.buffer.read(cx).language_at(point, cx)
}
++ pub fn file_at<'a, T: ToOffset>(&self, point: T, cx: &'a AppContext) -> Option<Arc<dyn File>> {
++ self.buffer.read(cx).read(cx).file_at(point).cloned()
++ }
++
pub fn active_excerpt(
&self,
cx: &AppContext,
@@@ -6846,11 -6844,28 +6863,31 @@@
let extension = Path::new(file.file_name(cx))
.extension()
.and_then(|e| e.to_str());
- project.read(cx).client().report_event(
- name,
- json!({ "File Extension": extension, "Vim Mode": settings.vim_mode }),
+ let telemetry = project.read(cx).client().telemetry().clone();
+ telemetry.report_mixpanel_event(
+ match name {
+ "open" => "open editor",
+ "save" => "save editor",
+ _ => name,
+ },
+ json!({ "File Extension": extension, "Vim Mode": settings.vim_mode, "In Clickhouse": true }),
settings.telemetry(),
);
+ let event = ClickhouseEvent::Editor {
+ file_extension: extension.map(ToString::to_string),
+ vim_mode: settings.vim_mode,
+ operation: name,
+ copilot_enabled: settings.features.copilot,
+ copilot_enabled_for_language: settings.show_copilot_suggestions(
+ self.language_at(0, cx)
+ .map(|language| language.name())
+ .as_deref(),
++ self.file_at(0, cx)
++ .map(|file| file.path().clone())
++ .as_deref(),
+ ),
+ };
+ telemetry.report_clickhouse_event(event, settings.telemetry())
}
}
diff --cc crates/feedback/Cargo.toml
index e94243bd301,e94243bd301..e74e14ff4ca
--- a/crates/feedback/Cargo.toml
+++ b/crates/feedback/Cargo.toml
@@@ -11,25 -11,25 +11,27 @@@ path = "src/feedback.rs
test-support = []
[dependencies]
--anyhow.workspace = true
client = { path = "../client" }
editor = { path = "../editor" }
language = { path = "../language" }
++gpui = { path = "../gpui" }
++project = { path = "../project" }
++search = { path = "../search" }
++settings = { path = "../settings" }
++theme = { path = "../theme" }
++util = { path = "../util" }
++workspace = { path = "../workspace" }
++
log.workspace = true
futures.workspace = true
--gpui = { path = "../gpui" }
++anyhow.workspace = true
++smallvec.workspace = true
human_bytes = "0.4.1"
isahc = "1.7"
lazy_static.workspace = true
postage.workspace = true
--project = { path = "../project" }
--search = { path = "../search" }
serde.workspace = true
serde_derive.workspace = true
--settings = { path = "../settings" }
sysinfo = "0.27.1"
--theme = { path = "../theme" }
tree-sitter-markdown = { git = "https://github.com/MDeiml/tree-sitter-markdown", rev = "330ecab87a3e3a7211ac69bbadc19eabecdb1cca" }
urlencoding = "2.1.2"
--util = { path = "../util" }
--workspace = { path = "../workspace" }
diff --cc crates/feedback/src/feedback_editor.rs
index 7bf53280486,2e513f6f06d..d5d20b069ac
--- a/crates/feedback/src/feedback_editor.rs
+++ b/crates/feedback/src/feedback_editor.rs
@@@ -19,18 -13,22 +13,22 @@@ use gpui::
use isahc::Request;
use language::Buffer;
use postage::prelude::Stream;
-
use project::Project;
use serde::Serialize;
++use smallvec::SmallVec;
+ use std::{
+ any::TypeId,
+ borrow::Cow,
+ ops::{Range, RangeInclusive},
+ sync::Arc,
+ };
use util::ResultExt;
use workspace::{
- item::{Item, ItemHandle},
+ item::{Item, ItemEvent, ItemHandle},
searchable::{SearchableItem, SearchableItemHandle},
- AppState, Workspace,
- smallvec::SmallVec,
+ Workspace,
};
- use crate::{submit_feedback_button::SubmitFeedbackButton, system_specs::SystemSpecs};
-
const FEEDBACK_CHAR_LIMIT: RangeInclusive<usize> = 10..=5000;
const FEEDBACK_SUBMISSION_ERROR_TEXT: &str =
"Feedback failed to submit, see error log for details.";
diff --cc crates/workspace/src/workspace.rs
index 64fa9c70629,5a008537376..bb2629862d3
--- a/crates/workspace/src/workspace.rs
+++ b/crates/workspace/src/workspace.rs
@@@ -42,11 -43,12 +42,12 @@@ use gpui::
CursorStyle, MouseButton, PathPromptOptions, Platform, PromptLevel, WindowBounds,
WindowOptions,
},
- Action, AnyModelHandle, AnyViewHandle, AppContext, AsyncAppContext, Entity, ModelContext,
- ModelHandle, SizeConstraint, Subscription, Task, View, ViewContext, ViewHandle, WeakViewHandle,
+ AnyModelHandle, AnyViewHandle, AppContext, AsyncAppContext, Entity, ModelContext, ModelHandle,
+ SizeConstraint, Subscription, Task, View, ViewContext, ViewHandle, WeakViewHandle,
+ WindowContext,
};
use item::{FollowableItem, FollowableItemHandle, Item, ItemHandle, ProjectItem};
-use language::LanguageRegistry;
+use language::{LanguageRegistry, Rope};
use std::{
any::TypeId,
borrow::Cow,
@@@ -316,18 -266,6 +268,17 @@@ pub fn init(app_state: Arc<AppState>, c
.detach();
});
- cx.add_action({
- let app_state = app_state.clone();
++ cx.add_action(
+ move |_: &mut Workspace, _: &OpenSettings, cx: &mut ViewContext<Workspace>| {
- create_and_open_local_file(&paths::SETTINGS, app_state.clone(), cx, || {
++ create_and_open_local_file(&paths::SETTINGS, cx, || {
+ Settings::initial_user_settings_content(&Assets)
+ .as_ref()
+ .into()
+ })
+ .detach_and_log_err(cx);
- }
- });
++ },
++ );
+
let client = &app_state.client;
client.add_view_request_handler(Workspace::handle_follow);
client.add_view_message_handler(Workspace::handle_unfollow);
@@@ -2995,34 -2955,6 +2968,33 @@@ pub fn open_new
})
}
+pub fn create_and_open_local_file(
+ path: &'static Path,
- app_state: Arc<AppState>,
+ cx: &mut ViewContext<Workspace>,
+ default_content: impl 'static + Send + FnOnce() -> Rope,
+) -> Task<Result<Box<dyn ItemHandle>>> {
+ cx.spawn(|workspace, mut cx| async move {
- let fs = &app_state.fs;
++ let fs = workspace.read_with(&cx, |workspace, _| workspace.app_state().fs.clone())?;
+ if !fs.is_file(path).await {
+ fs.create_file(path, Default::default()).await?;
+ fs.save(path, &default_content(), Default::default())
+ .await?;
+ }
+
+ let mut items = workspace
+ .update(&mut cx, |workspace, cx| {
- workspace.with_local_workspace(&app_state, cx, |workspace, cx| {
++ workspace.with_local_workspace(cx, |workspace, cx| {
+ workspace.open_paths(vec![path.to_path_buf()], false, cx)
+ })
+ })?
+ .await?
+ .await;
+
+ let item = items.pop().flatten();
+ item.ok_or_else(|| anyhow!("path {path:?} is not a file"))?
+ })
+}
+
pub fn join_remote_project(
project_id: u64,
follow_user_id: u64,
diff --cc crates/zed/src/main.rs
index 771775de57b,7a66953cff2..f498078b520
--- a/crates/zed/src/main.rs
+++ b/crates/zed/src/main.rs
@@@ -43,10 -52,9 +52,10 @@@ use staff_mode::StaffMode
use theme::ThemeRegistry;
use util::{channel::RELEASE_CHANNEL, paths, ResultExt, TryFutureExt};
use workspace::{
- self, dock::FocusDock, item::ItemHandle, notifications::NotifyResultExt, AppState, NewFile,
- OpenSettings, Workspace,
- self, dock::FocusDock, item::ItemHandle, notifications::NotifyResultExt, AppState, Workspace,
++ dock::FocusDock, item::ItemHandle, notifications::NotifyResultExt, AppState, OpenSettings,
++ Workspace,
};
-use zed::{self, build_window_options, initialize_workspace, languages, menus, OpenSettings};
+use zed::{self, build_window_options, initialize_workspace, languages, menus};
fn main() {
let http = http::client();
diff --cc crates/zed/src/zed.rs
index 4478a88837c,a28f1a9fb35..f687237bd22
--- a/crates/zed/src/zed.rs
+++ b/crates/zed/src/zed.rs
@@@ -20,8 -21,9 +21,8 @@@ use gpui::
geometry::vector::vec2f,
impl_actions,
platform::{Platform, PromptLevel, TitlebarOptions, WindowBounds, WindowKind, WindowOptions},
- ViewContext,
- AppContext, AssetSource, ViewContext,
++ AppContext, ViewContext,
};
-use language::Rope;
pub use lsp;
pub use project;
use project_panel::ProjectPanel;
@@@ -34,7 -36,7 +35,10 @@@ use terminal_view::terminal_button::Ter
use util::{channel::ReleaseChannel, paths, ResultExt};
use uuid::Uuid;
pub use workspace;
- use workspace::{create_and_open_local_file, sidebar::SidebarSide, AppState, Restart, Workspace};
-use workspace::{open_new, sidebar::SidebarSide, AppState, NewFile, NewWindow, Workspace};
++use workspace::{
++ create_and_open_local_file, open_new, sidebar::SidebarSide, AppState, NewFile, NewWindow,
++ Workspace,
++};
#[derive(Deserialize, Clone, PartialEq)]
pub struct OpenBrowser {
@@@ -146,55 -148,64 +149,50 @@@ pub fn init(app_state: &Arc<AppState>,
})
.detach_and_log_err(cx);
});
- cx.add_action({
- let app_state = app_state.clone();
- cx.add_action(
- move |workspace: &mut Workspace, _: &OpenSettings, cx: &mut ViewContext<Workspace>| {
- open_config_file(workspace, &paths::SETTINGS, cx, || {
- str::from_utf8(
- Assets
- .load("settings/initial_user_settings.json")
- .unwrap()
- .as_ref(),
- )
- .unwrap()
- .into()
- });
- },
- );
+ cx.add_action(
move |workspace: &mut Workspace, _: &OpenLog, cx: &mut ViewContext<Workspace>| {
- open_log_file(workspace, app_state.clone(), cx);
- }
- });
- cx.add_action({
- let app_state = app_state.clone();
- move |_: &mut Workspace, _: &OpenLicenses, cx: &mut ViewContext<Workspace>| {
+ open_log_file(workspace, cx);
+ },
+ );
+ cx.add_action(
+ move |workspace: &mut Workspace, _: &OpenLicenses, cx: &mut ViewContext<Workspace>| {
open_bundled_file(
- app_state.clone(),
+ workspace,
"licenses.md",
"Open Source License Attribution",
"Markdown",
cx,
);
- }
- });
- cx.add_action({
- let app_state = app_state.clone();
+ },
+ );
+ cx.add_action(
move |workspace: &mut Workspace, _: &OpenTelemetryLog, cx: &mut ViewContext<Workspace>| {
- open_telemetry_log_file(workspace, app_state.clone(), cx);
- }
- });
- cx.add_action({
- let app_state = app_state.clone();
+ open_telemetry_log_file(workspace, cx);
+ },
+ );
+ cx.add_action(
- move |workspace: &mut Workspace, _: &OpenKeymap, cx: &mut ViewContext<Workspace>| {
- open_config_file(workspace, &paths::KEYMAP, cx, Default::default);
+ move |_: &mut Workspace, _: &OpenKeymap, cx: &mut ViewContext<Workspace>| {
- create_and_open_local_file(&paths::KEYMAP, app_state.clone(), cx, Default::default)
- .detach_and_log_err(cx);
- }
- });
- cx.add_action({
- let app_state = app_state.clone();
- move |_: &mut Workspace, _: &OpenDefaultKeymap, cx: &mut ViewContext<Workspace>| {
++ create_and_open_local_file(&paths::KEYMAP, cx, Default::default).detach_and_log_err(cx);
+ },
+ );
+ cx.add_action(
+ move |workspace: &mut Workspace, _: &OpenDefaultKeymap, cx: &mut ViewContext<Workspace>| {
open_bundled_file(
- app_state.clone(),
+ workspace,
"keymaps/default.json",
"Default Key Bindings",
"JSON",
cx,
);
- }
- });
- cx.add_action({
- let app_state = app_state.clone();
- move |_: &mut Workspace, _: &OpenDefaultSettings, cx: &mut ViewContext<Workspace>| {
+ },
+ );
+ cx.add_action(
+ move |workspace: &mut Workspace,
+ _: &OpenDefaultSettings,
+ cx: &mut ViewContext<Workspace>| {
open_bundled_file(
- app_state.clone(),
+ workspace,
- "settings/default.json",
+ DEFAULT_SETTINGS_ASSET_PATH,
"Default Settings",
"JSON",
cx,
@@@ -464,11 -450,34 +437,7 @@@ fn about(_: &mut Workspace, _: &About,
cx.prompt(PromptLevel::Info, &format!("{app_name} {version}"), &["OK"]);
}
- fn open_log_file(
-fn open_config_file(
-- workspace: &mut Workspace,
- app_state: Arc<AppState>,
- path: &'static Path,
-- cx: &mut ViewContext<Workspace>,
- default_content: impl 'static + Send + FnOnce() -> Rope,
--) {
- let fs = workspace.app_state().fs.clone();
- cx.spawn(|workspace, mut cx| async move {
- if !fs.is_file(path).await {
- fs.create_file(path, Default::default()).await?;
- fs.save(path, &default_content(), Default::default())
- .await?;
- }
-
- workspace
- .update(&mut cx, |workspace, cx| {
- workspace.with_local_workspace(cx, |workspace, cx| {
- workspace.open_paths(vec![path.to_path_buf()], false, cx)
- })
- })?
- .await?
- .await;
- Ok::<_, anyhow::Error>(())
- })
- .detach_and_log_err(cx)
-}
-
+ fn open_log_file(workspace: &mut Workspace, cx: &mut ViewContext<Workspace>) {
const MAX_LINES: usize = 1000;
workspace