language: Accept multiple values in line_comment language knob. (#6713)
This opens up a possibility of supporting multiple comment continuation flavours in editor, e.g. doc comments for Rust (which we seize as well in this commit). Only the first `line_comment` value is used for Editor::ToggleComments Fixes: https://github.com/zed-industries/zed/issues/6692 Release Notes: - Added support for doc-comment continuations in Rust language.
This commit is contained in:
parent
dd25902aeb
commit
e9edad1d51
27 changed files with 95 additions and 73 deletions
|
@ -1657,7 +1657,7 @@ fn test_language_scope_at_with_javascript(cx: &mut AppContext) {
|
|||
let language = Language::new(
|
||||
LanguageConfig {
|
||||
name: "JavaScript".into(),
|
||||
line_comment: Some("// ".into()),
|
||||
line_comments: vec!["// ".into()],
|
||||
brackets: BracketPairConfig {
|
||||
pairs: vec![
|
||||
BracketPair {
|
||||
|
@ -1681,7 +1681,7 @@ fn test_language_scope_at_with_javascript(cx: &mut AppContext) {
|
|||
overrides: [(
|
||||
"element".into(),
|
||||
LanguageConfigOverride {
|
||||
line_comment: Override::Remove { remove: true },
|
||||
line_comments: Override::Remove { remove: true },
|
||||
block_comment: Override::Set(("{/*".into(), "*/}".into())),
|
||||
..Default::default()
|
||||
},
|
||||
|
@ -1718,7 +1718,7 @@ fn test_language_scope_at_with_javascript(cx: &mut AppContext) {
|
|||
let snapshot = buffer.snapshot();
|
||||
|
||||
let config = snapshot.language_scope_at(0).unwrap();
|
||||
assert_eq!(config.line_comment_prefix().unwrap().as_ref(), "// ");
|
||||
assert_eq!(config.line_comment_prefixes().unwrap(), &[Arc::from("// ")]);
|
||||
// Both bracket pairs are enabled
|
||||
assert_eq!(
|
||||
config.brackets().map(|e| e.1).collect::<Vec<_>>(),
|
||||
|
@ -1728,7 +1728,10 @@ fn test_language_scope_at_with_javascript(cx: &mut AppContext) {
|
|||
let string_config = snapshot
|
||||
.language_scope_at(text.find("b\"").unwrap())
|
||||
.unwrap();
|
||||
assert_eq!(string_config.line_comment_prefix().unwrap().as_ref(), "// ");
|
||||
assert_eq!(
|
||||
string_config.line_comment_prefixes().unwrap(),
|
||||
&[Arc::from("// ")]
|
||||
);
|
||||
// Second bracket pair is disabled
|
||||
assert_eq!(
|
||||
string_config.brackets().map(|e| e.1).collect::<Vec<_>>(),
|
||||
|
@ -1739,7 +1742,7 @@ fn test_language_scope_at_with_javascript(cx: &mut AppContext) {
|
|||
let element_config = snapshot
|
||||
.language_scope_at(text.find("<F>").unwrap())
|
||||
.unwrap();
|
||||
assert_eq!(element_config.line_comment_prefix(), None);
|
||||
assert_eq!(element_config.line_comment_prefixes(), None);
|
||||
assert_eq!(
|
||||
element_config.block_comment_delimiters(),
|
||||
Some((&"{/*".into(), &"*/}".into()))
|
||||
|
@ -1753,7 +1756,10 @@ fn test_language_scope_at_with_javascript(cx: &mut AppContext) {
|
|||
let tag_config = snapshot
|
||||
.language_scope_at(text.find(" d=").unwrap() + 1)
|
||||
.unwrap();
|
||||
assert_eq!(tag_config.line_comment_prefix().unwrap().as_ref(), "// ");
|
||||
assert_eq!(
|
||||
tag_config.line_comment_prefixes().unwrap(),
|
||||
&[Arc::from("// ")]
|
||||
);
|
||||
assert_eq!(
|
||||
tag_config.brackets().map(|e| e.1).collect::<Vec<_>>(),
|
||||
&[true, true]
|
||||
|
@ -1765,10 +1771,9 @@ fn test_language_scope_at_with_javascript(cx: &mut AppContext) {
|
|||
.unwrap();
|
||||
assert_eq!(
|
||||
expression_in_element_config
|
||||
.line_comment_prefix()
|
||||
.unwrap()
|
||||
.as_ref(),
|
||||
"// "
|
||||
.line_comment_prefixes()
|
||||
.unwrap(),
|
||||
&[Arc::from("// ")]
|
||||
);
|
||||
assert_eq!(
|
||||
expression_in_element_config
|
||||
|
@ -1884,14 +1889,17 @@ fn test_language_scope_at_with_combined_injections(cx: &mut AppContext) {
|
|||
|
||||
let snapshot = buffer.snapshot();
|
||||
let html_config = snapshot.language_scope_at(Point::new(2, 4)).unwrap();
|
||||
assert_eq!(html_config.line_comment_prefix(), None);
|
||||
assert_eq!(html_config.line_comment_prefixes(), Some(&vec![]));
|
||||
assert_eq!(
|
||||
html_config.block_comment_delimiters(),
|
||||
Some((&"<!--".into(), &"-->".into()))
|
||||
);
|
||||
|
||||
let ruby_config = snapshot.language_scope_at(Point::new(3, 12)).unwrap();
|
||||
assert_eq!(ruby_config.line_comment_prefix().unwrap().as_ref(), "# ");
|
||||
assert_eq!(
|
||||
ruby_config.line_comment_prefixes().unwrap(),
|
||||
&[Arc::from("# ")]
|
||||
);
|
||||
assert_eq!(ruby_config.block_comment_delimiters(), None);
|
||||
|
||||
buffer
|
||||
|
@ -2293,7 +2301,7 @@ fn ruby_lang() -> Language {
|
|||
LanguageConfig {
|
||||
name: "Ruby".into(),
|
||||
path_suffixes: vec!["rb".to_string()],
|
||||
line_comment: Some("# ".into()),
|
||||
line_comments: vec!["# ".into()],
|
||||
..Default::default()
|
||||
},
|
||||
Some(tree_sitter_ruby::language()),
|
||||
|
|
|
@ -416,8 +416,10 @@ pub struct LanguageConfig {
|
|||
#[serde(default)]
|
||||
pub collapsed_placeholder: String,
|
||||
/// A line comment string that is inserted in e.g. `toggle comments` action.
|
||||
/// A language can have multiple flavours of line comments. All of the provided line comments are
|
||||
/// used for comment continuations on the next line, but only the first one is used for Editor::ToggleComments.
|
||||
#[serde(default)]
|
||||
pub line_comment: Option<Arc<str>>,
|
||||
pub line_comments: Vec<Arc<str>>,
|
||||
/// Starting and closing characters of a block comment.
|
||||
#[serde(default)]
|
||||
pub block_comment: Option<(Arc<str>, Arc<str>)>,
|
||||
|
@ -460,7 +462,7 @@ pub struct LanguageScope {
|
|||
#[derive(Clone, Deserialize, Default, Debug)]
|
||||
pub struct LanguageConfigOverride {
|
||||
#[serde(default)]
|
||||
pub line_comment: Override<Arc<str>>,
|
||||
pub line_comments: Override<Vec<Arc<str>>>,
|
||||
#[serde(default)]
|
||||
pub block_comment: Override<(Arc<str>, Arc<str>)>,
|
||||
#[serde(skip_deserializing)]
|
||||
|
@ -506,7 +508,7 @@ impl Default for LanguageConfig {
|
|||
increase_indent_pattern: Default::default(),
|
||||
decrease_indent_pattern: Default::default(),
|
||||
autoclose_before: Default::default(),
|
||||
line_comment: Default::default(),
|
||||
line_comments: Default::default(),
|
||||
block_comment: Default::default(),
|
||||
scope_opt_in_language_servers: Default::default(),
|
||||
overrides: Default::default(),
|
||||
|
@ -1710,10 +1712,10 @@ impl LanguageScope {
|
|||
|
||||
/// Returns line prefix that is inserted in e.g. line continuations or
|
||||
/// in `toggle comments` action.
|
||||
pub fn line_comment_prefix(&self) -> Option<&Arc<str>> {
|
||||
pub fn line_comment_prefixes(&self) -> Option<&Vec<Arc<str>>> {
|
||||
Override::as_option(
|
||||
self.config_override().map(|o| &o.line_comment),
|
||||
self.language.config.line_comment.as_ref(),
|
||||
self.config_override().map(|o| &o.line_comments),
|
||||
Some(&self.language.config.line_comments),
|
||||
)
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue