Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
use super::*;
use crate::Buffer;
use clock::ReplicaId;
use collections::BTreeMap;
use futures::FutureExt as _;
use futures_lite::future::yield_now;
use gpui::{App, AppContext as _, BorrowAppContext, Entity};
use gpui::{HighlightStyle, TestAppContext};
use indoc::indoc;
use pretty_assertions::assert_eq;
use proto::deserialize_operation;
use rand::prelude::*;
use regex::RegexBuilder;
use settings::SettingsStore;
use settings::{AllLanguageSettingsContent, LanguageSettingsContent};
use std::collections::BTreeSet;
use std::{
env,
ops::Range,
sync::LazyLock,
time::{Duration, Instant},
};
use syntax_map::{MAX_BYTES_TO_QUERY, TreeSitterOptions};
use text::network::Network;
use text::{BufferId, LineEnding};
use text::{Point, ToPoint};
use theme::ActiveTheme;
use unindent::Unindent as _;
use util::rel_path::rel_path;
use util::test::marked_text_offsets;
use util::{RandomCharIter, assert_set_eq, post_inc, test::marked_text_ranges};
pub static TRAILING_WHITESPACE_REGEX: LazyLock<regex::Regex> = LazyLock::new(|| {
RegexBuilder::new(r"[ \t]+$")
.multi_line(true)
.build()
.expect("Failed to create TRAILING_WHITESPACE_REGEX")
});
#[cfg(test)]
#[ctor::ctor(unsafe)]
fn init_logger() {
zlog::init_test();
}
#[gpui::test]
fn test_line_endings(cx: &mut gpui::App) {
init_settings(cx, |_| {});
cx.new(|cx| {
let mut buffer = Buffer::local("one\r\ntwo\rthree", cx).with_language(rust_lang(), cx);
assert_eq!(buffer.text(), "one\ntwo\nthree");
assert_eq!(buffer.line_ending(), LineEnding::Windows);
buffer.check_invariants();
buffer.edit(
[(buffer.len()..buffer.len(), "\r\nfour")],
Some(AutoindentMode::EachLine),
cx,
);
buffer.edit([(0..0, "zero\r\n")], None, cx);
assert_eq!(buffer.text(), "zero\none\ntwo\nthree\nfour");
assert_eq!(buffer.line_ending(), LineEnding::Windows);
buffer.check_invariants();
buffer
});
}
#[gpui::test]
fn test_set_line_ending(cx: &mut TestAppContext) {
let base = cx.new(|cx| Buffer::local("one\ntwo\nthree\n", cx));
let base_replica = cx.new(|cx| {
Buffer::from_proto(
ReplicaId::new(1),
Capability::ReadWrite,
base.read(cx).to_proto(cx),
None,
cx,
)
.unwrap()
});
base.update(cx, |_buffer, cx| {
cx.subscribe(&base_replica, |this, _, event, cx| {
if let BufferEvent::Operation {
operation,
is_local: true,
} = event
{
this.apply_ops([operation.clone()], cx);
}
})
.detach();
});
base_replica.update(cx, |_buffer, cx| {
cx.subscribe(&base, |this, _, event, cx| {
if let BufferEvent::Operation {
operation,
is_local: true,
} = event
{
this.apply_ops([operation.clone()], cx);
}
})
.detach();
});
// Base
base_replica.read_with(cx, |buffer, _| {
assert_eq!(buffer.line_ending(), LineEnding::Unix);
});
base.update(cx, |buffer, cx| {
assert_eq!(buffer.line_ending(), LineEnding::Unix);
buffer.set_line_ending(LineEnding::Windows, cx);
assert_eq!(buffer.line_ending(), LineEnding::Windows);
});
base_replica.read_with(cx, |buffer, _| {
assert_eq!(buffer.line_ending(), LineEnding::Windows);
});
base.update(cx, |buffer, cx| {
buffer.set_line_ending(LineEnding::Unix, cx);
assert_eq!(buffer.line_ending(), LineEnding::Unix);
});
base_replica.read_with(cx, |buffer, _| {
assert_eq!(buffer.line_ending(), LineEnding::Unix);
});
// Replica
base.read_with(cx, |buffer, _| {
assert_eq!(buffer.line_ending(), LineEnding::Unix);
});
base_replica.update(cx, |buffer, cx| {
assert_eq!(buffer.line_ending(), LineEnding::Unix);
buffer.set_line_ending(LineEnding::Windows, cx);
assert_eq!(buffer.line_ending(), LineEnding::Windows);
});
base.read_with(cx, |buffer, _| {
assert_eq!(buffer.line_ending(), LineEnding::Windows);
});
base_replica.update(cx, |buffer, cx| {
buffer.set_line_ending(LineEnding::Unix, cx);
assert_eq!(buffer.line_ending(), LineEnding::Unix);
});
base.read_with(cx, |buffer, _| {
assert_eq!(buffer.line_ending(), LineEnding::Unix);
});
}
#[gpui::test]
fn test_select_language(cx: &mut App) {
init_settings(cx, |_| {});
let registry = Arc::new(LanguageRegistry::test(cx.background_executor().clone()));
registry.add(Arc::new(Language::new(
LanguageConfig {
name: LanguageName::new_static("Rust"),
matcher: (LanguageMatcher {
path_suffixes: vec!["rs".into()],
..Default::default()
})
.into(),
..Default::default()
},
Some(tree_sitter_rust::LANGUAGE.into()),
)));
registry.add(Arc::new(Language::new(
LanguageConfig {
name: "Rust with longer extension".into(),
matcher: (LanguageMatcher {
path_suffixes: vec!["longer.rs".into()],
..Default::default()
})
.into(),
..Default::default()
},
Some(tree_sitter_rust::LANGUAGE.into()),
)));
registry.add(Arc::new(Language::new(
LanguageConfig {
name: LanguageName::new_static("Make"),
matcher: (LanguageMatcher {
path_suffixes: vec!["Makefile".into(), "mk".into()],
..Default::default()
})
.into(),
..Default::default()
},
Some(tree_sitter_rust::LANGUAGE.into()),
)));
// matching file extension
assert_eq!(
registry
.language_for_file(&file("src/lib.rs"), None, cx)
.and_then(|id| registry.language_name_for_id(id)),
Some("Rust".into())
);
assert_eq!(
registry
.language_for_file(&file("src/lib.mk"), None, cx)
.and_then(|id| registry.language_name_for_id(id)),
Some("Make".into())
);
// matching longer, compound extension, part of which could also match another lang
assert_eq!(
registry
.language_for_file(&file("src/lib.longer.rs"), None, cx)
.and_then(|id| registry.language_name_for_id(id)),
Some("Rust with longer extension".into())
);
// matching filename
assert_eq!(
registry
.language_for_file(&file("src/Makefile"), None, cx)
.and_then(|id| registry.language_name_for_id(id)),
Some("Make".into())
);
// matching suffix that is not the full file extension or filename
assert_eq!(
registry
.language_for_file(&file("zed/cars"), None, cx)
.and_then(|id| registry.language_name_for_id(id)),
None
);
assert_eq!(
registry
.language_for_file(&file("zed/a.cars"), None, cx)
.and_then(|id| registry.language_name_for_id(id)),
None
);
assert_eq!(
registry
.language_for_file(&file("zed/sumk"), None, cx)
.and_then(|id| registry.language_name_for_id(id)),
None
);
}
#[gpui::test(iterations = 10)]
async fn test_first_line_pattern(cx: &mut TestAppContext) {
cx.update(|cx| init_settings(cx, |_| {}));
let languages = LanguageRegistry::test(cx.executor());
let languages = Arc::new(languages);
languages.register_test_language(LanguageConfig {
name: "JavaScript".into(),
matcher: (LanguageMatcher {
path_suffixes: vec!["js".into()],
first_line_pattern: Some(Regex::new(r"\bnode\b").unwrap()),
..LanguageMatcher::default()
})
.into(),
..Default::default()
});
assert!(
cx.read(|cx| languages.language_for_file(&file("the/script"), None, cx))
.is_none()
);
assert!(
cx.read(|cx| languages.language_for_file(&file("the/script"), Some(&"nothing".into()), cx))
.is_none()
);
assert_eq!(
cx.read(|cx| languages.language_for_file(
&file("the/script"),
Some(&"#!/bin/env node".into()),
cx
))
.and_then(|id| languages.language_name_for_id(id))
.unwrap(),
"JavaScript"
);
}
#[gpui::test]
async fn test_language_for_file_with_custom_file_types(cx: &mut TestAppContext) {
cx.update(|cx| {
init_settings(cx, |settings| {
settings.file_types.get_or_insert_default().0.extend([
("TypeScript".into(), vec!["js".into()].into()),
(
"JavaScript".into(),
vec!["*longer.ts".into(), "ecmascript".into()].into(),
),
("C++".into(), vec!["c".into(), "*.dev".into()].into()),
(
"Dockerfile".into(),
vec!["Dockerfile".into(), "Dockerfile.*".into()].into(),
),
("SSH Config".into(), vec!["**/.ssh/config".into()].into()),
]);
})
});
let languages = Arc::new(LanguageRegistry::test(cx.executor()));
let language_name = |id| languages.language_name_for_id(id).unwrap();
for config in [
LanguageConfig {
name: "JavaScript".into(),
matcher: (LanguageMatcher {
path_suffixes: vec!["js".into()],
..Default::default()
})
.into(),
..Default::default()
},
LanguageConfig {
name: "TypeScript".into(),
matcher: (LanguageMatcher {
path_suffixes: vec!["ts".into(), "ts.ecmascript".into()],
..Default::default()
})
.into(),
..Default::default()
},
LanguageConfig {
name: "C++".into(),
matcher: (LanguageMatcher {
path_suffixes: vec!["cpp".into()],
..Default::default()
})
.into(),
..Default::default()
},
LanguageConfig {
name: "C".into(),
matcher: (LanguageMatcher {
path_suffixes: vec!["c".into()],
..Default::default()
})
.into(),
..Default::default()
},
LanguageConfig {
name: "Dockerfile".into(),
matcher: (LanguageMatcher {
path_suffixes: vec!["Dockerfile".into()],
..Default::default()
})
.into(),
..Default::default()
},
LanguageConfig {
name: "SSH Config".into(),
..Default::default()
},
LanguageConfig {
name: "INI".into(),
matcher: (LanguageMatcher {
path_suffixes: vec!["config".into()],
..Default::default()
})
.into(),
..Default::default()
},
] {
languages.add(Arc::new(Language::new(config, None)));
}
// matches system-provided lang extension
let language = cx
.read(|cx| languages.language_for_file(&file("foo.ts"), None, cx))
.unwrap();
assert_eq!(language_name(language), "TypeScript");
let language = cx
.read(|cx| languages.language_for_file(&file("foo.ts.ecmascript"), None, cx))
.unwrap();
assert_eq!(language_name(language), "TypeScript");
let language = cx
.read(|cx| languages.language_for_file(&file("foo.cpp"), None, cx))
.unwrap();
assert_eq!(language_name(language), "C++");
// user configured lang extension, same length as system-provided
let language = cx
.read(|cx| languages.language_for_file(&file("foo.js"), None, cx))
.unwrap();
assert_eq!(language_name(language), "TypeScript");
let language = cx
.read(|cx| languages.language_for_file(&file("foo.c"), None, cx))
.unwrap();
assert_eq!(language_name(language), "C++");
// user configured lang extension, longer than system-provided
let language = cx
.read(|cx| languages.language_for_file(&file("foo.longer.ts"), None, cx))
.unwrap();
assert_eq!(language_name(language), "JavaScript");
// user configured lang extension, shorter than system-provided
let language = cx
.read(|cx| languages.language_for_file(&file("foo.ecmascript"), None, cx))
.unwrap();
assert_eq!(language_name(language), "JavaScript");
// user configured glob matches
let language = cx
.read(|cx| languages.language_for_file(&file("c-plus-plus.dev"), None, cx))
.unwrap();
assert_eq!(language_name(language), "C++");
// should match Dockerfile.* => Dockerfile, not *.dev => C++
let language = cx
.read(|cx| languages.language_for_file(&file("Dockerfile.dev"), None, cx))
.unwrap();
assert_eq!(language_name(language), "Dockerfile");
let language = cx
.read(|cx| languages.language_for_file(&local_file("/root/.ssh", "config"), None, cx))
.unwrap();
assert_eq!(language_name(language), "SSH Config");
}
#[gpui::test]
async fn test_reregistering_language_during_load_yields_current_language(cx: &mut TestAppContext) {
let registry = Arc::new(LanguageRegistry::test(cx.executor()));
let (unblock_stale_load_tx, unblock_stale_load_rx) = futures::channel::oneshot::channel::<()>();
let unblock_stale_load_rx = std::sync::Mutex::new(Some(unblock_stale_load_rx));
let stale_config = LanguageConfig {
name: LanguageName::new_static("TheLanguage"),
matcher: Arc::new(LanguageMatcher {
path_suffixes: vec!["stale".into()],
..LanguageMatcher::default()
}),
..LanguageConfig::default()
};
registry.register_language(
stale_config.name.clone(),
None,
stale_config.matcher.clone(),
false,
None,
Arc::new(move || {
let unblock_stale_load_rx = unblock_stale_load_rx
.lock()
.expect("the stale loader mutex should not be poisoned")
.take();
let stale_config = stale_config.clone();
async move {
if let Some(unblock_stale_load_rx) = unblock_stale_load_rx {
unblock_stale_load_rx.await.ok();
}
Ok(LoadedLanguage {
config: stale_config,
queries: LanguageQueries::default(),
context_provider: None,
toolchain_provider: None,
manifest_name: None,
})
}
.boxed()
}),
);
let pending_language = registry.language_for_name("TheLanguage");
cx.executor().run_until_parked();
registry.register_test_language(LanguageConfig {
name: LanguageName::new_static("TheLanguage"),
matcher: Arc::new(LanguageMatcher {
path_suffixes: vec!["fresh".into()],
..LanguageMatcher::default()
}),
..LanguageConfig::default()
});
unblock_stale_load_tx.send(()).unwrap();
let language = pending_language.await.unwrap();
assert_eq!(
language.config.matcher.path_suffixes,
vec!["fresh".to_string()],
"a load that races with a re-registration should resolve to the re-registered language"
);
}
#[gpui::test]
async fn test_reregistering_language_during_failed_load_yields_current_language(
cx: &mut TestAppContext,
) {
let registry = Arc::new(LanguageRegistry::test(cx.executor()));
let (unblock_stale_load_tx, unblock_stale_load_rx) = futures::channel::oneshot::channel::<()>();
let unblock_stale_load_rx = std::sync::Mutex::new(Some(unblock_stale_load_rx));
registry.register_language(
LanguageName::new_static("TheLanguage"),
None,
Arc::new(LanguageMatcher {
path_suffixes: vec!["stale".into()],
..LanguageMatcher::default()
}),
false,
None,
Arc::new(move || {
let unblock_stale_load_rx = unblock_stale_load_rx
.lock()
.expect("the stale loader mutex should not be poisoned")
.take();
async move {
if let Some(unblock_stale_load_rx) = unblock_stale_load_rx {
unblock_stale_load_rx.await.ok();
}
Err(anyhow::anyhow!("simulated load failure"))
}
.boxed()
}),
);
let pending_language = registry.language_for_name("TheLanguage");
cx.executor().run_until_parked();
registry.register_test_language(LanguageConfig {
name: LanguageName::new_static("TheLanguage"),
matcher: Arc::new(LanguageMatcher {
path_suffixes: vec!["fresh".into()],
..LanguageMatcher::default()
}),
..LanguageConfig::default()
});
unblock_stale_load_tx.send(()).unwrap();
let language = pending_language.await.unwrap();
assert_eq!(
language.config.matcher.path_suffixes,
vec!["fresh".to_string()],
"a failed load that races with a re-registration should resolve to the re-registered language"
);
}
#[gpui::test]
async fn test_extension_grammar_cannot_shadow_native_grammar(cx: &mut TestAppContext) {
let registry = Arc::new(LanguageRegistry::test(cx.executor()));
registry.register_native_grammars([("rust", tree_sitter_rust::LANGUAGE)]);
registry.register_wasm_grammars(vec![(
Arc::from("rust"),
PathBuf::from("/extensions/bogus/grammars/rust.wasm"),
)]);
registry.register_test_language(LanguageConfig {
name: LanguageName::new_static("TheLanguage"),
grammar: Some(Arc::from("rust")),
matcher: Arc::new(LanguageMatcher {
path_suffixes: vec!["the".into()],
..LanguageMatcher::default()
}),
..LanguageConfig::default()
});
let language = registry.language_for_name("TheLanguage").await.unwrap();
assert!(
language.grammar().is_some(),
"an extension grammar must not replace a native grammar with the same name"
);
registry.remove_languages(&[], &[Arc::from("rust")]);
registry.register_test_language(LanguageConfig {
name: LanguageName::new_static("TheOtherLanguage"),
grammar: Some(Arc::from("rust")),
..LanguageConfig::default()
});
let language = registry
.language_for_name("TheOtherLanguage")
.await
.unwrap();
assert!(
language.grammar().is_some(),
"removing an extension grammar must not remove the native grammar it failed to shadow"
);
}
fn file(path: &str) -> Arc<dyn File> {
Arc::new(TestFile {
path: Arc::from(rel_path(path)),
root_name: "zed".into(),
local_root: None,
})
}
fn local_file(local_root: &str, root_name: &str) -> Arc<dyn File> {
Arc::new(TestFile {
path: Arc::from(rel_path("")),
root_name: root_name.into(),
local_root: Some(PathBuf::from(local_root)),
})
}
#[gpui::test]
fn test_edit_events(cx: &mut gpui::App) {
let mut now = Instant::now();
let buffer_1_events = Arc::new(Mutex::new(Vec::new()));
let buffer_2_events = Arc::new(Mutex::new(Vec::new()));
let buffer1 = cx.new(|cx| Buffer::local("abcdef", cx));
let buffer2 = cx.new(|cx| {
Buffer::remote(
BufferId::from(cx.entity_id().as_non_zero_u64()),
ReplicaId::new(1),
Capability::ReadWrite,
"abcdef",
cx,
)
});
let buffer1_ops = Arc::new(Mutex::new(Vec::new()));
buffer1.update(cx, {
let buffer1_ops = buffer1_ops.clone();
|buffer, cx| {
let buffer_1_events = buffer_1_events.clone();
cx.subscribe(&buffer1, move |_, _, event, _| match event.clone() {
BufferEvent::Operation {
operation,
is_local: true,
} => buffer1_ops.lock().push(operation),
event => buffer_1_events.lock().push(event),
})
.detach();
let buffer_2_events = buffer_2_events.clone();
cx.subscribe(&buffer2, move |_, _, event, _| match event.clone() {
BufferEvent::Operation {
is_local: false, ..
} => {}
event => buffer_2_events.lock().push(event),
})
.detach();
// An edit emits an edited event, followed by a dirty changed event,
// since the buffer was previously in a clean state.
buffer.edit([(2..4, "XYZ")], None, cx);
// An empty transaction does not emit any events.
buffer.start_transaction();
buffer.end_transaction(cx);
// A transaction containing two edits emits one edited event.
now += Duration::from_secs(1);
buffer.start_transaction_at(now);
buffer.edit([(5..5, "u")], None, cx);
buffer.edit([(6..6, "w")], None, cx);
buffer.end_transaction_at(now, cx);
// Undoing a transaction emits one edited event.
buffer.undo(cx);
}
});
// Incorporating a set of remote ops emits a single edited event,
// followed by a dirty changed event.
buffer2.update(cx, |buffer, cx| {
buffer.apply_ops(buffer1_ops.lock().drain(..), cx);
});
assert_eq!(
mem::take(&mut *buffer_1_events.lock()),
vec![
BufferEvent::Edited {
source: BufferEditSource::User
},
BufferEvent::DirtyChanged,
BufferEvent::Edited {
source: BufferEditSource::User
},
BufferEvent::Edited {
source: BufferEditSource::User
},
]
);
assert_eq!(
mem::take(&mut *buffer_2_events.lock()),
vec![
BufferEvent::Edited {
source: BufferEditSource::Remote
},
BufferEvent::DirtyChanged
]
);
buffer1.update(cx, |buffer, cx| {
// Undoing the first transaction emits edited event, followed by a
// dirty changed event, since the buffer is again in a clean state.
buffer.undo(cx);
});
// Incorporating the remote ops again emits a single edited event,
// followed by a dirty changed event.
buffer2.update(cx, |buffer, cx| {
buffer.apply_ops(buffer1_ops.lock().drain(..), cx);
});
assert_eq!(
mem::take(&mut *buffer_1_events.lock()),
vec![
BufferEvent::Edited {
source: BufferEditSource::User
},
BufferEvent::DirtyChanged,
]
);
assert_eq!(
mem::take(&mut *buffer_2_events.lock()),
vec![
BufferEvent::Edited {
source: BufferEditSource::Remote
},
BufferEvent::DirtyChanged
]
);
}
#[gpui::test]
async fn test_apply_diff(cx: &mut TestAppContext) {
let (text, offsets) = marked_text_offsets(
"one two three\nfour fiˇve six\nseven eightˇ nine\nten eleven twelve\n",
);
let buffer = cx.new(|cx| Buffer::local(text, cx));
let anchors = buffer.update(cx, |buffer, _| {
offsets
.iter()
.map(|offset| buffer.anchor_before(offset))
.collect::<Vec<_>>()
});
let (text, offsets) = marked_text_offsets(
"one two three\n{\nfour FIVEˇ six\n}\nseven AND EIGHTˇ nine\nten eleven twelve\n",
);
let diff = buffer.update(cx, |b, cx| b.diff(text.clone(), cx)).await;
buffer.update(cx, |buffer, cx| {
buffer.apply_diff(diff, cx).unwrap();
assert_eq!(buffer.text(), text);
let actual_offsets = anchors
.iter()
.map(|anchor| anchor.to_offset(buffer))
.collect::<Vec<_>>();
assert_eq!(actual_offsets, offsets);
});
let (text, offsets) =
marked_text_offsets("one two three\n{\nˇ}\nseven AND EIGHTEENˇ nine\nten eleven twelve\n");
let diff = buffer.update(cx, |b, cx| b.diff(text.clone(), cx)).await;
buffer.update(cx, |buffer, cx| {
buffer.apply_diff(diff, cx).unwrap();
assert_eq!(buffer.text(), text);
let actual_offsets = anchors
.iter()
.map(|anchor| anchor.to_offset(buffer))
.collect::<Vec<_>>();
assert_eq!(actual_offsets, offsets);
});
}
#[gpui::test(iterations = 10)]
async fn test_normalize_whitespace(cx: &mut gpui::TestAppContext) {
let text = [
"zero", //
"one ", // 2 trailing spaces
"two", //
"three ", // 3 trailing spaces
"four", //
"five ", // 4 trailing spaces
]
.join("\n");
let buffer = cx.new(|cx| Buffer::local(text, cx));
// Spawn a task to format the buffer's whitespace.
// Pause so that the formatting task starts running.
let format = buffer.update(cx, |buffer, cx| buffer.remove_trailing_whitespace(None, cx));
yield_now().await;
// Edit the buffer while the normalization task is running.
let version_before_edit = buffer.update(cx, |buffer, _| buffer.version());
buffer.update(cx, |buffer, cx| {
buffer.edit(
[
(Point::new(0, 1)..Point::new(0, 1), "EE"),
(Point::new(3, 5)..Point::new(3, 5), "EEE"),
],
None,
cx,
);
});
let format_diff = format.await;
buffer.update(cx, |buffer, cx| {
let version_before_format = format_diff.base_version.clone();
buffer.apply_diff(format_diff, cx);
// The outcome depends on the order of concurrent tasks.
//
// If the edit occurred while searching for trailing whitespace ranges,
// then the trailing whitespace region touched by the edit is left intact.
if version_before_format == version_before_edit {
assert_eq!(
buffer.text(),
[
"zEEero", //
"one", //
"two", //
"threeEEE ", //
"four", //
"five", //
]
.join("\n")
);
}
// Otherwise, all trailing whitespace is removed.
else {
assert_eq!(
buffer.text(),
[
"zEEero", //
"one", //
"two", //
"threeEEE", //
"four", //
"five", //
]
.join("\n")
);
}
});
}
#[gpui::test]
async fn test_reparse(cx: &mut gpui::TestAppContext) {
let text = "fn a() {}";
let buffer = cx.new(|cx| Buffer::local(text, cx).with_language(rust_lang(), cx));
// Wait for the initial text to parse
cx.executor().run_until_parked();
assert!(!buffer.update(cx, |buffer, _| buffer.is_parsing()));
assert_eq!(
get_tree_sexp(&buffer, cx),
concat!(
"(source_file (function_item name: (identifier) ",
"parameters: (parameters) ",
"body: (block)))"
)
);
buffer.update(cx, |buffer, _| buffer.set_sync_parse_timeout(None));
// Perform some edits (add parameter and variable reference)
// Parsing doesn't begin until the transaction is complete
buffer.update(cx, |buf, cx| {
buf.start_transaction();
let offset = buf.text().find(')').unwrap();
buf.edit([(offset..offset, "b: C")], None, cx);
assert!(!buf.is_parsing());
let offset = buf.text().find('}').unwrap();
buf.edit([(offset..offset, " d; ")], None, cx);
assert!(!buf.is_parsing());
buf.end_transaction(cx);
assert_eq!(buf.text(), "fn a(b: C) { d; }");
assert!(buf.is_parsing());
});
cx.executor().run_until_parked();
assert!(!buffer.update(cx, |buffer, _| buffer.is_parsing()));
assert_eq!(
get_tree_sexp(&buffer, cx),
concat!(
"(source_file (function_item name: (identifier) ",
"parameters: (parameters (parameter pattern: (identifier) type: (type_identifier))) ",
"body: (block (expression_statement (identifier)))))"
)
);
// Perform a series of edits without waiting for the current parse to complete:
// * turn identifier into a field expression
// * turn field expression into a method call
// * add a turbofish to the method call
buffer.update(cx, |buf, cx| {
let offset = buf.text().find(';').unwrap();
buf.edit([(offset..offset, ".e")], None, cx);
assert_eq!(buf.text(), "fn a(b: C) { d.e; }");
assert!(buf.is_parsing());
});
buffer.update(cx, |buf, cx| {
let offset = buf.text().find(';').unwrap();
buf.edit([(offset..offset, "(f)")], None, cx);
assert_eq!(buf.text(), "fn a(b: C) { d.e(f); }");
assert!(buf.is_parsing());
});
buffer.update(cx, |buf, cx| {
let offset = buf.text().find("(f)").unwrap();
buf.edit([(offset..offset, "::<G>")], None, cx);
assert_eq!(buf.text(), "fn a(b: C) { d.e::<G>(f); }");
assert!(buf.is_parsing());
});
cx.executor().run_until_parked();
assert_eq!(
get_tree_sexp(&buffer, cx),
concat!(
"(source_file (function_item name: (identifier) ",
"parameters: (parameters (parameter pattern: (identifier) type: (type_identifier))) ",
"body: (block (expression_statement (call_expression ",
"function: (generic_function ",
"function: (field_expression value: (identifier) field: (field_identifier)) ",
"type_arguments: (type_arguments (type_identifier))) ",
"arguments: (arguments (identifier)))))))",
)
);
buffer.update(cx, |buf, cx| {
buf.undo(cx);
buf.undo(cx);
buf.undo(cx);
buf.undo(cx);
assert_eq!(buf.text(), "fn a() {}");
assert!(buf.is_parsing());
});
cx.executor().run_until_parked();
assert_eq!(
get_tree_sexp(&buffer, cx),
concat!(
"(source_file (function_item name: (identifier) ",
"parameters: (parameters) ",
"body: (block)))"
)
);
buffer.update(cx, |buf, cx| {
buf.redo(cx);
buf.redo(cx);
buf.redo(cx);
buf.redo(cx);
assert_eq!(buf.text(), "fn a(b: C) { d.e::<G>(f); }");
assert!(buf.is_parsing());
});
cx.executor().run_until_parked();
assert_eq!(
get_tree_sexp(&buffer, cx),
concat!(
"(source_file (function_item name: (identifier) ",
"parameters: (parameters (parameter pattern: (identifier) type: (type_identifier))) ",
"body: (block (expression_statement (call_expression ",
"function: (generic_function ",
"function: (field_expression value: (identifier) field: (field_identifier)) ",
"type_arguments: (type_arguments (type_identifier))) ",
"arguments: (arguments (identifier)))))))",
)
);
}
#[gpui::test]
async fn test_resetting_language(cx: &mut gpui::TestAppContext) {
let buffer = cx.new(|cx| {
let mut buffer = Buffer::local("{}", cx).with_language(rust_lang(), cx);
buffer.set_sync_parse_timeout(None);
buffer
});
// Wait for the initial text to parse
cx.executor().run_until_parked();
assert_eq!(
get_tree_sexp(&buffer, cx),
"(source_file (expression_statement (block)))"
);
buffer.update(cx, |buffer, cx| buffer.set_language(Some(json_lang()), cx));
cx.executor().run_until_parked();
assert_eq!(get_tree_sexp(&buffer, cx), "(document (object))");
}
#[gpui::test]
async fn test_outline(cx: &mut gpui::TestAppContext) {
let text = r#"
struct Person {
name: String,
age: usize,
}
mod module {
enum LoginState {
LoggedOut,
LoggingOn,
LoggedIn {
person: Person,
time: Instant,
}
}
}
impl Eq for Person {}
impl Drop for Person {
fn drop(&mut self) {
println!("bye");
}
}
"#
.unindent();
let buffer = cx.new(|cx| Buffer::local(text, cx).with_language(rust_lang(), cx));
let snapshot = buffer.update(cx, |buffer, _| buffer.snapshot());
let outline = snapshot.outline(None);
assert_eq!(
outline
.items
.iter()
.map(|item| (
item.text.as_str(),
item.depth,
item.to_point(&snapshot).body_range(&snapshot)
.map(|range| minimize_space(&snapshot.text_for_range(range).collect::<String>()))
))
.collect::<Vec<_>>(),
&[
("struct Person", 0, Some("name: String, age: usize,".to_string())),
("name", 1, None),
("age", 1, None),
(
"mod module",
0,
Some(
"enum LoginState { LoggedOut, LoggingOn, LoggedIn { person: Person, time: Instant, } }".to_string()
)
),
(
"enum LoginState",
1,
Some("LoggedOut, LoggingOn, LoggedIn { person: Person, time: Instant, }".to_string())
),
("LoggedOut", 2, None),
("LoggingOn", 2, None),
("LoggedIn", 2, Some("person: Person, time: Instant,".to_string())),
("person", 3, None),
("time", 3, None),
("impl Eq for Person", 0, Some("".to_string())),
(
"impl Drop for Person",
0,
Some("fn drop(&mut self) { println!(\"bye\"); }".to_string())
),
("fn drop", 1, Some("println!(\"bye\");".to_string())),
]
);
// Single-atom queries (no whitespace): all matched chars must land in the leaf,
// so items whose ancestor path coincidentally contains the query chars don't
// show up unless the leaf itself matches.
assert_eq!(
search(&outline, "oon", cx).await,
&[
("mod module", vec![]), // parent context for LoggingOn
("enum LoginState", vec![]), // parent context for LoggingOn
("LoggingOn", vec![1, 7, 8]), // all three chars in leaf
("impl Eq for Person", vec![9, 16, 17]), // o-o-n in "for Person"
("impl Drop for Person", vec![11, 18, 19]), // o-o-n in "for Person"
]
);
// Multi-atom queries: rows whose match lives entirely in an ancestor
// are kept as context (empty positions, score zeroed) so descendants
// of a matched container surface alongside it.
assert_eq!(
search(&outline, "dp p", cx).await,
&[("impl Drop for Person", vec![5, 14]), ("fn drop", vec![]),]
);
assert_eq!(
search(&outline, "dpn", cx).await,
&[("impl Drop for Person", vec![5, 14, 19])]
);
assert_eq!(
search(&outline, "impl ", cx).await,
&[
("impl Eq for Person", vec![0, 1, 2, 3]),
("impl Drop for Person", vec![0, 1, 2, 3]),
("fn drop", vec![]),
]
);
fn minimize_space(text: &str) -> String {
static WHITESPACE: LazyLock<Regex> = LazyLock::new(|| Regex::new("[\\n\\s]+").unwrap());
WHITESPACE.replace_all(text, " ").trim().to_string()
}
async fn search<'a>(
outline: &'a Outline<Anchor>,
query: &'a str,
cx: &'a gpui::TestAppContext,
) -> Vec<(&'a str, Vec<usize>)> {
let entries = cx
.update(|cx| outline.search(query, cx.background_executor().clone()))
.await;
entries
.into_iter()
.map(|entry| {
let candidate_id = entry.candidate_id();
let positions = entry.into_match().map(|m| m.positions).unwrap_or_default();
(outline.items[candidate_id].text.as_str(), positions)
})
.collect::<Vec<_>>()
}
}
#[gpui::test]
async fn test_outline_nodes_with_newlines(cx: &mut gpui::TestAppContext) {
let text = r#"
impl A for B<
C
> {
};
"#
.unindent();
let buffer = cx.new(|cx| Buffer::local(text, cx).with_language(rust_lang(), cx));
let outline = buffer.update(cx, |buffer, _| buffer.snapshot().outline(None));
assert_eq!(
outline
.items
.iter()
.map(|item| (item.text.as_str(), item.depth))
.collect::<Vec<_>>(),
&[("impl A for B<", 0)]
);
}
#[gpui::test]
async fn test_outline_with_extra_context(cx: &mut gpui::TestAppContext) {
let language = javascript_lang()
.with_outline_query(
r#"
(function_declaration
"function" @context
name: (_) @name
parameters: (formal_parameters
"(" @context.extra
")" @context.extra)) @item
"#,
)
.unwrap();
let text = r#"
function a() {}
function b(c) {}
"#
.unindent();
let buffer = cx.new(|cx| Buffer::local(text, cx).with_language(Arc::new(language), cx));
let snapshot = buffer.update(cx, |buffer, _| buffer.snapshot());
// extra context nodes are included in the outline.
let outline = snapshot.outline(None);
assert_eq!(
outline
.items
.iter()
.map(|item| (item.text.as_str(), item.depth))
.collect::<Vec<_>>(),
&[("function a()", 0), ("function b( )", 0),]
);
// extra context nodes do not appear in breadcrumbs.
let symbols = snapshot.symbols_containing(3, None);
assert_eq!(
symbols
.iter()
.map(|item| (item.text.as_str(), item.depth))
.collect::<Vec<_>>(),
&[("function a", 0)]
);
}
#[gpui::test]
async fn test_outline_selection_range_for_multiline_c_signature(cx: &mut gpui::TestAppContext) {
let text = indoc! {"
void
evdev_post_scroll(struct evdev_device *device,
usec_t time,
enum libinput_pointer_axis_source source,
const struct normalized_coords *delta)
{
return;
}
"};
let buffer = cx.new(|cx| Buffer::local(text, cx).with_language(c_lang(), cx));
let snapshot = buffer.update(cx, |buffer, _| buffer.snapshot());
let outline = snapshot.outline(None);
let item = outline
.items
.iter()
.find(|item| item.text.contains("evdev_post_scroll"))
.unwrap()
.to_point(&snapshot);
assert_eq!(item.source_range_for_text.start, Point::new(0, 0));
assert_eq!(item.selection_range.start, Point::new(1, 0));
assert_eq!(item.text, "void evdev_post_scroll( )");
}
#[gpui::test]
fn test_outline_annotations(cx: &mut App) {
// Add this new test case
let text = r#"
/// This is a doc comment
/// that spans multiple lines
fn annotated_function() {
// This is not an annotation
}
// This is a single-line annotation
fn another_function() {}
fn unannotated_function() {}
// This comment is not an annotation
fn function_after_blank_line() {}
"#
.unindent();
let buffer = cx.new(|cx| Buffer::local(text, cx).with_language(rust_lang(), cx));
let outline = buffer.update(cx, |buffer, _| buffer.snapshot().outline(None));
assert_eq!(
outline
.items
.into_iter()
.map(|item| (
item.text.to_string(),
item.depth,
item.annotation_range
.map(|range| { buffer.read(cx).text_for_range(range).collect::<String>() })
))
.collect::<Vec<_>>(),
&[
(
"fn annotated_function".to_string(),
0,
Some("/// This is a doc comment\n/// that spans multiple lines".to_string())
),
(
"fn another_function".to_string(),
0,
Some("// This is a single-line annotation".to_string())
),
("fn unannotated_function".to_string(), 0, None),
("fn function_after_blank_line".to_string(), 0, None),
]
);
}
#[gpui::test]
async fn test_symbols_containing(cx: &mut gpui::TestAppContext) {
let text = r#"
impl Person {
fn one() {
1
}
fn two() {
2
}fn three() {
3
}
}
"#
.unindent();
let buffer = cx.new(|cx| Buffer::local(text, cx).with_language(rust_lang(), cx));
let snapshot = buffer.update(cx, |buffer, _| buffer.snapshot());
// point is at the start of an item
assert_eq!(
symbols_containing(Point::new(1, 4), &snapshot),
vec![
(
"impl Person".to_string(),
Point::new(0, 0)..Point::new(10, 1)
),
("fn one".to_string(), Point::new(1, 4)..Point::new(3, 5))
]
);
// point is in the middle of an item
assert_eq!(
symbols_containing(Point::new(2, 8), &snapshot),
vec![
(
"impl Person".to_string(),
Point::new(0, 0)..Point::new(10, 1)
),
("fn one".to_string(), Point::new(1, 4)..Point::new(3, 5))
]
);
// point is at the end of an item
assert_eq!(
symbols_containing(Point::new(3, 5), &snapshot),
vec![
(
"impl Person".to_string(),
Point::new(0, 0)..Point::new(10, 1)
),
("fn one".to_string(), Point::new(1, 4)..Point::new(3, 5))
]
);
// point is in between two adjacent items
assert_eq!(
symbols_containing(Point::new(7, 5), &snapshot),
vec![
(
"impl Person".to_string(),
Point::new(0, 0)..Point::new(10, 1)
),
("fn two".to_string(), Point::new(5, 4)..Point::new(7, 5))
]
);
fn symbols_containing(
position: Point,
snapshot: &BufferSnapshot,
) -> Vec<(String, Range<Point>)> {
snapshot
.symbols_containing(position, None)
.into_iter()
.map(|item| {
(
item.text.to_string(),
item.range.start.to_point(snapshot)..item.range.end.to_point(snapshot),
)
})
.collect()
}
let (text, offsets) = marked_text_offsets(
&"
// ˇ😅 //
fn test() {
}
"
.unindent(),
);
let buffer = cx.new(|cx| Buffer::local(text, cx).with_language(rust_lang(), cx));
let snapshot = buffer.update(cx, |buffer, _| buffer.snapshot());
// note, it would be nice to actually return the method test in this
// case, but primarily asserting we don't crash because of the multibyte character.
assert_eq!(snapshot.symbols_containing(offsets[0], None), vec![]);
}
#[gpui::test]
fn test_text_objects(cx: &mut App) {
let (text, ranges) = marked_text_ranges(
indoc! {r#"
impl Hello {
fn say() -> u8 { return /* ˇhi */ 1 }
}"#
},
false,
);
let buffer = cx.new(|cx| Buffer::local(text.clone(), cx).with_language(rust_lang(), cx));
let snapshot = buffer.update(cx, |buffer, _| buffer.snapshot());
let matches = snapshot
.text_object_ranges(ranges[0].clone(), TreeSitterOptions::default())
.map(|(range, text_object)| (&text[range], text_object))
.collect::<Vec<_>>();
assert_eq!(
matches,
&[
("/* hi */", TextObject::AroundComment),
("return /* hi */ 1", TextObject::InsideFunction),
(
"fn say() -> u8 { return /* hi */ 1 }",
TextObject::AroundFunction
),
(
"fn say() -> u8 { return /* hi */ 1 }",
TextObject::InsideClass
),
(
"impl Hello {\n fn say() -> u8 { return /* hi */ 1 }\n}",
TextObject::AroundClass
),
],
)
}
#[gpui::test]
fn test_text_objects_with_has_parent_predicate(cx: &mut App) {
use std::borrow::Cow;
// Create a language with a custom text_objects query that uses #has-parent?
// This query only matches closure_expression when it's inside a call_expression
let language = Language::new(
LanguageConfig {
name: "Rust".into(),
matcher: (LanguageMatcher {
path_suffixes: vec!["rs".into()],
..Default::default()
})
.into(),
..Default::default()
},
Some(tree_sitter_rust::LANGUAGE.into()),
)
.with_queries(LanguageQueries {
text_objects: Some(Cow::from(indoc! {r#"
; Only match closures that are arguments to function calls
(closure_expression) @function.around
(#has-parent? @function.around arguments)
"#})),
..Default::default()
})
.expect("Could not parse queries");
let (text, ranges) = marked_text_ranges(
indoc! {r#"
fn main() {
let standalone = |x| x + 1;
let result = foo(|y| y * ˇ2);
}"#
},
false,
);
let buffer = cx.new(|cx| Buffer::local(text.clone(), cx).with_language(Arc::new(language), cx));
let snapshot = buffer.update(cx, |buffer, _| buffer.snapshot());
let matches = snapshot
.text_object_ranges(ranges[0].clone(), TreeSitterOptions::default())
.map(|(range, text_object)| (&text[range], text_object))
.collect::<Vec<_>>();
// Should only match the closure inside foo(), not the standalone closure
assert_eq!(matches, &[("|y| y * 2", TextObject::AroundFunction),]);
}
#[gpui::test]
fn test_text_objects_with_not_has_parent_predicate(cx: &mut App) {
use std::borrow::Cow;
// Create a language with a custom text_objects query that uses #not-has-parent?
// This query only matches closure_expression when it's NOT inside a call_expression
let language = Language::new(
LanguageConfig {
name: "Rust".into(),
matcher: (LanguageMatcher {
path_suffixes: vec!["rs".into()],
..Default::default()
})
.into(),
..Default::default()
},
Some(tree_sitter_rust::LANGUAGE.into()),
)
.with_queries(LanguageQueries {
text_objects: Some(Cow::from(indoc! {r#"
; Only match closures that are NOT arguments to function calls
(closure_expression) @function.around
(#not-has-parent? @function.around arguments)
"#})),
..Default::default()
})
.expect("Could not parse queries");
let (text, ranges) = marked_text_ranges(
indoc! {r#"
fn main() {
let standalone = |x| x +ˇ 1;
let result = foo(|y| y * 2);
}"#
},
false,
);
let buffer = cx.new(|cx| Buffer::local(text.clone(), cx).with_language(Arc::new(language), cx));
let snapshot = buffer.update(cx, |buffer, _| buffer.snapshot());
let matches = snapshot
.text_object_ranges(ranges[0].clone(), TreeSitterOptions::default())
.map(|(range, text_object)| (&text[range], text_object))
.collect::<Vec<_>>();
// Should only match the standalone closure, not the one inside foo()
assert_eq!(matches, &[("|x| x + 1", TextObject::AroundFunction),]);
}
#[gpui::test]
fn test_enclosing_bracket_ranges(cx: &mut App) {
#[track_caller]
fn assert(selection_text: &'static str, range_markers: Vec<&'static str>, cx: &mut App) {
assert_bracket_pairs(selection_text, range_markers, rust_lang(), cx)
}
assert(
indoc! {"
mod x {
moˇd y {
}
}
let foo = 1;"},
vec![indoc! {"
mod x «{»
mod y {
}
«}»
let foo = 1;"}],
cx,
);
assert(
indoc! {"
mod x {
mod y ˇ{
}
}
let foo = 1;"},
vec![
indoc! {"
mod x «{»
mod y {
}
«}»
let foo = 1;"},
indoc! {"
mod x {
mod y «{»
«}»
}
let foo = 1;"},
],
cx,
);
assert(
indoc! {"
mod x {
mod y {
}ˇ
}
let foo = 1;"},
vec![
indoc! {"
mod x «{»
mod y {
}
«}»
let foo = 1;"},
indoc! {"
mod x {
mod y «{»
«}»
}
let foo = 1;"},
],
cx,
);
assert(
indoc! {"
mod x {
mod y {
}
ˇ}
let foo = 1;"},
vec![indoc! {"
mod x «{»
mod y {
}
«}»
let foo = 1;"}],
cx,
);
assert(
indoc! {"
mod x {
mod y {
}
}
let fˇoo = 1;"},
Vec::new(),
cx,
);
// Regression test: avoid crash when querying at the end of the buffer.
assert(
indoc! {"
mod x {
mod y {
}
}
let foo = 1;ˇ"},
Vec::new(),
cx,
);
}
#[gpui::test]
fn test_bracket_colorization_indices_remain_stable_across_row_chunks(cx: &mut App) {
let mut text = String::from("{\n \"theme\": {\n");
let mut property_object_open_offsets = Vec::new();
for index in 0..500 {
text.push_str(&format!(" \"scope_{index:03}\": "));
property_object_open_offsets.push(text.len());
text.push_str("{\n \"color\": \"#ffffff\"\n },\n");
}
text.push_str(" \"last\": {}\n }\n}\n");
assert!(
text.len() > MAX_BYTES_TO_QUERY,
"fixture should exceed the bounded tree-sitter query window"
);
let buffer = cx.new(|cx| Buffer::local(text.clone(), cx).with_language(json_lang(), cx));
let snapshot = buffer.update(cx, |buffer, _| buffer.snapshot());
let late_open_offset = property_object_open_offsets[400];
let late_matches = snapshot.fetch_bracket_ranges(late_open_offset..late_open_offset + 1, None);
let late_color_index = color_index_for_open(&late_matches, late_open_offset);
assert_eq!(
late_color_index,
Some(2),
"Jumping directly into a later row chunk should preserve enclosing JSON object depth"
);
let first_open_offset = property_object_open_offsets[0];
let all_matches = snapshot.fetch_bracket_ranges(0..snapshot.len(), None);
assert_eq!(
color_index_for_open(&all_matches, first_open_offset),
late_color_index,
"Sibling object braces should keep the same color across row chunks"
);
for open_offset in property_object_open_offsets {
assert_eq!(
color_index_for_open(&all_matches, open_offset),
late_color_index,
"All generated sibling object braces should share the same depth color"
);
}
}
#[gpui::test]
fn test_c_bracket_ranges_in_error_nodes(cx: &mut App) {
let text = indoc! {r#"
CLAY(CLAY_ID("MenuContainer"),
CLAY_RECTANGLE({.color = {43, 41, 51, 255}}),
CLAY_LAYOUT({.layoutDirection = CLAY_LEFT_TO_RIGHT,
.sizing = {.width = CLAY_SIZING_FIT()},
.padding = {16, 16},
.childGap = 16})) {
CLAY(CLAY_ID("StartStopButton"), CLAY_LAYOUT({.padding = {16, 8}}),
CLAY_RECTANGLE({.color = {140, 140, 140, 255}, .cornerRadius = 5}),
Clay_OnHover(HandleStartButtonInteraction, 1)) {
CLAY_TEXT(CLAY_STRING("Start/Stop"),
CLAY_TEXT_CONFIG({.fontId = FONT_ID_BODY_16,
.fontSize = 16,
.textColor = {255, 255, 255, 255}}));
}
}
"#};
let buffer = cx.new(|cx| Buffer::local(text, cx).with_language(c_lang(), cx));
let snapshot = buffer.read(cx).snapshot();
assert_has_syntax_errors(&snapshot);
let matches = snapshot
.all_bracket_ranges(0..snapshot.len())
.map(BracketMatch::bracket_ranges)
// Quote pairs share one delimiter kind, so error recovery leaves them as queried.
.filter(|(open, _)| matches!(&text[open.clone()], "(" | "{"))
.collect::<Vec<_>>();
let (mut expected, unmatched_parens) = stack_paired_brackets(text, '(', ')');
let (curly_pairs, unmatched_curlies) = stack_paired_brackets(text, '{', '}');
expected.extend(curly_pairs);
assert_eq!(unmatched_parens, Vec::<Range<usize>>::new());
assert_eq!(unmatched_curlies, Vec::<Range<usize>>::new());
assert_set_eq!(matches, expected);
}
#[gpui::test]
fn test_bracket_ranges_do_not_repair_unbalanced_error_nodes(cx: &mut App) {
let (text, ranges) = marked_text_ranges(
indoc! {r#"
CLAY«(»CLAY_ID("MenuContainer"),
CLAY_LAYOUT({.layoutDirection = CLAY_LEFT_TO_RIGHT,
.sizing = {.width = CLAY_SIZING_FIT()},
.padding = {16, 16},
.childGap = 16}) {
}
"#},
false,
);
let buffer = cx.new(|cx| Buffer::local(text.clone(), cx).with_language(c_lang(), cx));
let snapshot = buffer.read(cx).snapshot();
assert_has_syntax_errors(&snapshot);
let matches = snapshot
.all_bracket_ranges(0..snapshot.len())
.map(BracketMatch::bracket_ranges)
// Tree-sitter pairs unmatched opens with zero-width MISSING close tokens.
.filter(|(open, close)| matches!(&text[open.clone()], "(" | "{") && !close.is_empty())
.collect::<Vec<_>>();
let (mut expected, unmatched_parens) = stack_paired_brackets(&text, '(', ')');
let (curly_pairs, unmatched_curlies) = stack_paired_brackets(&text, '{', '}');
expected.extend(curly_pairs);
assert_eq!(
unmatched_parens,
vec![ranges[0].clone()],
"the fixture should contain exactly one unbalanced opening parenthesis"
);
assert_eq!(unmatched_curlies, Vec::<Range<usize>>::new());
assert_set_eq!(matches, expected);
}
// This test passes without the error recovery too: it pins the retention of
// cross-chunk pairs, which the chunk-local repair can neither see nor verify.
#[gpui::test]
fn test_bracket_ranges_keep_chunk_spanning_pairs_amid_errors(cx: &mut App) {
let mut text = String::from("void outer(void) {\n");
for index in 0..60 {
text.push_str(&format!(" int before_{index:02} = 0;\n"));
}
text.push_str(
r#" CLAY(CLAY_ID("MenuContainer"),
CLAY_RECTANGLE({.color = {43, 41, 51, 255}}),
CLAY_LAYOUT({.layoutDirection = CLAY_LEFT_TO_RIGHT,
.sizing = {.width = CLAY_SIZING_FIT()},
.padding = {16, 16},
.childGap = 16})) {
}
"#,
);
for index in 0..60 {
text.push_str(&format!(" int after_{index:02} = 0;\n"));
}
text.push_str("}\n");
let buffer = cx.new(|cx| Buffer::local(text.clone(), cx).with_language(c_lang(), cx));
let snapshot = buffer.read(cx).snapshot();
assert_has_syntax_errors(&snapshot);
let open_offset = text.find('{').unwrap();
let close_offset = text.rfind('}').unwrap();
let matches = snapshot.fetch_bracket_ranges(0..snapshot.len(), None);
assert!(
matches.len() >= 3,
"the fixture should span at least three row chunks, got {:?}",
matches.keys().collect::<Vec<_>>()
);
for (row_range, chunk_matches) in &matches {
let enclosing = chunk_matches
.iter()
.find(|bracket_match| bracket_match.open_range.start == open_offset)
.map(|bracket_match| {
(
bracket_match.open_range.clone(),
bracket_match.close_range.clone(),
)
});
assert_eq!(
enclosing,
Some((open_offset..open_offset + 1, close_offset..close_offset + 1)),
"chunk {row_range:?} should keep the function body pair that spans all chunks"
);
}
}
#[gpui::test]
async fn test_bracket_ranges_keep_pairs_straddling_a_chunk_boundary_amid_errors(
cx: &mut TestAppContext,
) {
let mut text = String::from("void outer(void) {\n");
for index in 0..56 {
text.push_str(&format!(" int before_{index:02} = 0;\n"));
}
let if_open_offset = text.len() + " if (before_00) ".len();
text.push_str(" if (before_00) {\n");
text.push_str(
r#" CLAY(CLAY_ID("MenuContainer"),
CLAY_RECTANGLE({.color = {43, 41, 51, 255}}),
CLAY_LAYOUT({.layoutDirection = CLAY_LEFT_TO_RIGHT,
.sizing = {.width = CLAY_SIZING_FIT()},
.padding = {16, 16},
.childGap = 16})) {
}
"#,
);
for index in 0..55 {
text.push_str(&format!(" int after_{index:02} = 0;\n"));
}
let if_close_offset = text.len() + " ".len();
text.push_str(" }\n}\n");
let buffer = cx.new(|cx| Buffer::local(text.clone(), cx).with_language(c_lang(), cx));
buffer
.read_with(cx, |buffer, _| buffer.parsing_idle())
.await;
let snapshot = buffer.read_with(cx, |buffer, _| buffer.snapshot());
assert_has_syntax_errors(&snapshot);
let open_row = snapshot.offset_to_point(if_open_offset).row;
let close_row = snapshot.offset_to_point(if_close_offset).row;
let matches = snapshot.fetch_bracket_ranges(0..snapshot.len(), None);
assert!(
matches.len() >= 3,
"the fixture should span at least three row chunks, got {:?}",
matches.keys().collect::<Vec<_>>()
);
assert!(
matches
.keys()
.any(|row_range| row_range.contains(&open_row) && !row_range.contains(&close_row)),
"the if body pair should straddle a chunk boundary, opening at row {open_row} and closing at row {close_row}"
);
for (row_range, chunk_matches) in &matches {
if !row_range.contains(&open_row) && !row_range.contains(&close_row) {
continue;
}
let straddling = chunk_matches
.iter()
.find(|bracket_match| bracket_match.open_range.start == if_open_offset)
.map(|bracket_match| {
(
bracket_match.open_range.clone(),
bracket_match.close_range.clone(),
)
});
assert_eq!(
straddling,
Some((
if_open_offset..if_open_offset + 1,
if_close_offset..if_close_offset + 1
)),
"chunk {row_range:?} should keep the if body pair straddling the chunk boundary"
);
}
}
#[gpui::test]
async fn test_bracket_ranges_deduplicate_overlapping_patterns(cx: &mut TestAppContext) {
let text = indoc! {r#"
CLAY(CLAY_ID("MenuContainer"),
CLAY_RECTANGLE({.color = {43, 41, 51, 255}}),
CLAY_LAYOUT({.layoutDirection = CLAY_LEFT_TO_RIGHT,
.sizing = {.width = CLAY_SIZING_FIT()},
.padding = {16, 16},
.childGap = 16})) {
}
"#};
let language = Arc::new(
Language::new(
LanguageConfig {
name: "C".into(),
..Default::default()
},
Some(tree_sitter_c::LANGUAGE.into()),
)
.with_brackets_query(
r#"
("(" @open ")" @close)
("(" @open ")" @close)
"#,
)
.unwrap(),
);
let buffer = cx.new(|cx| Buffer::local(text, cx).with_language(language, cx));
buffer
.read_with(cx, |buffer, _| buffer.parsing_idle())
.await;
let snapshot = buffer.read_with(cx, |buffer, _| buffer.snapshot());
assert_has_syntax_errors(&snapshot);
let mut matches = snapshot
.all_bracket_ranges(0..snapshot.len())
.map(BracketMatch::bracket_ranges)
.collect::<Vec<_>>();
matches.sort_by_key(|(open, close)| (open.start, open.end, close.start, close.end));
let (mut expected, unmatched_parens) = stack_paired_brackets(text, '(', ')');
assert_eq!(unmatched_parens, Vec::<Range<usize>>::new());
expected.sort_by_key(|(open, close)| (open.start, open.end, close.start, close.end));
assert_eq!(
matches, expected,
"one match per physical pair, even when several query patterns capture it"
);
}
#[test]
fn test_applicable_row_chunks() {
let text = (0..125)
.map(|row| format!("line {row}\n"))
.collect::<String>();
let buffer = TextBuffer::new(ReplicaId::LOCAL, BufferId::new(1).unwrap(), text);
let snapshot = buffer.snapshot();
let chunks = row_chunk::RowChunks::new(snapshot, 10);
assert_eq!(chunks.len(), 13);
let row_ranges = |ranges: &[Range<Point>]| {
chunks
.applicable_chunks(ranges)
.map(|chunk| chunk.row_range())
.collect::<Vec<_>>()
};
assert_eq!(
row_ranges(&[Point::new(15, 0)..Point::new(17, 3)]),
vec![10..20],
"Range in the middle of a chunk should yield that chunk only"
);
assert_eq!(
row_ranges(&[Point::new(10, 0)..Point::new(12, 0)]),
vec![0..10, 10..20],
"Range starting exactly at a chunk boundary should also touch the previous chunk"
);
assert_eq!(
row_ranges(&[Point::new(5, 0)..Point::new(9, 0)]),
vec![0..10],
"Range ending before a chunk boundary should not touch the next chunk"
);
assert_eq!(
row_ranges(&[Point::new(5, 0)..Point::new(10, 0)]),
vec![0..10, 10..20],
"Range ending exactly at a chunk boundary should touch the next chunk"
);
assert_eq!(
row_ranges(&[
Point::new(112, 0)..Point::new(112, 0),
Point::new(15, 0)..Point::new(16, 0),
Point::new(11, 0)..Point::new(18, 0),
]),
vec![10..20, 110..120],
"Chunks for multiple ranges should be deduplicated and sorted"
);
let all_chunks = chunks
.applicable_chunks(&[Point::zero()..Point::new(125, 0)])
.collect::<Vec<_>>();
assert_eq!(
all_chunks.iter().map(|chunk| chunk.id).collect::<Vec<_>>(),
(0..13).collect::<Vec<_>>()
);
assert_eq!(all_chunks[12].row_range(), 120..125);
for chunk in &all_chunks {
assert_eq!(
chunk.start_anchor.to_point(snapshot),
Point::new(chunk.start, 0)
);
assert_eq!(
chunk.end_anchor.to_point(snapshot),
Point::new(chunk.end_exclusive, 0)
);
}
assert_eq!(
chunks
.applicable_chunks(&[Point::zero()..Point::new(125, 0)])
.collect::<Vec<_>>(),
all_chunks,
"Memoized chunks should be identical to the initially computed ones"
);
}
#[gpui::test]
fn test_enclosing_bracket_ranges_where_brackets_are_not_outermost_children(cx: &mut App) {
let mut assert = |selection_text, bracket_pair_texts| {
assert_bracket_pairs(
selection_text,
bracket_pair_texts,
Arc::new(javascript_lang()),
cx,
)
};
assert(
indoc! {"
for (const a in b)ˇ {
// a comment that's longer than the for-loop header
}"},
vec![indoc! {"
for «(»const a in b«)» {
// a comment that's longer than the for-loop header
}"}],
);
// Regression test: even though the parent node of the parentheses (the for loop) does
// intersect the given range, the parentheses themselves do not contain the range, so
// they should not be returned. Only the curly braces contain the range.
assert(
indoc! {"
for (const a in b) {ˇ
// a comment that's longer than the for-loop header
}"},
vec![indoc! {"
for (const a in b) «{»
// a comment that's longer than the for-loop header
«}»"}],
);
}
#[gpui::test]
fn test_range_for_syntax_ancestor(cx: &mut App) {
cx.new(|cx| {
let text = "fn a() { b(|c| {}) }";
let buffer = Buffer::local(text, cx).with_language(rust_lang(), cx);
let snapshot = buffer.snapshot();
assert_eq!(
snapshot
.syntax_ancestor(empty_range_at(text, "|"))
.unwrap()
.byte_range(),
range_of(text, "|")
);
assert_eq!(
snapshot
.syntax_ancestor(range_of(text, "|"))
.unwrap()
.byte_range(),
range_of(text, "|c|")
);
assert_eq!(
snapshot
.syntax_ancestor(range_of(text, "|c|"))
.unwrap()
.byte_range(),
range_of(text, "|c| {}")
);
assert_eq!(
snapshot
.syntax_ancestor(range_of(text, "|c| {}"))
.unwrap()
.byte_range(),
range_of(text, "(|c| {})")
);
buffer
});
fn empty_range_at(text: &str, part: &str) -> Range<usize> {
let start = text.find(part).unwrap();
start..start
}
fn range_of(text: &str, part: &str) -> Range<usize> {
let start = text.find(part).unwrap();
start..start + part.len()
}
}
#[gpui::test]
fn test_autoindent_with_soft_tabs(cx: &mut App) {
init_settings(cx, |_| {});
cx.new(|cx| {
let text = "fn a() {}";
let mut buffer = Buffer::local(text, cx).with_language(rust_lang(), cx);
buffer.edit([(8..8, "\n\n")], Some(AutoindentMode::EachLine), cx);
assert_eq!(buffer.text(), "fn a() {\n \n}");
buffer.edit(
[(Point::new(1, 4)..Point::new(1, 4), "b()\n")],
Some(AutoindentMode::EachLine),
cx,
);
assert_eq!(buffer.text(), "fn a() {\n b()\n \n}");
// Create a field expression on a new line, causing that line
// to be indented.
buffer.edit(
[(Point::new(2, 4)..Point::new(2, 4), ".c")],
Some(AutoindentMode::EachLine),
cx,
);
assert_eq!(buffer.text(), "fn a() {\n b()\n .c\n}");
// Remove the dot so that the line is no longer a field expression,
// causing the line to be outdented.
buffer.edit(
[(Point::new(2, 8)..Point::new(2, 9), "")],
Some(AutoindentMode::EachLine),
cx,
);
assert_eq!(buffer.text(), "fn a() {\n b()\n c\n}");
buffer
});
}
#[gpui::test]
fn test_autoindent_with_hard_tabs(cx: &mut App) {
init_settings(cx, |settings| {
settings.defaults.hard_tabs = Some(true);
});
cx.new(|cx| {
let text = "fn a() {}";
let mut buffer = Buffer::local(text, cx).with_language(rust_lang(), cx);
buffer.edit([(8..8, "\n\n")], Some(AutoindentMode::EachLine), cx);
assert_eq!(buffer.text(), "fn a() {\n\t\n}");
buffer.edit(
[(Point::new(1, 1)..Point::new(1, 1), "b()\n")],
Some(AutoindentMode::EachLine),
cx,
);
assert_eq!(buffer.text(), "fn a() {\n\tb()\n\t\n}");
// Create a field expression on a new line, causing that line
// to be indented.
buffer.edit(
[(Point::new(2, 1)..Point::new(2, 1), ".c")],
Some(AutoindentMode::EachLine),
cx,
);
assert_eq!(buffer.text(), "fn a() {\n\tb()\n\t\t.c\n}");
// Remove the dot so that the line is no longer a field expression,
// causing the line to be outdented.
buffer.edit(
[(Point::new(2, 2)..Point::new(2, 3), "")],
Some(AutoindentMode::EachLine),
cx,
);
assert_eq!(buffer.text(), "fn a() {\n\tb()\n\tc\n}");
buffer
});
}
#[gpui::test]
fn test_autoindent_does_not_adjust_lines_with_unchanged_suggestion(cx: &mut App) {
init_settings(cx, |_| {});
cx.new(|cx| {
let mut buffer = Buffer::local(
"
fn a() {
c;
d;
}
"
.unindent(),
cx,
)
.with_language(rust_lang(), cx);
// Lines 2 and 3 don't match the indentation suggestion. When editing these lines,
// their indentation is not adjusted.
buffer.edit_via_marked_text(
&"
fn a() {
c«()»;
d«()»;
}
"
.unindent(),
Some(AutoindentMode::EachLine),
cx,
);
assert_eq!(
buffer.text(),
"
fn a() {
c();
d();
}
"
.unindent()
);
// When appending new content after these lines, the indentation is based on the
// preceding lines' actual indentation.
buffer.edit_via_marked_text(
&"
fn a() {
c«
.f
.g()»;
d«
.f
.g()»;
}
"
.unindent(),
Some(AutoindentMode::EachLine),
cx,
);
assert_eq!(
buffer.text(),
"
fn a() {
c
.f
.g();
d
.f
.g();
}
"
.unindent()
);
// Insert a newline after the open brace. It is auto-indented
buffer.edit_via_marked_text(
&"
fn a() {«
»
c
.f
.g();
d
.f
.g();
}
"
.unindent(),
Some(AutoindentMode::EachLine),
cx,
);
assert_eq!(
buffer.text(),
"
fn a() {
ˇ
c
.f
.g();
d
.f
.g();
}
"
.unindent()
.replace("ˇ", "")
);
// Manually outdent the line. It stays outdented.
buffer.edit_via_marked_text(
&"
fn a() {
«»
c
.f
.g();
d
.f
.g();
}
"
.unindent(),
Some(AutoindentMode::EachLine),
cx,
);
assert_eq!(
buffer.text(),
"
fn a() {
c
.f
.g();
d
.f
.g();
}
"
.unindent()
);
buffer
});
cx.new(|cx| {
eprintln!("second buffer: {:?}", cx.entity_id());
let mut buffer = Buffer::local(
"
fn a() {
b();
|
"
.replace('|', "") // marker to preserve trailing whitespace
.unindent(),
cx,
)
.with_language(rust_lang(), cx);
// Insert a closing brace. It is outdented.
buffer.edit_via_marked_text(
&"
fn a() {
b();
«}»
"
.unindent(),
Some(AutoindentMode::EachLine),
cx,
);
assert_eq!(
buffer.text(),
"
fn a() {
b();
}
"
.unindent()
);
// Manually edit the leading whitespace. The edit is preserved.
buffer.edit_via_marked_text(
&"
fn a() {
b();
« »}
"
.unindent(),
Some(AutoindentMode::EachLine),
cx,
);
assert_eq!(
buffer.text(),
"
fn a() {
b();
}
"
.unindent()
);
buffer
});
eprintln!("DONE");
}
#[gpui::test]
fn test_autoindent_does_not_adjust_lines_within_newly_created_errors(cx: &mut App) {
init_settings(cx, |_| {});
cx.new(|cx| {
let mut buffer = Buffer::local(
"
fn a() {
i
}
"
.unindent(),
cx,
)
.with_language(rust_lang(), cx);
// Regression test: line does not get outdented due to syntax error
buffer.edit_via_marked_text(
&"
fn a() {
i«f let Some(x) = y»
}
"
.unindent(),
Some(AutoindentMode::EachLine),
cx,
);
assert_eq!(
buffer.text(),
"
fn a() {
if let Some(x) = y
}
"
.unindent()
);
buffer.edit_via_marked_text(
&"
fn a() {
if let Some(x) = y« {»
}
"
.unindent(),
Some(AutoindentMode::EachLine),
cx,
);
assert_eq!(
buffer.text(),
"
fn a() {
if let Some(x) = y {
}
"
.unindent()
);
buffer
});
}
#[gpui::test]
fn test_autoindent_adjusts_lines_when_only_text_changes(cx: &mut App) {
init_settings(cx, |_| {});
cx.new(|cx| {
let mut buffer = Buffer::local(
"
fn a() {}
"
.unindent(),
cx,
)
.with_language(rust_lang(), cx);
buffer.edit_via_marked_text(
&"
fn a(«
b») {}
"
.unindent(),
Some(AutoindentMode::EachLine),
cx,
);
assert_eq!(
buffer.text(),
"
fn a(
b) {}
"
.unindent()
);
// The indentation suggestion changed because `@end` node (a close paren)
// is now at the beginning of the line.
buffer.edit_via_marked_text(
&"
fn a(
ˇ) {}
"
.unindent(),
Some(AutoindentMode::EachLine),
cx,
);
assert_eq!(
buffer.text(),
"
fn a(
) {}
"
.unindent()
);
buffer
});
}
#[gpui::test]
fn test_autoindent_with_edit_at_end_of_buffer(cx: &mut App) {
init_settings(cx, |_| {});
cx.new(|cx| {
let text = "a\nb";
let mut buffer = Buffer::local(text, cx).with_language(rust_lang(), cx);
buffer.edit(
[(0..1, "\n"), (2..3, "\n")],
Some(AutoindentMode::EachLine),
cx,
);
assert_eq!(buffer.text(), "\n\n\n");
buffer
});
}
#[gpui::test]
fn test_autoindent_multi_line_insertion(cx: &mut App) {
init_settings(cx, |_| {});
cx.new(|cx| {
let text = "
const a: usize = 1;
fn b() {
if c {
let d = 2;
}
}
"
.unindent();
let mut buffer = Buffer::local(text, cx).with_language(rust_lang(), cx);
buffer.edit(
[(Point::new(3, 0)..Point::new(3, 0), "e(\n f()\n);\n")],
Some(AutoindentMode::EachLine),
cx,
);
assert_eq!(
buffer.text(),
"
const a: usize = 1;
fn b() {
if c {
e(
f()
);
let d = 2;
}
}
"
.unindent()
);
buffer
});
}
#[gpui::test]
fn test_autoindent_edit_before_insertion(cx: &mut App) {
init_settings(cx, |_| {});
cx.new(|cx| {
let text = "
fn a() {
b();
}
"
.unindent();
// Insert a new line above a line that is over-indented. Only the newly added line should
// be auto-formatted. The rest of the text should remain the same as before the operation.
let mut buffer = Buffer::local(text, cx).with_language(rust_lang(), cx);
buffer.edit_before(
[(Point::new(1, 0)..Point::new(1, 0), " c();\n")],
Some(AutoindentMode::EachLine),
cx,
);
assert_eq!(
buffer.text(),
"
fn a() {
c();
b();
}
"
.unindent()
);
buffer
});
}
#[gpui::test]
fn test_autoindent_block_mode(cx: &mut App) {
init_settings(cx, |_| {});
cx.new(|cx| {
let text = r#"
fn a() {
b();
}
"#
.unindent();
let mut buffer = Buffer::local(text, cx).with_language(rust_lang(), cx);
// When this text was copied, both of the quotation marks were at the same
// indent level, but the indentation of the first line was not included in
// the copied text. This information is retained in the
// 'original_indent_columns' vector.
let original_indent_columns = vec![Some(4)];
let inserted_text = r#"
"
c
d
e
"
"#
.unindent();
// Insert the block at column zero. The entire block is indented
// so that the first line matches the previous line's indentation.
buffer.edit(
[(Point::new(2, 0)..Point::new(2, 0), inserted_text.clone())],
Some(AutoindentMode::Block {
original_indent_columns: original_indent_columns.clone(),
}),
cx,
);
assert_eq!(
buffer.text(),
r#"
fn a() {
b();
"
c
d
e
"
}
"#
.unindent()
);
// Grouping is disabled in tests, so we need 2 undos
buffer.undo(cx); // Undo the auto-indent
buffer.undo(cx); // Undo the original edit
// Insert the block at a deeper indent level. The entire block is outdented.
buffer.edit([(Point::new(2, 0)..Point::new(2, 0), " ")], None, cx);
buffer.edit(
[(Point::new(2, 8)..Point::new(2, 8), inserted_text)],
Some(AutoindentMode::Block {
original_indent_columns,
}),
cx,
);
assert_eq!(
buffer.text(),
r#"
fn a() {
b();
"
c
d
e
"
}
"#
.unindent()
);
buffer
});
}
#[gpui::test]
fn test_autoindent_block_mode_with_newline(cx: &mut App) {
init_settings(cx, |_| {});
cx.new(|cx| {
let text = r#"
fn a() {
b();
}
"#
.unindent();
let mut buffer = Buffer::local(text, cx).with_language(rust_lang(), cx);
// First line contains just '\n', it's indentation is stored in "original_indent_columns"
let original_indent_columns = vec![Some(4)];
let inserted_text = r#"
c();
d();
e();
"#
.unindent();
buffer.edit(
[(Point::new(2, 0)..Point::new(2, 0), inserted_text)],
Some(AutoindentMode::Block {
original_indent_columns,
}),
cx,
);
// While making edit, we ignore first line as it only contains '\n'
// hence second line indent is used to calculate delta
assert_eq!(
buffer.text(),
r#"
fn a() {
b();
c();
d();
e();
}
"#
.unindent()
);
buffer
});
}
#[gpui::test]
fn test_autoindent_block_mode_without_original_indent_columns(cx: &mut App) {
init_settings(cx, |_| {});
cx.new(|cx| {
let text = r#"
fn a() {
if b() {
}
}
"#
.unindent();
let mut buffer = Buffer::local(text, cx).with_language(rust_lang(), cx);
// The original indent columns are not known, so this text is
// auto-indented in a block as if the first line was copied in
// its entirety.
let original_indent_columns = Vec::new();
let inserted_text = " c\n .d()\n .e();";
// Insert the block at column zero. The entire block is indented
// so that the first line matches the previous line's indentation.
buffer.edit(
[(Point::new(2, 0)..Point::new(2, 0), inserted_text)],
Some(AutoindentMode::Block {
original_indent_columns,
}),
cx,
);
assert_eq!(
buffer.text(),
r#"
fn a() {
if b() {
c
.d()
.e();
}
}
"#
.unindent()
);
// Grouping is disabled in tests, so we need 2 undos
buffer.undo(cx); // Undo the auto-indent
buffer.undo(cx); // Undo the original edit
// Insert the block at a deeper indent level. The entire block is outdented.
buffer.edit(
[(Point::new(2, 0)..Point::new(2, 0), " ".repeat(12))],
None,
cx,
);
buffer.edit(
[(Point::new(2, 12)..Point::new(2, 12), inserted_text)],
Some(AutoindentMode::Block {
original_indent_columns: Vec::new(),
}),
cx,
);
assert_eq!(
buffer.text(),
r#"
fn a() {
if b() {
c
.d()
.e();
}
}
"#
.unindent()
);
buffer
});
}
#[gpui::test]
fn test_autoindent_block_mode_with_hard_tabs(cx: &mut App) {
init_settings(cx, |settings| {
settings.defaults.hard_tabs = Some(true);
});
cx.new(|cx| {
let text = "fn a() {\n\tb();\n}";
let mut buffer = Buffer::local(text, cx).with_language(rust_lang(), cx);
// Insert a block whose indentation mixes tab-indented lines with
// lines that have no leading whitespace, like a snippet body.
let inserted_text = "if c {\n\td();\n}\n";
buffer.edit(
[(Point::new(2, 0)..Point::new(2, 0), inserted_text)],
Some(AutoindentMode::Block {
original_indent_columns: Vec::new(),
}),
cx,
);
// All of the block's lines are indented, including the ones that
// originally had no indentation.
assert_eq!(
buffer.text(),
"fn a() {\n\tb();\n\tif c {\n\t\td();\n\t}\n}"
);
buffer
});
}
#[gpui::test]
fn test_autoindent_block_mode_multiple_adjacent_ranges(cx: &mut App) {
init_settings(cx, |_| {});
cx.new(|cx| {
let (text, ranges_to_replace) = marked_text_ranges(
&"
mod numbers {
«fn one() {
1
}
»
«fn two() {
2
}
»
«fn three() {
3
}
»}
"
.unindent(),
false,
);
let mut buffer = Buffer::local(text, cx).with_language(rust_lang(), cx);
buffer.edit(
[
(ranges_to_replace[0].clone(), "fn one() {\n 101\n}\n"),
(ranges_to_replace[1].clone(), "fn two() {\n 102\n}\n"),
(ranges_to_replace[2].clone(), "fn three() {\n 103\n}\n"),
],
Some(AutoindentMode::Block {
original_indent_columns: vec![Some(0), Some(0), Some(0)],
}),
cx,
);
assert_eq!(
buffer.text(),
"
mod numbers {
fn one() {
101
}
fn two() {
102
}
fn three() {
103
}
}
"
.unindent()
);
buffer
});
}
#[gpui::test]
fn test_replacing_line_content_keeps_manual_indent(cx: &mut App) {
init_settings(cx, |_| {});
cx.new(|cx| {
let (text, ranges_to_replace) = marked_text_ranges(
// 8 spaces here to represent the additional manual indentation
indoc! {r#"
fn main() {
«println!("hello");»
}
"#},
false,
);
let mut buffer = Buffer::local(text, cx).with_language(rust_lang(), cx);
buffer.edit(
[(ranges_to_replace[0].clone(), "let x = 1;")],
Some(AutoindentMode::EachLine),
cx,
);
assert_eq!(
buffer.text(),
indoc! {r#"
fn main() {
let x = 1;
}
"#}
);
buffer
});
}
#[gpui::test]
fn test_autoindent_language_without_indents_query(cx: &mut App) {
init_settings(cx, |_| {});
cx.new(|cx| {
let text = "
* one
- a
- b
* two
"
.unindent();
let mut buffer = Buffer::local(text, cx).with_language(
Arc::new(Language::new(
LanguageConfig {
name: "Markdown".into(),
auto_indent_using_last_non_empty_line: false,
..Default::default()
},
Some(tree_sitter_json::LANGUAGE.into()),
)),
cx,
);
buffer.edit(
[(Point::new(3, 0)..Point::new(3, 0), "\n")],
Some(AutoindentMode::EachLine),
cx,
);
assert_eq!(
buffer.text(),
"
* one
- a
- b
* two
"
.unindent()
);
buffer
});
}
#[gpui::test]
fn test_autoindent_with_injected_languages(cx: &mut App) {
init_settings(cx, |settings| {
settings.languages.0.extend([
(
"HTML".into(),
LanguageSettingsContent {
tab_size: Some(2.try_into().unwrap()),
..Default::default()
},
),
(
"JavaScript".into(),
LanguageSettingsContent {
tab_size: Some(8.try_into().unwrap()),
..Default::default()
},
),
])
});
let html_language = Arc::new(html_lang());
let javascript_language = Arc::new(javascript_lang());
let language_registry = Arc::new(LanguageRegistry::test(cx.background_executor().clone()));
language_registry.add(html_language.clone());
language_registry.add(javascript_language);
cx.new(|cx| {
let (text, ranges) = marked_text_ranges(
&"
<div>ˇ
</div>
<script>
init({ˇ
})
</script>
<span>ˇ
</span>
"
.unindent(),
false,
);
let mut buffer = Buffer::local(text, cx);
buffer.set_language_registry(language_registry);
buffer.set_language(Some(html_language), cx);
buffer.edit(
ranges.into_iter().map(|range| (range, "\na")),
Some(AutoindentMode::EachLine),
cx,
);
assert_eq!(
buffer.text(),
"
<div>
a
</div>
<script>
init({
a
})
</script>
<span>
a
</span>
"
.unindent()
);
buffer
});
}
#[gpui::test]
fn test_autoindent_query_with_outdent_captures(cx: &mut App) {
init_settings(cx, |settings| {
settings.defaults.tab_size = Some(2.try_into().unwrap());
});
cx.new(|cx| {
let mut buffer = Buffer::local("", cx).with_language(Arc::new(ruby_lang()), cx);
let text = r#"
class C
def a(b, c)
puts b
puts c
rescue
puts "errored"
exit 1
end
end
"#
.unindent();
buffer.edit([(0..0, text)], Some(AutoindentMode::EachLine), cx);
assert_eq!(
buffer.text(),
r#"
class C
def a(b, c)
puts b
puts c
rescue
puts "errored"
exit 1
end
end
"#
.unindent()
);
buffer
});
}
#[gpui::test]
async fn test_async_autoindents_preserve_preview(cx: &mut TestAppContext) {
cx.update(|cx| init_settings(cx, |_| {}));
// First we insert some newlines to request an auto-indent (asynchronously).
// Then we request that a preview tab be preserved for the new version, even though it's edited.
let buffer = cx.new(|cx| {
let text = "fn a() {}";
let mut buffer = Buffer::local(text, cx).with_language(rust_lang(), cx);
// This causes autoindent to be async.
buffer.set_sync_parse_timeout(None);
buffer.edit([(8..8, "\n\n")], Some(AutoindentMode::EachLine), cx);
buffer.refresh_preview();
// Synchronously, we haven't auto-indented and we're still preserving the preview.
assert_eq!(buffer.text(), "fn a() {\n\n}");
assert!(buffer.preserve_preview());
buffer
});
// Now let the autoindent finish
cx.executor().run_until_parked();
// The auto-indent applied, but didn't dismiss our preview
buffer.update(cx, |buffer, cx| {
assert_eq!(buffer.text(), "fn a() {\n \n}");
assert!(buffer.preserve_preview());
// Edit inserting another line. It will autoindent async.
// Then refresh the preview version.
buffer.edit(
[(Point::new(1, 4)..Point::new(1, 4), "\n")],
Some(AutoindentMode::EachLine),
cx,
);
buffer.refresh_preview();
assert_eq!(buffer.text(), "fn a() {\n \n\n}");
assert!(buffer.preserve_preview());
// Then perform another edit, this time without refreshing the preview version.
buffer.edit([(Point::new(1, 4)..Point::new(1, 4), "x")], None, cx);
// This causes the preview to not be preserved.
assert!(!buffer.preserve_preview());
});
// Let the async autoindent from the first edit finish.
cx.executor().run_until_parked();
// The autoindent applies, but it shouldn't restore the preview status because we had an edit in the meantime.
buffer.update(cx, |buffer, _| {
assert_eq!(buffer.text(), "fn a() {\n x\n \n}");
assert!(!buffer.preserve_preview());
});
}
#[gpui::test]
fn test_insert_empty_line(cx: &mut App) {
init_settings(cx, |_| {});
// Insert empty line at the beginning, requesting an empty line above
cx.new(|cx| {
let mut buffer = Buffer::local("abc\ndef\nghi", cx);
let point = buffer.insert_empty_line(Point::new(0, 0), true, false, cx);
assert_eq!(buffer.text(), "\nabc\ndef\nghi");
assert_eq!(point, Point::new(0, 0));
buffer
});
// Insert empty line at the beginning, requesting an empty line above and below
cx.new(|cx| {
let mut buffer = Buffer::local("abc\ndef\nghi", cx);
let point = buffer.insert_empty_line(Point::new(0, 0), true, true, cx);
assert_eq!(buffer.text(), "\n\nabc\ndef\nghi");
assert_eq!(point, Point::new(0, 0));
buffer
});
// Insert empty line at the start of a line, requesting empty lines above and below
cx.new(|cx| {
let mut buffer = Buffer::local("abc\ndef\nghi", cx);
let point = buffer.insert_empty_line(Point::new(2, 0), true, true, cx);
assert_eq!(buffer.text(), "abc\ndef\n\n\n\nghi");
assert_eq!(point, Point::new(3, 0));
buffer
});
// Insert empty line in the middle of a line, requesting empty lines above and below
cx.new(|cx| {
let mut buffer = Buffer::local("abc\ndefghi\njkl", cx);
let point = buffer.insert_empty_line(Point::new(1, 3), true, true, cx);
assert_eq!(buffer.text(), "abc\ndef\n\n\n\nghi\njkl");
assert_eq!(point, Point::new(3, 0));
buffer
});
// Insert empty line in the middle of a line, requesting empty line above only
cx.new(|cx| {
let mut buffer = Buffer::local("abc\ndefghi\njkl", cx);
let point = buffer.insert_empty_line(Point::new(1, 3), true, false, cx);
assert_eq!(buffer.text(), "abc\ndef\n\n\nghi\njkl");
assert_eq!(point, Point::new(3, 0));
buffer
});
// Insert empty line in the middle of a line, requesting empty line below only
cx.new(|cx| {
let mut buffer = Buffer::local("abc\ndefghi\njkl", cx);
let point = buffer.insert_empty_line(Point::new(1, 3), false, true, cx);
assert_eq!(buffer.text(), "abc\ndef\n\n\nghi\njkl");
assert_eq!(point, Point::new(2, 0));
buffer
});
// Insert empty line at the end, requesting empty lines above and below
cx.new(|cx| {
let mut buffer = Buffer::local("abc\ndef\nghi", cx);
let point = buffer.insert_empty_line(Point::new(2, 3), true, true, cx);
assert_eq!(buffer.text(), "abc\ndef\nghi\n\n\n");
assert_eq!(point, Point::new(4, 0));
buffer
});
// Insert empty line at the end, requesting empty line above only
cx.new(|cx| {
let mut buffer = Buffer::local("abc\ndef\nghi", cx);
let point = buffer.insert_empty_line(Point::new(2, 3), true, false, cx);
assert_eq!(buffer.text(), "abc\ndef\nghi\n\n");
assert_eq!(point, Point::new(4, 0));
buffer
});
// Insert empty line at the end, requesting empty line below only
cx.new(|cx| {
let mut buffer = Buffer::local("abc\ndef\nghi", cx);
let point = buffer.insert_empty_line(Point::new(2, 3), false, true, cx);
assert_eq!(buffer.text(), "abc\ndef\nghi\n\n");
assert_eq!(point, Point::new(3, 0));
buffer
});
}
#[gpui::test]
fn test_language_scope_at_with_javascript(cx: &mut App) {
init_settings(cx, |_| {});
cx.new(|cx| {
let language = Language::new(
LanguageConfig {
name: "JavaScript".into(),
line_comments: vec!["// ".into()],
block_comment: Some(BlockCommentConfig {
start: "/*".into(),
end: "*/".into(),
prefix: "* ".into(),
tab_size: 1,
}),
brackets: BracketPairConfig {
pairs: vec![
BracketPair {
start: "{".into(),
end: "}".into(),
close: true,
surround: true,
newline: false,
},
BracketPair {
start: "'".into(),
end: "'".into(),
close: true,
surround: true,
newline: false,
},
],
disabled_scopes_by_bracket_ix: vec![
Vec::new(), //
vec!["string".into(), "comment".into()], // single quotes disabled
],
},
overrides: [(
"element".into(),
LanguageConfigOverride {
line_comments: Override::Remove { remove: true },
block_comment: Override::Set(BlockCommentConfig {
start: "{/*".into(),
prefix: "".into(),
end: "*/}".into(),
tab_size: 0,
}),
..Default::default()
},
)]
.into_iter()
.collect(),
..Default::default()
},
Some(tree_sitter_typescript::LANGUAGE_TSX.into()),
)
.with_override_query(
r#"
(jsx_element) @element
(string) @string
(comment) @comment.inclusive
[
(jsx_opening_element)
(jsx_closing_element)
(jsx_expression)
] @default
"#,
)
.unwrap();
let text = r#"
a["b"] = <C d="e">
<F></F>
{ g() }
</C>; // a comment
"#
.unindent();
let buffer = Buffer::local(&text, cx).with_language(Arc::new(language), cx);
let snapshot = buffer.snapshot();
let config = snapshot.language_scope_at(0).unwrap();
assert_eq!(config.line_comment_prefixes(), &[Arc::from("// ")]);
assert_eq!(
config.block_comment(),
Some(&BlockCommentConfig {
start: "/*".into(),
prefix: "* ".into(),
end: "*/".into(),
tab_size: 1,
})
);
// Both bracket pairs are enabled
assert_eq!(
config.brackets().map(|e| e.1).collect::<Vec<_>>(),
&[true, true]
);
let comment_config = snapshot
.language_scope_at(text.find("comment").unwrap() + "comment".len())
.unwrap();
assert_eq!(
comment_config.brackets().map(|e| e.1).collect::<Vec<_>>(),
&[true, false]
);
let string_config = snapshot
.language_scope_at(text.find("b\"").unwrap())
.unwrap();
assert_eq!(string_config.line_comment_prefixes(), &[Arc::from("// ")]);
assert_eq!(
string_config.block_comment(),
Some(&BlockCommentConfig {
start: "/*".into(),
prefix: "* ".into(),
end: "*/".into(),
tab_size: 1,
})
);
// Second bracket pair is disabled
assert_eq!(
string_config.brackets().map(|e| e.1).collect::<Vec<_>>(),
&[true, false]
);
// In between JSX tags: use the `element` override.
let element_config = snapshot
.language_scope_at(text.find("<F>").unwrap())
.unwrap();
// TODO nested blocks after newlines are captured with all whitespaces
// https://github.com/tree-sitter/tree-sitter-typescript/issues/306
// assert_eq!(element_config.line_comment_prefixes(), &[]);
// assert_eq!(
// element_config.block_comment_delimiters(),
// Some((&"{/*".into(), &"*/}".into()))
// );
assert_eq!(
element_config.brackets().map(|e| e.1).collect::<Vec<_>>(),
&[true, true]
);
// Within a JSX tag: use the default config.
let tag_config = snapshot
.language_scope_at(text.find(" d=").unwrap() + 1)
.unwrap();
assert_eq!(tag_config.line_comment_prefixes(), &[Arc::from("// ")]);
assert_eq!(
tag_config.block_comment(),
Some(&BlockCommentConfig {
start: "/*".into(),
prefix: "* ".into(),
end: "*/".into(),
tab_size: 1,
})
);
assert_eq!(
tag_config.brackets().map(|e| e.1).collect::<Vec<_>>(),
&[true, true]
);
// In a JSX expression: use the default config.
let expression_in_element_config = snapshot
.language_scope_at(text.find('{').unwrap() + 1)
.unwrap();
assert_eq!(
expression_in_element_config.line_comment_prefixes(),
&[Arc::from("// ")]
);
assert_eq!(
expression_in_element_config.block_comment(),
Some(&BlockCommentConfig {
start: "/*".into(),
prefix: "* ".into(),
end: "*/".into(),
tab_size: 1,
})
);
assert_eq!(
expression_in_element_config
.brackets()
.map(|e| e.1)
.collect::<Vec<_>>(),
&[true, true]
);
buffer
});
}
#[gpui::test]
fn test_language_scope_at_with_rust(cx: &mut App) {
init_settings(cx, |_| {});
cx.new(|cx| {
let language = Language::new(
LanguageConfig {
name: "Rust".into(),
brackets: BracketPairConfig {
pairs: vec![
BracketPair {
start: "{".into(),
end: "}".into(),
close: true,
surround: true,
newline: false,
},
BracketPair {
start: "'".into(),
end: "'".into(),
close: true,
surround: true,
newline: false,
},
],
disabled_scopes_by_bracket_ix: vec![
Vec::new(), //
vec!["string".into()],
],
},
..Default::default()
},
Some(tree_sitter_rust::LANGUAGE.into()),
)
.with_override_query(
r#"
(string_literal) @string
"#,
)
.unwrap();
let text = r#"
const S: &'static str = "hello";
"#
.unindent();
let buffer = Buffer::local(text.clone(), cx).with_language(Arc::new(language), cx);
let snapshot = buffer.snapshot();
// By default, all brackets are enabled
let config = snapshot.language_scope_at(0).unwrap();
assert_eq!(
config.brackets().map(|e| e.1).collect::<Vec<_>>(),
&[true, true]
);
// Within a string, the quotation brackets are disabled.
let string_config = snapshot
.language_scope_at(text.find("ello").unwrap())
.unwrap();
assert_eq!(
string_config.brackets().map(|e| e.1).collect::<Vec<_>>(),
&[true, false]
);
buffer
});
}
#[gpui::test]
fn test_language_scope_at_end_of_buffer(cx: &mut App) {
init_settings(cx, |_| {});
let make_language = || {
Language::new(
LanguageConfig {
name: "C".into(),
brackets: BracketPairConfig {
pairs: vec![
BracketPair {
start: "{".into(),
end: "}".into(),
close: true,
surround: true,
newline: false,
},
BracketPair {
start: "'".into(),
end: "'".into(),
close: true,
surround: true,
newline: false,
},
],
disabled_scopes_by_bracket_ix: vec![
Vec::new(),
vec!["string".into(), "comment".into()],
],
},
..Default::default()
},
Some(tree_sitter_c::LANGUAGE.into()),
)
.with_override_query(
r#"
(comment) @comment.inclusive
[(string_literal) (char_literal)] @string
"#,
)
.unwrap()
};
// Comment runs to EOF: the quote pair must be disabled at EOF.
cx.new(|cx| {
let text = "// it ''";
let buffer = Buffer::local(text, cx).with_language(Arc::new(make_language()), cx);
let snapshot = buffer.snapshot();
let eof_config = snapshot.language_scope_at(text.len()).unwrap();
assert_eq!(
eof_config.brackets().map(|e| e.1).collect::<Vec<_>>(),
&[true, false]
);
buffer
});
// Trailing newline: EOF is past the comment, so both pairs stay enabled.
cx.new(|cx| {
let text = "// it ''\n";
let buffer = Buffer::local(text, cx).with_language(Arc::new(make_language()), cx);
let snapshot = buffer.snapshot();
let eof_config = snapshot.language_scope_at(text.len()).unwrap();
assert_eq!(
eof_config.brackets().map(|e| e.1).collect::<Vec<_>>(),
&[true, true]
);
buffer
});
}
#[gpui::test]
fn test_language_scope_at_with_combined_injections(cx: &mut App) {
init_settings(cx, |_| {});
cx.new(|cx| {
let text = r#"
<ol>
<% people.each do |person| %>
<li>
<%= person.name %>
</li>
<% end %>
</ol>
"#
.unindent();
let language_registry = Arc::new(LanguageRegistry::test(cx.background_executor().clone()));
language_registry.add(Arc::new(ruby_lang()));
language_registry.add(Arc::new(html_lang()));
language_registry.add(Arc::new(erb_lang()));
let mut buffer = Buffer::local(text, cx);
buffer.set_language_registry(language_registry.clone());
let language = language_registry
.language_for_name("HTML+ERB")
.now_or_never()
.and_then(Result::ok);
buffer.set_language(language, cx);
let snapshot = buffer.snapshot();
let html_config = snapshot.language_scope_at(Point::new(2, 4)).unwrap();
assert_eq!(html_config.line_comment_prefixes(), &[]);
assert_eq!(
html_config.block_comment(),
Some(&BlockCommentConfig {
start: "<!--".into(),
end: "-->".into(),
prefix: "".into(),
tab_size: 0,
})
);
let ruby_config = snapshot.language_scope_at(Point::new(3, 12)).unwrap();
assert_eq!(ruby_config.line_comment_prefixes(), &[Arc::from("# ")]);
assert_eq!(ruby_config.block_comment(), None);
buffer
});
}
#[gpui::test]
fn test_language_at_with_hidden_languages(cx: &mut App) {
init_settings(cx, |_| {});
cx.new(|cx| {
let text = r#"
this is an *emphasized* word.
"#
.unindent();
let language_registry = Arc::new(LanguageRegistry::test(cx.background_executor().clone()));
language_registry.add(markdown_lang());
language_registry.add(Arc::new(markdown_inline_lang()));
let mut buffer = Buffer::local(text, cx);
buffer.set_language_registry(language_registry.clone());
buffer.set_language(
language_registry
.language_for_name("Markdown")
.now_or_never()
.unwrap()
.ok(),
cx,
);
let snapshot = buffer.snapshot();
for point in [Point::new(0, 4), Point::new(0, 16)] {
let config = snapshot.language_scope_at(point).unwrap();
assert_eq!(config.language_name(), "Markdown");
let language = snapshot.language_at(point).unwrap();
assert_eq!(language.name().as_ref(), "Markdown");
}
buffer
});
}
#[gpui::test]
fn test_language_at_for_markdown_code_block(cx: &mut App) {
init_settings(cx, |_| {});
cx.new(|cx| {
let text = r#"
```rs
let a = 2;
// let b = 3;
```
"#
.unindent();
let language_registry = Arc::new(LanguageRegistry::test(cx.background_executor().clone()));
language_registry.add(markdown_lang());
language_registry.add(Arc::new(markdown_inline_lang()));
language_registry.add(rust_lang());
let mut buffer = Buffer::local(text, cx);
buffer.set_language_registry(language_registry.clone());
buffer.set_language(
language_registry
.language_for_name("Markdown")
.now_or_never()
.unwrap()
.ok(),
cx,
);
let snapshot = buffer.snapshot();
// Test points in the code line
for point in [Point::new(1, 4), Point::new(1, 6)] {
let config = snapshot.language_scope_at(point).unwrap();
assert_eq!(config.language_name(), "Rust");
let language = snapshot.language_at(point).unwrap();
assert_eq!(language.name().as_ref(), "Rust");
}
// Test points in the comment line to verify it's still detected as Rust
for point in [Point::new(2, 4), Point::new(2, 6)] {
let config = snapshot.language_scope_at(point).unwrap();
assert_eq!(config.language_name(), "Rust");
let language = snapshot.language_at(point).unwrap();
assert_eq!(language.name().as_ref(), "Rust");
}
buffer
});
}
#[gpui::test]
async fn test_markdown_inline_html_highlighting(cx: &mut TestAppContext) {
let markdown_language = markdown_lang();
let markdown_inline_language = Arc::new(
Language::new(
LanguageConfig {
name: "markdown-inline".into(),
grammar: Some("markdown-inline".into()),
..Default::default()
},
Some(tree_sitter_md::INLINE_LANGUAGE.into()),
)
.with_highlights_query(include_str!(
"../../grammars/src/markdown-inline/highlights.scm"
))
.unwrap()
.with_injection_query(include_str!(
"../../grammars/src/markdown-inline/injections.scm"
))
.unwrap(),
);
let html_language = Arc::new(
Language::new(
LanguageConfig {
name: "HTML".into(),
..Default::default()
},
Some(tree_sitter_html::LANGUAGE.into()),
)
.with_highlights_query("(comment) @comment (tag_name) @tag")
.unwrap(),
);
let syntax_theme = SyntaxTheme::new([
("comment".to_string(), gpui::rgba(0xffffffff).into()),
("tag".to_string(), gpui::rgba(0xff0000ff).into()),
]);
markdown_language.set_theme(&syntax_theme);
markdown_inline_language.set_theme(&syntax_theme);
html_language.set_theme(&syntax_theme);
let language_registry = Arc::new(LanguageRegistry::test(cx.background_executor.clone()));
language_registry.add(markdown_language.clone());
language_registry.add(markdown_inline_language);
language_registry.add(html_language);
let text = "<!--Annotation from the start is OK-->\n\n\
Annotation in the middle <!--is rendered badly.-->\n\n\
An inline comment can span <!--multiple\nlines--> within a paragraph.\n\n\
Ordinary inline HTML: <em>emphasized</em>.";
let buffer = cx.new(|cx| {
let mut buffer = Buffer::local(text, cx);
buffer.set_language_registry(language_registry);
buffer.set_language(Some(markdown_language), cx);
buffer
});
cx.run_until_parked();
buffer.read_with(cx, |buffer, _cx| {
let snapshot = buffer.snapshot();
let highlighted_text = |capture_name: &str| {
let highlight_id = syntax_theme
.highlight_id(capture_name)
.map(HighlightId::new);
assert!(highlight_id.is_some(), "{capture_name} not in test theme");
let mut runs: Vec<String> = Vec::new();
let mut previous_chunk_matched = false;
let chunks = snapshot.chunks(
0..snapshot.len(),
LanguageAwareStyling {
tree_sitter: true,
diagnostics: false,
},
);
for chunk in chunks {
let chunk_matches = chunk.syntax_highlight_id == highlight_id;
if chunk_matches {
match runs.last_mut() {
Some(last_run) if previous_chunk_matched => last_run.push_str(chunk.text),
_ => runs.push(chunk.text.to_string()),
}
}
previous_chunk_matched = chunk_matches;
}
runs
};
assert_eq!(
highlighted_text("comment"),
vec![
"<!--Annotation from the start is OK-->",
"<!--is rendered badly.-->",
"<!--multiple\nlines-->",
]
);
assert_eq!(highlighted_text("tag"), vec!["em", "em"]);
});
}
#[gpui::test]
fn test_syntax_layer_at_for_combined_injections(cx: &mut App) {
init_settings(cx, |_| {});
cx.new(|cx| {
// ERB template with HTML and Ruby content
let text = r#"
<div>Hello</div>
<%= link_to "Click", url %>
<p>World</p>
"#
.unindent();
let language_registry = Arc::new(LanguageRegistry::test(cx.background_executor().clone()));
language_registry.add(Arc::new(erb_lang()));
language_registry.add(Arc::new(html_lang()));
language_registry.add(Arc::new(ruby_lang()));
let mut buffer = Buffer::local(text, cx);
buffer.set_language_registry(language_registry.clone());
let language = language_registry
.language_for_name("HTML+ERB")
.now_or_never()
.and_then(Result::ok);
buffer.set_language(language, cx);
let snapshot = buffer.snapshot();
// Test language_at for HTML content (line 0: "<div>Hello</div>")
let html_point = Point::new(0, 4);
let language = snapshot.language_at(html_point).unwrap();
assert_eq!(
language.name().as_ref(),
"HTML",
"Expected HTML at {:?}, got {}",
html_point,
language.name()
);
// Test language_at for Ruby code (line 1: "<%= link_to ... %>")
let ruby_point = Point::new(1, 6);
let language = snapshot.language_at(ruby_point).unwrap();
assert_eq!(
language.name().as_ref(),
"Ruby",
"Expected Ruby at {:?}, got {}",
ruby_point,
language.name()
);
// Test language_at for HTML after Ruby (line 2: "<p>World</p>")
let html_after_ruby = Point::new(2, 2);
let language = snapshot.language_at(html_after_ruby).unwrap();
assert_eq!(
language.name().as_ref(),
"HTML",
"Expected HTML at {:?}, got {}",
html_after_ruby,
language.name()
);
buffer
});
}
#[gpui::test]
fn test_languages_at_for_combined_injections(cx: &mut App) {
init_settings(cx, |_| {});
cx.new(|cx| {
// ERB template with HTML and Ruby content
let text = r#"
<div>Hello</div>
<%= yield %>
<p>World</p>
"#
.unindent();
let language_registry = Arc::new(LanguageRegistry::test(cx.background_executor().clone()));
language_registry.add(Arc::new(erb_lang()));
language_registry.add(Arc::new(html_lang()));
language_registry.add(Arc::new(ruby_lang()));
let mut buffer = Buffer::local(text, cx);
buffer.set_language_registry(language_registry.clone());
buffer.set_language(
language_registry
.language_for_name("HTML+ERB")
.now_or_never()
.unwrap()
.ok(),
cx,
);
// Test languages_at for HTML content - should NOT include Ruby
let html_point = Point::new(0, 4);
let languages = buffer.languages_at(html_point);
let language_names: Vec<_> = languages.iter().map(|language| language.name()).collect();
assert!(
language_names
.iter()
.any(|language_name| language_name.as_ref() == "HTML"),
"Expected HTML in languages at {:?}, got {:?}",
html_point,
language_names
);
assert!(
!language_names
.iter()
.any(|language_name| language_name.as_ref() == "Ruby"),
"Did not expect Ruby in languages at {:?}, got {:?}",
html_point,
language_names
);
// Test languages_at for Ruby code - should NOT include HTML
let ruby_point = Point::new(1, 6);
let languages = buffer.languages_at(ruby_point);
let language_names: Vec<_> = languages.iter().map(|language| language.name()).collect();
assert!(
language_names
.iter()
.any(|language_name| language_name.as_ref() == "Ruby"),
"Expected Ruby in languages at {:?}, got {:?}",
ruby_point,
language_names
);
assert!(
!language_names
.iter()
.any(|language_name| language_name.as_ref() == "HTML"),
"Did not expect HTML in languages at {:?}, got {:?}",
ruby_point,
language_names
);
buffer
});
}
#[gpui::test]
fn test_serialization(cx: &mut gpui::App) {
let mut now = Instant::now();
let buffer1 = cx.new(|cx| {
let mut buffer = Buffer::local("abc", cx);
buffer.edit([(3..3, "D")], None, cx);
now += Duration::from_secs(1);
buffer.start_transaction_at(now);
buffer.edit([(4..4, "E")], None, cx);
buffer.end_transaction_at(now, cx);
assert_eq!(buffer.text(), "abcDE");
buffer.undo(cx);
assert_eq!(buffer.text(), "abcD");
buffer.edit([(4..4, "F")], None, cx);
assert_eq!(buffer.text(), "abcDF");
buffer
});
assert_eq!(buffer1.read(cx).text(), "abcDF");
let state = buffer1.read(cx).to_proto(cx);
let ops = cx
.foreground_executor()
.block_on(buffer1.read(cx).serialize_ops(None, cx));
let buffer2 = cx.new(|cx| {
let mut buffer =
Buffer::from_proto(ReplicaId::new(1), Capability::ReadWrite, state, None, cx).unwrap();
buffer.apply_ops(
ops.into_iter()
.map(|op| proto::deserialize_operation(op).unwrap()),
cx,
);
buffer
});
assert_eq!(buffer2.read(cx).text(), "abcDF");
}
#[gpui::test]
fn test_branch_and_merge(cx: &mut TestAppContext) {
cx.update(|cx| init_settings(cx, |_| {}));
let base = cx.new(|cx| Buffer::local("one\ntwo\nthree\n", cx));
// Create a remote replica of the base buffer.
let base_replica = cx.new(|cx| {
Buffer::from_proto(
ReplicaId::new(1),
Capability::ReadWrite,
base.read(cx).to_proto(cx),
None,
cx,
)
.unwrap()
});
base.update(cx, |_buffer, cx| {
cx.subscribe(&base_replica, |this, _, event, cx| {
if let BufferEvent::Operation {
operation,
is_local: true,
} = event
{
this.apply_ops([operation.clone()], cx);
}
})
.detach();
});
// Create a branch, which initially has the same state as the base buffer.
let branch = base.update(cx, |buffer, cx| buffer.branch(cx));
branch.read_with(cx, |buffer, _| {
assert_eq!(buffer.text(), "one\ntwo\nthree\n");
});
// Edits to the branch are not applied to the base.
branch.update(cx, |buffer, cx| {
buffer.edit(
[
(Point::new(1, 0)..Point::new(1, 0), "1.5\n"),
(Point::new(2, 0)..Point::new(2, 5), "THREE"),
],
None,
cx,
)
});
branch.read_with(cx, |buffer, cx| {
assert_eq!(base.read(cx).text(), "one\ntwo\nthree\n");
assert_eq!(buffer.text(), "one\n1.5\ntwo\nTHREE\n");
});
// Convert from branch buffer ranges to the corresponding ranges in the
// base buffer.
branch.read_with(cx, |buffer, cx| {
assert_eq!(
buffer.range_to_version(4..7, &base.read(cx).version()),
4..4
);
assert_eq!(
buffer.range_to_version(2..9, &base.read(cx).version()),
2..5
);
});
// Edits to the base are applied to the branch.
base.update(cx, |buffer, cx| {
buffer.edit([(Point::new(0, 0)..Point::new(0, 0), "ZERO\n")], None, cx)
});
branch.read_with(cx, |buffer, cx| {
assert_eq!(base.read(cx).text(), "ZERO\none\ntwo\nthree\n");
assert_eq!(buffer.text(), "ZERO\none\n1.5\ntwo\nTHREE\n");
});
// Edits to any replica of the base are applied to the branch.
base_replica.update(cx, |buffer, cx| {
buffer.edit([(Point::new(2, 0)..Point::new(2, 0), "2.5\n")], None, cx)
});
branch.read_with(cx, |buffer, cx| {
assert_eq!(base.read(cx).text(), "ZERO\none\ntwo\n2.5\nthree\n");
assert_eq!(buffer.text(), "ZERO\none\n1.5\ntwo\n2.5\nTHREE\n");
});
// Merging the branch applies all of its changes to the base.
branch.update(cx, |buffer, cx| {
buffer.merge_into_base(Vec::new(), cx);
});
branch.update(cx, |buffer, cx| {
assert_eq!(base.read(cx).text(), "ZERO\none\n1.5\ntwo\n2.5\nTHREE\n");
assert_eq!(buffer.text(), "ZERO\none\n1.5\ntwo\n2.5\nTHREE\n");
});
}
#[gpui::test]
fn test_merge_into_base(cx: &mut TestAppContext) {
cx.update(|cx| init_settings(cx, |_| {}));
let base = cx.new(|cx| Buffer::local("abcdefghijk", cx));
let branch = base.update(cx, |buffer, cx| buffer.branch(cx));
// Make 3 edits, merge one into the base.
branch.update(cx, |branch, cx| {
branch.edit([(0..3, "ABC"), (7..9, "HI"), (11..11, "LMN")], None, cx);
branch.merge_into_base(vec![5..8], cx);
});
branch.read_with(cx, |branch, _| assert_eq!(branch.text(), "ABCdefgHIjkLMN"));
base.read_with(cx, |base, _| assert_eq!(base.text(), "abcdefgHIjk"));
// Undo the one already-merged edit. Merge that into the base.
branch.update(cx, |branch, cx| {
branch.edit([(7..9, "hi")], None, cx);
branch.merge_into_base(vec![5..8], cx);
});
base.read_with(cx, |base, _| assert_eq!(base.text(), "abcdefghijk"));
// Merge an insertion into the base.
branch.update(cx, |branch, cx| {
branch.merge_into_base(vec![11..11], cx);
});
branch.read_with(cx, |branch, _| assert_eq!(branch.text(), "ABCdefghijkLMN"));
base.read_with(cx, |base, _| assert_eq!(base.text(), "abcdefghijkLMN"));
// Deleted the inserted text and merge that into the base.
branch.update(cx, |branch, cx| {
branch.edit([(11..14, "")], None, cx);
branch.merge_into_base(vec![10..11], cx);
});
base.read_with(cx, |base, _| assert_eq!(base.text(), "abcdefghijk"));
}
#[gpui::test]
fn test_undo_after_merge_into_base(cx: &mut TestAppContext) {
cx.update(|cx| init_settings(cx, |_| {}));
let base = cx.new(|cx| Buffer::local("abcdefghijk", cx));
let branch = base.update(cx, |buffer, cx| buffer.branch(cx));
// Make 2 edits, merge one into the base.
branch.update(cx, |branch, cx| {
branch.edit([(0..3, "ABC"), (7..9, "HI")], None, cx);
branch.merge_into_base(vec![7..7], cx);
});
base.read_with(cx, |base, _| assert_eq!(base.text(), "abcdefgHIjk"));
branch.read_with(cx, |branch, _| assert_eq!(branch.text(), "ABCdefgHIjk"));
// Undo the merge in the base buffer.
base.update(cx, |base, cx| {
base.undo(cx);
});
base.read_with(cx, |base, _| assert_eq!(base.text(), "abcdefghijk"));
branch.read_with(cx, |branch, _| assert_eq!(branch.text(), "ABCdefgHIjk"));
// Merge that operation into the base again.
branch.update(cx, |branch, cx| {
branch.merge_into_base(vec![7..7], cx);
});
base.read_with(cx, |base, _| assert_eq!(base.text(), "abcdefgHIjk"));
branch.read_with(cx, |branch, _| assert_eq!(branch.text(), "ABCdefgHIjk"));
}
#[gpui::test]
async fn test_preview_edits(cx: &mut TestAppContext) {
cx.update(|cx| {
init_settings(cx, |_| {});
theme_settings::init(theme::LoadThemes::JustBase, cx);
});
let insertion_style = HighlightStyle {
background_color: Some(cx.read(|cx| cx.theme().status().created_background)),
..Default::default()
};
let deletion_style = HighlightStyle {
background_color: Some(cx.read(|cx| cx.theme().status().deleted_background)),
..Default::default()
};
// no edits
assert_preview_edits(
indoc! {"
fn test_empty() -> bool {
false
}"
},
vec![],
true,
cx,
|hl| {
assert!(hl.text.is_empty());
assert!(hl.highlights.is_empty());
},
)
.await;
// only insertions
assert_preview_edits(
indoc! {"
fn calculate_area(: f64) -> f64 {
std::f64::consts::PI * .powi(2)
}"
},
vec![
(Point::new(0, 18)..Point::new(0, 18), "radius"),
(Point::new(1, 27)..Point::new(1, 27), "radius"),
],
true,
cx,
|hl| {
assert_eq!(
hl.text,
indoc! {"
fn calculate_area(radius: f64) -> f64 {
std::f64::consts::PI * radius.powi(2)"
}
);
assert_eq!(hl.highlights.len(), 2);
assert_eq!(hl.highlights[0], ((18..24), insertion_style));
assert_eq!(hl.highlights[1], ((67..73), insertion_style));
},
)
.await;
// insertions & deletions
assert_preview_edits(
indoc! {"
struct Person {
first_name: String,
}
impl Person {
fn first_name(&self) -> &String {
&self.first_name
}
}"
},
vec![
(Point::new(1, 4)..Point::new(1, 9), "last"),
(Point::new(5, 7)..Point::new(5, 12), "last"),
(Point::new(6, 14)..Point::new(6, 19), "last"),
],
true,
cx,
|hl| {
assert_eq!(
hl.text,
indoc! {"
firstlast_name: String,
}
impl Person {
fn firstlast_name(&self) -> &String {
&self.firstlast_name"
}
);
assert_eq!(hl.highlights.len(), 6);
assert_eq!(hl.highlights[0], ((4..9), deletion_style));
assert_eq!(hl.highlights[1], ((9..13), insertion_style));
assert_eq!(hl.highlights[2], ((52..57), deletion_style));
assert_eq!(hl.highlights[3], ((57..61), insertion_style));
assert_eq!(hl.highlights[4], ((101..106), deletion_style));
assert_eq!(hl.highlights[5], ((106..110), insertion_style));
},
)
.await;
async fn assert_preview_edits(
text: &str,
edits: Vec<(Range<Point>, &str)>,
include_deletions: bool,
cx: &mut TestAppContext,
assert_fn: impl Fn(HighlightedText),
) {
let buffer = cx.new(|cx| Buffer::local(text, cx).with_language(rust_lang(), cx));
let edits = buffer.read_with(cx, |buffer, _| {
edits
.into_iter()
.map(|(range, text)| {
(
buffer.anchor_before(range.start)..buffer.anchor_after(range.end),
text.into(),
)
})
.collect::<Arc<[_]>>()
});
let edit_preview = buffer
.read_with(cx, |buffer, cx| buffer.preview_edits(edits.clone(), cx))
.await;
let highlighted_edits = cx.read(|cx| {
edit_preview.highlight_edits(&buffer.read(cx).snapshot(), &edits, include_deletions, cx)
});
assert_fn(highlighted_edits);
}
}
#[gpui::test(iterations = 100)]
fn test_random_collaboration(cx: &mut App, mut rng: StdRng) {
let min_peers = env::var("MIN_PEERS")
.map(|i| i.parse().expect("invalid `MIN_PEERS` variable"))
.unwrap_or(1);
let max_peers = env::var("MAX_PEERS")
.map(|i| i.parse().expect("invalid `MAX_PEERS` variable"))
.unwrap_or(5);
let operations = env::var("OPERATIONS")
.map(|i| i.parse().expect("invalid `OPERATIONS` variable"))
.unwrap_or(10);
let base_text_len = rng.random_range(0..10);
let base_text = RandomCharIter::new(&mut rng)
.take(base_text_len)
.collect::<String>();
let mut replica_ids = Vec::new();
let mut buffers = Vec::new();
let network = Arc::new(Mutex::new(Network::new(rng.clone())));
let base_buffer = cx.new(|cx| Buffer::local(base_text.as_str(), cx));
for i in 0..rng.random_range(min_peers..=max_peers) {
let buffer = cx.new(|cx| {
let state = base_buffer.read(cx).to_proto(cx);
let ops = cx
.foreground_executor()
.block_on(base_buffer.read(cx).serialize_ops(None, cx));
let mut buffer = Buffer::from_proto(
ReplicaId::new(i as u16),
Capability::ReadWrite,
state,
None,
cx,
)
.unwrap();
buffer.apply_ops(
ops.into_iter()
.map(|op| proto::deserialize_operation(op).unwrap()),
cx,
);
buffer.set_group_interval(Duration::from_millis(rng.random_range(0..=200)));
let network = network.clone();
cx.subscribe(&cx.entity(), move |buffer, _, event, _| {
if let BufferEvent::Operation {
operation,
is_local: true,
} = event
{
network.lock().broadcast(
buffer.replica_id(),
vec![proto::serialize_operation(operation)],
);
}
})
.detach();
buffer
});
buffers.push(buffer);
replica_ids.push(ReplicaId::new(i as u16));
network.lock().add_peer(ReplicaId::new(i as u16));
log::info!("Adding initial peer with replica id {:?}", replica_ids[i]);
}
log::info!("initial text: {:?}", base_text);
let mut now = Instant::now();
let mut mutation_count = operations;
let mut next_diagnostic_id = 0;
let mut active_selections = BTreeMap::default();
loop {
let replica_index = rng.random_range(0..replica_ids.len());
let replica_id = replica_ids[replica_index];
let buffer = &mut buffers[replica_index];
let mut new_buffer = None;
match rng.random_range(0..100) {
0..=29 if mutation_count != 0 => {
buffer.update(cx, |buffer, cx| {
buffer.start_transaction_at(now);
buffer.randomly_edit(&mut rng, 5, cx);
buffer.end_transaction_at(now, cx);
log::info!("buffer {:?} text: {:?}", buffer.replica_id(), buffer.text());
});
mutation_count -= 1;
}
30..=39 if mutation_count != 0 => {
buffer.update(cx, |buffer, cx| {
if rng.random_bool(0.2) {
log::info!("peer {:?} clearing active selections", replica_id);
active_selections.remove(&replica_id);
buffer.remove_active_selections(cx);
} else {
let mut selections = Vec::new();
for id in 0..rng.random_range(1..=5) {
let range = buffer.random_byte_range(0, &mut rng);
selections.push(Selection {
id,
start: buffer.anchor_before(range.start),
end: buffer.anchor_before(range.end),
reversed: false,
goal: SelectionGoal::None,
});
}
let selections: Arc<[Selection<Anchor>]> = selections.into();
log::info!(
"peer {:?} setting active selections: {:?}",
replica_id,
selections
);
active_selections.insert(replica_id, selections.clone());
buffer.set_active_selections(selections, false, Default::default(), cx);
}
});
mutation_count -= 1;
}
40..=49 if mutation_count != 0 && replica_id == ReplicaId::REMOTE_SERVER => {
let entry_count = rng.random_range(1..=5);
buffer.update(cx, |buffer, cx| {
let diagnostics = DiagnosticSet::new(
(0..entry_count).map(|_| {
let range = buffer.random_byte_range(0, &mut rng);
let range = range.to_point_utf16(buffer);
let range = range.start..range.end;
DiagnosticEntry::new(
range,
Diagnostic {
message: post_inc(&mut next_diagnostic_id).to_string().into(),
..Default::default()
},
)
}),
buffer,
);
log::info!(
"peer {:?} setting diagnostics: {:?}",
replica_id,
diagnostics
);
buffer.update_diagnostics(LanguageServerId(0), diagnostics, cx);
});
mutation_count -= 1;
}
50..=59 if replica_ids.len() < max_peers => {
let old_buffer_state = buffer.read(cx).to_proto(cx);
let old_buffer_ops = cx
.foreground_executor()
.block_on(buffer.read(cx).serialize_ops(None, cx));
let new_replica_id = (0..=replica_ids.len() as u16)
.map(ReplicaId::new)
.filter(|replica_id| *replica_id != buffer.read(cx).replica_id())
.choose(&mut rng)
.unwrap();
log::info!(
"Adding new replica {:?} (replicating from {:?})",
new_replica_id,
replica_id
);
new_buffer = Some(cx.new(|cx| {
let mut new_buffer = Buffer::from_proto(
new_replica_id,
Capability::ReadWrite,
old_buffer_state,
None,
cx,
)
.unwrap();
new_buffer.apply_ops(
old_buffer_ops
.into_iter()
.map(|op| deserialize_operation(op).unwrap()),
cx,
);
log::info!(
"New replica {:?} text: {:?}",
new_buffer.replica_id(),
new_buffer.text()
);
new_buffer.set_group_interval(Duration::from_millis(rng.random_range(0..=200)));
let network = network.clone();
cx.subscribe(&cx.entity(), move |buffer, _, event, _| {
if let BufferEvent::Operation {
operation,
is_local: true,
} = event
{
network.lock().broadcast(
buffer.replica_id(),
vec![proto::serialize_operation(operation)],
);
}
})
.detach();
new_buffer
}));
network.lock().replicate(replica_id, new_replica_id);
if new_replica_id.as_u16() as usize == replica_ids.len() {
replica_ids.push(new_replica_id);
} else {
let new_buffer = new_buffer.take().unwrap();
while network.lock().has_unreceived(new_replica_id) {
let ops = network
.lock()
.receive(new_replica_id)
.into_iter()
.map(|op| proto::deserialize_operation(op).unwrap());
if ops.len() > 0 {
log::info!(
"peer {:?} (version: {:?}) applying {} ops from the network. {:?}",
new_replica_id,
buffer.read(cx).version(),
ops.len(),
ops
);
new_buffer.update(cx, |new_buffer, cx| {
new_buffer.apply_ops(ops, cx);
});
}
}
buffers[new_replica_id.as_u16() as usize] = new_buffer;
}
}
60..=69 if mutation_count != 0 => {
buffer.update(cx, |buffer, cx| {
buffer.randomly_undo_redo(&mut rng, cx);
log::info!("buffer {:?} text: {:?}", buffer.replica_id(), buffer.text());
});
mutation_count -= 1;
}
_ if network.lock().has_unreceived(replica_id) => {
let ops = network
.lock()
.receive(replica_id)
.into_iter()
.map(|op| proto::deserialize_operation(op).unwrap());
if ops.len() > 0 {
log::info!(
"peer {:?} (version: {:?}) applying {} ops from the network. {:?}",
replica_id,
buffer.read(cx).version(),
ops.len(),
ops
);
buffer.update(cx, |buffer, cx| buffer.apply_ops(ops, cx));
}
}
_ => {}
}
now += Duration::from_millis(rng.random_range(0..=200));
buffers.extend(new_buffer);
for buffer in &buffers {
buffer.read(cx).check_invariants();
}
if mutation_count == 0 && network.lock().is_idle() {
break;
}
}
let first_buffer = buffers[0].read(cx).snapshot();
for buffer in &buffers[1..] {
let buffer = buffer.read(cx).snapshot();
assert_eq!(
buffer.version(),
first_buffer.version(),
"Replica {:?} version != Replica 0 version",
buffer.replica_id()
);
assert_eq!(
buffer.text(),
first_buffer.text(),
"Replica {:?} text != Replica 0 text",
buffer.replica_id()
);
assert_eq!(
buffer
.diagnostics_in_range::<_, usize>(0..buffer.len(), false)
.collect::<Vec<_>>(),
first_buffer
.diagnostics_in_range::<_, usize>(0..first_buffer.len(), false)
.collect::<Vec<_>>(),
"Replica {:?} diagnostics != Replica 0 diagnostics",
buffer.replica_id()
);
}
for buffer in &buffers {
let buffer = buffer.read(cx).snapshot();
let actual_remote_selections = buffer
.selections_in_range(Anchor::min_max_range_for_buffer(buffer.remote_id()), false)
.map(|(replica_id, _, _, selections)| (replica_id, selections.collect::<Vec<_>>()))
.collect::<Vec<_>>();
let expected_remote_selections = active_selections
.iter()
.filter(|(replica_id, _)| **replica_id != buffer.replica_id())
.map(|(replica_id, selections)| (*replica_id, selections.iter().collect::<Vec<_>>()))
.collect::<Vec<_>>();
assert_eq!(
actual_remote_selections,
expected_remote_selections,
"Replica {:?} remote selections != expected selections",
buffer.replica_id()
);
}
}
#[test]
fn test_contiguous_ranges() {
assert_eq!(
contiguous_ranges([1, 2, 3, 5, 6, 9, 10, 11, 12].into_iter(), 100).collect::<Vec<_>>(),
&[1..4, 5..7, 9..13]
);
// Respects the `max_len` parameter
assert_eq!(
contiguous_ranges(
[2, 3, 4, 5, 6, 7, 8, 9, 23, 24, 25, 26, 30, 31].into_iter(),
3
)
.collect::<Vec<_>>(),
&[2..5, 5..8, 8..10, 23..26, 26..27, 30..32],
);
}
#[gpui::test]
fn test_insertion_after_deletion(cx: &mut gpui::App) {
let buffer = cx.new(|cx| Buffer::local("struct Foo {\n \n}", cx));
buffer.update(cx, |buffer, cx| {
let mut anchor = buffer.anchor_after(17);
buffer.edit([(12..18, "")], None, cx);
let snapshot = buffer.snapshot();
assert_eq!(snapshot.text(), "struct Foo {}");
if !anchor.is_valid(&snapshot) {
anchor = snapshot.anchor_after(snapshot.offset_for_anchor(&anchor));
}
buffer.edit([(anchor..anchor, "\n")], None, cx);
buffer.edit([(anchor..anchor, "field1:")], None, cx);
buffer.edit([(anchor..anchor, " i32,")], None, cx);
let snapshot = buffer.snapshot();
assert_eq!(snapshot.text(), "struct Foo {\nfield1: i32,}");
})
}
#[gpui::test(iterations = 500)]
fn test_trailing_whitespace_ranges(mut rng: StdRng) {
// Generate a random multi-line string containing
// some lines with trailing whitespace.
let mut text = String::new();
for _ in 0..rng.random_range(0..16) {
for _ in 0..rng.random_range(0..36) {
text.push(match rng.random_range(0..10) {
0..=1 => ' ',
3 => '\t',
_ => rng.random_range('a'..='z'),
});
}
text.push('\n');
}
match rng.random_range(0..10) {
// sometimes remove the last newline
0..=1 => drop(text.pop()), //
// sometimes add extra newlines
2..=3 => text.push_str(&"\n".repeat(rng.random_range(1..5))),
_ => {}
}
let rope = Rope::from(text.as_str());
let actual_ranges = trailing_whitespace_ranges(&rope, None);
let expected_ranges = TRAILING_WHITESPACE_REGEX
.find_iter(&text)
.map(|m| m.range())
.collect::<Vec<_>>();
assert_eq!(
actual_ranges,
expected_ranges,
"wrong ranges for text lines:\n{:?}",
text.split('\n').collect::<Vec<_>>()
);
}
#[gpui::test(iterations = 500)]
fn test_trailing_whitespace_ranges_in_rows(mut rng: StdRng) {
let mut text = String::new();
for _ in 0..rng.random_range(0..16) {
for _ in 0..rng.random_range(0..36) {
text.push(match rng.random_range(0..10) {
0..=1 => ' ',
3 => '\t',
_ => rng.random_range('a'..='z'),
});
}
text.push('\n');
}
match rng.random_range(0..10) {
0..=1 => drop(text.pop()),
2..=3 => text.push_str(&"\n".repeat(rng.random_range(1..5))),
_ => {}
}
let rope = Rope::from(text.as_str());
let all_ranges = trailing_whitespace_ranges(&rope, None);
let lines = text.split('\n').collect::<Vec<_>>();
// A range covering every row must reproduce the unfiltered full scan exactly.
assert_eq!(
trailing_whitespace_ranges(&rope, Some(&[0..u32::MAX])),
all_ranges,
"full-coverage mismatch for lines:\n{lines:?}",
);
// For a random (possibly gappy) subset of rows, the filtered variant must equal
// the full scan restricted to ranges whose line is in the subset.
let max_row = rope.max_point().row;
let mut row_ranges = Vec::new();
let mut row = 0;
while row <= max_row {
let span = rng.random_range(0..=3);
if span > 0 {
let end = (row + span).min(max_row + 1);
row_ranges.push(row..end);
row = end;
}
row += 1;
}
let expected = all_ranges
.iter()
.filter(|range| {
let row = rope.offset_to_point(range.start).row;
row_ranges.iter().any(|r| r.contains(&row))
})
.cloned()
.collect::<Vec<_>>();
assert_eq!(
trailing_whitespace_ranges(&rope, Some(&row_ranges)),
expected,
"subset mismatch for ranges {row_ranges:?} and lines:\n{lines:?}",
);
}
#[gpui::test]
async fn test_trailing_whitespace_in_ranges(cx: &mut gpui::TestAppContext) {
// line 0: "zero" (no trailing whitespace)
// line 1: "one " (2 trailing spaces)
// line 2: "two" (no trailing whitespace)
// line 3: "three " (3 trailing spaces)
// line 4: "four" (no trailing whitespace)
// line 5: "five " (4 trailing spaces)
let text = ["zero", "one ", "two", "three ", "four", "five "].join("\n");
let buffer = cx.new(|cx| Buffer::local(text, cx));
// Only rows 1 and 5 are modified, so only those lines get cleaned; line 3 stays untouched.
let modified_rows = [1u32..2, 5..6];
let diff = buffer
.update(cx, |buffer, cx| {
buffer.remove_trailing_whitespace(Some(&modified_rows), cx)
})
.await;
buffer.update(cx, |buffer, cx| {
buffer.apply_diff(diff, cx);
assert_eq!(
buffer.text(),
["zero", "one", "two", "three ", "four", "five"].join("\n")
);
});
}
#[gpui::test]
async fn test_trailing_whitespace_empty_ranges(cx: &mut gpui::TestAppContext) {
let text = ["zero", "one ", "two "].join("\n");
let buffer = cx.new(|cx| Buffer::local(text.clone(), cx));
let diff = buffer
.update(cx, |buffer, cx| {
buffer.remove_trailing_whitespace(Some(&[]), cx)
})
.await;
buffer.update(cx, |buffer, cx| {
buffer.apply_diff(diff, cx);
assert_eq!(buffer.text(), text);
});
}
#[gpui::test]
async fn test_final_newline_modified_last_line(cx: &mut gpui::TestAppContext) {
// No final newline; the modified range (rows 0..3) includes the last line (row 2).
let text = "line0\nline1\nline2";
let buffer = cx.new(|cx| Buffer::local(text, cx));
buffer.update(cx, |buffer, cx| {
let diff = buffer.ensure_final_newline(Some(&[0u32..3]));
buffer.apply_diff(diff, cx);
assert_eq!(buffer.text(), "line0\nline1\nline2\n");
});
}
#[gpui::test]
async fn test_final_newline_unmodified_last_line(cx: &mut gpui::TestAppContext) {
// No final newline; the modified range (rows 0..2) excludes the last line (row 2), so nothing changes.
let text = "line0\nline1\nline2";
let buffer = cx.new(|cx| Buffer::local(text, cx));
buffer.update(cx, |buffer, cx| {
let diff = buffer.ensure_final_newline(Some(&[0u32..2]));
buffer.apply_diff(diff, cx);
assert_eq!(buffer.text(), "line0\nline1\nline2");
});
}
// An empty last line (file already ends with a newline) is left untouched, even with extra
// trailing blank lines. With `None` these would collapse; scoped to rows they must not, to
// avoid deleting unselected rows.
#[gpui::test]
async fn test_final_newline_does_not_collapse_trailing_blank_lines(cx: &mut gpui::TestAppContext) {
let text = "line0\nline1\n\n";
let buffer = cx.new(|cx| Buffer::local(text, cx));
buffer.update(cx, |buffer, cx| {
let diff = buffer.ensure_final_newline(Some(&[0u32..4]));
buffer.apply_diff(diff, cx);
assert_eq!(buffer.text(), "line0\nline1\n\n");
});
}
// When scoped to rows, only a newline is inserted; unlike the `None` (whole-buffer) case, it
// does not trim trailing whitespace on the last line.
#[gpui::test]
async fn test_final_newline_in_range_only_inserts(cx: &mut gpui::TestAppContext) {
let text = "line0\nline1 ";
let buffer = cx.new(|cx| Buffer::local(text, cx));
buffer.update(cx, |buffer, cx| {
let diff = buffer.ensure_final_newline(Some(&[0u32..2]));
buffer.apply_diff(diff, cx);
assert_eq!(buffer.text(), "line0\nline1 \n");
});
}
#[gpui::test]
async fn test_final_newline_whole_buffer(cx: &mut gpui::TestAppContext) {
// (input, expected) pairs for the whole-buffer (`None`) case.
let cases = [
// Content without a trailing newline gets exactly one appended.
("line0\nline1", "line0\nline1\n"),
// A buffer already ending in a single newline is left untouched.
("line0\nline1\n", "line0\nline1\n"),
// Trailing blank lines and whitespace at the end of the file collapse to one newline.
("line0\nline1\n\n\n", "line0\nline1\n"),
("line0\nline1 \n ", "line0\nline1\n"),
// An empty buffer stays empty.
("", ""),
];
for (input, expected) in cases {
let buffer = cx.new(|cx| Buffer::local(input, cx));
buffer.update(cx, |buffer, cx| {
let diff = buffer.ensure_final_newline(None);
buffer.apply_diff(diff, cx);
assert_eq!(buffer.text(), expected, "wrong result for input {input:?}");
});
}
}
#[gpui::test]
async fn test_trailing_whitespace_in_ranges_crlf(cx: &mut gpui::TestAppContext) {
let text = "zero\r\none \r\ntwo\r\nthree \r\nfour\r\nfive ";
let buffer = cx.new(|cx| {
let buffer = Buffer::local(text, cx);
assert_eq!(buffer.line_ending(), LineEnding::Windows);
buffer
});
let modified_rows = [1u32..2, 5..6];
let diff = buffer
.update(cx, |buffer, cx| {
buffer.remove_trailing_whitespace(Some(&modified_rows), cx)
})
.await;
buffer.update(cx, |buffer, cx| {
buffer.apply_diff(diff, cx);
assert_eq!(buffer.text(), "zero\none\ntwo\nthree \nfour\nfive");
assert_eq!(buffer.line_ending(), LineEnding::Windows);
});
}
#[gpui::test]
async fn test_final_newline_in_range_crlf(cx: &mut gpui::TestAppContext) {
let text = "line0\r\nline1\r\nline2";
let buffer = cx.new(|cx| {
let buffer = Buffer::local(text, cx);
assert_eq!(buffer.line_ending(), LineEnding::Windows);
buffer
});
buffer.update(cx, |buffer, cx| {
let diff = buffer.ensure_final_newline(Some(&[0u32..3]));
buffer.apply_diff(diff, cx);
assert_eq!(buffer.text(), "line0\nline1\nline2\n");
assert_eq!(buffer.line_ending(), LineEnding::Windows);
});
}
#[gpui::test]
fn test_words_in_range(cx: &mut gpui::App) {
init_settings(cx, |_| {});
// The first line are words excluded from the results with heuristics, we do not expect them in the test assertions.
let contents = r#"
0_isize 123 3.4 4
let word=öäpple.bar你 Öäpple word2-öÄpPlE-Pizza-word ÖÄPPLE word
"#;
let buffer = cx.new(|cx| {
let buffer = Buffer::local(contents, cx).with_language(rust_lang(), cx);
assert_eq!(buffer.text(), contents);
buffer.check_invariants();
buffer
});
buffer.update(cx, |buffer, _| {
let snapshot = buffer.snapshot();
assert_eq!(
BTreeSet::from_iter(["Pizza".to_string()]),
snapshot
.words_in_range(WordsQuery {
fuzzy_contents: Some("piz"),
skip_digits: true,
range: 0..snapshot.len(),
})
.into_keys()
.collect::<BTreeSet<_>>()
);
assert_eq!(
BTreeSet::from_iter([
"öäpple".to_string(),
"Öäpple".to_string(),
"öÄpPlE".to_string(),
"ÖÄPPLE".to_string(),
]),
snapshot
.words_in_range(WordsQuery {
fuzzy_contents: Some("öp"),
skip_digits: true,
range: 0..snapshot.len(),
})
.into_keys()
.collect::<BTreeSet<_>>()
);
assert_eq!(
BTreeSet::from_iter([
"öÄpPlE".to_string(),
"Öäpple".to_string(),
"ÖÄPPLE".to_string(),
"öäpple".to_string(),
]),
snapshot
.words_in_range(WordsQuery {
fuzzy_contents: Some("öÄ"),
skip_digits: true,
range: 0..snapshot.len(),
})
.into_keys()
.collect::<BTreeSet<_>>()
);
assert_eq!(
BTreeSet::default(),
snapshot
.words_in_range(WordsQuery {
fuzzy_contents: Some("öÄ好"),
skip_digits: true,
range: 0..snapshot.len(),
})
.into_keys()
.collect::<BTreeSet<_>>()
);
assert_eq!(
BTreeSet::from_iter(["bar你".to_string(),]),
snapshot
.words_in_range(WordsQuery {
fuzzy_contents: Some("你"),
skip_digits: true,
range: 0..snapshot.len(),
})
.into_keys()
.collect::<BTreeSet<_>>()
);
assert_eq!(
BTreeSet::default(),
snapshot
.words_in_range(WordsQuery {
fuzzy_contents: Some(""),
skip_digits: true,
range: 0..snapshot.len(),
},)
.into_keys()
.collect::<BTreeSet<_>>()
);
assert_eq!(
BTreeSet::from_iter([
"bar你".to_string(),
"öÄpPlE".to_string(),
"Öäpple".to_string(),
"ÖÄPPLE".to_string(),
"öäpple".to_string(),
"let".to_string(),
"Pizza".to_string(),
"word".to_string(),
"word2".to_string(),
]),
snapshot
.words_in_range(WordsQuery {
fuzzy_contents: None,
skip_digits: true,
range: 0..snapshot.len(),
})
.into_keys()
.collect::<BTreeSet<_>>()
);
assert_eq!(
BTreeSet::from_iter([
"0_isize".to_string(),
"123".to_string(),
"3".to_string(),
"4".to_string(),
"bar你".to_string(),
"öÄpPlE".to_string(),
"Öäpple".to_string(),
"ÖÄPPLE".to_string(),
"öäpple".to_string(),
"let".to_string(),
"Pizza".to_string(),
"word".to_string(),
"word2".to_string(),
]),
snapshot
.words_in_range(WordsQuery {
fuzzy_contents: None,
skip_digits: false,
range: 0..snapshot.len(),
})
.into_keys()
.collect::<BTreeSet<_>>()
);
});
}
fn ruby_lang() -> Language {
Language::new(
LanguageConfig {
name: "Ruby".into(),
matcher: (LanguageMatcher {
path_suffixes: vec!["rb".into()],
..Default::default()
})
.into(),
line_comments: vec!["# ".into()],
..Default::default()
},
Some(tree_sitter_ruby::LANGUAGE.into()),
)
.with_indents_query(
r#"
(class "end" @end) @indent
(method "end" @end) @indent
(rescue) @outdent
(then) @indent
"#,
)
.unwrap()
}
fn html_lang() -> Language {
Language::new(
LanguageConfig {
name: LanguageName::new_static("HTML"),
block_comment: Some(BlockCommentConfig {
start: "<!--".into(),
prefix: "".into(),
end: "-->".into(),
tab_size: 0,
}),
..Default::default()
},
Some(tree_sitter_html::LANGUAGE.into()),
)
.with_indents_query(
"
(element
(start_tag) @start
(end_tag)? @end) @indent
",
)
.unwrap()
.with_injection_query(
r#"
(script_element
(raw_text) @injection.content
(#set! injection.language "javascript"))
"#,
)
.unwrap()
}
fn erb_lang() -> Language {
Language::new(
LanguageConfig {
name: "HTML+ERB".into(),
matcher: (LanguageMatcher {
path_suffixes: vec!["erb".into()],
..Default::default()
})
.into(),
block_comment: Some(BlockCommentConfig {
start: "<%#".into(),
prefix: "".into(),
end: "%>".into(),
tab_size: 0,
}),
..Default::default()
},
Some(tree_sitter_embedded_template::LANGUAGE.into()),
)
.with_injection_query(
r#"
(
(code) @content
(#set! "language" "ruby")
(#set! "combined")
)
(
(content) @content
(#set! "language" "html")
(#set! "combined")
)
"#,
)
.unwrap()
}
fn color_index_for_open(
matches: &HashMap<Range<BufferRow>, Vec<BracketMatch>>,
open_offset: usize,
) -> Option<usize> {
matches
.values()
.flatten()
.find(|bracket_match| bracket_match.open_range.start == open_offset)
.and_then(|bracket_match| bracket_match.color_index)
}
fn javascript_lang() -> Language {
Language::new(
LanguageConfig {
name: "JavaScript".into(),
..Default::default()
},
Some(tree_sitter_typescript::LANGUAGE_TSX.into()),
)
.with_brackets_query(
r#"
("{" @open "}" @close)
("(" @open ")" @close)
"#,
)
.unwrap()
.with_indents_query(
r#"
(object "}" @end) @indent
"#,
)
.unwrap()
}
fn c_lang() -> Arc<Language> {
Arc::new(
Language::new(
LanguageConfig {
name: "C".into(),
..Default::default()
},
Some(tree_sitter_c::LANGUAGE.into()),
)
.with_outline_query(include_str!("../../grammars/src/c/outline.scm"))
.unwrap()
.with_brackets_query(include_str!("../../grammars/src/c/brackets.scm"))
.unwrap(),
)
}
#[track_caller]
fn assert_has_syntax_errors(snapshot: &BufferSnapshot) {
assert!(
snapshot
.syntax
.layers_for_range(0..snapshot.len(), &snapshot.text, true)
.any(|layer| layer.node().has_error()),
"the fixture should parse with syntax errors"
);
}
/// Pairs bracket characters anywhere in `text`, including inside string literals.
#[track_caller]
fn stack_paired_brackets(
text: &str,
open: char,
close: char,
) -> (Vec<(Range<usize>, Range<usize>)>, Vec<Range<usize>>) {
let mut open_offsets = Vec::new();
let mut pairs = Vec::new();
for (offset, character) in text.char_indices() {
if character == open {
open_offsets.push(offset);
} else if character == close {
let Some(open_offset) = open_offsets.pop() else {
panic!("unexpected closing {close} at offset {offset}");
};
pairs.push((open_offset..open_offset + 1, offset..offset + 1));
}
}
let unmatched_opens = open_offsets
.into_iter()
.map(|offset| offset..offset + 1)
.collect::<Vec<_>>();
(pairs, unmatched_opens)
}
pub fn markdown_inline_lang() -> Language {
Language::new(
LanguageConfig {
name: "Markdown-Inline".into(),
hidden: true,
..LanguageConfig::default()
},
Some(tree_sitter_md::INLINE_LANGUAGE.into()),
)
.with_highlights_query("(emphasis) @emphasis")
.unwrap()
}
fn get_tree_sexp(buffer: &Entity<Buffer>, cx: &mut gpui::TestAppContext) -> String {
buffer.update(cx, |buffer, _| {
let snapshot = buffer.snapshot();
let layers = snapshot.syntax.layers(buffer.as_text_snapshot());
layers[0].node().to_sexp()
})
}
fn typescript_lang_with_indents() -> Arc<Language> {
Arc::new(
Language::new(
LanguageConfig {
name: "TypeScript".into(),
..Default::default()
},
Some(tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into()),
)
.with_brackets_query(r#"("{" @open "}" @close) ("(" @open ")" @close)"#)
.unwrap()
.with_indents_query(include_str!("../../grammars/src/typescript/indents.scm"))
.unwrap(),
)
}
fn tsx_lang_with_indents() -> Arc<Language> {
Arc::new(
Language::new(
LanguageConfig {
name: "TSX".into(),
..Default::default()
},
Some(tree_sitter_typescript::LANGUAGE_TSX.into()),
)
.with_brackets_query(r#"("{" @open "}" @close) ("(" @open ")" @close)"#)
.unwrap()
.with_indents_query(include_str!("../../grammars/src/tsx/indents.scm"))
.unwrap(),
)
}
#[gpui::test]
fn test_autoindent_typescript_braceless_control_flow(cx: &mut App) {
init_settings(cx, |_| {});
cx.new(|cx| {
for lang in [typescript_lang_with_indents(), tsx_lang_with_indents()] {
let mut indent = |header: &str, header_len: usize, body: &str| {
let mut buffer = Buffer::local(header, cx).with_language(lang.clone(), cx);
buffer.edit(
[(header_len..header_len, body)],
Some(AutoindentMode::EachLine),
cx,
);
buffer.text()
};
// A braceless body is indented under its `if`/`for`/`while`.
assert_eq!(indent("if (true)", 9, "\nx()"), "if (true)\n x()");
assert_eq!(indent("for (;;)", 8, "\nx()"), "for (;;)\n x()");
assert_eq!(indent("while (true)", 12, "\nx()"), "while (true)\n x()");
assert_eq!(
indent("for (const a of b)", 18, "\nx()"),
"for (const a of b)\n x()"
);
// The statement after a braceless body returns to the outer indent.
assert_eq!(
indent("if (true)\n x()", 17, "\ny()"),
"if (true)\n x()\ny()"
);
// A `{}` block keeps its brace unindented (Allman style), leaving the
// block rule to indent the contents. Regression guard for #24976.
assert_eq!(indent("if (true)", 9, "\n{}"), "if (true)\n{}");
assert_eq!(indent("for (;;)", 8, "\n{}"), "for (;;)\n{}");
assert_eq!(indent("while (true)", 12, "\n{}"), "while (true)\n{}");
// K&R braced bodies indent their contents once.
assert_eq!(
indent("if (true) {\n}", 11, "\nx()"),
"if (true) {\n x()\n}"
);
// A braceless `else` body is indented under the `else`.
assert_eq!(
indent("if (true)\n x()\nelse", 22, "\ny()"),
"if (true)\n x()\nelse\n y()"
);
}
Buffer::local("", cx)
});
}
#[gpui::test]
fn test_completion_triggers_across_language_servers(cx: &mut TestAppContext) {
cx.update(|cx| init_settings(cx, |_| {}));
let buffer = cx.new(|cx| Buffer::local("", cx));
let replica = cx.new(|cx| {
Buffer::from_proto(
ReplicaId::new(1),
Capability::ReadWrite,
buffer.read(cx).to_proto(cx),
None,
cx,
)
.unwrap()
});
replica.update(cx, |_, cx| {
cx.subscribe(&buffer, |this, _, event, cx| {
if let BufferEvent::Operation {
operation,
is_local: true,
} = event
{
this.apply_ops([operation.clone()], cx);
}
})
.detach();
});
let server_a = LanguageServerId(1);
let server_b = LanguageServerId(2);
let triggers = |buffer: &Entity<Buffer>, cx: &mut TestAppContext| {
buffer.read_with(cx, |buffer, _| buffer.completion_triggers().clone())
};
buffer.update(cx, |buffer, cx| {
buffer.set_completion_triggers(server_a, BTreeSet::from_iter([".".to_string()]), cx);
buffer.set_completion_triggers(server_b, BTreeSet::from_iter([":".to_string()]), cx);
});
let expected = BTreeSet::from_iter([".".to_string(), ":".to_string()]);
assert_eq!(
triggers(&buffer, cx),
expected,
"expected triggers from both servers to be combined",
);
assert_eq!(
triggers(&replica, cx),
expected,
"expected the replica to combine triggers from both servers",
);
buffer.update(cx, |buffer, cx| {
buffer.set_completion_triggers(server_a, BTreeSet::from_iter([",".to_string()]), cx);
});
let expected = BTreeSet::from_iter([",".to_string(), ":".to_string()]);
assert_eq!(
triggers(&buffer, cx),
expected,
"expected replaced triggers to not linger in the combined set",
);
assert_eq!(
triggers(&replica, cx),
expected,
"expected the replica to not keep replaced triggers in the combined set",
);
buffer.update(cx, |buffer, cx| {
buffer.set_completion_triggers(server_a, BTreeSet::new(), cx);
});
let expected = BTreeSet::from_iter([":".to_string()]);
assert_eq!(
triggers(&buffer, cx),
expected,
"expected the other server's triggers to survive clearing one server's triggers",
);
assert_eq!(
triggers(&replica, cx),
expected,
"expected the replica to keep the other server's triggers",
);
buffer.update(cx, |buffer, cx| {
buffer.set_completion_triggers(server_b, BTreeSet::new(), cx);
});
assert_eq!(
triggers(&buffer, cx),
BTreeSet::new(),
"expected no triggers after clearing all servers",
);
assert_eq!(
triggers(&replica, cx),
BTreeSet::new(),
"expected no triggers on the replica after clearing all servers",
);
}
// Assert that the enclosing bracket ranges around the selection match the pairs indicated by the marked text in `range_markers`
#[track_caller]
fn assert_bracket_pairs(
selection_text: &'static str,
bracket_pair_texts: Vec<&'static str>,
language: Arc<Language>,
cx: &mut App,
) {
let (expected_text, selection_ranges) = marked_text_ranges(selection_text, false);
let buffer = cx.new(|cx| Buffer::local(expected_text.clone(), cx).with_language(language, cx));
let buffer = buffer.update(cx, |buffer, _cx| buffer.snapshot());
let selection_range = selection_ranges[0].clone();
let bracket_pairs = bracket_pair_texts
.into_iter()
.map(|pair_text| {
let (bracket_text, ranges) = marked_text_ranges(pair_text, false);
assert_eq!(bracket_text, expected_text);
(ranges[0].clone(), ranges[1].clone())
})
.collect::<Vec<_>>();
assert_set_eq!(
buffer
.bracket_ranges(selection_range)
.map(|pair| (pair.open_range, pair.close_range))
.collect::<Vec<_>>(),
bracket_pairs
);
}
fn init_settings(cx: &mut App, f: fn(&mut AllLanguageSettingsContent)) {
let settings_store = SettingsStore::test(cx);
cx.set_global(settings_store);
cx.update_global::<SettingsStore, _>(|settings, cx| {
settings.update_user_settings(cx, |content| f(&mut content.project.all_languages));
});
}
#[gpui::test]
fn test_settings_changed_event(cx: &mut TestAppContext) {
cx.update(|cx| init_settings(cx, |_| {}));
let buffer = cx.new(|cx| Buffer::local("one\ntwo\nthree\n", cx));
let settings_change_count = std::rc::Rc::new(std::cell::Cell::new(0));
let subscription = cx.update(|cx| {
cx.subscribe(&buffer, {
let settings_change_count = settings_change_count.clone();
move |_, event, _| {
if let BufferEvent::SettingsChanged = event {
settings_change_count.set(settings_change_count.get() + 1);
}
}
})
});
assert_eq!(
buffer.read_with(cx, |buffer, cx| {
crate::language_settings::LanguageSettings::for_buffer(buffer, cx)
.tab_size
.get()
}),
4
);
assert_eq!(settings_change_count.get(), 0);
cx.update(|cx| {
cx.update_global::<SettingsStore, _>(|settings, cx| {
settings.update_user_settings(cx, |content| {
content.project.all_languages.defaults.tab_size = Some(3.try_into().unwrap());
});
});
});
assert_eq!(settings_change_count.get(), 1);
assert_eq!(
buffer.read_with(cx, |buffer, cx| {
crate::language_settings::LanguageSettings::for_buffer(buffer, cx)
.tab_size
.get()
}),
3
);
cx.update(|cx| {
cx.update_global::<SettingsStore, _>(|settings, cx| {
settings.update_user_settings(cx, |content| {
content.project.all_languages.defaults.tab_size = Some(3.try_into().unwrap());
});
});
});
assert_eq!(
settings_change_count.get(),
1,
"a no-op settings update should not emit SettingsChanged"
);
drop(subscription);
}
#[gpui::test]
fn test_chunk_highlights_follow_edits_and_theme_changes(cx: &mut TestAppContext) {
cx.update(|cx| init_settings(cx, |_| {}));
let language = keyword_and_function_lang();
let theme_with_keyword = keyword_and_function_theme();
let theme_without_keyword =
SyntaxTheme::new([("function".to_string(), gpui::rgba(0x0000ffff).into())]);
language.set_theme(&theme_with_keyword);
let buffer = cx.new(|cx| {
let mut buffer = Buffer::local("fn main() {}", cx);
buffer.set_language(Some(language.clone()), cx);
buffer
});
cx.run_until_parked();
let highlighted_chunks = |snapshot: &BufferSnapshot| {
snapshot
.chunks(
0..snapshot.len(),
LanguageAwareStyling {
tree_sitter: true,
diagnostics: false,
},
)
.filter_map(|chunk| Some((chunk.text.to_string(), chunk.syntax_highlight_id?)))
.collect::<Vec<_>>()
};
let snapshot = buffer.read_with(cx, |buffer, _| buffer.snapshot());
let expected_with_keyword = vec![
(
"fn".to_string(),
theme_highlight_id(&theme_with_keyword, "keyword"),
),
(
"main".to_string(),
theme_highlight_id(&theme_with_keyword, "function"),
),
];
assert_eq!(highlighted_chunks(&snapshot), expected_with_keyword);
assert_eq!(
highlighted_chunks(&snapshot),
expected_with_keyword,
"repeated chunking of the same snapshot must return the same highlights"
);
language.set_theme(&theme_without_keyword);
assert_eq!(
highlighted_chunks(&snapshot),
vec![(
"main".to_string(),
theme_highlight_id(&theme_without_keyword, "function")
)],
"a theme change must invalidate highlights of an existing snapshot"
);
buffer.update(cx, |buffer, cx| {
buffer.edit([(3..7, "launch")], None, cx);
});
cx.run_until_parked();
let edited_snapshot = buffer.read_with(cx, |buffer, _| buffer.snapshot());
assert_eq!(
highlighted_chunks(&edited_snapshot),
vec![(
"launch".to_string(),
theme_highlight_id(&theme_without_keyword, "function")
)],
"an edit must invalidate the cached highlights"
);
}
#[gpui::test]
async fn test_snapshot_with_edits_refreshes_chunk_highlights(cx: &mut TestAppContext) {
cx.update(|cx| init_settings(cx, |_| {}));
let language = keyword_and_function_lang();
let theme = keyword_and_function_theme();
language.set_theme(&theme);
let keyword = theme_highlight_id(&theme, "keyword");
let function = theme_highlight_id(&theme, "function");
let row = "fn replacement() {}\n";
let text = row.repeat(MAX_ROWS_IN_A_CHUNK as usize + 1);
for original_text in ["", "fn original() {}"] {
let buffer =
cx.new(|cx| Buffer::local(original_text, cx).with_language(language.clone(), cx));
cx.run_until_parked();
let original_snapshot = buffer.read_with(cx, |buffer, _| buffer.snapshot());
let original_highlights =
merged_highlight_runs(&original_snapshot, 0..original_snapshot.len());
let edited = buffer
.update(cx, |buffer, cx| {
buffer.snapshot_with_edits([(0..buffer.len(), text.clone())], cx)
})
.await;
let snapshot = edited.snapshot();
let expected = vec![
("fn".to_string(), keyword),
("replacement".to_string(), function),
];
assert_eq!(merged_highlight_runs(snapshot, 0..row.len()), expected);
let last_row_start = row.len() * MAX_ROWS_IN_A_CHUNK as usize;
assert_eq!(
merged_highlight_runs(snapshot, last_row_start..snapshot.len()),
expected,
);
assert_eq!(
merged_highlight_runs(&original_snapshot, 0..original_snapshot.len()),
original_highlights,
);
}
}
#[gpui::test]
fn test_chunk_highlights_across_row_chunk_seeks(cx: &mut TestAppContext) {
cx.update(|cx| init_settings(cx, |_| {}));
let language = keyword_and_function_lang();
let theme = keyword_and_function_theme();
language.set_theme(&theme);
let short_row = "fn short() {}\n";
let last_row = "fn omega() {}";
let text = format!(
"{}{last_row}",
short_row.repeat(MAX_ROWS_IN_A_CHUNK as usize)
);
let buffer = cx.new(|cx| {
let mut buffer = Buffer::local(text, cx);
buffer.set_language(Some(language.clone()), cx);
buffer
});
cx.run_until_parked();
let snapshot = buffer.read_with(cx, |buffer, _| buffer.snapshot());
let keyword = theme_highlight_id(&theme, "keyword");
let function = theme_highlight_id(&theme, "function");
assert_eq!(
merged_highlight_runs(&snapshot, 0..short_row.len()),
vec![("fn".to_string(), keyword), ("short".to_string(), function)],
);
let last_row_start = short_row.len() * MAX_ROWS_IN_A_CHUNK as usize;
let expected_last_row_runs = vec![("fn".to_string(), keyword), ("omega".to_string(), function)];
assert_eq!(
merged_highlight_runs(&snapshot, last_row_start..snapshot.len()),
expected_last_row_runs,
);
let mut chunks = snapshot.chunks(
0..short_row.len(),
LanguageAwareStyling {
tree_sitter: true,
diagnostics: false,
},
);
chunks.seek(last_row_start..snapshot.len());
let mut runs_after_seek = Vec::new();
for chunk in chunks {
merge_highlighted_chunk(&mut runs_after_seek, &chunk);
}
assert_eq!(
runs_after_seek, expected_last_row_runs,
"seeking into another row chunk must refetch that chunk's highlights"
);
}
#[gpui::test]
fn test_oversized_chunks_bypass_the_highlight_cache(cx: &mut TestAppContext) {
if std::env::var_os("ZED_DISABLE_HIGHLIGHT_CACHE").is_some() {
return;
}
cx.update(|cx| init_settings(cx, |_| {}));
let language = keyword_and_function_lang();
let theme = keyword_and_function_theme();
language.set_theme(&theme);
let short_row = "fn short() {}\n";
let giant_row = format!(
"fn omega() {{}}{}",
" ".repeat(MAX_BYTES_TO_HIGHLIGHT_IN_A_CHUNK)
);
let text = format!(
"{}{giant_row}",
short_row.repeat(MAX_ROWS_IN_A_CHUNK as usize)
);
let buffer = cx.new(|cx| {
let mut buffer = Buffer::local(text, cx);
buffer.set_language(Some(language.clone()), cx);
buffer
});
cx.run_until_parked();
let snapshot = buffer.read_with(cx, |buffer, _| buffer.snapshot());
let keyword = theme_highlight_id(&theme, "keyword");
let function = theme_highlight_id(&theme, "function");
let expected_giant_row_runs =
vec![("fn".to_string(), keyword), ("omega".to_string(), function)];
let last_row_start = short_row.len() * MAX_ROWS_IN_A_CHUNK as usize;
assert_eq!(
snapshot.cached_highlight_runs(last_row_start..snapshot.len()),
None,
"chunks larger than the byte cap must not be cached"
);
assert!(
snapshot.cached_highlight_runs(0..short_row.len()).is_some(),
"chunks within the byte cap must still be cached"
);
assert_eq!(
snapshot.cached_highlight_runs(last_row_start + 1..last_row_start + 1),
Some(Vec::new()),
"an empty range must not compute captures for its oversized chunk"
);
assert_eq!(
merged_highlight_runs(&snapshot, last_row_start..snapshot.len()),
expected_giant_row_runs,
"oversized chunks must fall back to direct highlighting"
);
let mut chunks = snapshot.chunks(
0..short_row.len(),
LanguageAwareStyling {
tree_sitter: true,
diagnostics: false,
},
);
chunks.seek(last_row_start..snapshot.len());
let mut runs_after_seek = Vec::new();
for chunk in chunks {
merge_highlighted_chunk(&mut runs_after_seek, &chunk);
}
assert_eq!(
runs_after_seek, expected_giant_row_runs,
"seeking into an oversized chunk must fall back to direct highlighting"
);
}
#[gpui::test]
fn test_language_change_invalidates_cached_chunk_highlights(cx: &mut TestAppContext) {
cx.update(|cx| init_settings(cx, |_| {}));
let rust = keyword_and_function_lang();
let identifiers_only = Arc::new(
Language::new(
LanguageConfig {
name: "Identifiers".into(),
..LanguageConfig::default()
},
Some(tree_sitter_rust::LANGUAGE.into()),
)
.with_highlights_query("(identifier) @variable")
.unwrap(),
);
let theme = SyntaxTheme::new([
("keyword".to_string(), gpui::rgba(0xff0000ff).into()),
("function".to_string(), gpui::rgba(0x00ff00ff).into()),
("variable".to_string(), gpui::rgba(0x0000ffff).into()),
]);
rust.set_theme(&theme);
identifiers_only.set_theme(&theme);
let buffer = cx.new(|cx| {
let mut buffer = Buffer::local("fn main() {}", cx);
buffer.set_language(Some(rust), cx);
buffer
});
cx.run_until_parked();
let keyword = theme_highlight_id(&theme, "keyword");
let function = theme_highlight_id(&theme, "function");
let variable = theme_highlight_id(&theme, "variable");
let snapshot = buffer.read_with(cx, |buffer, _| buffer.snapshot());
assert_eq!(
merged_highlight_runs(&snapshot, 0..snapshot.len()),
vec![("fn".to_string(), keyword), ("main".to_string(), function)],
);
buffer.update(cx, |buffer, cx| {
buffer.set_sync_parse_timeout(None);
buffer.set_language(Some(identifiers_only), cx);
});
let snapshot = buffer.read_with(cx, |buffer, _| buffer.snapshot());
assert_eq!(
merged_highlight_runs(&snapshot, 0..snapshot.len()),
Vec::new(),
"a language change must not serve the old language's cached highlights while the new parse is pending"
);
cx.run_until_parked();
let snapshot = buffer.read_with(cx, |buffer, _| buffer.snapshot());
assert_eq!(
merged_highlight_runs(&snapshot, 0..snapshot.len()),
vec![("main".to_string(), variable)],
);
}
#[gpui::test(iterations = 100)]
fn test_random_chunk_bitmaps(cx: &mut App, mut rng: StdRng) {
use util::RandomCharIter;
// Generate random text
let len = rng.random_range(0..10000);
let text = RandomCharIter::new(&mut rng).take(len).collect::<String>();
let buffer = cx.new(|cx| Buffer::local(text, cx));
let snapshot = buffer.read(cx).snapshot();
// Get all chunks and verify their bitmaps
let chunks = snapshot.chunks(
0..snapshot.len(),
LanguageAwareStyling {
tree_sitter: false,
diagnostics: false,
},
);
for chunk in chunks {
let chunk_text = chunk.text;
let chars_bitmap = chunk.chars;
let tabs_bitmap = chunk.tabs;
// Check empty chunks have empty bitmaps
if chunk_text.is_empty() {
assert_eq!(
chars_bitmap, 0,
"Empty chunk should have empty chars bitmap"
);
assert_eq!(tabs_bitmap, 0, "Empty chunk should have empty tabs bitmap");
continue;
}
// Verify that chunk text doesn't exceed 128 bytes
assert!(
chunk_text.len() <= 128,
"Chunk text length {} exceeds 128 bytes",
chunk_text.len()
);
// Verify chars bitmap
let char_indices = chunk_text
.char_indices()
.map(|(i, _)| i)
.collect::<Vec<_>>();
for byte_idx in 0..chunk_text.len() {
let should_have_bit = char_indices.contains(&byte_idx);
let has_bit = chars_bitmap & (1 << byte_idx) != 0;
if has_bit != should_have_bit {
eprintln!("Chunk text bytes: {:?}", chunk_text.as_bytes());
eprintln!("Char indices: {:?}", char_indices);
eprintln!("Chars bitmap: {:#b}", chars_bitmap);
}
assert_eq!(
has_bit, should_have_bit,
"Chars bitmap mismatch at byte index {} in chunk {:?}. Expected bit: {}, Got bit: {}",
byte_idx, chunk_text, should_have_bit, has_bit
);
}
// Verify tabs bitmap
for (byte_idx, byte) in chunk_text.bytes().enumerate() {
let is_tab = byte == b'\t';
let has_bit = tabs_bitmap & (1 << byte_idx) != 0;
if has_bit != is_tab {
eprintln!("Chunk text bytes: {:?}", chunk_text.as_bytes());
eprintln!("Tabs bitmap: {:#b}", tabs_bitmap);
assert_eq!(
has_bit, is_tab,
"Tabs bitmap mismatch at byte index {} in chunk {:?}. Byte: {:?}, Expected bit: {}, Got bit: {}",
byte_idx, chunk_text, byte as char, is_tab, has_bit
);
}
}
}
}
#[gpui::test]
fn test_formatted_chunks(cx: &mut gpui::App) {
init_settings(cx, |_| {});
let buffer = cx.new(|cx| Buffer::local("use std::cmp::Eq;", cx).with_language(rust_lang(), cx));
let snapshot = buffer.read(cx).snapshot();
let chunks = snapshot.chunks(
0..snapshot.len(),
LanguageAwareStyling {
tree_sitter: true,
diagnostics: false,
},
);
for chunk in chunks {
let chunk_text = chunk.text;
let chars_bitmap = chunk.chars;
// Verify chars bitmap
let char_indices = chunk_text
.char_indices()
.map(|(i, _)| i)
.collect::<Vec<_>>();
assert_eq!(char_indices.len() as u32, chars_bitmap.count_ones());
for byte_idx in 0..chunk_text.len() {
let should_have_bit = char_indices.contains(&byte_idx);
let has_bit = chars_bitmap & (1 << byte_idx) != 0;
if has_bit != should_have_bit {
eprintln!("Chunk text bytes: {:?}", chunk_text.as_bytes());
eprintln!("Char indices: {:?}", char_indices);
eprintln!("Chars bitmap: {:#b}", chars_bitmap);
}
assert_eq!(
has_bit, should_have_bit,
"Chars bitmap mismatch at byte index {} in chunk {:?}. Expected bit: {}, Got bit: {}",
byte_idx, chunk_text, should_have_bit, has_bit
);
}
}
}
fn merged_highlight_runs(
snapshot: &BufferSnapshot,
range: Range<usize>,
) -> Vec<(String, HighlightId)> {
let chunks = snapshot.chunks(
range,
LanguageAwareStyling {
tree_sitter: true,
diagnostics: false,
},
);
let mut runs = Vec::new();
for chunk in chunks {
merge_highlighted_chunk(&mut runs, &chunk);
}
runs
}
fn merge_highlighted_chunk(runs: &mut Vec<(String, HighlightId)>, chunk: &Chunk<'_>) {
let Some(highlight_id) = chunk.syntax_highlight_id else {
return;
};
match runs.last_mut() {
Some((last_text, last_highlight_id)) if *last_highlight_id == highlight_id => {
last_text.push_str(chunk.text);
}
_ => runs.push((chunk.text.to_string(), highlight_id)),
}
}
fn keyword_and_function_lang() -> Arc<Language> {
Arc::new(
Language::new(
LanguageConfig {
name: "Rust".into(),
..LanguageConfig::default()
},
Some(tree_sitter_rust::LANGUAGE.into()),
)
.with_highlights_query(
r#"
"fn" @keyword
(identifier) @function
"#,
)
.unwrap(),
)
}
fn keyword_and_function_theme() -> SyntaxTheme {
SyntaxTheme::new([
("keyword".to_string(), gpui::rgba(0xff0000ff).into()),
("function".to_string(), gpui::rgba(0x00ff00ff).into()),
])
}
fn theme_highlight_id(theme: &SyntaxTheme, capture_name: &str) -> HighlightId {
HighlightId::new(theme.highlight_id(capture_name).unwrap())
}