mirror of https://github.com/helix-editor/helix
Format document on save
parent
d5466eddf5
commit
2c48d65b15
|
@ -890,12 +890,12 @@ mod cmd {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write(editor: &mut Editor, args: &[&str], event: PromptEvent) {
|
fn write(editor: &mut Editor, args: &[&str], event: PromptEvent) {
|
||||||
let id = editor.view().doc;
|
let (view, doc) = editor.current();
|
||||||
let doc = &mut editor.documents[id];
|
|
||||||
if doc.path().is_none() {
|
if doc.path().is_none() {
|
||||||
editor.set_error("cannot write a buffer without a filename".to_string());
|
editor.set_error("cannot write a buffer without a filename".to_string());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
doc.format(view.id); // TODO: merge into save
|
||||||
tokio::spawn(doc.save());
|
tokio::spawn(doc.save());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -906,25 +906,7 @@ mod cmd {
|
||||||
fn format(editor: &mut Editor, args: &[&str], event: PromptEvent) {
|
fn format(editor: &mut Editor, args: &[&str], event: PromptEvent) {
|
||||||
let (view, doc) = editor.current();
|
let (view, doc) = editor.current();
|
||||||
|
|
||||||
if let Some(language_server) = doc.language_server() {
|
doc.format(view.id)
|
||||||
// TODO: await, no blocking
|
|
||||||
let transaction = helix_lsp::block_on(
|
|
||||||
language_server
|
|
||||||
.text_document_formatting(doc.identifier(), lsp::FormattingOptions::default()),
|
|
||||||
)
|
|
||||||
.map(|edits| {
|
|
||||||
helix_lsp::util::generate_transaction_from_edits(
|
|
||||||
doc.text(),
|
|
||||||
edits,
|
|
||||||
language_server.offset_encoding(),
|
|
||||||
)
|
|
||||||
});
|
|
||||||
|
|
||||||
if let Ok(transaction) = transaction {
|
|
||||||
doc.apply(&transaction, view.id);
|
|
||||||
doc.append_changes_to_history(view.id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const COMMAND_LIST: &[Command] = &[
|
pub const COMMAND_LIST: &[Command] = &[
|
||||||
|
@ -2277,7 +2259,8 @@ pub fn space_mode(cx: &mut Context) {
|
||||||
'v' => vsplit(cx),
|
'v' => vsplit(cx),
|
||||||
'w' => {
|
'w' => {
|
||||||
// save current buffer
|
// save current buffer
|
||||||
let doc = cx.doc();
|
let (view, doc) = cx.current();
|
||||||
|
doc.format(view.id); // TODO: merge into save
|
||||||
tokio::spawn(doc.save());
|
tokio::spawn(doc.save());
|
||||||
}
|
}
|
||||||
'c' => {
|
'c' => {
|
||||||
|
|
|
@ -116,6 +116,29 @@ impl Document {
|
||||||
Ok(doc)
|
Ok(doc)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: remove view_id dependency here
|
||||||
|
pub fn format(&mut self, view_id: ViewId) {
|
||||||
|
if let Some(language_server) = self.language_server() {
|
||||||
|
// TODO: await, no blocking
|
||||||
|
let transaction = helix_lsp::block_on(
|
||||||
|
language_server
|
||||||
|
.text_document_formatting(self.identifier(), lsp::FormattingOptions::default()),
|
||||||
|
)
|
||||||
|
.map(|edits| {
|
||||||
|
helix_lsp::util::generate_transaction_from_edits(
|
||||||
|
self.text(),
|
||||||
|
edits,
|
||||||
|
language_server.offset_encoding(),
|
||||||
|
)
|
||||||
|
});
|
||||||
|
|
||||||
|
if let Ok(transaction) = transaction {
|
||||||
|
self.apply(&transaction, view_id);
|
||||||
|
self.append_changes_to_history(view_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: do we need some way of ensuring two save operations on the same doc can't run at once?
|
// TODO: do we need some way of ensuring two save operations on the same doc can't run at once?
|
||||||
// or is that handled by the OS/async layer
|
// or is that handled by the OS/async layer
|
||||||
pub fn save(&mut self) -> impl Future<Output = Result<(), anyhow::Error>> {
|
pub fn save(&mut self) -> impl Future<Output = Result<(), anyhow::Error>> {
|
||||||
|
|
Loading…
Reference in New Issue