@@ -3085,40 +3085,59 @@ pub mod insert {
3085 let curr = contents.get_char(pos).unwrap_or(' ');
3085 let curr = contents.get_char(pos).unwrap_or(' ');
3087 let current_line = text.char_to_line(pos);
3087 let current_line = text.char_to_line(pos);
3088− let indent = indent::indent_for_newline(
3088+ let line_is_only_whitespace = text
3089− doc.language_config(),
3089+ .line(current_line)
3090− doc.syntax(),
3090+ .chars()
3091− &doc.indent_style,
3091+ .all(|char| char.is_ascii_whitespace());
3092− doc.tab_width(),
3092+
3098− let mut text = String::new();
3093+ let mut new_text = String::new();
3099− // If we are between pairs (such as brackets), we want to
3094+
3100− // insert an additional line which is indented one level
3095+ // If the current line is all whitespace, insert a line ending at the beginning of
3101− // more and place the cursor there
3096+ // the current line. This makes the current line empty and the new line contain the
3102− let on_auto_pair = doc
3097+ // indentation of the old line.
3103− .auto_pairs(cx.editor)
3104− .and_then(|pairs| pairs.get(prev))
3105− .and_then(|pair| if pair.close == curr { Some(pair) } else { None })
3108− let local_offs = if on_auto_pair {
3098+ let (from, to, local_offs) = if line_is_only_whitespace {
3109− let inner_indent = indent.clone() + doc.indent_style.as_str();
3099+ let line_start = text.line_to_char(current_line);
3110− text.reserve_exact(2 + indent.len() + inner_indent.len());
3111− text.push_str(doc.line_ending.as_str());
3100+ new_text.push_str(doc.line_ending.as_str());
3112− text.push_str(&inner_indent);
3101+
3113− let local_offs = text.chars().count();
3102+ (line_start, line_start, new_text.chars().count())
3114− text.push_str(doc.line_ending.as_str());
3115− text.push_str(&indent);
3117 } else {
3103 } else {
3104+ let indent = indent::indent_for_newline(
3105+ doc.language_config(),
3115+ // If we are between pairs (such as brackets), we want to
3116+ // insert an additional line which is indented one level
3117+ // more and place the cursor there
3118+ let on_auto_pair = doc
3119+ .auto_pairs(cx.editor)
3120+ .and_then(|pairs| pairs.get(prev))
3121+ .and_then(|pair| if pair.close == curr { Some(pair) } else { None })
3124+ let local_offs = if on_auto_pair {
3125+ let inner_indent = indent.clone() + doc.indent_style.as_str();
3126+ new_text.reserve_exact(2 + indent.len() + inner_indent.len());
3127+ new_text.push_str(doc.line_ending.as_str());
3128+ new_text.push_str(&inner_indent);
3129+ let local_offs = new_text.chars().count();
3130+ new_text.push_str(doc.line_ending.as_str());
3131+ new_text.push_str(&indent);
3118− text.reserve_exact(1 + indent.len());
3134+ new_text.reserve_exact(1 + indent.len());
3119− text.push_str(doc.line_ending.as_str());
3135+ new_text.push_str(doc.line_ending.as_str());
3120− text.push_str(&indent);
3136+ new_text.push_str(&indent);
3121− text.chars().count()
3137+ new_text.chars().count()
3140+ (pos, pos, local_offs)
3124 let new_range = if doc.restore_cursor {
3143 let new_range = if doc.restore_cursor {
@@ -3139,9 +3158,9 @@ pub mod insert {
3139 // range.replace(|range| range.is_empty(), head); -> fn extend if cond true, new head pos
3158 // range.replace(|range| range.is_empty(), head); -> fn extend if cond true, new head pos
3140 // can be used with cx.mode to do replace or extend on most changes
3159 // can be used with cx.mode to do replace or extend on most changes
3141 ranges.push(new_range);
3160 ranges.push(new_range);
3142− global_offs += text.chars().count();
3161+ global_offs += new_text.chars().count();
3144− (pos, pos, Some(text.into()))
3163+ (from, to, Some(new_text.into()))
3147 transaction = transaction.with_selection(Selection::new(ranges, selection.primary_index()));
3166 transaction = transaction.with_selection(Selection::new(ranges, selection.primary_index()));