Add option to disable auto indentation (#36259)
Closes https://github.com/zed-industries/zed/issues/11780 While auto indentation is generally nice to have, there are cases where it is currently just not good enough for some languages (e.g. Haskell) or users just straight up do not want their editor to auto indent for them. Hence, this PR adds the possibilty to disable auto indentation for either all language or on a per-language basis. Manual invocation via the `editor: auto indent` action will continue to work. Also takes a similar approach as https://github.com/zed-industries/zed/pull/31569 to ensure performance is fine for larger multicursor edits. Release Notes: - Added the possibility to configure auto indentation for all languages and per language. Add `"auto_indent": false"` to your settings or desired language to disable the feature.
This commit is contained in:
parent
5225844c9e
commit
1add1d042d
4 changed files with 250 additions and 14 deletions
|
@ -2271,13 +2271,11 @@ impl Buffer {
|
|||
}
|
||||
let new_text = new_text.into();
|
||||
if !new_text.is_empty() || !range.is_empty() {
|
||||
if let Some((prev_range, prev_text)) = edits.last_mut() {
|
||||
if prev_range.end >= range.start {
|
||||
prev_range.end = cmp::max(prev_range.end, range.end);
|
||||
*prev_text = format!("{prev_text}{new_text}").into();
|
||||
} else {
|
||||
edits.push((range, new_text));
|
||||
}
|
||||
if let Some((prev_range, prev_text)) = edits.last_mut()
|
||||
&& prev_range.end >= range.start
|
||||
{
|
||||
prev_range.end = cmp::max(prev_range.end, range.end);
|
||||
*prev_text = format!("{prev_text}{new_text}").into();
|
||||
} else {
|
||||
edits.push((range, new_text));
|
||||
}
|
||||
|
@ -2297,10 +2295,27 @@ impl Buffer {
|
|||
|
||||
if let Some((before_edit, mode)) = autoindent_request {
|
||||
let mut delta = 0isize;
|
||||
let entries = edits
|
||||
let mut previous_setting = None;
|
||||
let entries: Vec<_> = edits
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.zip(&edit_operation.as_edit().unwrap().new_text)
|
||||
.filter(|((_, (range, _)), _)| {
|
||||
let language = before_edit.language_at(range.start);
|
||||
let language_id = language.map(|l| l.id());
|
||||
if let Some((cached_language_id, auto_indent)) = previous_setting
|
||||
&& cached_language_id == language_id
|
||||
{
|
||||
auto_indent
|
||||
} else {
|
||||
// The auto-indent setting is not present in editorconfigs, hence
|
||||
// we can avoid passing the file here.
|
||||
let auto_indent =
|
||||
language_settings(language.map(|l| l.name()), None, cx).auto_indent;
|
||||
previous_setting = Some((language_id, auto_indent));
|
||||
auto_indent
|
||||
}
|
||||
})
|
||||
.map(|((ix, (range, _)), new_text)| {
|
||||
let new_text_length = new_text.len();
|
||||
let old_start = range.start.to_point(&before_edit);
|
||||
|
@ -2374,12 +2389,14 @@ impl Buffer {
|
|||
})
|
||||
.collect();
|
||||
|
||||
self.autoindent_requests.push(Arc::new(AutoindentRequest {
|
||||
before_edit,
|
||||
entries,
|
||||
is_block_mode: matches!(mode, AutoindentMode::Block { .. }),
|
||||
ignore_empty_lines: false,
|
||||
}));
|
||||
if !entries.is_empty() {
|
||||
self.autoindent_requests.push(Arc::new(AutoindentRequest {
|
||||
before_edit,
|
||||
entries,
|
||||
is_block_mode: matches!(mode, AutoindentMode::Block { .. }),
|
||||
ignore_empty_lines: false,
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
self.end_transaction(cx);
|
||||
|
|
|
@ -133,6 +133,8 @@ pub struct LanguageSettings {
|
|||
/// Whether to use additional LSP queries to format (and amend) the code after
|
||||
/// every "trigger" symbol input, defined by LSP server capabilities.
|
||||
pub use_on_type_format: bool,
|
||||
/// Whether indentation should be adjusted based on the context whilst typing.
|
||||
pub auto_indent: bool,
|
||||
/// Whether indentation of pasted content should be adjusted based on the context.
|
||||
pub auto_indent_on_paste: bool,
|
||||
/// Controls how the editor handles the autoclosed characters.
|
||||
|
@ -561,6 +563,10 @@ pub struct LanguageSettingsContent {
|
|||
///
|
||||
/// Default: true
|
||||
pub linked_edits: Option<bool>,
|
||||
/// Whether indentation should be adjusted based on the context whilst typing.
|
||||
///
|
||||
/// Default: true
|
||||
pub auto_indent: Option<bool>,
|
||||
/// Whether indentation of pasted content should be adjusted based on the context.
|
||||
///
|
||||
/// Default: true
|
||||
|
@ -1517,6 +1523,7 @@ fn merge_settings(settings: &mut LanguageSettings, src: &LanguageSettingsContent
|
|||
merge(&mut settings.use_autoclose, src.use_autoclose);
|
||||
merge(&mut settings.use_auto_surround, src.use_auto_surround);
|
||||
merge(&mut settings.use_on_type_format, src.use_on_type_format);
|
||||
merge(&mut settings.auto_indent, src.auto_indent);
|
||||
merge(&mut settings.auto_indent_on_paste, src.auto_indent_on_paste);
|
||||
merge(
|
||||
&mut settings.always_treat_brackets_as_autoclosed,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue