Merge branch 'main' into copilot

This commit is contained in:
Mikayla Maki 2023-03-30 16:57:18 -07:00
commit 81411b9114
17 changed files with 176 additions and 62 deletions

1
Cargo.lock generated
View file

@ -5074,6 +5074,7 @@ dependencies = [
"settings", "settings",
"smol", "smol",
"text", "text",
"util",
"workspace", "workspace",
] ]

View file

@ -225,8 +225,14 @@ impl Copilot {
let server_path = get_copilot_lsp(http).await?; let server_path = get_copilot_lsp(http).await?;
let node_path = node_runtime.binary_path().await?; let node_path = node_runtime.binary_path().await?;
let arguments: &[OsString] = &[server_path.into(), "--stdio".into()]; let arguments: &[OsString] = &[server_path.into(), "--stdio".into()];
let server = let server = LanguageServer::new(
LanguageServer::new(0, &node_path, arguments, Path::new("/"), cx.clone())?; 0,
&node_path,
arguments,
Path::new("/"),
None,
cx.clone(),
)?;
let server = server.initialize(Default::default()).await?; let server = server.initialize(Default::default()).await?;
let status = server let status = server

View file

@ -48,7 +48,6 @@ impl TabMap {
new_snapshot.version += 1; new_snapshot.version += 1;
} }
let old_max_offset = old_snapshot.suggestion_snapshot.len();
let mut tab_edits = Vec::with_capacity(suggestion_edits.len()); let mut tab_edits = Vec::with_capacity(suggestion_edits.len());
if old_snapshot.tab_size == new_snapshot.tab_size { if old_snapshot.tab_size == new_snapshot.tab_size {
@ -56,50 +55,47 @@ impl TabMap {
// and any subsequent tabs on that line that moved across the tab expansion // and any subsequent tabs on that line that moved across the tab expansion
// boundary. // boundary.
for suggestion_edit in &mut suggestion_edits { for suggestion_edit in &mut suggestion_edits {
let old_end_column = old_snapshot let old_end = old_snapshot
.suggestion_snapshot .suggestion_snapshot
.to_point(suggestion_edit.old.end) .to_point(suggestion_edit.old.end);
.column(); let old_end_row_successor_offset =
let new_end_column = new_snapshot old_snapshot.suggestion_snapshot.to_offset(cmp::min(
SuggestionPoint::new(old_end.row() + 1, 0),
old_snapshot.suggestion_snapshot.max_point(),
));
let new_end = new_snapshot
.suggestion_snapshot .suggestion_snapshot
.to_point(suggestion_edit.new.end) .to_point(suggestion_edit.new.end);
.column();
let mut offset_from_edit = 0; let mut offset_from_edit = 0;
let mut first_tab_offset = None; let mut first_tab_offset = None;
let mut last_tab_with_changed_expansion_offset = None; let mut last_tab_with_changed_expansion_offset = None;
'outer: for chunk in old_snapshot.suggestion_snapshot.chunks( 'outer: for chunk in old_snapshot.suggestion_snapshot.chunks(
suggestion_edit.old.end..old_max_offset, suggestion_edit.old.end..old_end_row_successor_offset,
false, false,
None, None,
None, None,
) { ) {
for (ix, mat) in chunk.text.match_indices(&['\t', '\n']) { for (ix, _) in chunk.text.match_indices('\t') {
let offset_from_edit = offset_from_edit + (ix as u32); let offset_from_edit = offset_from_edit + (ix as u32);
match mat { if first_tab_offset.is_none() {
"\t" => { first_tab_offset = Some(offset_from_edit);
if first_tab_offset.is_none() { }
first_tab_offset = Some(offset_from_edit);
}
let old_column = old_end_column + offset_from_edit; let old_column = old_end.column() + offset_from_edit;
let new_column = new_end_column + offset_from_edit; let new_column = new_end.column() + offset_from_edit;
let was_expanded = old_column < old_snapshot.max_expansion_column; let was_expanded = old_column < old_snapshot.max_expansion_column;
let is_expanded = new_column < new_snapshot.max_expansion_column; let is_expanded = new_column < new_snapshot.max_expansion_column;
if was_expanded != is_expanded { if was_expanded != is_expanded {
last_tab_with_changed_expansion_offset = Some(offset_from_edit); last_tab_with_changed_expansion_offset = Some(offset_from_edit);
} else if !was_expanded && !is_expanded { } else if !was_expanded && !is_expanded {
break 'outer; break 'outer;
}
}
"\n" => break 'outer,
_ => unreachable!(),
} }
} }
offset_from_edit += chunk.text.len() as u32; offset_from_edit += chunk.text.len() as u32;
if old_end_column + offset_from_edit >= old_snapshot.max_expansion_column if old_end.column() + offset_from_edit >= old_snapshot.max_expansion_column
&& new_end_column | offset_from_edit >= new_snapshot.max_expansion_column && new_end.column() + offset_from_edit >= new_snapshot.max_expansion_column
{ {
break; break;
} }

View file

@ -254,6 +254,19 @@ impl App {
self self
} }
/// Handle the application being re-activated when no windows are open.
pub fn on_reopen<F>(&mut self, mut callback: F) -> &mut Self
where
F: 'static + FnMut(&mut MutableAppContext),
{
let cx = self.0.clone();
self.0
.borrow_mut()
.foreground_platform
.on_reopen(Box::new(move || callback(&mut *cx.borrow_mut())));
self
}
pub fn on_event<F>(&mut self, mut callback: F) -> &mut Self pub fn on_event<F>(&mut self, mut callback: F) -> &mut Self
where where
F: 'static + FnMut(Event, &mut MutableAppContext) -> bool, F: 'static + FnMut(Event, &mut MutableAppContext) -> bool,
@ -276,9 +289,7 @@ impl App {
self.0 self.0
.borrow_mut() .borrow_mut()
.foreground_platform .foreground_platform
.on_open_urls(Box::new(move |paths| { .on_open_urls(Box::new(move |urls| callback(urls, &mut *cx.borrow_mut())));
callback(paths, &mut *cx.borrow_mut())
}));
self self
} }

View file

@ -90,6 +90,10 @@ pub(crate) trait ForegroundPlatform {
fn on_become_active(&self, callback: Box<dyn FnMut()>); fn on_become_active(&self, callback: Box<dyn FnMut()>);
fn on_resign_active(&self, callback: Box<dyn FnMut()>); fn on_resign_active(&self, callback: Box<dyn FnMut()>);
fn on_quit(&self, callback: Box<dyn FnMut()>); fn on_quit(&self, callback: Box<dyn FnMut()>);
/// Handle the application being re-activated with no windows open.
fn on_reopen(&self, callback: Box<dyn FnMut()>);
fn on_event(&self, callback: Box<dyn FnMut(Event) -> bool>); fn on_event(&self, callback: Box<dyn FnMut(Event) -> bool>);
fn on_open_urls(&self, callback: Box<dyn FnMut(Vec<String>)>); fn on_open_urls(&self, callback: Box<dyn FnMut(Vec<String>)>);
fn run(&self, on_finish_launching: Box<dyn FnOnce()>); fn run(&self, on_finish_launching: Box<dyn FnOnce()>);

View file

@ -82,6 +82,10 @@ unsafe fn build_classes() {
sel!(applicationDidFinishLaunching:), sel!(applicationDidFinishLaunching:),
did_finish_launching as extern "C" fn(&mut Object, Sel, id), did_finish_launching as extern "C" fn(&mut Object, Sel, id),
); );
decl.add_method(
sel!(applicationShouldHandleReopen:hasVisibleWindows:),
should_handle_reopen as extern "C" fn(&mut Object, Sel, id, bool),
);
decl.add_method( decl.add_method(
sel!(applicationDidBecomeActive:), sel!(applicationDidBecomeActive:),
did_become_active as extern "C" fn(&mut Object, Sel, id), did_become_active as extern "C" fn(&mut Object, Sel, id),
@ -144,6 +148,7 @@ pub struct MacForegroundPlatform(RefCell<MacForegroundPlatformState>);
pub struct MacForegroundPlatformState { pub struct MacForegroundPlatformState {
become_active: Option<Box<dyn FnMut()>>, become_active: Option<Box<dyn FnMut()>>,
resign_active: Option<Box<dyn FnMut()>>, resign_active: Option<Box<dyn FnMut()>>,
reopen: Option<Box<dyn FnMut()>>,
quit: Option<Box<dyn FnMut()>>, quit: Option<Box<dyn FnMut()>>,
event: Option<Box<dyn FnMut(crate::Event) -> bool>>, event: Option<Box<dyn FnMut(crate::Event) -> bool>>,
menu_command: Option<Box<dyn FnMut(&dyn Action)>>, menu_command: Option<Box<dyn FnMut(&dyn Action)>>,
@ -158,15 +163,16 @@ pub struct MacForegroundPlatformState {
impl MacForegroundPlatform { impl MacForegroundPlatform {
pub fn new(foreground: Rc<executor::Foreground>) -> Self { pub fn new(foreground: Rc<executor::Foreground>) -> Self {
Self(RefCell::new(MacForegroundPlatformState { Self(RefCell::new(MacForegroundPlatformState {
become_active: Default::default(), become_active: None,
resign_active: Default::default(), resign_active: None,
quit: Default::default(), reopen: None,
event: Default::default(), quit: None,
menu_command: Default::default(), event: None,
validate_menu_command: Default::default(), menu_command: None,
will_open_menu: Default::default(), validate_menu_command: None,
open_urls: Default::default(), will_open_menu: None,
finish_launching: Default::default(), open_urls: None,
finish_launching: None,
menu_actions: Default::default(), menu_actions: Default::default(),
foreground, foreground,
})) }))
@ -332,6 +338,10 @@ impl platform::ForegroundPlatform for MacForegroundPlatform {
self.0.borrow_mut().quit = Some(callback); self.0.borrow_mut().quit = Some(callback);
} }
fn on_reopen(&self, callback: Box<dyn FnMut()>) {
self.0.borrow_mut().reopen = Some(callback);
}
fn on_event(&self, callback: Box<dyn FnMut(crate::Event) -> bool>) { fn on_event(&self, callback: Box<dyn FnMut(crate::Event) -> bool>) {
self.0.borrow_mut().event = Some(callback); self.0.borrow_mut().event = Some(callback);
} }
@ -943,6 +953,15 @@ extern "C" fn did_finish_launching(this: &mut Object, _: Sel, _: id) {
} }
} }
extern "C" fn should_handle_reopen(this: &mut Object, _: Sel, _: id, has_open_windows: bool) {
if !has_open_windows {
let platform = unsafe { get_foreground_platform(this) };
if let Some(callback) = platform.0.borrow_mut().reopen.as_mut() {
callback();
}
}
}
extern "C" fn did_become_active(this: &mut Object, _: Sel, _: id) { extern "C" fn did_become_active(this: &mut Object, _: Sel, _: id) {
let platform = unsafe { get_foreground_platform(this) }; let platform = unsafe { get_foreground_platform(this) };
if let Some(callback) = platform.0.borrow_mut().become_active.as_mut() { if let Some(callback) = platform.0.borrow_mut().become_active.as_mut() {

View file

@ -61,13 +61,10 @@ impl ForegroundPlatform {
impl super::ForegroundPlatform for ForegroundPlatform { impl super::ForegroundPlatform for ForegroundPlatform {
fn on_become_active(&self, _: Box<dyn FnMut()>) {} fn on_become_active(&self, _: Box<dyn FnMut()>) {}
fn on_resign_active(&self, _: Box<dyn FnMut()>) {} fn on_resign_active(&self, _: Box<dyn FnMut()>) {}
fn on_quit(&self, _: Box<dyn FnMut()>) {} fn on_quit(&self, _: Box<dyn FnMut()>) {}
fn on_reopen(&self, _: Box<dyn FnMut()>) {}
fn on_event(&self, _: Box<dyn FnMut(crate::Event) -> bool>) {} fn on_event(&self, _: Box<dyn FnMut(crate::Event) -> bool>) {}
fn on_open_urls(&self, _: Box<dyn FnMut(Vec<String>)>) {} fn on_open_urls(&self, _: Box<dyn FnMut(Vec<String>)>) {}
fn run(&self, _on_finish_launching: Box<dyn FnOnce()>) { fn run(&self, _on_finish_launching: Box<dyn FnOnce()>) {

View file

@ -19,6 +19,7 @@ use futures::{
use gpui::{executor::Background, MutableAppContext, Task}; use gpui::{executor::Background, MutableAppContext, Task};
use highlight_map::HighlightMap; use highlight_map::HighlightMap;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use lsp::CodeActionKind;
use parking_lot::{Mutex, RwLock}; use parking_lot::{Mutex, RwLock};
use postage::watch; use postage::watch;
use regex::Regex; use regex::Regex;
@ -140,6 +141,10 @@ impl CachedLspAdapter {
self.adapter.cached_server_binary(container_dir).await self.adapter.cached_server_binary(container_dir).await
} }
pub fn code_action_kinds(&self) -> Option<Vec<CodeActionKind>> {
self.adapter.code_action_kinds()
}
pub fn workspace_configuration( pub fn workspace_configuration(
&self, &self,
cx: &mut MutableAppContext, cx: &mut MutableAppContext,
@ -225,6 +230,16 @@ pub trait LspAdapter: 'static + Send + Sync {
None None
} }
fn code_action_kinds(&self) -> Option<Vec<CodeActionKind>> {
Some(vec![
CodeActionKind::EMPTY,
CodeActionKind::QUICKFIX,
CodeActionKind::REFACTOR,
CodeActionKind::REFACTOR_EXTRACT,
CodeActionKind::SOURCE,
])
}
async fn disk_based_diagnostic_sources(&self) -> Vec<String> { async fn disk_based_diagnostic_sources(&self) -> Vec<String> {
Default::default() Default::default()
} }
@ -825,6 +840,7 @@ impl LanguageRegistry {
&binary.path, &binary.path,
&binary.arguments, &binary.arguments,
&root_path, &root_path,
adapter.code_action_kinds(),
cx, cx,
)?; )?;

View file

@ -40,6 +40,7 @@ pub struct LanguageServer {
outbound_tx: channel::Sender<Vec<u8>>, outbound_tx: channel::Sender<Vec<u8>>,
name: String, name: String,
capabilities: ServerCapabilities, capabilities: ServerCapabilities,
code_action_kinds: Option<Vec<CodeActionKind>>,
notification_handlers: Arc<Mutex<HashMap<&'static str, NotificationHandler>>>, notification_handlers: Arc<Mutex<HashMap<&'static str, NotificationHandler>>>,
response_handlers: Arc<Mutex<Option<HashMap<usize, ResponseHandler>>>>, response_handlers: Arc<Mutex<Option<HashMap<usize, ResponseHandler>>>>,
executor: Arc<executor::Background>, executor: Arc<executor::Background>,
@ -110,6 +111,7 @@ impl LanguageServer {
binary_path: &Path, binary_path: &Path,
arguments: &[T], arguments: &[T],
root_path: &Path, root_path: &Path,
code_action_kinds: Option<Vec<CodeActionKind>>,
cx: AsyncAppContext, cx: AsyncAppContext,
) -> Result<Self> { ) -> Result<Self> {
let working_dir = if root_path.is_dir() { let working_dir = if root_path.is_dir() {
@ -135,6 +137,7 @@ impl LanguageServer {
stout, stout,
Some(server), Some(server),
root_path, root_path,
code_action_kinds,
cx, cx,
|notification| { |notification| {
log::info!( log::info!(
@ -160,6 +163,7 @@ impl LanguageServer {
stdout: Stdout, stdout: Stdout,
server: Option<Child>, server: Option<Child>,
root_path: &Path, root_path: &Path,
code_action_kinds: Option<Vec<CodeActionKind>>,
cx: AsyncAppContext, cx: AsyncAppContext,
on_unhandled_notification: F, on_unhandled_notification: F,
) -> Self ) -> Self
@ -197,6 +201,7 @@ impl LanguageServer {
response_handlers, response_handlers,
name: Default::default(), name: Default::default(),
capabilities: Default::default(), capabilities: Default::default(),
code_action_kinds,
next_id: Default::default(), next_id: Default::default(),
outbound_tx, outbound_tx,
executor: cx.background(), executor: cx.background(),
@ -207,6 +212,10 @@ impl LanguageServer {
} }
} }
pub fn code_action_kinds(&self) -> Option<Vec<CodeActionKind>> {
self.code_action_kinds.clone()
}
async fn handle_input<Stdout, F>( async fn handle_input<Stdout, F>(
stdout: Stdout, stdout: Stdout,
mut on_unhandled_notification: F, mut on_unhandled_notification: F,
@ -715,6 +724,7 @@ impl LanguageServer {
stdout_reader, stdout_reader,
None, None,
Path::new("/"), Path::new("/"),
None,
cx.clone(), cx.clone(),
|_| {}, |_| {},
); );
@ -725,6 +735,7 @@ impl LanguageServer {
stdin_reader, stdin_reader,
None, None,
Path::new("/"), Path::new("/"),
None,
cx, cx,
move |msg| { move |msg| {
notifications_tx notifications_tx

View file

@ -3773,7 +3773,7 @@ impl Project {
worktree = file.worktree.clone(); worktree = file.worktree.clone();
buffer_abs_path = file.as_local().map(|f| f.abs_path(cx)); buffer_abs_path = file.as_local().map(|f| f.abs_path(cx));
} else { } else {
return Task::ready(Ok(Default::default())); return Task::ready(Ok(Vec::new()));
}; };
let range = buffer.anchor_before(range.start)..buffer.anchor_before(range.end); let range = buffer.anchor_before(range.start)..buffer.anchor_before(range.end);
@ -3783,13 +3783,13 @@ impl Project {
{ {
server.clone() server.clone()
} else { } else {
return Task::ready(Ok(Default::default())); return Task::ready(Ok(Vec::new()));
}; };
let lsp_range = range_to_lsp(range.to_point_utf16(buffer)); let lsp_range = range_to_lsp(range.to_point_utf16(buffer));
cx.foreground().spawn(async move { cx.foreground().spawn(async move {
if lang_server.capabilities().code_action_provider.is_none() { if lang_server.capabilities().code_action_provider.is_none() {
return Ok(Default::default()); return Ok(Vec::new());
} }
Ok(lang_server Ok(lang_server
@ -3802,13 +3802,7 @@ impl Project {
partial_result_params: Default::default(), partial_result_params: Default::default(),
context: lsp::CodeActionContext { context: lsp::CodeActionContext {
diagnostics: relevant_diagnostics, diagnostics: relevant_diagnostics,
only: Some(vec![ only: lang_server.code_action_kinds(),
lsp::CodeActionKind::EMPTY,
lsp::CodeActionKind::QUICKFIX,
lsp::CodeActionKind::REFACTOR,
lsp::CodeActionKind::REFACTOR_EXTRACT,
lsp::CodeActionKind::SOURCE,
]),
}, },
}) })
.await? .await?

View file

@ -21,3 +21,4 @@ workspace = { path = "../workspace" }
ordered-float = "2.1.1" ordered-float = "2.1.1"
postage = { workspace = true } postage = { workspace = true }
smol = "1.2" smol = "1.2"
util = { path = "../util"}

View file

@ -61,6 +61,7 @@ impl HighlightedWorkspaceLocation {
.paths() .paths()
.iter() .iter()
.map(|path| { .map(|path| {
let path = util::paths::compact(&path);
let highlighted_text = Self::highlights_for_path( let highlighted_text = Self::highlights_for_path(
path.as_ref(), path.as_ref(),
&string_match.positions, &string_match.positions,

View file

@ -6,7 +6,7 @@ publish = false
[lib] [lib]
path = "src/util.rs" path = "src/util.rs"
doctest = false doctest = true
[features] [features]
test-support = ["tempdir", "git2"] test-support = ["tempdir", "git2"]

View file

@ -1,4 +1,4 @@
use std::path::PathBuf; use std::path::{Path, PathBuf};
lazy_static::lazy_static! { lazy_static::lazy_static! {
pub static ref HOME: PathBuf = dirs::home_dir().expect("failed to determine home directory"); pub static ref HOME: PathBuf = dirs::home_dir().expect("failed to determine home directory");
@ -24,3 +24,49 @@ pub mod legacy {
pub static ref KEYMAP: PathBuf = CONFIG_DIR.join("keymap.json"); pub static ref KEYMAP: PathBuf = CONFIG_DIR.join("keymap.json");
} }
} }
/// Compacts a given file path by replacing the user's home directory
/// prefix with a tilde (`~`).
///
/// # Arguments
///
/// * `path` - A reference to a `Path` representing the file path to compact.
///
/// # Examples
///
/// ```
/// use std::path::{Path, PathBuf};
/// use util::paths::compact;
/// let path: PathBuf = [
/// util::paths::HOME.to_string_lossy().to_string(),
/// "some_file.txt".to_string(),
/// ]
/// .iter()
/// .collect();
/// if cfg!(target_os = "linux") || cfg!(target_os = "macos") {
/// assert_eq!(compact(&path).to_str(), Some("~/some_file.txt"));
/// } else {
/// assert_eq!(compact(&path).to_str(), path.to_str());
/// }
/// ```
///
/// # Returns
///
/// * A `PathBuf` containing the compacted file path. If the input path
/// does not have the user's home directory prefix, or if we are not on
/// Linux or macOS, the original path is returned unchanged.
pub fn compact(path: &Path) -> PathBuf {
if cfg!(target_os = "linux") || cfg!(target_os = "macos") {
match path.strip_prefix(HOME.as_path()) {
Ok(relative_path) => {
let mut shortened_path = PathBuf::new();
shortened_path.push("~");
shortened_path.push(relative_path);
shortened_path
}
Err(_) => path.to_path_buf(),
}
} else {
path.to_path_buf()
}
}

View file

@ -87,21 +87,21 @@ pub fn marked_text_ranges_by(
/// 1. To mark a range of text, surround it with the `«` and `»` angle brackets, /// 1. To mark a range of text, surround it with the `«` and `»` angle brackets,
/// which can be typed on a US keyboard with the `alt-|` and `alt-shift-|` keys. /// which can be typed on a US keyboard with the `alt-|` and `alt-shift-|` keys.
/// ///
/// ``` /// ```text
/// foo «selected text» bar /// foo «selected text» bar
/// ``` /// ```
/// ///
/// 2. To mark a single position in the text, use the `ˇ` caron, /// 2. To mark a single position in the text, use the `ˇ` caron,
/// which can be typed on a US keyboard with the `alt-shift-t` key. /// which can be typed on a US keyboard with the `alt-shift-t` key.
/// ///
/// ``` /// ```text
/// the cursors are hereˇ and hereˇ. /// the cursors are hereˇ and hereˇ.
/// ``` /// ```
/// ///
/// 3. To mark a range whose direction is meaningful (like a selection), /// 3. To mark a range whose direction is meaningful (like a selection),
/// put a caron character beside one of its bounds, on the inside: /// put a caron character beside one of its bounds, on the inside:
/// ///
/// ``` /// ```text
/// one «ˇreversed» selection and one «forwardˇ» selection /// one «ˇreversed» selection and one «forwardˇ» selection
/// ``` /// ```
pub fn marked_text_ranges( pub fn marked_text_ranges(

View file

@ -3,6 +3,7 @@ use async_trait::async_trait;
use futures::StreamExt; use futures::StreamExt;
use language::{LanguageServerBinary, LanguageServerName, LspAdapter}; use language::{LanguageServerBinary, LanguageServerName, LspAdapter};
use node_runtime::NodeRuntime; use node_runtime::NodeRuntime;
use lsp::CodeActionKind;
use serde_json::json; use serde_json::json;
use smol::fs; use smol::fs;
use std::{ use std::{
@ -134,6 +135,15 @@ impl LspAdapter for TypeScriptLspAdapter {
.log_err() .log_err()
} }
fn code_action_kinds(&self) -> Option<Vec<CodeActionKind>> {
Some(vec![
CodeActionKind::QUICKFIX,
CodeActionKind::REFACTOR,
CodeActionKind::REFACTOR_EXTRACT,
CodeActionKind::SOURCE,
])
}
async fn label_for_completion( async fn label_for_completion(
&self, &self,
item: &lsp::CompletionItem, item: &lsp::CompletionItem,

View file

@ -103,7 +103,8 @@ fn main() {
.map_err(|_| anyhow!("no listener for open urls requests")) .map_err(|_| anyhow!("no listener for open urls requests"))
.log_err(); .log_err();
} }
}); })
.on_reopen(move |cx| cx.dispatch_global_action(NewFile));
app.run(move |cx| { app.run(move |cx| {
cx.set_global(*RELEASE_CHANNEL); cx.set_global(*RELEASE_CHANNEL);