Skip to content

Commit

Permalink
dedicated tests for editorconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
BuonOmo committed Aug 17, 2024
1 parent 4c55d63 commit 1587660
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 39 deletions.
1 change: 1 addition & 0 deletions crates/language/src/language_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ fn editorconfig_settings(
// avoids a performance hit of cloning settings. We could consider showing
// the error to the user though (it may be a parsing error).
.and_then(|local_file| ec4rs::properties_of(local_file.abs_path(cx)).ok())
.and_then(|cfg| if cfg.is_empty() { None } else { Some(cfg) })
.map(|mut cfg| {
cfg.use_fallbacks();
let max_line_length = cfg.get::<MaxLineLen>().ok().and_then(|v| match v {
Expand Down
159 changes: 120 additions & 39 deletions crates/project/src/project_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use futures::{future, StreamExt};
use gpui::{AppContext, SemanticVersion, UpdateGlobal};
use http_client::Url;
use language::{
language_settings::{AllLanguageSettings, LanguageSettingsContent},
language_settings::{AllLanguageSettings, LanguageSettingsContent, SoftWrap},
tree_sitter_rust, tree_sitter_typescript, Diagnostic, FakeLspAdapter, LanguageConfig,
LanguageMatcher, LineEnding, OffsetRangeExt, Point, ToPoint,
};
Expand All @@ -14,7 +14,7 @@ use pretty_assertions::assert_eq;
use serde_json::json;
#[cfg(not(windows))]
use std::os;
use std::task::Poll;
use std::{num::NonZeroU32, task::Poll};
use task::{ResolvedTask, TaskContext, TaskTemplate, TaskTemplates};
use unindent::Unindent as _;
use util::{assert_set_eq, paths::PathMatcher, test::temp_tree};
Expand Down Expand Up @@ -91,48 +91,132 @@ async fn test_symlinks(cx: &mut gpui::TestAppContext) {
}

#[gpui::test]
async fn test_managing_project_specific_settings(cx: &mut gpui::TestAppContext) {
async fn test_editorconfig_support(cx: &mut gpui::TestAppContext) {
init_test(cx);
cx.executor().allow_parking();

let dir = temp_tree(json!({
".editorconfig": r#"
root = true
[*.rs]
indent_style = tab
indent_size = 3
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
max_line_length = 80
[*.rb]
tab_width = 10
"#,
".zed": {
"settings.json": r#"{ "tab_size": 8, "ensure_final_newline_on_save": false }"#,
"tasks.json": r#"[{
"label": "cargo check",
"command": "cargo",
"args": ["check", "--all"]
},]"#,
"settings.json": r#"{
"tab_size": 8,
"hard_tabs": false,
"ensure_final_newline_on_save": false,
"remove_trailing_whitespace_on_save": false,
"preferred_line_length": 64,
"soft_wrap": "editor_width"
}"#,
},
".editorconfig": r#"root = true
[*.rs]
insert_final_newline = true
"a.rs": "fn a() {\n A\n}",
"b": {
".editorconfig": r#"
[*.rs]
indent_size = 2
max_line_length = off
"#,
"a": {
"a.rs": "fn a() {\n A\n}"
"b.rs": "fn b() {\n B\n}",
},
"b": {
"c.rb": "def c\n C\nend",
"README.md": "tabs are better\n",
}));

let path = dir.path();
let fs = FakeFs::new(cx.executor());
fs.insert_tree_from_real_fs(path, path).await;
let project = Project::test(fs, [path], cx).await;
let worktree = project.update(cx, |project, cx| project.worktrees(cx).next().unwrap());

cx.executor().run_until_parked();

cx.update(|cx| {
let tree = worktree.read(cx);
let settings_for = |path: &str| {
language_settings(
None,
Some(
&(File::for_entry(tree.entry_for_path(path).unwrap().clone(), worktree.clone())
as _),
),
cx,
)
};

let settings_a = settings_for("a.rs");
let settings_b = settings_for("b/b.rs");
let settings_c = settings_for("c.rb");
let settings_readme = settings_for("README.md");

// .editorconfig overrides .zed/settings
assert_eq!(Some(settings_a.tab_size), NonZeroU32::new(3));
assert_eq!(settings_a.hard_tabs, true);
assert_eq!(settings_a.ensure_final_newline_on_save, true);
assert_eq!(settings_a.remove_trailing_whitespace_on_save, true);
assert_eq!(settings_a.preferred_line_length, 80);

// "max_line_length" also sets "soft_wrap"
assert_eq!(settings_a.soft_wrap, SoftWrap::PreferredLineLength);

// .editorconfig in b/ overrides .editorconfig in root
assert_eq!(Some(settings_b.tab_size), NonZeroU32::new(2));

// "indent_size" is not set, so "tab_width" is used
assert_eq!(Some(settings_c.tab_size), NonZeroU32::new(10));

// When max_line_length is "off", default to .zed/settings.json
assert_eq!(settings_b.preferred_line_length, 64);
assert_eq!(settings_b.soft_wrap, SoftWrap::EditorWidth);

// README.md should not be affected by .editorconfig's globe "*.rs"
assert_eq!(Some(settings_readme.tab_size), NonZeroU32::new(8));
});
}

#[gpui::test]
async fn test_managing_project_specific_settings(cx: &mut gpui::TestAppContext) {
init_test(cx);

let fs = FakeFs::new(cx.executor());
fs.insert_tree(
"/the-root",
json!({
".zed": {
"settings.json": r#"{ "tab_size": 2 }"#,
"settings.json": r#"{ "tab_size": 8 }"#,
"tasks.json": r#"[{
"label": "cargo check",
"command": "cargo",
"args": ["check", "--all"]
},]"#,
},
"a": {
"a.rs": "fn a() {\n A\n}"
},
"b": {
".zed": {
"settings.json": r#"{ "tab_size": 2 }"#,
"tasks.json": r#"[{
"label": "cargo check",
"command": "cargo",
"args": ["check"]
},]"#,
},
"b.rs": "fn b() {\n B\n}"
},
"c": {
"c.rs": "fn c() {\n C\n}",
".editorconfig": r#"[*.rs]
indent_size = 4
"#,
}
}));

let path = dir.path();
let fs = FakeFs::new(cx.executor());
fs.insert_tree_from_real_fs(path, path).await;
},
"b.rs": "fn b() {\n B\n}"
}
}),
)
.await;

