Add assertions to test for autoclose with embedded languages
This commit is contained in:
parent
4f44375abd
commit
c354b9b959
2 changed files with 173 additions and 33 deletions
|
@ -6001,6 +6001,10 @@ impl Editor {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EditorSnapshot {
|
impl EditorSnapshot {
|
||||||
|
pub fn language_at<T: ToOffset>(&self, position: T) -> Option<&Arc<Language>> {
|
||||||
|
self.display_snapshot.buffer_snapshot.language_at(position)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn is_focused(&self) -> bool {
|
pub fn is_focused(&self) -> bool {
|
||||||
self.is_focused
|
self.is_focused
|
||||||
}
|
}
|
||||||
|
@ -9788,13 +9792,24 @@ mod tests {
|
||||||
Language::new(
|
Language::new(
|
||||||
LanguageConfig {
|
LanguageConfig {
|
||||||
name: "HTML".into(),
|
name: "HTML".into(),
|
||||||
brackets: vec![BracketPair {
|
brackets: vec![
|
||||||
start: "<".to_string(),
|
BracketPair {
|
||||||
end: ">".to_string(),
|
start: "<".into(),
|
||||||
close: true,
|
end: ">".into(),
|
||||||
newline: true,
|
..Default::default()
|
||||||
}],
|
},
|
||||||
autoclose_before: "})]".to_string(),
|
BracketPair {
|
||||||
|
start: "{".into(),
|
||||||
|
end: "}".into(),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
BracketPair {
|
||||||
|
start: "(".into(),
|
||||||
|
end: ")".into(),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
],
|
||||||
|
autoclose_before: "})]>".into(),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
Some(tree_sitter_html::language()),
|
Some(tree_sitter_html::language()),
|
||||||
|
@ -9812,13 +9827,24 @@ mod tests {
|
||||||
let javascript_language = Arc::new(Language::new(
|
let javascript_language = Arc::new(Language::new(
|
||||||
LanguageConfig {
|
LanguageConfig {
|
||||||
name: "JavaScript".into(),
|
name: "JavaScript".into(),
|
||||||
brackets: vec![BracketPair {
|
brackets: vec![
|
||||||
start: "/*".to_string(),
|
BracketPair {
|
||||||
end: "*/".to_string(),
|
start: "/*".into(),
|
||||||
close: true,
|
end: " */".into(),
|
||||||
newline: true,
|
..Default::default()
|
||||||
}],
|
},
|
||||||
autoclose_before: "})]".to_string(),
|
BracketPair {
|
||||||
|
start: "{".into(),
|
||||||
|
end: "}".into(),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
BracketPair {
|
||||||
|
start: "(".into(),
|
||||||
|
end: ")".into(),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
|
],
|
||||||
|
autoclose_before: "})]>".into(),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
Some(tree_sitter_javascript::language()),
|
Some(tree_sitter_javascript::language()),
|
||||||
|
@ -9839,31 +9865,145 @@ mod tests {
|
||||||
<script>
|
<script>
|
||||||
var x = 1;ˇ
|
var x = 1;ˇ
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>ˇ
|
||||||
"#
|
"#
|
||||||
.unindent(),
|
.unindent(),
|
||||||
);
|
);
|
||||||
|
|
||||||
let cursors = cx.update_editor(|editor, cx| editor.selections.ranges::<usize>(cx));
|
// Precondition: different languages are active at different locations.
|
||||||
cx.update_buffer(|buffer, _| {
|
cx.update_editor(|editor, cx| {
|
||||||
let snapshot = buffer.snapshot();
|
let snapshot = editor.snapshot(cx);
|
||||||
|
let cursors = editor.selections.ranges::<usize>(cx);
|
||||||
|
let languages = cursors
|
||||||
|
.iter()
|
||||||
|
.map(|c| snapshot.language_at(c.start).unwrap().name())
|
||||||
|
.collect::<Vec<_>>();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
snapshot
|
languages,
|
||||||
.language_at(cursors[0].start)
|
&["HTML".into(), "JavaScript".into(), "HTML".into()]
|
||||||
.unwrap()
|
|
||||||
.name()
|
|
||||||
.as_ref(),
|
|
||||||
"HTML"
|
|
||||||
);
|
|
||||||
assert_eq!(
|
|
||||||
snapshot
|
|
||||||
.language_at(cursors[1].start)
|
|
||||||
.unwrap()
|
|
||||||
.name()
|
|
||||||
.as_ref(),
|
|
||||||
"JavaScript"
|
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Angle brackets autoclose in HTML, but not JavaScript.
|
||||||
|
cx.update_editor(|editor, cx| {
|
||||||
|
editor.handle_input("<", cx);
|
||||||
|
editor.handle_input("a", cx);
|
||||||
|
});
|
||||||
|
cx.assert_editor_state(
|
||||||
|
&r#"
|
||||||
|
<body><aˇ>
|
||||||
|
<script>
|
||||||
|
var x = 1;<aˇ
|
||||||
|
</script>
|
||||||
|
</body><aˇ>
|
||||||
|
"#
|
||||||
|
.unindent(),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Curly braces and parens autoclose in both HTML and JavaScript.
|
||||||
|
cx.update_editor(|editor, cx| {
|
||||||
|
editor.handle_input(" b=", cx);
|
||||||
|
editor.handle_input("{", cx);
|
||||||
|
editor.handle_input("c", cx);
|
||||||
|
editor.handle_input("(", cx);
|
||||||
|
});
|
||||||
|
cx.assert_editor_state(
|
||||||
|
&r#"
|
||||||
|
<body><a b={c(ˇ)}>
|
||||||
|
<script>
|
||||||
|
var x = 1;<a b={c(ˇ)}
|
||||||
|
</script>
|
||||||
|
</body><a b={c(ˇ)}>
|
||||||
|
"#
|
||||||
|
.unindent(),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Brackets that were already autoclosed are skipped.
|
||||||
|
cx.update_editor(|editor, cx| {
|
||||||
|
editor.handle_input(")", cx);
|
||||||
|
editor.handle_input("d", cx);
|
||||||
|
editor.handle_input("}", cx);
|
||||||
|
});
|
||||||
|
cx.assert_editor_state(
|
||||||
|
&r#"
|
||||||
|
<body><a b={c()d}ˇ>
|
||||||
|
<script>
|
||||||
|
var x = 1;<a b={c()d}ˇ
|
||||||
|
</script>
|
||||||
|
</body><a b={c()d}ˇ>
|
||||||
|
"#
|
||||||
|
.unindent(),
|
||||||
|
);
|
||||||
|
cx.update_editor(|editor, cx| {
|
||||||
|
editor.handle_input(">", cx);
|
||||||
|
});
|
||||||
|
cx.assert_editor_state(
|
||||||
|
&r#"
|
||||||
|
<body><a b={c()d}>ˇ
|
||||||
|
<script>
|
||||||
|
var x = 1;<a b={c()d}>ˇ
|
||||||
|
</script>
|
||||||
|
</body><a b={c()d}>ˇ
|
||||||
|
"#
|
||||||
|
.unindent(),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Reset
|
||||||
|
cx.set_state(
|
||||||
|
&r#"
|
||||||
|
<body>ˇ
|
||||||
|
<script>
|
||||||
|
var x = 1;ˇ
|
||||||
|
</script>
|
||||||
|
</body>ˇ
|
||||||
|
"#
|
||||||
|
.unindent(),
|
||||||
|
);
|
||||||
|
|
||||||
|
cx.update_editor(|editor, cx| {
|
||||||
|
editor.handle_input("<", cx);
|
||||||
|
});
|
||||||
|
cx.assert_editor_state(
|
||||||
|
&r#"
|
||||||
|
<body><ˇ>
|
||||||
|
<script>
|
||||||
|
var x = 1;<ˇ
|
||||||
|
</script>
|
||||||
|
</body><ˇ>
|
||||||
|
"#
|
||||||
|
.unindent(),
|
||||||
|
);
|
||||||
|
|
||||||
|
// When backspacing, the closing angle brackets are removed.
|
||||||
|
cx.update_editor(|editor, cx| {
|
||||||
|
editor.backspace(&Backspace, cx);
|
||||||
|
});
|
||||||
|
cx.assert_editor_state(
|
||||||
|
&r#"
|
||||||
|
<body>ˇ
|
||||||
|
<script>
|
||||||
|
var x = 1;ˇ
|
||||||
|
</script>
|
||||||
|
</body>ˇ
|
||||||
|
"#
|
||||||
|
.unindent(),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Block comments autoclose in JavaScript, but not HTML.
|
||||||
|
cx.update_editor(|editor, cx| {
|
||||||
|
editor.handle_input("/", cx);
|
||||||
|
editor.handle_input("*", cx);
|
||||||
|
});
|
||||||
|
cx.assert_editor_state(
|
||||||
|
&r#"
|
||||||
|
<body>/*ˇ
|
||||||
|
<script>
|
||||||
|
var x = 1;/*ˇ */
|
||||||
|
</script>
|
||||||
|
</body>/*ˇ
|
||||||
|
"#
|
||||||
|
.unindent(),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[gpui::test]
|
#[gpui::test]
|
||||||
|
|
|
@ -271,7 +271,7 @@ pub struct FakeLspAdapter {
|
||||||
pub disk_based_diagnostics_sources: Vec<String>,
|
pub disk_based_diagnostics_sources: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Deserialize)]
|
#[derive(Clone, Debug, Default, Deserialize)]
|
||||||
pub struct BracketPair {
|
pub struct BracketPair {
|
||||||
pub start: String,
|
pub start: String,
|
||||||
pub end: String,
|
pub end: String,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue