Remove indent_size
parameter from Buffer::edit_with_autoindent
Instead, compute the indent size by reading the settings inside that method. Co-authored-by: Mikayla Maki <mikayla.c.maki@gmail.com>
This commit is contained in:
parent
b1b252ee45
commit
cdf6ae25bb
8 changed files with 77 additions and 104 deletions
|
@ -11,7 +11,6 @@ use language::{
|
|||
IndentSize, Language, OffsetRangeExt, Outline, OutlineItem, Selection, ToOffset as _,
|
||||
ToOffsetUtf16 as _, ToPoint as _, ToPointUtf16 as _, TransactionId,
|
||||
};
|
||||
use settings::Settings;
|
||||
use smallvec::SmallVec;
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
|
@ -347,14 +346,7 @@ impl MultiBuffer {
|
|||
if let Some(buffer) = self.as_singleton() {
|
||||
return buffer.update(cx, |buffer, cx| {
|
||||
if autoindent {
|
||||
let language_name = buffer.language().map(|language| language.name());
|
||||
let settings = cx.global::<Settings>();
|
||||
let indent_size = if settings.hard_tabs(language_name.as_deref()) {
|
||||
IndentSize::tab()
|
||||
} else {
|
||||
IndentSize::spaces(settings.tab_size(language_name.as_deref()).get())
|
||||
};
|
||||
buffer.edit_with_autoindent(edits, indent_size, cx);
|
||||
buffer.edit_with_autoindent(edits, cx);
|
||||
} else {
|
||||
buffer.edit(edits, cx);
|
||||
}
|
||||
|
@ -471,18 +463,10 @@ impl MultiBuffer {
|
|||
));
|
||||
}
|
||||
}
|
||||
let language_name = buffer.language().map(|l| l.name());
|
||||
|
||||
if autoindent {
|
||||
let settings = cx.global::<Settings>();
|
||||
let indent_size = if settings.hard_tabs(language_name.as_deref()) {
|
||||
IndentSize::tab()
|
||||
} else {
|
||||
IndentSize::spaces(settings.tab_size(language_name.as_deref()).get())
|
||||
};
|
||||
|
||||
buffer.edit_with_autoindent(deletions, indent_size, cx);
|
||||
buffer.edit_with_autoindent(insertions, indent_size, cx);
|
||||
buffer.edit_with_autoindent(deletions, cx);
|
||||
buffer.edit_with_autoindent(insertions, cx);
|
||||
} else {
|
||||
buffer.edit(deletions, cx);
|
||||
buffer.edit(insertions, cx);
|
||||
|
@ -3220,6 +3204,7 @@ mod tests {
|
|||
use gpui::MutableAppContext;
|
||||
use language::{Buffer, Rope};
|
||||
use rand::prelude::*;
|
||||
use settings::Settings;
|
||||
use std::{env, rc::Rc};
|
||||
use text::{Point, RandomCharIter};
|
||||
use util::test::sample_text;
|
||||
|
|
|
@ -27,6 +27,7 @@ fuzzy = { path = "../fuzzy" }
|
|||
gpui = { path = "../gpui" }
|
||||
lsp = { path = "../lsp" }
|
||||
rpc = { path = "../rpc" }
|
||||
settings = { path = "../settings" }
|
||||
sum_tree = { path = "../sum_tree" }
|
||||
text = { path = "../text" }
|
||||
theme = { path = "../theme" }
|
||||
|
|
|
@ -14,6 +14,7 @@ use futures::FutureExt as _;
|
|||
use gpui::{fonts::HighlightStyle, AppContext, Entity, ModelContext, MutableAppContext, Task};
|
||||
use lazy_static::lazy_static;
|
||||
use parking_lot::Mutex;
|
||||
use settings::Settings;
|
||||
use similar::{ChangeTag, TextDiff};
|
||||
use smol::future::yield_now;
|
||||
use std::{
|
||||
|
@ -1156,7 +1157,6 @@ impl Buffer {
|
|||
pub fn edit_with_autoindent<I, S, T>(
|
||||
&mut self,
|
||||
edits_iter: I,
|
||||
indent_size: IndentSize,
|
||||
cx: &mut ModelContext<Self>,
|
||||
) -> Option<clock::Local>
|
||||
where
|
||||
|
@ -1164,6 +1164,13 @@ impl Buffer {
|
|||
S: ToOffset,
|
||||
T: Into<Arc<str>>,
|
||||
{
|
||||
let language_name = self.language().map(|language| language.name());
|
||||
let settings = cx.global::<Settings>();
|
||||
let indent_size = if settings.hard_tabs(language_name.as_deref()) {
|
||||
IndentSize::tab()
|
||||
} else {
|
||||
IndentSize::spaces(settings.tab_size(language_name.as_deref()).get())
|
||||
};
|
||||
self.edit_internal(edits_iter, Some(indent_size), cx)
|
||||
}
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@ use clock::ReplicaId;
|
|||
use collections::BTreeMap;
|
||||
use gpui::{ModelHandle, MutableAppContext};
|
||||
use rand::prelude::*;
|
||||
use settings::Settings;
|
||||
use std::{
|
||||
cell::RefCell,
|
||||
env,
|
||||
|
@ -24,6 +25,7 @@ fn init_logger() {
|
|||
|
||||
#[gpui::test]
|
||||
fn test_line_endings(cx: &mut gpui::MutableAppContext) {
|
||||
cx.set_global(Settings::test(cx));
|
||||
cx.add_model(|cx| {
|
||||
let mut buffer =
|
||||
Buffer::new(0, "one\r\ntwo\rthree", cx).with_language(Arc::new(rust_lang()), cx);
|
||||
|
@ -31,11 +33,7 @@ fn test_line_endings(cx: &mut gpui::MutableAppContext) {
|
|||
assert_eq!(buffer.line_ending(), LineEnding::Windows);
|
||||
|
||||
buffer.check_invariants();
|
||||
buffer.edit_with_autoindent(
|
||||
[(buffer.len()..buffer.len(), "\r\nfour")],
|
||||
IndentSize::spaces(2),
|
||||
cx,
|
||||
);
|
||||
buffer.edit_with_autoindent([(buffer.len()..buffer.len(), "\r\nfour")], cx);
|
||||
buffer.edit([(0..0, "zero\r\n")], cx);
|
||||
assert_eq!(buffer.text(), "zero\none\ntwo\nthree\nfour");
|
||||
assert_eq!(buffer.line_ending(), LineEnding::Windows);
|
||||
|
@ -545,6 +543,7 @@ async fn test_symbols_containing(cx: &mut gpui::TestAppContext) {
|
|||
|
||||
#[gpui::test]
|
||||
fn test_enclosing_bracket_ranges(cx: &mut MutableAppContext) {
|
||||
cx.set_global(Settings::test(cx));
|
||||
let buffer = cx.add_model(|cx| {
|
||||
let text = "
|
||||
mod x {
|
||||
|
@ -620,36 +619,27 @@ fn test_range_for_syntax_ancestor(cx: &mut MutableAppContext) {
|
|||
|
||||
#[gpui::test]
|
||||
fn test_autoindent_with_soft_tabs(cx: &mut MutableAppContext) {
|
||||
let settings = Settings::test(cx);
|
||||
cx.set_global(settings);
|
||||
|
||||
cx.add_model(|cx| {
|
||||
let text = "fn a() {}";
|
||||
let mut buffer = Buffer::new(0, text, cx).with_language(Arc::new(rust_lang()), cx);
|
||||
|
||||
buffer.edit_with_autoindent([(8..8, "\n\n")], IndentSize::spaces(4), cx);
|
||||
buffer.edit_with_autoindent([(8..8, "\n\n")], cx);
|
||||
assert_eq!(buffer.text(), "fn a() {\n \n}");
|
||||
|
||||
buffer.edit_with_autoindent(
|
||||
[(Point::new(1, 4)..Point::new(1, 4), "b()\n")],
|
||||
IndentSize::spaces(4),
|
||||
cx,
|
||||
);
|
||||
buffer.edit_with_autoindent([(Point::new(1, 4)..Point::new(1, 4), "b()\n")], 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_with_autoindent(
|
||||
[(Point::new(2, 4)..Point::new(2, 4), ".c")],
|
||||
IndentSize::spaces(4),
|
||||
cx,
|
||||
);
|
||||
buffer.edit_with_autoindent([(Point::new(2, 4)..Point::new(2, 4), ".c")], 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_with_autoindent(
|
||||
[(Point::new(2, 8)..Point::new(2, 9), "")],
|
||||
IndentSize::spaces(4),
|
||||
cx,
|
||||
);
|
||||
buffer.edit_with_autoindent([(Point::new(2, 8)..Point::new(2, 9), "")], cx);
|
||||
assert_eq!(buffer.text(), "fn a() {\n b()\n c\n}");
|
||||
|
||||
buffer
|
||||
|
@ -658,36 +648,28 @@ fn test_autoindent_with_soft_tabs(cx: &mut MutableAppContext) {
|
|||
|
||||
#[gpui::test]
|
||||
fn test_autoindent_with_hard_tabs(cx: &mut MutableAppContext) {
|
||||
let mut settings = Settings::test(cx);
|
||||
settings.editor_overrides.hard_tabs = Some(true);
|
||||
cx.set_global(settings);
|
||||
|
||||
cx.add_model(|cx| {
|
||||
let text = "fn a() {}";
|
||||
let mut buffer = Buffer::new(0, text, cx).with_language(Arc::new(rust_lang()), cx);
|
||||
|
||||
buffer.edit_with_autoindent([(8..8, "\n\n")], IndentSize::tab(), cx);
|
||||
buffer.edit_with_autoindent([(8..8, "\n\n")], cx);
|
||||
assert_eq!(buffer.text(), "fn a() {\n\t\n}");
|
||||
|
||||
buffer.edit_with_autoindent(
|
||||
[(Point::new(1, 1)..Point::new(1, 1), "b()\n")],
|
||||
IndentSize::tab(),
|
||||
cx,
|
||||
);
|
||||
buffer.edit_with_autoindent([(Point::new(1, 1)..Point::new(1, 1), "b()\n")], 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_with_autoindent(
|
||||
[(Point::new(2, 1)..Point::new(2, 1), ".c")],
|
||||
IndentSize::tab(),
|
||||
cx,
|
||||
);
|
||||
buffer.edit_with_autoindent([(Point::new(2, 1)..Point::new(2, 1), ".c")], 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_with_autoindent(
|
||||
[(Point::new(2, 2)..Point::new(2, 3), "")],
|
||||
IndentSize::tab(),
|
||||
cx,
|
||||
);
|
||||
buffer.edit_with_autoindent([(Point::new(2, 2)..Point::new(2, 3), "")], cx);
|
||||
assert_eq!(buffer.text(), "fn a() {\n\tb()\n\tc\n}");
|
||||
|
||||
buffer
|
||||
|
@ -696,6 +678,9 @@ fn test_autoindent_with_hard_tabs(cx: &mut MutableAppContext) {
|
|||
|
||||
#[gpui::test]
|
||||
fn test_autoindent_does_not_adjust_lines_with_unchanged_suggestion(cx: &mut MutableAppContext) {
|
||||
let settings = Settings::test(cx);
|
||||
cx.set_global(settings);
|
||||
|
||||
cx.add_model(|cx| {
|
||||
let text = "
|
||||
fn a() {
|
||||
|
@ -714,7 +699,6 @@ fn test_autoindent_does_not_adjust_lines_with_unchanged_suggestion(cx: &mut Muta
|
|||
(empty(Point::new(1, 1)), "()"),
|
||||
(empty(Point::new(2, 1)), "()"),
|
||||
],
|
||||
IndentSize::spaces(4),
|
||||
cx,
|
||||
);
|
||||
assert_eq!(
|
||||
|
@ -735,7 +719,6 @@ fn test_autoindent_does_not_adjust_lines_with_unchanged_suggestion(cx: &mut Muta
|
|||
(empty(Point::new(1, 1)), "\n.f\n.g"),
|
||||
(empty(Point::new(2, 1)), "\n.f\n.g"),
|
||||
],
|
||||
IndentSize::spaces(4),
|
||||
cx,
|
||||
);
|
||||
assert_eq!(
|
||||
|
@ -768,11 +751,7 @@ fn test_autoindent_does_not_adjust_lines_with_unchanged_suggestion(cx: &mut Muta
|
|||
let mut buffer = Buffer::new(0, text, cx).with_language(Arc::new(rust_lang()), cx);
|
||||
|
||||
// Delete a closing curly brace changes the suggested indent for the line.
|
||||
buffer.edit_with_autoindent(
|
||||
[(Point::new(3, 4)..Point::new(3, 5), "")],
|
||||
IndentSize::spaces(4),
|
||||
cx,
|
||||
);
|
||||
buffer.edit_with_autoindent([(Point::new(3, 4)..Point::new(3, 5), "")], cx);
|
||||
assert_eq!(
|
||||
buffer.text(),
|
||||
"
|
||||
|
@ -788,11 +767,7 @@ fn test_autoindent_does_not_adjust_lines_with_unchanged_suggestion(cx: &mut Muta
|
|||
);
|
||||
|
||||
// Manually editing the leading whitespace
|
||||
buffer.edit_with_autoindent(
|
||||
[(Point::new(3, 0)..Point::new(3, 12), "")],
|
||||
IndentSize::spaces(4),
|
||||
cx,
|
||||
);
|
||||
buffer.edit_with_autoindent([(Point::new(3, 0)..Point::new(3, 12), "")], cx);
|
||||
assert_eq!(
|
||||
buffer.text(),
|
||||
"
|
||||
|
@ -811,6 +786,7 @@ fn test_autoindent_does_not_adjust_lines_with_unchanged_suggestion(cx: &mut Muta
|
|||
|
||||
#[gpui::test]
|
||||
fn test_autoindent_adjusts_lines_when_only_text_changes(cx: &mut MutableAppContext) {
|
||||
cx.set_global(Settings::test(cx));
|
||||
cx.add_model(|cx| {
|
||||
let text = "
|
||||
fn a() {}
|
||||
|
@ -819,7 +795,7 @@ fn test_autoindent_adjusts_lines_when_only_text_changes(cx: &mut MutableAppConte
|
|||
|
||||
let mut buffer = Buffer::new(0, text, cx).with_language(Arc::new(rust_lang()), cx);
|
||||
|
||||
buffer.edit_with_autoindent([(5..5, "\nb")], IndentSize::spaces(4), cx);
|
||||
buffer.edit_with_autoindent([(5..5, "\nb")], cx);
|
||||
assert_eq!(
|
||||
buffer.text(),
|
||||
"
|
||||
|
@ -831,11 +807,7 @@ fn test_autoindent_adjusts_lines_when_only_text_changes(cx: &mut MutableAppConte
|
|||
|
||||
// The indentation suggestion changed because `@end` node (a close paren)
|
||||
// is now at the beginning of the line.
|
||||
buffer.edit_with_autoindent(
|
||||
[(Point::new(1, 4)..Point::new(1, 5), "")],
|
||||
IndentSize::spaces(4),
|
||||
cx,
|
||||
);
|
||||
buffer.edit_with_autoindent([(Point::new(1, 4)..Point::new(1, 5), "")], cx);
|
||||
assert_eq!(
|
||||
buffer.text(),
|
||||
"
|
||||
|
@ -851,10 +823,11 @@ fn test_autoindent_adjusts_lines_when_only_text_changes(cx: &mut MutableAppConte
|
|||
|
||||
#[gpui::test]
|
||||
fn test_autoindent_with_edit_at_end_of_buffer(cx: &mut MutableAppContext) {
|
||||
cx.set_global(Settings::test(cx));
|
||||
cx.add_model(|cx| {
|
||||
let text = "a\nb";
|
||||
let mut buffer = Buffer::new(0, text, cx).with_language(Arc::new(rust_lang()), cx);
|
||||
buffer.edit_with_autoindent([(0..1, "\n"), (2..3, "\n")], IndentSize::spaces(4), cx);
|
||||
buffer.edit_with_autoindent([(0..1, "\n"), (2..3, "\n")], cx);
|
||||
assert_eq!(buffer.text(), "\n\n\n");
|
||||
buffer
|
||||
});
|
||||
|
@ -862,6 +835,7 @@ fn test_autoindent_with_edit_at_end_of_buffer(cx: &mut MutableAppContext) {
|
|||
|
||||
#[gpui::test]
|
||||
fn test_autoindent_multi_line_insertion(cx: &mut MutableAppContext) {
|
||||
cx.set_global(Settings::test(cx));
|
||||
cx.add_model(|cx| {
|
||||
let text = "
|
||||
const a: usize = 1;
|
||||
|
@ -876,7 +850,6 @@ fn test_autoindent_multi_line_insertion(cx: &mut MutableAppContext) {
|
|||
let mut buffer = Buffer::new(0, text, cx).with_language(Arc::new(rust_lang()), cx);
|
||||
buffer.edit_with_autoindent(
|
||||
[(Point::new(3, 0)..Point::new(3, 0), "e(\n f()\n);\n")],
|
||||
IndentSize::spaces(4),
|
||||
cx,
|
||||
);
|
||||
assert_eq!(
|
||||
|
@ -903,6 +876,7 @@ fn test_autoindent_multi_line_insertion(cx: &mut MutableAppContext) {
|
|||
fn test_autoindent_preserves_relative_indentation_in_multi_line_insertion(
|
||||
cx: &mut MutableAppContext,
|
||||
) {
|
||||
cx.set_global(Settings::test(cx));
|
||||
cx.add_model(|cx| {
|
||||
let text = "
|
||||
fn a() {
|
||||
|
@ -924,7 +898,6 @@ fn test_autoindent_preserves_relative_indentation_in_multi_line_insertion(
|
|||
// insert at the beginning of a line
|
||||
buffer.edit_with_autoindent(
|
||||
[(Point::new(2, 0)..Point::new(2, 0), pasted_text.clone())],
|
||||
IndentSize::spaces(4),
|
||||
cx,
|
||||
);
|
||||
assert_eq!(
|
||||
|
@ -947,7 +920,8 @@ fn test_autoindent_preserves_relative_indentation_in_multi_line_insertion(
|
|||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn test_autoindent_disabled(cx: &mut MutableAppContext) {
|
||||
fn test_autoindent_language_without_indents_query(cx: &mut MutableAppContext) {
|
||||
cx.set_global(Settings::test(cx));
|
||||
cx.add_model(|cx| {
|
||||
let text = "
|
||||
* one
|
||||
|
@ -967,11 +941,7 @@ fn test_autoindent_disabled(cx: &mut MutableAppContext) {
|
|||
)),
|
||||
cx,
|
||||
);
|
||||
buffer.edit_with_autoindent(
|
||||
[(Point::new(3, 0)..Point::new(3, 0), "\n")],
|
||||
IndentSize::spaces(4),
|
||||
cx,
|
||||
);
|
||||
buffer.edit_with_autoindent([(Point::new(3, 0)..Point::new(3, 0), "\n")], cx);
|
||||
assert_eq!(
|
||||
buffer.text(),
|
||||
"
|
||||
|
|
|
@ -249,34 +249,37 @@ impl super::LspAdapter for CLspAdapter {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use gpui::MutableAppContext;
|
||||
use language::{Buffer, IndentSize};
|
||||
use language::Buffer;
|
||||
use settings::Settings;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[gpui::test]
|
||||
fn test_c_autoindent(cx: &mut MutableAppContext) {
|
||||
cx.foreground().set_block_on_ticks(usize::MAX..=usize::MAX);
|
||||
let mut settings = Settings::test(cx);
|
||||
settings.editor_overrides.tab_size = Some(2.try_into().unwrap());
|
||||
cx.set_global(settings);
|
||||
let language = crate::languages::language("c", tree_sitter_c::language(), None);
|
||||
|
||||
cx.add_model(|cx| {
|
||||
let mut buffer = Buffer::new(0, "", cx).with_language(Arc::new(language), cx);
|
||||
let size = IndentSize::spaces(2);
|
||||
|
||||
// empty function
|
||||
buffer.edit_with_autoindent([(0..0, "int main() {}")], size, cx);
|
||||
buffer.edit_with_autoindent([(0..0, "int main() {}")], cx);
|
||||
|
||||
// indent inside braces
|
||||
let ix = buffer.len() - 1;
|
||||
buffer.edit_with_autoindent([(ix..ix, "\n\n")], size, cx);
|
||||
buffer.edit_with_autoindent([(ix..ix, "\n\n")], cx);
|
||||
assert_eq!(buffer.text(), "int main() {\n \n}");
|
||||
|
||||
// indent body of single-statement if statement
|
||||
let ix = buffer.len() - 2;
|
||||
buffer.edit_with_autoindent([(ix..ix, "if (a)\nb;")], size, cx);
|
||||
buffer.edit_with_autoindent([(ix..ix, "if (a)\nb;")], cx);
|
||||
assert_eq!(buffer.text(), "int main() {\n if (a)\n b;\n}");
|
||||
|
||||
// indent inside field expression
|
||||
let ix = buffer.len() - 3;
|
||||
buffer.edit_with_autoindent([(ix..ix, "\n.c")], size, cx);
|
||||
buffer.edit_with_autoindent([(ix..ix, "\n.c")], cx);
|
||||
assert_eq!(buffer.text(), "int main() {\n if (a)\n b\n .c;\n}");
|
||||
|
||||
buffer
|
||||
|
|
|
@ -147,20 +147,23 @@ impl LspAdapter for PythonLspAdapter {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use gpui::{ModelContext, MutableAppContext};
|
||||
use language::{Buffer, IndentSize};
|
||||
use language::Buffer;
|
||||
use settings::Settings;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[gpui::test]
|
||||
fn test_python_autoindent(cx: &mut MutableAppContext) {
|
||||
cx.foreground().set_block_on_ticks(usize::MAX..=usize::MAX);
|
||||
let language = crate::languages::language("python", tree_sitter_python::language(), None);
|
||||
let mut settings = Settings::test(cx);
|
||||
settings.editor_overrides.tab_size = Some(2.try_into().unwrap());
|
||||
cx.set_global(settings);
|
||||
|
||||
cx.add_model(|cx| {
|
||||
let mut buffer = Buffer::new(0, "", cx).with_language(Arc::new(language), cx);
|
||||
let size = IndentSize::spaces(2);
|
||||
let append = |buffer: &mut Buffer, text: &str, cx: &mut ModelContext<Buffer>| {
|
||||
let ix = buffer.len();
|
||||
buffer.edit_with_autoindent([(ix..ix, text)], size, cx);
|
||||
buffer.edit_with_autoindent([(ix..ix, text)], cx);
|
||||
};
|
||||
|
||||
// indent after "def():"
|
||||
|
@ -204,7 +207,7 @@ mod tests {
|
|||
|
||||
// dedent the closing paren if it is shifted to the beginning of the line
|
||||
let argument_ix = buffer.text().find("1").unwrap();
|
||||
buffer.edit_with_autoindent([(argument_ix..argument_ix + 1, "")], size, cx);
|
||||
buffer.edit_with_autoindent([(argument_ix..argument_ix + 1, "")], cx);
|
||||
assert_eq!(
|
||||
buffer.text(),
|
||||
"def a():\n \n if a:\n b()\n else:\n foo(\n )"
|
||||
|
@ -219,7 +222,7 @@ mod tests {
|
|||
|
||||
// manually outdent the last line
|
||||
let end_whitespace_ix = buffer.len() - 4;
|
||||
buffer.edit_with_autoindent([(end_whitespace_ix..buffer.len(), "")], size, cx);
|
||||
buffer.edit_with_autoindent([(end_whitespace_ix..buffer.len(), "")], cx);
|
||||
assert_eq!(
|
||||
buffer.text(),
|
||||
"def a():\n \n if a:\n b()\n else:\n foo(\n )\n"
|
||||
|
|
|
@ -257,6 +257,7 @@ mod tests {
|
|||
use super::*;
|
||||
use crate::languages::{language, CachedLspAdapter};
|
||||
use gpui::{color::Color, MutableAppContext};
|
||||
use settings::Settings;
|
||||
use theme::SyntaxTheme;
|
||||
|
||||
#[gpui::test]
|
||||
|
@ -433,37 +434,39 @@ mod tests {
|
|||
fn test_rust_autoindent(cx: &mut MutableAppContext) {
|
||||
cx.foreground().set_block_on_ticks(usize::MAX..=usize::MAX);
|
||||
let language = crate::languages::language("rust", tree_sitter_rust::language(), None);
|
||||
let mut settings = Settings::test(cx);
|
||||
settings.editor_overrides.tab_size = Some(2.try_into().unwrap());
|
||||
cx.set_global(settings);
|
||||
|
||||
cx.add_model(|cx| {
|
||||
let mut buffer = Buffer::new(0, "", cx).with_language(Arc::new(language), cx);
|
||||
let size = IndentSize::spaces(2);
|
||||
|
||||
// indent between braces
|
||||
buffer.set_text("fn a() {}", cx);
|
||||
let ix = buffer.len() - 1;
|
||||
buffer.edit_with_autoindent([(ix..ix, "\n\n")], size, cx);
|
||||
buffer.edit_with_autoindent([(ix..ix, "\n\n")], cx);
|
||||
assert_eq!(buffer.text(), "fn a() {\n \n}");
|
||||
|
||||
// indent between braces, even after empty lines
|
||||
buffer.set_text("fn a() {\n\n\n}", cx);
|
||||
let ix = buffer.len() - 2;
|
||||
buffer.edit_with_autoindent([(ix..ix, "\n")], size, cx);
|
||||
buffer.edit_with_autoindent([(ix..ix, "\n")], cx);
|
||||
assert_eq!(buffer.text(), "fn a() {\n\n\n \n}");
|
||||
|
||||
// indent a line that continues a field expression
|
||||
buffer.set_text("fn a() {\n \n}", cx);
|
||||
let ix = buffer.len() - 2;
|
||||
buffer.edit_with_autoindent([(ix..ix, "b\n.c")], size, cx);
|
||||
buffer.edit_with_autoindent([(ix..ix, "b\n.c")], cx);
|
||||
assert_eq!(buffer.text(), "fn a() {\n b\n .c\n}");
|
||||
|
||||
// indent further lines that continue the field expression, even after empty lines
|
||||
let ix = buffer.len() - 2;
|
||||
buffer.edit_with_autoindent([(ix..ix, "\n\n.d")], size, cx);
|
||||
buffer.edit_with_autoindent([(ix..ix, "\n\n.d")], cx);
|
||||
assert_eq!(buffer.text(), "fn a() {\n b\n .c\n \n .d\n}");
|
||||
|
||||
// dedent the line after the field expression
|
||||
let ix = buffer.len() - 2;
|
||||
buffer.edit_with_autoindent([(ix..ix, ";\ne")], size, cx);
|
||||
buffer.edit_with_autoindent([(ix..ix, ";\ne")], cx);
|
||||
assert_eq!(
|
||||
buffer.text(),
|
||||
"fn a() {\n b\n .c\n \n .d;\n e\n}"
|
||||
|
@ -472,17 +475,17 @@ mod tests {
|
|||
// indent inside a struct within a call
|
||||
buffer.set_text("const a: B = c(D {});", cx);
|
||||
let ix = buffer.len() - 3;
|
||||
buffer.edit_with_autoindent([(ix..ix, "\n\n")], size, cx);
|
||||
buffer.edit_with_autoindent([(ix..ix, "\n\n")], cx);
|
||||
assert_eq!(buffer.text(), "const a: B = c(D {\n \n});");
|
||||
|
||||
// indent further inside a nested call
|
||||
let ix = buffer.len() - 4;
|
||||
buffer.edit_with_autoindent([(ix..ix, "e: f(\n\n)")], size, cx);
|
||||
buffer.edit_with_autoindent([(ix..ix, "e: f(\n\n)")], cx);
|
||||
assert_eq!(buffer.text(), "const a: B = c(D {\n e: f(\n \n )\n});");
|
||||
|
||||
// keep that indent after an empty line
|
||||
let ix = buffer.len() - 8;
|
||||
buffer.edit_with_autoindent([(ix..ix, "\n")], size, cx);
|
||||
buffer.edit_with_autoindent([(ix..ix, "\n")], cx);
|
||||
assert_eq!(
|
||||
buffer.text(),
|
||||
"const a: B = c(D {\n e: f(\n \n \n )\n});"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue