Draft a project part of the prettier

This commit is contained in:
Kirill Bulatov 2023-09-01 18:31:16 +03:00
parent eced842dfc
commit 553abd01be
6 changed files with 122 additions and 14 deletions

7
Cargo.lock generated
View file

@ -5520,6 +5520,12 @@ checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]] [[package]]
name = "prettier" name = "prettier"
version = "0.1.0" version = "0.1.0"
dependencies = [
"anyhow",
"fs",
"gpui",
"language",
]
[[package]] [[package]]
name = "pretty_assertions" name = "pretty_assertions"
@ -5635,6 +5641,7 @@ dependencies = [
"lsp", "lsp",
"parking_lot 0.11.2", "parking_lot 0.11.2",
"postage", "postage",
"prettier",
"pretty_assertions", "pretty_assertions",
"rand 0.8.5", "rand 0.8.5",
"regex", "regex",

View file

@ -149,10 +149,15 @@ pub enum ShowWhitespaceSetting {
All, All,
} }
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema)] #[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")] #[serde(rename_all = "snake_case")]
pub enum Formatter { pub enum Formatter {
#[default]
Auto,
LanguageServer, LanguageServer,
Prettier {
config: (), // Support some of the most important settings in the prettier-vscode extension.
},
External { External {
command: Arc<str>, command: Arc<str>,
arguments: Arc<[String]>, arguments: Arc<[String]>,

View file

@ -3,7 +3,18 @@ name = "prettier"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies]
[lib] [lib]
path = "src/prettier.rs" path = "src/prettier.rs"
[dependencies]
language = { path = "../language" }
gpui = { path = "../gpui" }
fs = { path = "../fs" }
anyhow.workspace = true
[dev-dependencies]
language = { path = "../language", features = ["test-support"] }
gpui = { path = "../gpui", features = ["test-support"] }
fs = { path = "../fs", features = ["test-support"] }

View file

@ -1,14 +1,46 @@
pub fn add(left: usize, right: usize) -> usize { pub use std::path::{Path, PathBuf};
left + right pub use std::sync::Arc;
use fs::Fs;
use gpui::ModelHandle;
use language::{Buffer, Diff};
pub struct Prettier {
_private: (),
} }
#[cfg(test)] type NodeRuntime = ();
mod tests {
use super::*;
#[test] impl Prettier {
fn it_works() { // This was taken from the prettier-vscode extension.
let result = add(2, 2); pub const CONFIG_FILE_NAMES: &'static [&'static str] = &[
assert_eq!(result, 4); ".prettierrc",
".prettierrc.json",
".prettierrc.json5",
".prettierrc.yaml",
".prettierrc.yml",
".prettierrc.toml",
".prettierrc.js",
".prettierrc.cjs",
"package.json",
"prettier.config.js",
"prettier.config.cjs",
".editorconfig",
];
pub async fn locate(starting_path: Option<&Path>, fs: Arc<dyn Fs>) -> PathBuf {
todo!()
}
pub async fn start(prettier_path: &Path, node: Arc<NodeRuntime>) -> anyhow::Result<Self> {
todo!()
}
pub async fn format(&self, buffer: &ModelHandle<Buffer>) -> anyhow::Result<Diff> {
todo!()
}
pub async fn clear_cache(&self) -> anyhow::Result<()> {
todo!()
} }
} }

View file

@ -31,6 +31,7 @@ git = { path = "../git" }
gpui = { path = "../gpui" } gpui = { path = "../gpui" }
language = { path = "../language" } language = { path = "../language" }
lsp = { path = "../lsp" } lsp = { path = "../lsp" }
prettier = { path = "../prettier" }
rpc = { path = "../rpc" } rpc = { path = "../rpc" }
settings = { path = "../settings" } settings = { path = "../settings" }
sum_tree = { path = "../sum_tree" } sum_tree = { path = "../sum_tree" }

View file

@ -50,6 +50,7 @@ use lsp::{
}; };
use lsp_command::*; use lsp_command::*;
use postage::watch; use postage::watch;
use prettier::Prettier;
use project_settings::{LspSettings, ProjectSettings}; use project_settings::{LspSettings, ProjectSettings};
use rand::prelude::*; use rand::prelude::*;
use search::SearchQuery; use search::SearchQuery;
@ -152,6 +153,7 @@ pub struct Project {
copilot_lsp_subscription: Option<gpui::Subscription>, copilot_lsp_subscription: Option<gpui::Subscription>,
copilot_log_subscription: Option<lsp::Subscription>, copilot_log_subscription: Option<lsp::Subscription>,
current_lsp_settings: HashMap<Arc<str>, LspSettings>, current_lsp_settings: HashMap<Arc<str>, LspSettings>,
prettier_instances: HashMap<(WorktreeId, PathBuf), Shared<Task<Result<Arc<Prettier>>>>>,
} }
struct DelayedDebounced { struct DelayedDebounced {
@ -660,6 +662,7 @@ impl Project {
copilot_lsp_subscription, copilot_lsp_subscription,
copilot_log_subscription: None, copilot_log_subscription: None,
current_lsp_settings: settings::get::<ProjectSettings>(cx).lsp.clone(), current_lsp_settings: settings::get::<ProjectSettings>(cx).lsp.clone(),
prettier_instances: HashMap::default(),
} }
}) })
} }
@ -757,6 +760,7 @@ impl Project {
copilot_lsp_subscription, copilot_lsp_subscription,
copilot_log_subscription: None, copilot_log_subscription: None,
current_lsp_settings: settings::get::<ProjectSettings>(cx).lsp.clone(), current_lsp_settings: settings::get::<ProjectSettings>(cx).lsp.clone(),
prettier_instances: HashMap::default(),
}; };
for worktree in worktrees { for worktree in worktrees {
let _ = this.add_worktree(&worktree, cx); let _ = this.add_worktree(&worktree, cx);
@ -4027,6 +4031,7 @@ impl Project {
enum FormatOperation { enum FormatOperation {
Lsp(Vec<(Range<Anchor>, String)>), Lsp(Vec<(Range<Anchor>, String)>),
External(Diff), External(Diff),
Prettier(Diff),
} }
// Apply language-specific formatting using either a language server // Apply language-specific formatting using either a language server
@ -4062,8 +4067,8 @@ impl Project {
| (_, FormatOnSave::External { command, arguments }) => { | (_, FormatOnSave::External { command, arguments }) => {
if let Some(buffer_abs_path) = buffer_abs_path { if let Some(buffer_abs_path) = buffer_abs_path {
format_operation = Self::format_via_external_command( format_operation = Self::format_via_external_command(
&buffer, buffer,
&buffer_abs_path, buffer_abs_path,
&command, &command,
&arguments, &arguments,
&mut cx, &mut cx,
@ -4076,6 +4081,45 @@ impl Project {
.map(FormatOperation::External); .map(FormatOperation::External);
} }
} }
(Formatter::Auto, FormatOnSave::On | FormatOnSave::Off) => {
if let Some(prettier) = this.update(&mut cx, |project, _| {
project.prettier_instance_for_buffer(buffer)
}) {
format_operation = Some(FormatOperation::Prettier(
prettier
.format(buffer)
.await
.context("autoformatting via prettier")?,
));
} else if let Some((language_server, buffer_abs_path)) =
language_server.as_ref().zip(buffer_abs_path.as_ref())
{
format_operation = Some(FormatOperation::Lsp(
Self::format_via_lsp(
&this,
&buffer,
buffer_abs_path,
&language_server,
tab_size,
&mut cx,
)
.await
.context("failed to format via language server")?,
));
}
}
(Formatter::Prettier { .. }, FormatOnSave::On | FormatOnSave::Off) => {
if let Some(prettier) = this.update(&mut cx, |project, _| {
project.prettier_instance_for_buffer(buffer)
}) {
format_operation = Some(FormatOperation::Prettier(
prettier
.format(buffer)
.await
.context("formatting via prettier")?,
));
}
}
}; };
buffer.update(&mut cx, |b, cx| { buffer.update(&mut cx, |b, cx| {
@ -4100,6 +4144,9 @@ impl Project {
FormatOperation::External(diff) => { FormatOperation::External(diff) => {
b.apply_diff(diff, cx); b.apply_diff(diff, cx);
} }
FormatOperation::Prettier(diff) => {
b.apply_diff(diff, cx);
}
} }
if let Some(transaction_id) = whitespace_transaction_id { if let Some(transaction_id) = whitespace_transaction_id {
@ -8109,6 +8156,11 @@ impl Project {
Vec::new() Vec::new()
} }
} }
fn prettier_instance_for_buffer(&self, buffer: &ModelHandle<Buffer>) -> Option<Prettier> {
// TODO kb
None
}
} }
fn subscribe_for_copilot_events( fn subscribe_for_copilot_events(