Skip to content

Commit 8011029

Browse files
committed
Auto merge of rust-lang#13975 - DropDemBits:on-enter-after-dot-chains, r=DropDemBits
fix: Suppress extra indent after the end of field and function chains (spurred on by <rust-lang/rust-analyzer#4182 (comment)>) Caveat that this doesn't work for after tail expressions, although there shouldn't be anything after those anyways. This also complicates when to reload the language configuration by nature of now always having a language configuration applicable. Examples of indentation fixes: ```rs fn main() { println!("Hello!"); // < enter here! // ... indents down here fs::read_to_string("soup") // < enter here! // ... still indents down here :( .map(|_| ()) .map(|_| ()) // < enter here! // ... still indents down here :D .map_err(|_| ()) .unwrap(); // < enter here! // ... indents down here :D // ... and subsequent enters stay at the same indent 0.0f64 .to_radians() .to_radians() .to_radians() // force semi on a new line ; // < enter here! // ... indents down here :D } fn tail_end() -> i32 { 0i32.wrapping_abs() .wrapping_abs() .wrapping_abs() .wrapping_abs() // < enter here! // ... still indents here 🤷 } ```
2 parents d9c020d + c7bd3c6 commit 8011029

File tree

1 file changed

+74
-48
lines changed

1 file changed

+74
-48
lines changed

editors/code/src/config.ts

Lines changed: 74 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -87,58 +87,84 @@ export class Config {
8787
* [1]: https://github.com/Microsoft/vscode/issues/11514#issuecomment-244707076
8888
*/
8989
private configureLanguage() {
90-
if (this.typingContinueCommentsOnNewline && !this.configureLang) {
90+
// Only need to dispose of the config if there's a change
91+
if (this.configureLang) {
92+
this.configureLang.dispose();
93+
this.configureLang = undefined;
94+
}
95+
96+
let onEnterRules: vscode.OnEnterRule[] = [
97+
{
98+
// Carry indentation from the previous line
99+
beforeText: /^\s*$/,
100+
action: { indentAction: vscode.IndentAction.None },
101+
},
102+
{
103+
// After the end of a function/field chain,
104+
// with the semicolon on the same line
105+
beforeText: /^\s+\..*;/,
106+
action: { indentAction: vscode.IndentAction.Outdent },
107+
},
108+
{
109+
// After the end of a function/field chain,
110+
// with semicolon detached from the rest
111+
beforeText: /^\s+;/,
112+
previousLineText: /^\s+\..*/,
113+
action: { indentAction: vscode.IndentAction.Outdent },
114+
},
115+
];
116+
117+
if (this.typingContinueCommentsOnNewline) {
91118
const indentAction = vscode.IndentAction.None;
92119

93-
this.configureLang = vscode.languages.setLanguageConfiguration("rust", {
94-
onEnterRules: [
95-
{
96-
// Doc single-line comment
97-
// e.g. ///|
98-
beforeText: /^\s*\/{3}.*$/,
99-
action: { indentAction, appendText: "/// " },
100-
},
101-
{
102-
// Parent doc single-line comment
103-
// e.g. //!|
104-
beforeText: /^\s*\/{2}\!.*$/,
105-
action: { indentAction, appendText: "//! " },
106-
},
107-
{
108-
// Begins an auto-closed multi-line comment (standard or parent doc)
109-
// e.g. /** | */ or /*! | */
110-
beforeText: /^\s*\/\*(\*|\!)(?!\/)([^\*]|\*(?!\/))*$/,
111-
afterText: /^\s*\*\/$/,
112-
action: {
113-
indentAction: vscode.IndentAction.IndentOutdent,
114-
appendText: " * ",
115-
},
116-
},
117-
{
118-
// Begins a multi-line comment (standard or parent doc)
119-
// e.g. /** ...| or /*! ...|
120-
beforeText: /^\s*\/\*(\*|\!)(?!\/)([^\*]|\*(?!\/))*$/,
121-
action: { indentAction, appendText: " * " },
120+
onEnterRules = [
121+
...onEnterRules,
122+
{
123+
// Doc single-line comment
124+
// e.g. ///|
125+
beforeText: /^\s*\/{3}.*$/,
126+
action: { indentAction, appendText: "/// " },
127+
},
128+
{
129+
// Parent doc single-line comment
130+
// e.g. //!|
131+
beforeText: /^\s*\/{2}\!.*$/,
132+
action: { indentAction, appendText: "//! " },
133+
},
134+
{
135+
// Begins an auto-closed multi-line comment (standard or parent doc)
136+
// e.g. /** | */ or /*! | */
137+
beforeText: /^\s*\/\*(\*|\!)(?!\/)([^\*]|\*(?!\/))*$/,
138+
afterText: /^\s*\*\/$/,
139+
action: {
140+
indentAction: vscode.IndentAction.IndentOutdent,
141+
appendText: " * ",
122142
},
123-
{
124-
// Continues a multi-line comment
125-
// e.g. * ...|
126-
beforeText: /^(\ \ )*\ \*(\ ([^\*]|\*(?!\/))*)?$/,
127-
action: { indentAction, appendText: "* " },
128-
},
129-
{
130-
// Dedents after closing a multi-line comment
131-
// e.g. */|
132-
beforeText: /^(\ \ )*\ \*\/\s*$/,
133-
action: { indentAction, removeText: 1 },
134-
},
135-
],
136-
});
137-
}
138-
if (!this.typingContinueCommentsOnNewline && this.configureLang) {
139-
this.configureLang.dispose();
140-
this.configureLang = undefined;
143+
},
144+
{
145+
// Begins a multi-line comment (standard or parent doc)
146+
// e.g. /** ...| or /*! ...|
147+
beforeText: /^\s*\/\*(\*|\!)(?!\/)([^\*]|\*(?!\/))*$/,
148+
action: { indentAction, appendText: " * " },
149+
},
150+
{
151+
// Continues a multi-line comment
152+
// e.g. * ...|
153+
beforeText: /^(\ \ )*\ \*(\ ([^\*]|\*(?!\/))*)?$/,
154+
action: { indentAction, appendText: "* " },
155+
},
156+
{
157+
// Dedents after closing a multi-line comment
158+
// e.g. */|
159+
beforeText: /^(\ \ )*\ \*\/\s*$/,
160+
action: { indentAction, removeText: 1 },
161+
},
162+
];
141163
}
164+
165+
this.configureLang = vscode.languages.setLanguageConfiguration("rust", {
166+
onEnterRules,
167+
});
142168
}
143169

144170
// We don't do runtime config validation here for simplicity. More on stackoverflow:

0 commit comments

Comments
 (0)