Do not infuse `FakeNodeRuntime` with prettier exceptions, rather keep the default formatter installation method as no-op.
Unified · Split
| @@ -4555,11 +4555,7 @@ async fn test_prettier_formatting_buffer( · 4554 unmodified lines | ||
| 4555 | 4555 | .insert_tree(&directory, json!({ "a.rs": buffer_text })) |
| 4556 | 4556 | .await; |
| 4557 | 4557 | let (project_a, worktree_id) = client_a.build_local_project(&directory, cx_a).await; |
| 4558 | − let prettier_format_suffix = project_a.update(cx_a, |project, _| { | |
| 4559 | − let suffix = project.enable_test_prettier(&[test_plugin]); | |
| 4560 | − project.languages().add(language); | |
| 4561 | − suffix | |
| 4562 | − }); | |
| 4558 | + let prettier_format_suffix = project::TEST_PRETTIER_FORMAT_SUFFIX; | |
| 4563 | 4559 | let buffer_a = cx_a |
| 4564 | 4560 | .background() |
| 4565 | 4561 | .spawn(project_a.update(cx_a, |p, cx| p.open_buffer((worktree_id, "a.rs"), cx))) |
| @@ -5117,7 +5117,6 @@ async fn test_document_format_manual_trigger(cx: &mut gpui::TestAppContext) { · 5116 unmodified lines | ||
| 5117 | 5117 | |
| 5118 | 5118 | let project = Project::test(fs, ["/file.rs".as_ref()], cx).await; |
| 5119 | 5119 | project.update(cx, |project, _| { |
| 5120 | − project.enable_test_prettier(&[]); | |
| 5121 | 5120 | project.languages().add(Arc::new(language)); |
| 5122 | 5121 | }); |
| 5123 | 5122 | let buffer = project |
| @@ -7864,10 +7863,9 @@ async fn test_document_format_with_prettier(cx: &mut gpui::TestAppContext) { · 2740 unmodified lines | ||
| 7864 | 7863 | fs.insert_file("/file.rs", Default::default()).await; |
| 7865 | 7864 | |
| 7866 | 7865 | let project = Project::test(fs, ["/file.rs".as_ref()], cx).await; |
| 7867 | − let prettier_format_suffix = project.update(cx, |project, _| { | |
| 7868 | − let suffix = project.enable_test_prettier(&[test_plugin]); | |
| 7866 | + let prettier_format_suffix = project::TEST_PRETTIER_FORMAT_SUFFIX; | |
| 7867 | + project.update(cx, |project, _| { | |
| 7869 | 7868 | project.languages().add(Arc::new(language)); |
| 7870 | − suffix | |
| 7871 | 7869 | }); |
| 7872 | 7870 | let buffer = project |
| 7873 | 7871 | .update(cx, |project, cx| project.open_local_buffer("/file.rs", cx)) |
| @@ -220,96 +220,31 @@ impl NodeRuntime for RealNodeRuntime { · 219 unmodified lines | ||
| 220 | 220 | } |
| 221 | 221 | } |
| 222 | 222 | |
| 223 | −pub struct FakeNodeRuntime(Option<PrettierSupport>); | |
| 224 | − | |
| 225 | −struct PrettierSupport { | |
| 226 | − plugins: Vec<&'static str>, | |
| 227 | −} | |
| 223 | +pub struct FakeNodeRuntime; | |
| 228 | 224 | |
| 229 | 225 | impl FakeNodeRuntime { |
| 230 | 226 | pub fn new() -> Arc<dyn NodeRuntime> { |
| 231 | − Arc::new(FakeNodeRuntime(None)) | |
| 232 | − } | |
| 233 | − | |
| 234 | − pub fn with_prettier_support(plugins: &[&'static str]) -> Arc<dyn NodeRuntime> { | |
| 235 | − Arc::new(FakeNodeRuntime(Some(PrettierSupport::new(plugins)))) | |
| 227 | + Arc::new(Self) | |
| 236 | 228 | } |
| 237 | 229 | } |
| 238 | 230 | |
| 239 | 231 | #[async_trait::async_trait] |
| 240 | 232 | impl NodeRuntime for FakeNodeRuntime { |
| 241 | 233 | async fn binary_path(&self) -> anyhow::Result<PathBuf> { |
| 242 | − if let Some(prettier_support) = &self.0 { | |
| 243 | − prettier_support.binary_path().await | |
| 244 | − } else { | |
| 245 | − unreachable!() | |
| 246 | − } | |
| 234 | + unreachable!() | |
| 247 | 235 | } |
| 248 | 236 | |
| 249 | 237 | async fn run_npm_subcommand( |
| 250 | 238 | &self, |
| 251 | − directory: Option<&Path>, | |
| 239 | + _: Option<&Path>, | |
| 252 | 240 | subcommand: &str, |
| 253 | 241 | args: &[&str], |
| 254 | 242 | ) -> anyhow::Result<Output> { |
| 255 | − if let Some(prettier_support) = &self.0 { | |
| 256 | − prettier_support | |
| 257 | − .run_npm_subcommand(directory, subcommand, args) | |
| 258 | − .await | |
| 259 | − } else { | |
| 260 | − unreachable!() | |
| 261 | − } | |
| 262 | − } | |
| 263 | − | |
| 264 | − async fn npm_package_latest_version(&self, name: &str) -> anyhow::Result<String> { | |
| 265 | − if let Some(prettier_support) = &self.0 { | |
| 266 | − prettier_support.npm_package_latest_version(name).await | |
| 267 | − } else { | |
| 268 | − unreachable!() | |
| 269 | − } | |
| 270 | − } | |
| 271 | − | |
| 272 | − async fn npm_install_packages( | |
| 273 | − &self, | |
| 274 | − directory: &Path, | |
| 275 | − packages: &[(&str, &str)], | |
| 276 | − ) -> anyhow::Result<()> { | |
| 277 | − if let Some(prettier_support) = &self.0 { | |
| 278 | − prettier_support | |
| 279 | − .npm_install_packages(directory, packages) | |
| 280 | − .await | |
| 281 | − } else { | |
| 282 | − unreachable!() | |
| 283 | − } | |
| 284 | − } | |
| 285 | −} | |
| 286 | − | |
| 287 | −impl PrettierSupport { | |
| 288 | − const PACKAGE_VERSION: &str = "0.0.1"; | |
| 289 | − | |
| 290 | − fn new(plugins: &[&'static str]) -> Self { | |
| 291 | − Self { | |
| 292 | − plugins: plugins.to_vec(), | |
| 293 | − } | |
| 294 | − } | |
| 295 | −} | |
| 296 | − | |
| 297 | −#[async_trait::async_trait] | |
| 298 | −impl NodeRuntime for PrettierSupport { | |
| 299 | − async fn binary_path(&self) -> anyhow::Result<PathBuf> { | |
| 300 | − Ok(PathBuf::from("prettier_fake_node")) | |
| 301 | − } | |
| 302 | − | |
| 303 | − async fn run_npm_subcommand(&self, _: Option<&Path>, _: &str, _: &[&str]) -> Result<Output> { | |
| 304 | − unreachable!() | |
| 243 | + unreachable!("Should not run npm subcommand '{subcommand}' with args {args:?}") | |
| 305 | 244 | } |
| 306 | 245 | |
| 307 | 246 | async fn npm_package_latest_version(&self, name: &str) -> anyhow::Result<String> { |
| 308 | − if name == "prettier" || self.plugins.contains(&name) { | |
| 309 | − Ok(Self::PACKAGE_VERSION.to_string()) | |
| 310 | − } else { | |
| 311 | − panic!("Unexpected package name: {name}") | |
| 312 | − } | |
| 247 | + unreachable!("Should not query npm package '{name}' for latest version") | |
| 313 | 248 | } |
| 314 | 249 | |
| 315 | 250 | async fn npm_install_packages( |
| @@ -317,32 +252,6 @@ impl NodeRuntime for PrettierSupport { · 1 unmodified line | ||
| 317 | 252 | _: &Path, |
| 318 | 253 | packages: &[(&str, &str)], |
| 319 | 254 | ) -> anyhow::Result<()> { |
| 320 | − assert_eq!( | |
| 321 | − packages.len(), | |
| 322 | − self.plugins.len() + 1, | |
| 323 | − "Unexpected packages length to install: {:?}, expected `prettier` + {:?}", | |
| 324 | − packages, | |
| 325 | − self.plugins | |
| 326 | − ); | |
| 327 | − for (name, version) in packages { | |
| 328 | − assert!( | |
| 329 | − name == &"prettier" || self.plugins.contains(name), | |
| 330 | − "Unexpected package `{}` to install in packages {:?}, expected {} for `prettier` + {:?}", | |
| 331 | − name, | |
| 332 | − packages, | |
| 333 | − Self::PACKAGE_VERSION, | |
| 334 | − self.plugins | |
| 335 | − ); | |
| 336 | − assert_eq!( | |
| 337 | − version, | |
| 338 | − &Self::PACKAGE_VERSION, | |
| 339 | − "Unexpected package version `{}` to install in packages {:?}, expected {} for `prettier` + {:?}", | |
| 340 | − version, | |
| 341 | − packages, | |
| 342 | − Self::PACKAGE_VERSION, | |
| 343 | − self.plugins | |
| 344 | − ); | |
| 345 | − } | |
| 346 | − Ok(()) | |
| 255 | + unreachable!("Should not install packages {packages:?}") | |
| 347 | 256 | } |
| 348 | 257 | } |
| @@ -44,6 +44,9 @@ pub const PRETTIER_SERVER_JS: &str = include_str!("./prettier_server.js"); · 43 unmodified lines | ||
| 44 | 44 | const PRETTIER_PACKAGE_NAME: &str = "prettier"; |
| 45 | 45 | const TAILWIND_PRETTIER_PLUGIN_PACKAGE_NAME: &str = "prettier-plugin-tailwindcss"; |
| 46 | 46 | |
| 47 | +#[cfg(any(test, feature = "test-support"))] | |
| 48 | +pub const FORMAT_SUFFIX: &str = "\nformatted by test prettier"; | |
| 49 | + | |
| 47 | 50 | impl Prettier { |
| 48 | 51 | pub const CONFIG_FILE_NAMES: &'static [&'static str] = &[ |
| 49 | 52 | ".prettierrc", |
| @@ -60,9 +63,6 @@ impl Prettier { · 10 unmodified lines | ||
| 60 | 63 | ".editorconfig", |
| 61 | 64 | ]; |
| 62 | 65 | |
| 63 | − #[cfg(any(test, feature = "test-support"))] | |
| 64 | − pub const FORMAT_SUFFIX: &str = "\nformatted by test prettier"; | |
| 65 | − | |
| 66 | 66 | pub async fn locate( |
| 67 | 67 | starting_path: Option<LocateStart>, |
| 68 | 68 | fs: Arc<dyn Fs>, |
| @@ -349,7 +349,7 @@ impl Prettier { · 280 unmodified lines | ||
| 349 | 349 | #[cfg(any(test, feature = "test-support"))] |
| 350 | 350 | Self::Test(_) => Ok(buffer |
| 351 | 351 | .read_with(cx, |buffer, cx| { |
| 352 | − let formatted_text = buffer.text() + Self::FORMAT_SUFFIX; | |
| 352 | + let formatted_text = buffer.text() + FORMAT_SUFFIX; | |
| 353 | 353 | buffer.diff(formatted_text, cx) |
| 354 | 354 | }) |
| 355 | 355 | .await), |
| @@ -86,6 +86,8 @@ use util::{ · 85 unmodified lines | ||
| 86 | 86 | }; |
| 87 | 87 | |
| 88 | 88 | pub use fs::*; |
| 89 | +#[cfg(any(test, feature = "test-support"))] | |
| 90 | +pub use prettier::FORMAT_SUFFIX as TEST_PRETTIER_FORMAT_SUFFIX; | |
| 89 | 91 | pub use worktree::*; |
| 90 | 92 | |
| 91 | 93 | pub trait Item { |
| @@ -833,16 +835,6 @@ impl Project { · 741 unmodified lines | ||
| 833 | 835 | project |
| 834 | 836 | } |
| 835 | 837 | |
| 836 | − /// Enables a prettier mock that avoids interacting with node runtime, prettier LSP wrapper, or any real file changes. | |
| 837 | − /// Instead, if appends the suffix to every input, this suffix is returned by this method. | |
| 838 | − #[cfg(any(test, feature = "test-support"))] | |
| 839 | − pub fn enable_test_prettier(&mut self, plugins: &[&'static str]) -> &'static str { | |
| 840 | − self.node = Some(node_runtime::FakeNodeRuntime::with_prettier_support( | |
| 841 | − plugins, | |
| 842 | − )); | |
| 843 | − Prettier::FORMAT_SUFFIX | |
| 844 | − } | |
| 845 | − | |
| 846 | 838 | fn on_settings_changed(&mut self, cx: &mut ModelContext<Self>) { |
| 847 | 839 | let mut language_servers_to_start = Vec::new(); |
| 848 | 840 | let mut language_formatters_to_check = Vec::new(); |