Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lsp): will save #8952

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 44 additions & 7 deletions helix-lsp/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use helix_loader::{self, VERSION_AND_GIT_HASH};
use helix_stdx::path;
use lsp::{
notification::DidChangeWorkspaceFolders, CodeActionCapabilityResolveSupport,
DidChangeWorkspaceFoldersParams, OneOf, PositionEncodingKind, SignatureHelp, Url,
WorkspaceFolder, WorkspaceFoldersChangeEvent,
DidChangeWorkspaceFoldersParams, OneOf, PositionEncodingKind, SignatureHelp,
TextDocumentSaveReason, TextEdit, Url, WorkspaceFolder, WorkspaceFoldersChangeEvent,
};
use lsp_types as lsp;
use parking_lot::Mutex;
Expand All @@ -33,6 +33,8 @@ use tokio::{
},
};

static DOCUMENT_OPS_TIMEOUT: u64 = 5;

fn workspace_for_uri(uri: lsp::Url) -> WorkspaceFolder {
lsp::WorkspaceFolder {
name: uri
Expand Down Expand Up @@ -264,7 +266,7 @@ impl Client {
.expect("language server not yet initialized!")
}

pub(crate) fn file_operations_intests(&self) -> &FileOperationsInterest {
pub(crate) fn file_operations_interest(&self) -> &FileOperationsInterest {
self.file_operation_interest
.get_or_init(|| FileOperationsInterest::new(self.capabilities()))
}
Expand Down Expand Up @@ -720,7 +722,7 @@ impl Client {
new_path: &Path,
is_dir: bool,
) -> Option<impl Future<Output = Result<lsp::WorkspaceEdit>>> {
let capabilities = self.file_operations_intests();
let capabilities = self.file_operations_interest();
if !capabilities.will_rename.has_interest(old_path, is_dir) {
return None;
}
Expand All @@ -738,7 +740,7 @@ impl Client {
}];
let request = self.call_with_timeout::<lsp::request::WillRenameFiles>(
lsp::RenameFilesParams { files },
5,
DOCUMENT_OPS_TIMEOUT,
);

Some(async move {
Expand All @@ -754,7 +756,7 @@ impl Client {
new_path: &Path,
is_dir: bool,
) -> Option<impl Future<Output = std::result::Result<(), Error>>> {
let capabilities = self.file_operations_intests();
let capabilities = self.file_operations_interest();
if !capabilities.did_rename.has_interest(new_path, is_dir) {
return None;
}
Expand All @@ -776,6 +778,7 @@ impl Client {

// -------------------------------------------------------------------------------------------
// Text document
// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_synchronization
// -------------------------------------------------------------------------------------------

pub fn text_document_did_open(
Expand Down Expand Up @@ -960,7 +963,36 @@ impl Client {
})
}

// will_save / will_save_wait_until
pub fn text_document_will_save(
&self,
text_document: lsp::TextDocumentIdentifier,
) -> Option<impl Future<Output = Result<()>>> {
Some(self.notify::<lsp::notification::WillSaveTextDocument>(
lsp::WillSaveTextDocumentParams {
text_document,
reason: TextDocumentSaveReason::MANUAL,
},
))
}

pub fn text_document_will_save_wait_until(
&self,
text_document: lsp::TextDocumentIdentifier,
) -> Option<impl Future<Output = Result<Vec<lsp::TextEdit>>>> {
let request = self.call_with_timeout::<lsp::request::WillSaveWaitUntil>(
lsp::WillSaveTextDocumentParams {
text_document,
reason: TextDocumentSaveReason::MANUAL,
},
DOCUMENT_OPS_TIMEOUT,
);

Some(async move {
let json = request.await?;
let response: Option<Vec<TextEdit>> = serde_json::from_value(json)?;
Ok(response.unwrap_or_default())
})
}

pub fn text_document_did_save(
&self,
Expand Down Expand Up @@ -992,6 +1024,11 @@ impl Client {
))
}

// -------------------------------------------------------------------------------------------
// Language features
// https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#languageFeatures
// -------------------------------------------------------------------------------------------
Comment on lines +1027 to +1030
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems unrelated


pub fn completion(
&self,
text_document: lsp::TextDocumentIdentifier,
Expand Down
55 changes: 45 additions & 10 deletions helix-view/src/document.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::{anyhow, bail, Context, Error};
use arc_swap::access::DynAccess;
use arc_swap::ArcSwap;
use futures_util::future::BoxFuture;
use futures_util::future::{self, BoxFuture};
use futures_util::FutureExt;
use helix_core::auto_pairs::AutoPairs;
use helix_core::chars::char_is_word;
Expand Down Expand Up @@ -871,6 +871,31 @@ impl Document {
// We encode the file according to the `Document`'s encoding.
let future = async move {
use tokio::{fs, fs::File};

if let Some(identifier) = &identifier {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should definitly4 be handeled by the even t system and what I meant with my previous comments. We essentially want to keep LSP out of the main code and move it all behind events. So there should be a willsave event here and the lsp stuff should be moved to a hook. Same with didsave

for language_server in language_servers.values() {
let mut notifications = Vec::new();

if language_server.is_initialized() {
let Some(notification) =
language_server.text_document_will_save(identifier.clone())
else {
continue
};

notifications.push(async move {
if let Err(err) = notification.await {
log::error!(
"failed to send textDocument/willSave notification: {err:?}"
);
}
});
}

future::join_all(notifications).await;
}
};

if let Some(parent) = path.parent() {
// TODO: display a prompt asking the user if the directories should be created
if !parent.exists() {
Expand Down Expand Up @@ -903,17 +928,27 @@ impl Document {
text: text.clone(),
};

for (_, language_server) in language_servers {
if !language_server.is_initialized() {
return Ok(event);
}
if let Some(identifier) = &identifier {
if let Some(notification) =
language_server.text_document_did_save(identifier.clone(), &text)
{
notification.await?;
if let Some(identifier) = &identifier {
let mut notifications = Vec::new();
for language_server in language_servers.values() {
if language_server.is_initialized() {
let Some(notification) =
language_server.text_document_did_save(identifier.clone(), &text)
else {
continue
};

notifications.push(async move {
if let Err(err) = notification.await {
log::error!(
"failed to send textDocument/didSave notification: {err:?}"
);
}
});
}
}

future::join_all(notifications).await;
}

Ok(event)
Expand Down
Loading