Broadcast completion triggers to remote participants
This commit is contained in:
@@ -911,22 +911,11 @@ impl MultiBuffer {
|
|||||||
let snapshot = self.snapshot(cx);
|
let snapshot = self.snapshot(cx);
|
||||||
let anchor = snapshot.anchor_before(position);
|
let anchor = snapshot.anchor_before(position);
|
||||||
let buffer = self.buffers.borrow()[&anchor.buffer_id].buffer.clone();
|
let buffer = self.buffers.borrow()[&anchor.buffer_id].buffer.clone();
|
||||||
if let Some(language_server) = buffer.read(cx).language_server() {
|
buffer
|
||||||
language_server
|
.read(cx)
|
||||||
.capabilities()
|
.completion_triggers()
|
||||||
.completion_provider
|
.iter()
|
||||||
.as_ref()
|
.any(|string| string == text)
|
||||||
.map_or(false, |provider| {
|
|
||||||
provider
|
|
||||||
.trigger_characters
|
|
||||||
.as_ref()
|
|
||||||
.map_or(false, |characters| {
|
|
||||||
characters.iter().any(|string| string == text)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn apply_additional_edits_for_completion(
|
pub fn apply_additional_edits_for_completion(
|
||||||
|
|||||||
+108
-62
@@ -74,6 +74,7 @@ pub struct Buffer {
|
|||||||
selections_update_count: usize,
|
selections_update_count: usize,
|
||||||
diagnostics_update_count: usize,
|
diagnostics_update_count: usize,
|
||||||
language_server: Option<LanguageServerState>,
|
language_server: Option<LanguageServerState>,
|
||||||
|
completion_triggers: Vec<String>,
|
||||||
deferred_ops: OperationQueue<Operation>,
|
deferred_ops: OperationQueue<Operation>,
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
pub(crate) operations: Vec<Operation>,
|
pub(crate) operations: Vec<Operation>,
|
||||||
@@ -126,7 +127,7 @@ struct LanguageServerState {
|
|||||||
latest_snapshot: watch::Sender<Option<LanguageServerSnapshot>>,
|
latest_snapshot: watch::Sender<Option<LanguageServerSnapshot>>,
|
||||||
pending_snapshots: BTreeMap<usize, LanguageServerSnapshot>,
|
pending_snapshots: BTreeMap<usize, LanguageServerSnapshot>,
|
||||||
next_version: usize,
|
next_version: usize,
|
||||||
_maintain_server: Task<Option<()>>,
|
_maintain_server: Task<()>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
@@ -148,6 +149,9 @@ pub enum Operation {
|
|||||||
selections: Arc<[Selection<Anchor>]>,
|
selections: Arc<[Selection<Anchor>]>,
|
||||||
lamport_timestamp: clock::Lamport,
|
lamport_timestamp: clock::Lamport,
|
||||||
},
|
},
|
||||||
|
UpdateCompletionTriggers {
|
||||||
|
triggers: Vec<String>,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||||
@@ -448,6 +452,8 @@ impl Buffer {
|
|||||||
cx,
|
cx,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
this.completion_triggers = message.completion_triggers;
|
||||||
|
|
||||||
let deferred_ops = message
|
let deferred_ops = message
|
||||||
.deferred_operations
|
.deferred_operations
|
||||||
.into_iter()
|
.into_iter()
|
||||||
@@ -496,6 +502,7 @@ impl Buffer {
|
|||||||
.map(|op| proto::serialize_operation(&Operation::Buffer(op.clone()))),
|
.map(|op| proto::serialize_operation(&Operation::Buffer(op.clone()))),
|
||||||
)
|
)
|
||||||
.collect(),
|
.collect(),
|
||||||
|
completion_triggers: self.completion_triggers.clone(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -538,6 +545,7 @@ impl Buffer {
|
|||||||
diagnostics: Default::default(),
|
diagnostics: Default::default(),
|
||||||
diagnostics_update_count: 0,
|
diagnostics_update_count: 0,
|
||||||
language_server: None,
|
language_server: None,
|
||||||
|
completion_triggers: Default::default(),
|
||||||
deferred_ops: OperationQueue::new(),
|
deferred_ops: OperationQueue::new(),
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
operations: Default::default(),
|
operations: Default::default(),
|
||||||
@@ -639,75 +647,102 @@ impl Buffer {
|
|||||||
cx: &mut ModelContext<Self>,
|
cx: &mut ModelContext<Self>,
|
||||||
) {
|
) {
|
||||||
self.language_server = if let Some(server) = language_server {
|
self.language_server = if let Some(server) = language_server {
|
||||||
let (latest_snapshot_tx, mut latest_snapshot_rx) = watch::channel();
|
let (latest_snapshot_tx, mut latest_snapshot_rx) =
|
||||||
|
watch::channel::<Option<LanguageServerSnapshot>>();
|
||||||
|
|
||||||
|
let maintain_changes = cx.background().spawn({
|
||||||
|
let server = server.clone();
|
||||||
|
async move {
|
||||||
|
let mut prev_snapshot: Option<LanguageServerSnapshot> = None;
|
||||||
|
while let Some(snapshot) = latest_snapshot_rx.recv().await {
|
||||||
|
if let Some(snapshot) = snapshot {
|
||||||
|
let uri = lsp::Url::from_file_path(&snapshot.path).unwrap();
|
||||||
|
if let Some(prev_snapshot) = prev_snapshot {
|
||||||
|
let changes = lsp::DidChangeTextDocumentParams {
|
||||||
|
text_document: lsp::VersionedTextDocumentIdentifier::new(
|
||||||
|
uri,
|
||||||
|
snapshot.version as i32,
|
||||||
|
),
|
||||||
|
content_changes: snapshot
|
||||||
|
.buffer_snapshot
|
||||||
|
.edits_since::<(PointUtf16, usize)>(
|
||||||
|
prev_snapshot.buffer_snapshot.version(),
|
||||||
|
)
|
||||||
|
.map(|edit| {
|
||||||
|
let edit_start = edit.new.start.0;
|
||||||
|
let edit_end =
|
||||||
|
edit_start + (edit.old.end.0 - edit.old.start.0);
|
||||||
|
let new_text = snapshot
|
||||||
|
.buffer_snapshot
|
||||||
|
.text_for_range(edit.new.start.1..edit.new.end.1)
|
||||||
|
.collect();
|
||||||
|
lsp::TextDocumentContentChangeEvent {
|
||||||
|
range: Some(lsp::Range::new(
|
||||||
|
edit_start.to_lsp_position(),
|
||||||
|
edit_end.to_lsp_position(),
|
||||||
|
)),
|
||||||
|
range_length: None,
|
||||||
|
text: new_text,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
|
};
|
||||||
|
server
|
||||||
|
.notify::<lsp::notification::DidChangeTextDocument>(changes)
|
||||||
|
.await?;
|
||||||
|
} else {
|
||||||
|
server
|
||||||
|
.notify::<lsp::notification::DidOpenTextDocument>(
|
||||||
|
lsp::DidOpenTextDocumentParams {
|
||||||
|
text_document: lsp::TextDocumentItem::new(
|
||||||
|
uri,
|
||||||
|
Default::default(),
|
||||||
|
snapshot.version as i32,
|
||||||
|
snapshot.buffer_snapshot.text().to_string(),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
|
||||||
|
prev_snapshot = Some(snapshot);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
Some(LanguageServerState {
|
Some(LanguageServerState {
|
||||||
latest_snapshot: latest_snapshot_tx,
|
latest_snapshot: latest_snapshot_tx,
|
||||||
pending_snapshots: Default::default(),
|
pending_snapshots: Default::default(),
|
||||||
next_version: 0,
|
next_version: 0,
|
||||||
server: server.clone(),
|
server: server.clone(),
|
||||||
_maintain_server: cx.background().spawn(
|
_maintain_server: cx.spawn_weak(|this, mut cx| async move {
|
||||||
async move {
|
let mut capabilities = server.capabilities();
|
||||||
let mut prev_snapshot: Option<LanguageServerSnapshot> = None;
|
loop {
|
||||||
while let Some(snapshot) = latest_snapshot_rx.recv().await {
|
if let Some(capabilities) = capabilities.recv().await.flatten() {
|
||||||
if let Some(snapshot) = snapshot {
|
if let Some(this) = this.upgrade(&cx) {
|
||||||
let uri = lsp::Url::from_file_path(&snapshot.path).unwrap();
|
let triggers = capabilities
|
||||||
if let Some(prev_snapshot) = prev_snapshot {
|
.completion_provider
|
||||||
let changes = lsp::DidChangeTextDocumentParams {
|
.and_then(|c| c.trigger_characters)
|
||||||
text_document: lsp::VersionedTextDocumentIdentifier::new(
|
.unwrap_or_default();
|
||||||
uri,
|
this.update(&mut cx, |this, cx| {
|
||||||
snapshot.version as i32,
|
this.completion_triggers = triggers.clone();
|
||||||
),
|
this.send_operation(
|
||||||
content_changes: snapshot
|
Operation::UpdateCompletionTriggers { triggers },
|
||||||
.buffer_snapshot
|
cx,
|
||||||
.edits_since::<(PointUtf16, usize)>(
|
);
|
||||||
prev_snapshot.buffer_snapshot.version(),
|
});
|
||||||
)
|
} else {
|
||||||
.map(|edit| {
|
return;
|
||||||
let edit_start = edit.new.start.0;
|
|
||||||
let edit_end = edit_start
|
|
||||||
+ (edit.old.end.0 - edit.old.start.0);
|
|
||||||
let new_text = snapshot
|
|
||||||
.buffer_snapshot
|
|
||||||
.text_for_range(
|
|
||||||
edit.new.start.1..edit.new.end.1,
|
|
||||||
)
|
|
||||||
.collect();
|
|
||||||
lsp::TextDocumentContentChangeEvent {
|
|
||||||
range: Some(lsp::Range::new(
|
|
||||||
edit_start.to_lsp_position(),
|
|
||||||
edit_end.to_lsp_position(),
|
|
||||||
)),
|
|
||||||
range_length: None,
|
|
||||||
text: new_text,
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.collect(),
|
|
||||||
};
|
|
||||||
server
|
|
||||||
.notify::<lsp::notification::DidChangeTextDocument>(changes)
|
|
||||||
.await?;
|
|
||||||
} else {
|
|
||||||
server
|
|
||||||
.notify::<lsp::notification::DidOpenTextDocument>(
|
|
||||||
lsp::DidOpenTextDocumentParams {
|
|
||||||
text_document: lsp::TextDocumentItem::new(
|
|
||||||
uri,
|
|
||||||
Default::default(),
|
|
||||||
snapshot.version as i32,
|
|
||||||
snapshot.buffer_snapshot.text().to_string(),
|
|
||||||
),
|
|
||||||
},
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
}
|
|
||||||
|
|
||||||
prev_snapshot = Some(snapshot);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
.log_err(),
|
|
||||||
),
|
maintain_changes.log_err().await;
|
||||||
|
}),
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
@@ -1591,6 +1626,7 @@ impl Buffer {
|
|||||||
Operation::UpdateSelections { selections, .. } => selections
|
Operation::UpdateSelections { selections, .. } => selections
|
||||||
.iter()
|
.iter()
|
||||||
.all(|s| self.can_resolve(&s.start) && self.can_resolve(&s.end)),
|
.all(|s| self.can_resolve(&s.start) && self.can_resolve(&s.end)),
|
||||||
|
Operation::UpdateCompletionTriggers { .. } => true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1630,6 +1666,9 @@ impl Buffer {
|
|||||||
self.text.lamport_clock.observe(lamport_timestamp);
|
self.text.lamport_clock.observe(lamport_timestamp);
|
||||||
self.selections_update_count += 1;
|
self.selections_update_count += 1;
|
||||||
}
|
}
|
||||||
|
Operation::UpdateCompletionTriggers { triggers } => {
|
||||||
|
self.completion_triggers = triggers;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1812,6 +1851,10 @@ impl Buffer {
|
|||||||
Ok::<_, anyhow::Error>(())
|
Ok::<_, anyhow::Error>(())
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn completion_triggers(&self) -> &[String] {
|
||||||
|
&self.completion_triggers
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(any(test, feature = "test-support"))]
|
#[cfg(any(test, feature = "test-support"))]
|
||||||
@@ -2529,6 +2572,9 @@ impl operation_queue::Operation for Operation {
|
|||||||
| Operation::UpdateSelections {
|
| Operation::UpdateSelections {
|
||||||
lamport_timestamp, ..
|
lamport_timestamp, ..
|
||||||
} => *lamport_timestamp,
|
} => *lamport_timestamp,
|
||||||
|
Operation::UpdateCompletionTriggers { .. } => {
|
||||||
|
unreachable!("updating completion triggers should never be deferred")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,6 +58,13 @@ pub fn serialize_operation(operation: &Operation) -> proto::Operation {
|
|||||||
lamport_timestamp: lamport_timestamp.value,
|
lamport_timestamp: lamport_timestamp.value,
|
||||||
diagnostics: serialize_diagnostics(diagnostics.iter()),
|
diagnostics: serialize_diagnostics(diagnostics.iter()),
|
||||||
}),
|
}),
|
||||||
|
Operation::UpdateCompletionTriggers { triggers } => {
|
||||||
|
proto::operation::Variant::UpdateCompletionTriggers(
|
||||||
|
proto::operation::UpdateCompletionTriggers {
|
||||||
|
triggers: triggers.clone(),
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -238,6 +245,11 @@ pub fn deserialize_operation(message: proto::Operation) -> Result<Operation> {
|
|||||||
value: message.lamport_timestamp,
|
value: message.lamport_timestamp,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
proto::operation::Variant::UpdateCompletionTriggers(message) => {
|
||||||
|
Operation::UpdateCompletionTriggers {
|
||||||
|
triggers: message.triggers,
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-10
@@ -1,8 +1,8 @@
|
|||||||
use anyhow::{anyhow, Context, Result};
|
use anyhow::{anyhow, Context, Result};
|
||||||
use futures::{io::BufWriter, AsyncRead, AsyncWrite};
|
use futures::{io::BufWriter, AsyncRead, AsyncWrite};
|
||||||
use gpui::{executor, Task};
|
use gpui::{executor, Task};
|
||||||
use parking_lot::{Mutex, RwLock, RwLockReadGuard};
|
use parking_lot::{Mutex, RwLock};
|
||||||
use postage::{barrier, oneshot, prelude::Stream, sink::Sink};
|
use postage::{barrier, oneshot, prelude::Stream, sink::Sink, watch};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::{json, value::RawValue, Value};
|
use serde_json::{json, value::RawValue, Value};
|
||||||
use smol::{
|
use smol::{
|
||||||
@@ -34,7 +34,7 @@ type ResponseHandler = Box<dyn Send + FnOnce(Result<&str, Error>)>;
|
|||||||
pub struct LanguageServer {
|
pub struct LanguageServer {
|
||||||
next_id: AtomicUsize,
|
next_id: AtomicUsize,
|
||||||
outbound_tx: RwLock<Option<channel::Sender<Vec<u8>>>>,
|
outbound_tx: RwLock<Option<channel::Sender<Vec<u8>>>>,
|
||||||
capabilities: RwLock<lsp_types::ServerCapabilities>,
|
capabilities: watch::Receiver<Option<ServerCapabilities>>,
|
||||||
notification_handlers: Arc<RwLock<HashMap<&'static str, NotificationHandler>>>,
|
notification_handlers: Arc<RwLock<HashMap<&'static str, NotificationHandler>>>,
|
||||||
response_handlers: Arc<Mutex<HashMap<usize, ResponseHandler>>>,
|
response_handlers: Arc<Mutex<HashMap<usize, ResponseHandler>>>,
|
||||||
executor: Arc<executor::Background>,
|
executor: Arc<executor::Background>,
|
||||||
@@ -195,10 +195,11 @@ impl LanguageServer {
|
|||||||
);
|
);
|
||||||
|
|
||||||
let (initialized_tx, initialized_rx) = barrier::channel();
|
let (initialized_tx, initialized_rx) = barrier::channel();
|
||||||
|
let (mut capabilities_tx, capabilities_rx) = watch::channel();
|
||||||
let this = Arc::new(Self {
|
let this = Arc::new(Self {
|
||||||
notification_handlers,
|
notification_handlers,
|
||||||
response_handlers,
|
response_handlers,
|
||||||
capabilities: Default::default(),
|
capabilities: capabilities_rx,
|
||||||
next_id: Default::default(),
|
next_id: Default::default(),
|
||||||
outbound_tx: RwLock::new(Some(outbound_tx)),
|
outbound_tx: RwLock::new(Some(outbound_tx)),
|
||||||
executor: executor.clone(),
|
executor: executor.clone(),
|
||||||
@@ -212,7 +213,10 @@ impl LanguageServer {
|
|||||||
.spawn({
|
.spawn({
|
||||||
let this = this.clone();
|
let this = this.clone();
|
||||||
async move {
|
async move {
|
||||||
this.init(root_uri).log_err().await;
|
if let Some(capabilities) = this.init(root_uri).log_err().await {
|
||||||
|
*capabilities_tx.borrow_mut() = Some(capabilities);
|
||||||
|
}
|
||||||
|
|
||||||
drop(initialized_tx);
|
drop(initialized_tx);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -221,7 +225,7 @@ impl LanguageServer {
|
|||||||
Ok(this)
|
Ok(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn init(self: Arc<Self>, root_uri: Url) -> Result<()> {
|
async fn init(self: Arc<Self>, root_uri: Url) -> Result<ServerCapabilities> {
|
||||||
#[allow(deprecated)]
|
#[allow(deprecated)]
|
||||||
let params = InitializeParams {
|
let params = InitializeParams {
|
||||||
process_id: Default::default(),
|
process_id: Default::default(),
|
||||||
@@ -269,12 +273,11 @@ impl LanguageServer {
|
|||||||
params,
|
params,
|
||||||
);
|
);
|
||||||
let response = request.await?;
|
let response = request.await?;
|
||||||
*this.capabilities.write() = response.capabilities;
|
|
||||||
Self::notify_internal::<notification::Initialized>(
|
Self::notify_internal::<notification::Initialized>(
|
||||||
this.outbound_tx.read().as_ref(),
|
this.outbound_tx.read().as_ref(),
|
||||||
InitializedParams {},
|
InitializedParams {},
|
||||||
)?;
|
)?;
|
||||||
Ok(())
|
Ok(response.capabilities)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn shutdown(&self) -> Option<impl 'static + Send + Future<Output = Result<()>>> {
|
pub fn shutdown(&self) -> Option<impl 'static + Send + Future<Output = Result<()>>> {
|
||||||
@@ -328,8 +331,8 @@ impl LanguageServer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn capabilities(&self) -> RwLockReadGuard<ServerCapabilities> {
|
pub fn capabilities(&self) -> watch::Receiver<Option<ServerCapabilities>> {
|
||||||
self.capabilities.read()
|
self.capabilities.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn request<T: request::Request>(
|
pub fn request<T: request::Request>(
|
||||||
|
|||||||
@@ -358,6 +358,7 @@ message BufferState {
|
|||||||
repeated Diagnostic diagnostics = 9;
|
repeated Diagnostic diagnostics = 9;
|
||||||
uint32 lamport_timestamp = 10;
|
uint32 lamport_timestamp = 10;
|
||||||
repeated Operation deferred_operations = 11;
|
repeated Operation deferred_operations = 11;
|
||||||
|
repeated string completion_triggers = 12;
|
||||||
}
|
}
|
||||||
|
|
||||||
message BufferFragment {
|
message BufferFragment {
|
||||||
@@ -428,6 +429,7 @@ message Operation {
|
|||||||
Undo undo = 2;
|
Undo undo = 2;
|
||||||
UpdateSelections update_selections = 3;
|
UpdateSelections update_selections = 3;
|
||||||
UpdateDiagnostics update_diagnostics = 4;
|
UpdateDiagnostics update_diagnostics = 4;
|
||||||
|
UpdateCompletionTriggers update_completion_triggers = 5;
|
||||||
}
|
}
|
||||||
|
|
||||||
message Edit {
|
message Edit {
|
||||||
@@ -453,6 +455,10 @@ message Operation {
|
|||||||
uint32 lamport_timestamp = 2;
|
uint32 lamport_timestamp = 2;
|
||||||
repeated Selection selections = 3;
|
repeated Selection selections = 3;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
message UpdateCompletionTriggers {
|
||||||
|
repeated string triggers = 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
message UndoMapEntry {
|
message UndoMapEntry {
|
||||||
|
|||||||
Reference in New Issue
Block a user