Skip to content

Commit

Permalink
Add setting to choose sepcific language server for formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
luckydye committed May 18, 2024
1 parent 410c46a commit 21e3353
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
4 changes: 4 additions & 0 deletions crates/language/src/language_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,8 @@ pub enum FormatOnSave {
Off,
/// Files should be formatted using the current language server.
LanguageServer,
/// Format code using a specific language server.
LanguageServerName(Arc<str>),
/// The external program to use to format the files on save.
External {
/// The external program to run.
Expand Down Expand Up @@ -398,6 +400,8 @@ pub enum Formatter {
Auto,
/// Format code using the current language server.
LanguageServer,
/// Format code using a specific language server.
LanguageServerName(Arc<str>),
/// Format code using Zed's Prettier integration.
Prettier,
/// Format code using an external command.
Expand Down
5 changes: 4 additions & 1 deletion crates/project/src/prettier_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ pub fn prettier_plugins_for_language(
) -> Option<&HashSet<String>> {
match &language_settings.formatter {
Formatter::Prettier { .. } | Formatter::Auto => Some(&language_settings.prettier.plugins),
Formatter::LanguageServer | Formatter::External { .. } | Formatter::CodeActions(_) => None,
Formatter::LanguageServerName { .. }
| Formatter::LanguageServer
| Formatter::External { .. }
| Formatter::CodeActions(_) => None,
}
}

Expand Down
44 changes: 43 additions & 1 deletion crates/project/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4832,6 +4832,39 @@ impl Project {
}
}

(
Formatter::LanguageServerName { 0: language_server },
FormatOnSave::On | FormatOnSave::Off,
)
| (_, FormatOnSave::LanguageServerName { 0: language_server }) => {
let formatter_language_server = adapters_and_servers
.iter()
.find(|(adapter, _)| {
let server_name = &adapter.name.0;
language_server == server_name
})
.map(|(_, server)| server.clone());

if let Some((language_server, buffer_abs_path)) = formatter_language_server
.as_ref()
.zip(buffer_abs_path.as_ref())
{
log::info!("formatting with {} language server", language_server.name());

format_operation = Some(FormatOperation::Lsp(
Self::format_via_lsp(
&project,
buffer,
buffer_abs_path,
language_server,
tab_size,
&mut cx,
)
.await
.context("failed to format via language server")?,
));
}
}
(
Formatter::External { command, arguments },
FormatOnSave::On | FormatOnSave::Off,
Expand Down Expand Up @@ -4975,7 +5008,16 @@ impl Project {
})
.await?
} else {
None
log::error!("Language Server does not provide formatting");

language_server
.request::<lsp::request::Formatting>(lsp::DocumentFormattingParams {
text_document,
options: lsp_command::lsp_formatting_options(tab_size.get()),
work_done_progress_params: Default::default(),
})
.await?
// None
};

if let Some(lsp_edits) = lsp_edits {
Expand Down

0 comments on commit 21e3353

Please sign in to comment.