let project = Project::test(fs.clone(), [path], cx).await;
let project = Project::test(fs.clone(), ["/the-root".as_ref()], cx).await;
let worktree = project.update(cx, |project, cx| project.worktrees(cx).next().unwrap());
let task_context = TaskContext::default();

Expand All @@ -144,7 +228,7 @@ async fn test_managing_project_specific_settings(cx: &mut gpui::TestAppContext)
});
let global_task_source_kind = TaskSourceKind::Worktree {
id: worktree_id,
abs_path: PathBuf::from(format!("{}/.zed/tasks.json", path.display())),
abs_path: PathBuf::from("/the-root/.zed/tasks.json"),
id_base: "local_tasks_for_worktree".into(),
};

Expand Down Expand Up @@ -202,7 +286,7 @@ async fn test_managing_project_specific_settings(cx: &mut gpui::TestAppContext)
(
TaskSourceKind::Worktree {
id: worktree_id,
abs_path: PathBuf::from(format!("{}/b/.zed/tasks.json", path.display())),
abs_path: PathBuf::from("/the-root/b/.zed/tasks.json"),
id_base: "local_tasks_for_worktree".into(),
},
"cargo check".to_string(),
Expand Down Expand Up @@ -243,10 +327,7 @@ async fn test_managing_project_specific_settings(cx: &mut gpui::TestAppContext)
cx.update(|cx| {
project.update(cx, |project, cx| {
project.task_inventory().update(cx, |inventory, cx| {
inventory.remove_local_static_source(&PathBuf::from(format!(
"{}/.zed/tasks.json",
path.display()
)));
inventory.remove_local_static_source(Path::new("/the-root/.zed/tasks.json"));
inventory.add_source(
global_task_source_kind.clone(),
|tx, cx| StaticSource::new(TrackedFile::new(rx, tx, cx)),
Expand Down Expand Up @@ -278,7 +359,7 @@ async fn test_managing_project_specific_settings(cx: &mut gpui::TestAppContext)
(
TaskSourceKind::Worktree {
id: worktree_id,
abs_path: PathBuf::from(format!("{}/.zed/tasks.json", path.display())),
abs_path: PathBuf::from("/the-root/.zed/tasks.json"),
id_base: "local_tasks_for_worktree".into(),
},
"cargo check".to_string(),
Expand All @@ -295,7 +376,7 @@ async fn test_managing_project_specific_settings(cx: &mut gpui::TestAppContext)
(
TaskSourceKind::Worktree {
id: worktree_id,
abs_path: PathBuf::from(format!("{}/b/.zed/tasks.json", path.display())),
abs_path: PathBuf::from("/the-root/b/.zed/tasks.json"),
id_base: "local_tasks_for_worktree".into(),
},
"cargo check".to_string(),
Expand Down

0 comments on commit 1587660

Please sign in to comment.