Better pass prettier options
This commit is contained in:
parent
e2056756ef
commit
2a5b9b635b
11 changed files with 55 additions and 34 deletions
|
@ -341,6 +341,7 @@ pub trait LspAdapter: 'static + Send + Sync {
|
||||||
// TODO kb enable this for
|
// TODO kb enable this for
|
||||||
// markdown somehow?
|
// markdown somehow?
|
||||||
// tailwind (needs a css plugin, there are 2 of them)
|
// tailwind (needs a css plugin, there are 2 of them)
|
||||||
|
// svelte (needs a plugin)
|
||||||
fn enabled_formatters(&self) -> Vec<BundledFormatter> {
|
fn enabled_formatters(&self) -> Vec<BundledFormatter> {
|
||||||
Vec::new()
|
Vec::new()
|
||||||
}
|
}
|
||||||
|
@ -348,12 +349,16 @@ pub trait LspAdapter: 'static + Send + Sync {
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||||
pub enum BundledFormatter {
|
pub enum BundledFormatter {
|
||||||
Prettier { plugin_names: Vec<String> },
|
Prettier {
|
||||||
|
parser_name: &'static str,
|
||||||
|
plugin_names: Vec<String>,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BundledFormatter {
|
impl BundledFormatter {
|
||||||
pub fn prettier() -> Self {
|
pub fn prettier(parser_name: &'static str) -> Self {
|
||||||
Self::Prettier {
|
Self::Prettier {
|
||||||
|
parser_name,
|
||||||
plugin_names: Vec::new(),
|
plugin_names: Vec::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ use std::sync::Arc;
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use fs::Fs;
|
use fs::Fs;
|
||||||
use gpui::{AsyncAppContext, ModelHandle, Task};
|
use gpui::{AsyncAppContext, ModelHandle, Task};
|
||||||
use language::{Buffer, Diff};
|
use language::{Buffer, BundledFormatter, Diff};
|
||||||
use lsp::{LanguageServer, LanguageServerBinary, LanguageServerId};
|
use lsp::{LanguageServer, LanguageServerBinary, LanguageServerId};
|
||||||
use node_runtime::NodeRuntime;
|
use node_runtime::NodeRuntime;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
@ -187,16 +187,30 @@ impl Prettier {
|
||||||
buffer: &ModelHandle<Buffer>,
|
buffer: &ModelHandle<Buffer>,
|
||||||
cx: &AsyncAppContext,
|
cx: &AsyncAppContext,
|
||||||
) -> anyhow::Result<Diff> {
|
) -> anyhow::Result<Diff> {
|
||||||
// TODO kb prettier needs either a path or a `parser` (depends on a language) option to format
|
let params = buffer.read_with(cx, |buffer, cx| {
|
||||||
let (buffer_text, buffer_language) =
|
let path = buffer
|
||||||
buffer.read_with(cx, |buffer, _| (buffer.text(), buffer.language().cloned()));
|
.file()
|
||||||
|
.map(|file| file.full_path(cx))
|
||||||
|
.map(|path| path.to_path_buf());
|
||||||
|
let parser = buffer.language().and_then(|language| {
|
||||||
|
language
|
||||||
|
.lsp_adapters()
|
||||||
|
.iter()
|
||||||
|
.flat_map(|adapter| adapter.enabled_formatters())
|
||||||
|
.find_map(|formatter| match formatter {
|
||||||
|
BundledFormatter::Prettier { parser_name, .. } => {
|
||||||
|
Some(parser_name.to_string())
|
||||||
|
}
|
||||||
|
})
|
||||||
|
});
|
||||||
|
PrettierFormatParams {
|
||||||
|
text: buffer.text(),
|
||||||
|
options: FormatOptions { parser, path },
|
||||||
|
}
|
||||||
|
});
|
||||||
let response = self
|
let response = self
|
||||||
.server
|
.server
|
||||||
.request::<PrettierFormat>(PrettierFormatParams {
|
.request::<PrettierFormat>(params)
|
||||||
text: buffer_text,
|
|
||||||
path: None,
|
|
||||||
parser: None,
|
|
||||||
})
|
|
||||||
.await
|
.await
|
||||||
.context("prettier format request")?;
|
.context("prettier format request")?;
|
||||||
let diff_task = buffer.read_with(cx, |buffer, cx| buffer.diff(response.text, cx));
|
let diff_task = buffer.read_with(cx, |buffer, cx| buffer.diff(response.text, cx));
|
||||||
|
@ -262,9 +276,14 @@ enum PrettierFormat {}
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
struct PrettierFormatParams {
|
struct PrettierFormatParams {
|
||||||
text: String,
|
text: String,
|
||||||
// TODO kb have "options" or something more generic instead?
|
options: FormatOptions,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
struct FormatOptions {
|
||||||
parser: Option<String>,
|
parser: Option<String>,
|
||||||
path: Option<String>,
|
path: Option<PathBuf>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
|
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
|
||||||
|
|
|
@ -131,12 +131,16 @@ async function handleMessage(messageText, prettier) {
|
||||||
if (params === undefined || params.text === undefined) {
|
if (params === undefined || params.text === undefined) {
|
||||||
throw new Error(`Message params.text is undefined: ${messageText}`);
|
throw new Error(`Message params.text is undefined: ${messageText}`);
|
||||||
}
|
}
|
||||||
|
if (params.options === undefined) {
|
||||||
|
throw new Error(`Message params.options is undefined: ${messageText}`);
|
||||||
|
}
|
||||||
|
|
||||||
let options = {};
|
let options = {};
|
||||||
if (message.path !== undefined) {
|
if (params.options.path !== undefined) {
|
||||||
options.filepath = message.path;
|
options.filepath = params.options.path;
|
||||||
} else {
|
}
|
||||||
options.parser = message.parser || 'babel';
|
if (params.options.parser !== undefined) {
|
||||||
|
options.parser = params.options.parser;
|
||||||
}
|
}
|
||||||
const formattedText = await prettier.format(params.text, options);
|
const formattedText = await prettier.format(params.text, options);
|
||||||
sendResponse({ id, result: { text: formattedText } });
|
sendResponse({ id, result: { text: formattedText } });
|
||||||
|
|
|
@ -8323,7 +8323,7 @@ impl Project {
|
||||||
.flat_map(|adapter| adapter.enabled_formatters())
|
.flat_map(|adapter| adapter.enabled_formatters())
|
||||||
{
|
{
|
||||||
match formatter {
|
match formatter {
|
||||||
BundledFormatter::Prettier { plugin_names } => prettier_plugins
|
BundledFormatter::Prettier { plugin_names, .. } => prettier_plugins
|
||||||
.get_or_insert_with(|| HashSet::default())
|
.get_or_insert_with(|| HashSet::default())
|
||||||
.extend(plugin_names),
|
.extend(plugin_names),
|
||||||
}
|
}
|
||||||
|
|
|
@ -98,7 +98,7 @@ impl LspAdapter for CssLspAdapter {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn enabled_formatters(&self) -> Vec<BundledFormatter> {
|
fn enabled_formatters(&self) -> Vec<BundledFormatter> {
|
||||||
vec![BundledFormatter::prettier()]
|
vec![BundledFormatter::prettier("css")]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -98,7 +98,7 @@ impl LspAdapter for HtmlLspAdapter {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn enabled_formatters(&self) -> Vec<BundledFormatter> {
|
fn enabled_formatters(&self) -> Vec<BundledFormatter> {
|
||||||
vec![BundledFormatter::prettier()]
|
vec![BundledFormatter::prettier("html")]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -148,7 +148,7 @@ impl LspAdapter for JsonLspAdapter {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn enabled_formatters(&self) -> Vec<BundledFormatter> {
|
fn enabled_formatters(&self) -> Vec<BundledFormatter> {
|
||||||
vec![BundledFormatter::prettier()]
|
vec![BundledFormatter::prettier("json")]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ use anyhow::{anyhow, Result};
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use collections::HashMap;
|
use collections::HashMap;
|
||||||
|
|
||||||
use language::{BundledFormatter, LanguageServerName, LspAdapter, LspAdapterDelegate};
|
use language::{LanguageServerName, LspAdapter, LspAdapterDelegate};
|
||||||
use lsp::LanguageServerBinary;
|
use lsp::LanguageServerBinary;
|
||||||
use node_runtime::NodeRuntime;
|
use node_runtime::NodeRuntime;
|
||||||
|
|
||||||
|
@ -100,13 +100,10 @@ impl LspAdapter for IntelephenseLspAdapter {
|
||||||
async fn initialization_options(&self) -> Option<serde_json::Value> {
|
async fn initialization_options(&self) -> Option<serde_json::Value> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn language_ids(&self) -> HashMap<String, String> {
|
async fn language_ids(&self) -> HashMap<String, String> {
|
||||||
HashMap::from_iter([("PHP".into(), "php".into())])
|
HashMap::from_iter([("PHP".into(), "php".into())])
|
||||||
}
|
}
|
||||||
|
|
||||||
fn enabled_formatters(&self) -> Vec<BundledFormatter> {
|
|
||||||
vec![BundledFormatter::prettier()]
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_cached_server_binary(
|
async fn get_cached_server_binary(
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use anyhow::{anyhow, Result};
|
use anyhow::{anyhow, Result};
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use futures::StreamExt;
|
use futures::StreamExt;
|
||||||
use language::{BundledFormatter, LanguageServerName, LspAdapter, LspAdapterDelegate};
|
use language::{LanguageServerName, LspAdapter, LspAdapterDelegate};
|
||||||
use lsp::LanguageServerBinary;
|
use lsp::LanguageServerBinary;
|
||||||
use node_runtime::NodeRuntime;
|
use node_runtime::NodeRuntime;
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
|
@ -95,10 +95,6 @@ impl LspAdapter for SvelteLspAdapter {
|
||||||
"provideFormatter": true
|
"provideFormatter": true
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn enabled_formatters(&self) -> Vec<BundledFormatter> {
|
|
||||||
vec![BundledFormatter::prettier()]
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_cached_server_binary(
|
async fn get_cached_server_binary(
|
||||||
|
|
|
@ -163,7 +163,7 @@ impl LspAdapter for TypeScriptLspAdapter {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn enabled_formatters(&self) -> Vec<BundledFormatter> {
|
fn enabled_formatters(&self) -> Vec<BundledFormatter> {
|
||||||
vec![BundledFormatter::prettier()]
|
vec![BundledFormatter::prettier("typescript")]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -315,7 +315,7 @@ impl LspAdapter for EsLintLspAdapter {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn enabled_formatters(&self) -> Vec<BundledFormatter> {
|
fn enabled_formatters(&self) -> Vec<BundledFormatter> {
|
||||||
vec![BundledFormatter::prettier()]
|
vec![BundledFormatter::prettier("babel")]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -111,7 +111,7 @@ impl LspAdapter for YamlLspAdapter {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn enabled_formatters(&self) -> Vec<BundledFormatter> {
|
fn enabled_formatters(&self) -> Vec<BundledFormatter> {
|
||||||
vec![BundledFormatter::prettier()]
|
vec![BundledFormatter::prettier("yaml")]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue