editor: Add language setting for comment continuations (#2605)

Per @JosephTLyons request I've added a language setting for comment
continuations.

Release Notes:

- Added a language setting for comment continuations.
This commit is contained in:
Piotr Osiewicz 2023-06-13 18:59:46 +02:00 committed by GitHub
parent aedef7bc58
commit b272db9e21
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 98 additions and 72 deletions

View file

@ -108,6 +108,8 @@
// Whether or not to remove any trailing whitespace from lines of a buffer // Whether or not to remove any trailing whitespace from lines of a buffer
// before saving it. // before saving it.
"remove_trailing_whitespace_on_save": true, "remove_trailing_whitespace_on_save": true,
// Whether to start a new line with a comment when a previous line is a comment as well.
"extend_comment_on_newline": true,
// Whether or not to ensure there's a single newline at the end of a buffer // Whether or not to ensure there's a single newline at the end of a buffer
// when saving it. // when saving it.
"ensure_final_newline_on_save": true, "ensure_final_newline_on_save": true,

View file

@ -2169,8 +2169,8 @@ impl Editor {
self.transact(cx, |this, cx| { self.transact(cx, |this, cx| {
let (edits, selection_fixup_info): (Vec<_>, Vec<_>) = { let (edits, selection_fixup_info): (Vec<_>, Vec<_>) = {
let selections = this.selections.all::<usize>(cx); let selections = this.selections.all::<usize>(cx);
let multi_buffer = this.buffer.read(cx);
let buffer = this.buffer.read(cx).snapshot(cx); let buffer = multi_buffer.snapshot(cx);
selections selections
.iter() .iter()
.map(|selection| { .map(|selection| {
@ -2181,8 +2181,9 @@ impl Editor {
let end = selection.end; let end = selection.end;
let is_cursor = start == end; let is_cursor = start == end;
let language_scope = buffer.language_scope_at(start); let language_scope = buffer.language_scope_at(start);
let (comment_delimiter, insert_extra_newline) = let (comment_delimiter, insert_extra_newline) = if let Some(language) =
if let Some(language) = &language_scope { &language_scope
{
let leading_whitespace_len = buffer let leading_whitespace_len = buffer
.reversed_chars_at(start) .reversed_chars_at(start)
.take_while(|c| c.is_whitespace() && *c != '\n') .take_while(|c| c.is_whitespace() && *c != '\n')
@ -2213,8 +2214,11 @@ impl Editor {
) )
}); });
// Comment extension on newline is allowed only for cursor selections // Comment extension on newline is allowed only for cursor selections
let comment_delimiter = let comment_delimiter = language.line_comment_prefix().filter(|_| {
language.line_comment_prefix().filter(|_| is_cursor); let is_comment_extension_enabled =
multi_buffer.settings_at(0, cx).extend_comment_on_newline;
is_cursor && is_comment_extension_enabled
});
let comment_delimiter = if let Some(delimiter) = comment_delimiter { let comment_delimiter = if let Some(delimiter) = comment_delimiter {
buffer buffer
.buffer_line_for_row(start_point.row) .buffer_line_for_row(start_point.row)

View file

@ -1732,7 +1732,7 @@ async fn test_newline_comments(cx: &mut gpui::TestAppContext) {
}, },
None, None,
)); ));
{
let mut cx = EditorTestContext::new(cx).await; let mut cx = EditorTestContext::new(cx).await;
cx.update_buffer(|buffer, cx| buffer.set_language(Some(language), cx)); cx.update_buffer(|buffer, cx| buffer.set_language(Some(language), cx));
cx.set_state(indoc! {" cx.set_state(indoc! {"
@ -1754,6 +1754,20 @@ async fn test_newline_comments(cx: &mut gpui::TestAppContext) {
ˇ// Foo ˇ// Foo
"}); "});
} }
// Ensure that comment continuations can be disabled.
update_test_settings(cx, |settings| {
settings.defaults.extend_comment_on_newline = Some(false);
});
let mut cx = EditorTestContext::new(cx).await;
cx.set_state(indoc! {"
// Fooˇ
"});
cx.update_editor(|e, cx| e.newline(&Newline, cx));
cx.assert_editor_state(indoc! {"
// Foo
ˇ
"});
}
#[gpui::test] #[gpui::test]
fn test_insert_with_old_selections(cx: &mut TestAppContext) { fn test_insert_with_old_selections(cx: &mut TestAppContext) {

View file

@ -51,6 +51,7 @@ pub struct LanguageSettings {
pub enable_language_server: bool, pub enable_language_server: bool,
pub show_copilot_suggestions: bool, pub show_copilot_suggestions: bool,
pub show_whitespaces: ShowWhitespaceSetting, pub show_whitespaces: ShowWhitespaceSetting,
pub extend_comment_on_newline: bool,
} }
#[derive(Clone, Debug, Default)] #[derive(Clone, Debug, Default)]
@ -95,6 +96,8 @@ pub struct LanguageSettingsContent {
pub show_copilot_suggestions: Option<bool>, pub show_copilot_suggestions: Option<bool>,
#[serde(default)] #[serde(default)]
pub show_whitespaces: Option<ShowWhitespaceSetting>, pub show_whitespaces: Option<ShowWhitespaceSetting>,
#[serde(default)]
pub extend_comment_on_newline: Option<bool>,
} }
#[derive(Clone, Debug, Default, Serialize, Deserialize, JsonSchema)] #[derive(Clone, Debug, Default, Serialize, Deserialize, JsonSchema)]
@ -340,7 +343,10 @@ fn merge_settings(settings: &mut LanguageSettings, src: &LanguageSettingsContent
src.show_copilot_suggestions, src.show_copilot_suggestions,
); );
merge(&mut settings.show_whitespaces, src.show_whitespaces); merge(&mut settings.show_whitespaces, src.show_whitespaces);
merge(
&mut settings.extend_comment_on_newline,
src.extend_comment_on_newline,
);
fn merge<T>(target: &mut T, value: Option<T>) { fn merge<T>(target: &mut T, value: Option<T>) {
if let Some(value) = value { if let Some(value) = value {
*target = value; *target = value;