Introduce AutoindentMode parameter to Buffer::edit
This controls whether or not we preserve the relative indentation of inserted text blocks. Co-authored-by: Mikayla Maki <mikayla.c.maki@gmail.com>
This commit is contained in:
parent
cdf6ae25bb
commit
fa5af4383d
18 changed files with 308 additions and 236 deletions
|
@ -249,7 +249,7 @@ impl super::LspAdapter for CLspAdapter {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use gpui::MutableAppContext;
|
||||
use language::Buffer;
|
||||
use language::{AutoindentMode, Buffer};
|
||||
use settings::Settings;
|
||||
use std::sync::Arc;
|
||||
|
||||
|
@ -265,21 +265,25 @@ mod tests {
|
|||
let mut buffer = Buffer::new(0, "", cx).with_language(Arc::new(language), cx);
|
||||
|
||||
// empty function
|
||||
buffer.edit_with_autoindent([(0..0, "int main() {}")], cx);
|
||||
buffer.edit([(0..0, "int main() {}")], None, cx);
|
||||
|
||||
// indent inside braces
|
||||
let ix = buffer.len() - 1;
|
||||
buffer.edit_with_autoindent([(ix..ix, "\n\n")], cx);
|
||||
buffer.edit([(ix..ix, "\n\n")], Some(AutoindentMode::Independent), 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;")], cx);
|
||||
buffer.edit(
|
||||
[(ix..ix, "if (a)\nb;")],
|
||||
Some(AutoindentMode::Independent),
|
||||
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")], cx);
|
||||
buffer.edit([(ix..ix, "\n.c")], Some(AutoindentMode::Independent), cx);
|
||||
assert_eq!(buffer.text(), "int main() {\n if (a)\n b\n .c;\n}");
|
||||
|
||||
buffer
|
||||
|
|
|
@ -147,7 +147,7 @@ impl LspAdapter for PythonLspAdapter {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use gpui::{ModelContext, MutableAppContext};
|
||||
use language::Buffer;
|
||||
use language::{AutoindentMode, Buffer};
|
||||
use settings::Settings;
|
||||
use std::sync::Arc;
|
||||
|
||||
|
@ -163,7 +163,7 @@ mod tests {
|
|||
let mut buffer = Buffer::new(0, "", cx).with_language(Arc::new(language), cx);
|
||||
let append = |buffer: &mut Buffer, text: &str, cx: &mut ModelContext<Buffer>| {
|
||||
let ix = buffer.len();
|
||||
buffer.edit_with_autoindent([(ix..ix, text)], cx);
|
||||
buffer.edit([(ix..ix, text)], Some(AutoindentMode::Independent), cx);
|
||||
};
|
||||
|
||||
// indent after "def():"
|
||||
|
@ -207,7 +207,11 @@ 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, "")], cx);
|
||||
buffer.edit(
|
||||
[(argument_ix..argument_ix + 1, "")],
|
||||
Some(AutoindentMode::Independent),
|
||||
cx,
|
||||
);
|
||||
assert_eq!(
|
||||
buffer.text(),
|
||||
"def a():\n \n if a:\n b()\n else:\n foo(\n )"
|
||||
|
@ -222,7 +226,11 @@ mod tests {
|
|||
|
||||
// manually outdent the last line
|
||||
let end_whitespace_ix = buffer.len() - 4;
|
||||
buffer.edit_with_autoindent([(end_whitespace_ix..buffer.len(), "")], cx);
|
||||
buffer.edit(
|
||||
[(end_whitespace_ix..buffer.len(), "")],
|
||||
Some(AutoindentMode::Independent),
|
||||
cx,
|
||||
);
|
||||
assert_eq!(
|
||||
buffer.text(),
|
||||
"def a():\n \n if a:\n b()\n else:\n foo(\n )\n"
|
||||
|
@ -236,7 +244,7 @@ mod tests {
|
|||
);
|
||||
|
||||
// reset to a simple if statement
|
||||
buffer.edit([(0..buffer.len(), "if a:\n b(\n )")], cx);
|
||||
buffer.edit([(0..buffer.len(), "if a:\n b(\n )")], None, cx);
|
||||
|
||||
// dedent "else" on the line after a closing paren
|
||||
append(&mut buffer, "\n else:\n", cx);
|
||||
|
|
|
@ -444,29 +444,29 @@ mod tests {
|
|||
// indent between braces
|
||||
buffer.set_text("fn a() {}", cx);
|
||||
let ix = buffer.len() - 1;
|
||||
buffer.edit_with_autoindent([(ix..ix, "\n\n")], cx);
|
||||
buffer.edit([(ix..ix, "\n\n")], Some(AutoindentMode::Independent), 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")], cx);
|
||||
buffer.edit([(ix..ix, "\n")], Some(AutoindentMode::Independent), 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")], cx);
|
||||
buffer.edit([(ix..ix, "b\n.c")], Some(AutoindentMode::Independent), 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")], cx);
|
||||
buffer.edit([(ix..ix, "\n\n.d")], Some(AutoindentMode::Independent), 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")], cx);
|
||||
buffer.edit([(ix..ix, ";\ne")], Some(AutoindentMode::Independent), cx);
|
||||
assert_eq!(
|
||||
buffer.text(),
|
||||
"fn a() {\n b\n .c\n \n .d;\n e\n}"
|
||||
|
@ -475,17 +475,21 @@ 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")], cx);
|
||||
buffer.edit([(ix..ix, "\n\n")], Some(AutoindentMode::Independent), 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)")], cx);
|
||||
buffer.edit(
|
||||
[(ix..ix, "e: f(\n\n)")],
|
||||
Some(AutoindentMode::Independent),
|
||||
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")], cx);
|
||||
buffer.edit([(ix..ix, "\n")], Some(AutoindentMode::Independent), 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