Skip to content

Commit

Permalink
Format selection mouse context menu button
Browse files Browse the repository at this point in the history
  • Loading branch information
terziele committed Sep 1, 2024
1 parent 4cb8a43 commit ea947a9
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 16 deletions.
1 change: 1 addition & 0 deletions crates/editor/src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ gpui::actions!(
Fold,
FoldSelectedRanges,
Format,
FormatSelection,
GoToDefinition,
GoToDefinitionSplit,
GoToDeclaration,
Expand Down
13 changes: 13 additions & 0 deletions crates/editor/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9969,6 +9969,19 @@ impl Editor {
Some(self.perform_format(project, FormatTrigger::Manual, cx))
}

fn format_selection(
&mut self,
_: &FormatSelection,
cx: &mut ViewContext<Self>,
) -> Option<Task<Result<()>>> {
let project = match &self.project {
Some(project) => project.clone(),
None => return None,
};

Some(self.perform_format(project, FormatTrigger::Manual, cx))
}

fn perform_format(
&mut self,
project: Model<Project>,
Expand Down
7 changes: 7 additions & 0 deletions crates/editor/src/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,13 @@ impl EditorElement {
cx.propagate();
}
});
register_action(view, cx, |editor, action, cx| {
if let Some(task) = editor.format_selection(action, cx) {
task.detach_and_log_err(cx);
} else {
cx.propagate();
}
});
register_action(view, cx, Editor::restart_language_server);
register_action(view, cx, Editor::cancel_language_server_work);
register_action(view, cx, Editor::show_character_palette);
Expand Down
12 changes: 12 additions & 0 deletions crates/editor/src/mouse_context_menu.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::ops::Range;

use crate::actions::FormatSelection;
use crate::{
actions::Format, selections_collection::SelectionsCollection, Copy, CopyPermalinkToLine, Cut,
DisplayPoint, DisplaySnapshot, Editor, EditorMode, FindAllReferences, GoToDeclaration,
Expand All @@ -8,6 +9,7 @@ use crate::{
};
use gpui::prelude::FluentBuilder;
use gpui::{DismissEvent, Pixels, Point, Subscription, View, ViewContext};
use text::PointUtf16;
use workspace::OpenInTerminal;

#[derive(Debug)]
Expand Down Expand Up @@ -156,6 +158,13 @@ pub fn deploy_context_menu(
s.set_pending_anchor_range(anchor..anchor, SelectMode::Character);
});
}
let selections = editor
.selections
.all::<language::Point>(cx)
.iter()
.filter(|s| s.start != s.end)
.collect::<Vec<_>>()
.is_empty();

let focus = cx.focused();
ui::ContextMenu::build(cx, |menu, _cx| {
Expand All @@ -169,6 +178,9 @@ pub fn deploy_context_menu(
.separator()
.action("Rename Symbol", Box::new(Rename))
.action("Format Buffer", Box::new(Format))
.when(!selections, |builder| {
builder.action("Format Selection", Box::new(FormatSelection))
})
.action(
"Code Actions",
Box::new(ToggleCodeActions {
Expand Down
60 changes: 44 additions & 16 deletions crates/project/src/lsp_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ use language::{
proto::{deserialize_anchor, deserialize_version, serialize_anchor, serialize_version},
range_from_lsp, Bias, Buffer, BufferSnapshot, CachedLspAdapter, CodeLabel, Diagnostic,
DiagnosticEntry, DiagnosticSet, Documentation, File as _, Language, LanguageRegistry,
LanguageServerName, LocalFile, LspAdapterDelegate, Patch, PendingLanguageServer, PointUtf16,
TextBufferSnapshot, ToOffset, ToPointUtf16, Transaction, Unclipped,
LanguageServerName, LocalFile, LspAdapterDelegate, Patch, PendingLanguageServer, Point,
PointUtf16, TextBufferSnapshot, ToOffset, ToPointUtf16, Transaction, Unclipped,
};
use lsp::{
CompletionContext, DiagnosticSeverity, DiagnosticTag, DidChangeWatchedFilesRegistrationOptions,
Expand Down Expand Up @@ -64,7 +64,7 @@ use std::{
sync::{atomic::Ordering::SeqCst, Arc},
time::{Duration, Instant},
};
use text::{Anchor, BufferId, LineEnding};
use text::{Anchor, BufferId, LineEnding, Selection};
use util::{
debug_panic, defer, maybe, merge_json_value_into, post_inc, ResultExt, TryFutureExt as _,
};
Expand Down Expand Up @@ -807,6 +807,7 @@ impl LspStore {
abs_path: &Path,
language_server: &Arc<LanguageServer>,
settings: &LanguageSettings,
selections: Vec<Selection<Point>>,
cx: &mut AsyncAppContext,
) -> Result<Vec<(Range<Anchor>, String)>> {
let uri = lsp::Url::from_file_path(abs_path)
Expand All @@ -817,22 +818,49 @@ impl LspStore {
let formatting_provider = capabilities.document_formatting_provider.as_ref();
let range_formatting_provider = capabilities.document_range_formatting_provider.as_ref();

let lsp_edits = if matches!(formatting_provider, Some(p) if *p != OneOf::Left(false)) {
language_server
.request::<lsp::request::Formatting>(lsp::DocumentFormattingParams {
text_document,
options: lsp_command::lsp_formatting_options(settings),
work_done_progress_params: Default::default(),
})
.await?
} else if matches!(range_formatting_provider, Some(p) if *p != OneOf::Left(false)) {
let buffer_start = lsp::Position::new(0, 0);
let buffer_end = buffer.update(cx, |b, _| point_to_lsp(b.max_point_utf16()))?;
let lsp_edits = if matches!(range_formatting_provider, Some(p) if *p != OneOf::Left(false))
{
let ranges = if selections.is_empty() {
let buffer_start = lsp::Position::new(0, 0);
let buffer_end = buffer.update(cx, |b, _| point_to_lsp(b.max_point_utf16()))?;
vec![(buffer_start, buffer_end)]
} else {
selections
.into_iter()
.map(|selection| {
let selection_start = selection.start;
let selection_end = selection.end;

(
lsp::Position::new(selection_start.row, selection_start.column),
lsp::Position::new(selection_end.row, selection_end.column),
)
})
.collect()
};

let mut text_edits = vec![];
for (buffer_start, buffer_end) in ranges {
language_server
.request::<lsp::request::RangeFormatting>(lsp::DocumentRangeFormattingParams {
text_document: text_document.clone(),
range: lsp::Range::new(buffer_start, buffer_end),
options: lsp_command::lsp_formatting_options(settings),
work_done_progress_params: Default::default(),
})
.await?
.map(|mut edits| text_edits.append(&mut edits));
}
if text_edits.is_empty() {
None
} else {
Some(text_edits)
}
} else if matches!(formatting_provider, Some(p) if *p != OneOf::Left(false)) {
println!("formatting all");
language_server
.request::<lsp::request::RangeFormatting>(lsp::DocumentRangeFormattingParams {
.request::<lsp::request::Formatting>(lsp::DocumentFormattingParams {
text_document,
range: lsp::Range::new(buffer_start, buffer_end),
options: lsp_command::lsp_formatting_options(settings),
work_done_progress_params: Default::default(),
})
Expand Down

0 comments on commit ea947a9

Please sign in to comment.