Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jpttrssn committed Aug 31, 2024
1 parent 1c2284c commit 53bafe7
Show file tree
Hide file tree
Showing 11 changed files with 797 additions and 35 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions helix-term/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ tokio-stream = "0.1"
futures-util = { version = "0.3", features = ["std", "async-await"], default-features = false }
arc-swap = { version = "1.7.1" }
termini = "1"
pin-project-lite = "0.2"

# Logging
fern = "0.6"
Expand Down
41 changes: 38 additions & 3 deletions helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@ use crate::{
use log::{debug, error, info, warn};
#[cfg(not(feature = "integration"))]
use std::io::stdout;
use std::{collections::btree_map::Entry, io::stdin, path::Path, sync::Arc};
use std::{
collections::btree_map::Entry,
io::stdin,
path::{Path, PathBuf},
sync::Arc,
};

#[cfg(not(windows))]
use anyhow::Context;
Expand Down Expand Up @@ -335,7 +340,7 @@ impl Application {
self.handle_terminal_events(event).await;
}
Some(callback) = self.jobs.callbacks.recv() => {
self.jobs.handle_callback(&mut self.editor, &mut self.compositor, Ok(Some(callback))).await;
self.jobs.handle_callback(&mut self.editor, &mut self.compositor, Ok(Some(callback)));
self.render().await;
}
Some(msg) = self.jobs.status_messages.recv() => {
Expand All @@ -350,9 +355,39 @@ impl Application {
helix_event::request_redraw();
}
Some(callback) = self.jobs.wait_futures.next() => {
self.jobs.handle_callback(&mut self.editor, &mut self.compositor, callback).await;
self.jobs.handle_callback(&mut self.editor, &mut self.compositor, callback);
self.render().await;
}
// Some(Ok(on_save_job)) = self.jobs.on_save.next() => {
// let doc = doc!(self.editor, &on_save_job.doc_id);
// self.editor.on_save(doc).await;

// // if self.editor.config().auto_format {
// // if let Some(fmt) = doc.auto_format() {
// // let scrolloff = self.editor.config().scrolloff;
// // let doc = doc_mut!(self.editor, &on_save_job.doc_id);
// // let view = view_mut!(self.editor, on_save_job.view_id);

// // if let Ok(format) = fmt.await {
// // if doc.version() == on_save_job.doc_version {
// // doc.apply(&format, view.id);
// // doc.append_changes_to_history(view);
// // doc.detect_indent_and_line_ending();
// // view.ensure_cursor_in_view(doc, scrolloff);
// // } else {
// // log::info!("discarded formatting changes because the document changed");
// // }
// // }
// // }
// // log::debug!("CODEACTION FORMATTED");
// // }

// if let Err(err) = self.editor.save::<PathBuf>(on_save_job.doc_id, on_save_job.path, on_save_job.force) {
// self.editor.set_error(format!("Error saving: {}", err));
// }
// log::debug!("CODEACTION SAVED");
// self.render().await;
// }
event = self.editor.wait_event() => {
let _idle_handled = self.handle_editor_event(event).await;

Expand Down
88 changes: 83 additions & 5 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3397,6 +3397,45 @@ async fn make_format_callback(

Ok(call)
}
async fn make_format_callback_old(
doc_id: DocumentId,
doc_version: i32,
view_id: ViewId,
format: impl Future<Output = Result<Transaction, FormatterError>> + Send + 'static,
write: Option<(Option<PathBuf>, bool)>,
) -> anyhow::Result<job::Callback> {
let format = format.await;

let call: job::Callback = Callback::Editor(Box::new(move |editor| {
if !editor.documents.contains_key(&doc_id) || !editor.tree.contains(view_id) {
return;
}

let scrolloff = editor.config().scrolloff;
let doc = doc_mut!(editor, &doc_id);
let view = view_mut!(editor, view_id);

if let Ok(format) = format {
if doc.version() == doc_version {
doc.apply(&format, view.id);
doc.append_changes_to_history(view);
doc.detect_indent_and_line_ending();
view.ensure_cursor_in_view(doc, scrolloff);
} else {
log::info!("discarded formatting changes because the document changed");
}
}

if let Some((path, force)) = write {
let id = doc.id();
if let Err(err) = editor.save(id, path, force) {
editor.set_error(format!("Error saving: {}", err));
}
}
}));

Ok(call)
}

pub fn format_callback(
doc_id: DocumentId,
Expand Down Expand Up @@ -3446,11 +3485,8 @@ pub fn on_save_callback(
code_action_on_save_cfg
);
let doc = doc!(editor, &doc_id);
// let code_actions =
// helix_lsp::block_on(code_actions_on_save(doc, code_action_on_save_cfg.clone()));
let code_actions = tokio::task::spawn_blocking(|| {
code_actions_on_save(doc, code_action_on_save_cfg.clone())
});
let code_actions =
helix_lsp::block_on(code_actions_on_save(doc, code_action_on_save_cfg.clone()));

if code_actions.is_empty() {
log::debug!(
Expand Down Expand Up @@ -3503,6 +3539,48 @@ pub async fn make_on_save_callback(
Ok(call)
}

pub fn code_action_callback(
editor: &mut Editor,
code_action_on_save_cfg: String,
code_actions: Vec<CodeActionOrCommandItem>,
) {
if code_actions.is_empty() {
log::debug!(
"Code action on save not found {:?}",
code_action_on_save_cfg
);
editor.set_error(format!(
"Code Action not found: {:?}",
code_action_on_save_cfg
));
}

for code_action in code_actions {
log::debug!(
"Applying code action on save {:?} for language server {:?}",
code_action.lsp_item,
code_action.language_server_id
);
apply_code_action(editor, &code_action);
}
}

pub async fn make_code_action_callback(
doc: &Document,
code_action_on_save_cfg: String,
) -> anyhow::Result<job::Callback> {
log::debug!(
"Attempting code action on save {:?}",
code_action_on_save_cfg
);
let code_actions = code_actions_on_save(doc, code_action_on_save_cfg.clone()).await;

let call = Callback::Editor(Box::new(move |editor| {
code_action_callback(editor, code_action_on_save_cfg, code_actions);
}));
Ok(call)
}

#[derive(PartialEq, Eq)]
pub enum Open {
Below,
Expand Down
25 changes: 24 additions & 1 deletion helix-term/src/commands/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use helix_lsp::{
block_on,
lsp::{
self, CodeAction, CodeActionKind, CodeActionOrCommand, CodeActionTriggerKind,
DiagnosticSeverity, NumberOrString,
DiagnosticSeverity, NumberOrString, WorkspaceEdit,
},
util::{diagnostic_to_lsp_diagnostic, lsp_range_to_range, range_to_lsp_range},
Client, LanguageServerId, OffsetEncoding,
Expand Down Expand Up @@ -850,6 +850,29 @@ pub fn apply_code_action(editor: &mut Editor, action: &CodeActionOrCommandItem)
}
}

pub fn apply_code_action_on_save(
language_server: &Client,
action: &CodeActionOrCommandItem,
) -> Option<WorkspaceEdit> {
match &action.lsp_item {
lsp::CodeActionOrCommand::CodeAction(code_action) => {
log::debug!("code action: {:?}", code_action);
// we support lsp "codeAction/resolve" for `edit` and `command` fields
if code_action.edit.is_none() || code_action.command.is_none() {
if let Some(future) = language_server.resolve_code_action(code_action.clone()) {
if let Ok(response) = helix_lsp::block_on(future) {
if let Ok(code_action) = serde_json::from_value::<CodeAction>(response) {
return code_action.edit;
}
}
}
}
None
}
_ => None, // Commands are deprecated and not supported
}
}

impl ui::menu::Item for lsp::Command {
type Data = ();
fn format(&self, _data: &Self::Data) -> Row {
Expand Down
Loading

0 comments on commit 53bafe7

Please sign in to comment.