Add support for auto surround (#13217)

![result](https://github.com/zed-industries/zed/assets/32017007/c400081f-be5d-48fa-994f-90a00e2be359)

In the past, Zed used a single switch called `autoclose` to control both
`autoclose` and `auto_surround` functionalities:
+ `autoclose`: when input '(', append ')' automatically.
+ `auto_surround`: when select text and input '(', surround text with
'(' and ')' automatically.

This PR separates `auto_surround` from `autoclose` to support `<`. 

Previously, if `autoclose` of `<` was set to `false`, `auto_surround`
couldn't be used. However, setting `autoclose` to `true` would affect
the default behavior of simple expression. For example, `a < b` would
become `a <> b`.

For more information, see #13187.

Fix #12898.

Release Notes:

- Added support for `auto_surround`
([#12898](https://github.com/zed-industries/zed/issues/12898)).
This commit is contained in:
ᴀᴍᴛᴏᴀᴇʀ 2024-06-20 17:48:46 +08:00 committed by GitHub
parent 963b0c010a
commit 95b06097ee
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 96 additions and 6 deletions

View file

@ -40,6 +40,7 @@ pub fn add_surrounds(text: Arc<str>, target: SurroundsType, cx: &mut WindowConte
start: text.to_string(),
end: text.to_string(),
close: true,
surround: true,
newline: false,
},
};
@ -227,6 +228,7 @@ pub fn change_surrounds(text: Arc<str>, target: Object, cx: &mut WindowContext)
start: text.to_string(),
end: text.to_string(),
close: true,
surround: true,
newline: false,
},
};
@ -388,54 +390,63 @@ fn all_support_surround_pair() -> Vec<BracketPair> {
start: "{".into(),
end: "}".into(),
close: true,
surround: true,
newline: false,
},
BracketPair {
start: "'".into(),
end: "'".into(),
close: true,
surround: true,
newline: false,
},
BracketPair {
start: "`".into(),
end: "`".into(),
close: true,
surround: true,
newline: false,
},
BracketPair {
start: "\"".into(),
end: "\"".into(),
close: true,
surround: true,
newline: false,
},
BracketPair {
start: "(".into(),
end: ")".into(),
close: true,
surround: true,
newline: false,
},
BracketPair {
start: "|".into(),
end: "|".into(),
close: true,
surround: true,
newline: false,
},
BracketPair {
start: "[".into(),
end: "]".into(),
close: true,
surround: true,
newline: false,
},
BracketPair {
start: "{".into(),
end: "}".into(),
close: true,
surround: true,
newline: false,
},
BracketPair {
start: "<".into(),
end: ">".into(),
close: true,
surround: true,
newline: false,
},
];
@ -461,48 +472,56 @@ fn object_to_bracket_pair(object: Object) -> Option<BracketPair> {
start: "'".to_string(),
end: "'".to_string(),
close: true,
surround: true,
newline: false,
}),
Object::BackQuotes => Some(BracketPair {
start: "`".to_string(),
end: "`".to_string(),
close: true,
surround: true,
newline: false,
}),
Object::DoubleQuotes => Some(BracketPair {
start: "\"".to_string(),
end: "\"".to_string(),
close: true,
surround: true,
newline: false,
}),
Object::VerticalBars => Some(BracketPair {
start: "|".to_string(),
end: "|".to_string(),
close: true,
surround: true,
newline: false,
}),
Object::Parentheses => Some(BracketPair {
start: "(".to_string(),
end: ")".to_string(),
close: true,
surround: true,
newline: false,
}),
Object::SquareBrackets => Some(BracketPair {
start: "[".to_string(),
end: "]".to_string(),
close: true,
surround: true,
newline: false,
}),
Object::CurlyBrackets => Some(BracketPair {
start: "{".to_string(),
end: "}".to_string(),
close: true,
surround: true,
newline: false,
}),
Object::AngleBrackets => Some(BracketPair {
start: "<".to_string(),
end: ">".to_string(),
close: true,
surround: true,
newline: false,
}),
_ => None,