Unnamed repository; edit this file 'description' to name the repository.
Auto merge of #13975 - DropDemBits:on-enter-after-dot-chains, r=DropDemBits
fix: Suppress extra indent after the end of field and function chains (spurred on by <https://github.com/rust-lang/rust-analyzer/issues/4182#issuecomment-671275652>) 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 :shrug: } ```
bors 2023-02-11
parent d9c020d · parent c7bd3c6 · commit 8011029
-rw-r--r--editors/code/src/config.ts122
1 files changed, 74 insertions, 48 deletions
diff --git a/editors/code/src/config.ts b/editors/code/src/config.ts
index 114abf062b..1faa0ad910 100644
--- a/editors/code/src/config.ts
+++ b/editors/code/src/config.ts
@@ -87,58 +87,84 @@ export class Config {
* [1]: https://github.com/Microsoft/vscode/issues/11514#issuecomment-244707076
*/
private configureLanguage() {
- if (this.typingContinueCommentsOnNewline && !this.configureLang) {
+ // Only need to dispose of the config if there's a change
+ if (this.configureLang) {
+ this.configureLang.dispose();
+ this.configureLang = undefined;
+ }
+
+ let onEnterRules: vscode.OnEnterRule[] = [
+ {
+ // Carry indentation from the previous line
+ beforeText: /^\s*$/,
+ action: { indentAction: vscode.IndentAction.None },
+ },
+ {
+ // After the end of a function/field chain,
+ // with the semicolon on the same line
+ beforeText: /^\s+\..*;/,
+ action: { indentAction: vscode.IndentAction.Outdent },
+ },
+ {
+ // After the end of a function/field chain,
+ // with semicolon detached from the rest
+ beforeText: /^\s+;/,
+ previousLineText: /^\s+\..*/,
+ action: { indentAction: vscode.IndentAction.Outdent },
+ },
+ ];
+
+ if (this.typingContinueCommentsOnNewline) {
const indentAction = vscode.IndentAction.None;
- this.configureLang = vscode.languages.setLanguageConfiguration("rust", {
- onEnterRules: [
- {
- // Doc single-line comment
- // e.g. ///|
- beforeText: /^\s*\/{3}.*$/,
- action: { indentAction, appendText: "/// " },
- },
- {
- // Parent doc single-line comment
- // e.g. //!|
- beforeText: /^\s*\/{2}\!.*$/,
- action: { indentAction, appendText: "//! " },
- },
- {
- // Begins an auto-closed multi-line comment (standard or parent doc)
- // e.g. /** | */ or /*! | */
- beforeText: /^\s*\/\*(\*|\!)(?!\/)([^\*]|\*(?!\/))*$/,
- afterText: /^\s*\*\/$/,
- action: {
- indentAction: vscode.IndentAction.IndentOutdent,
- appendText: " * ",
- },
- },
- {
- // Begins a multi-line comment (standard or parent doc)
- // e.g. /** ...| or /*! ...|
- beforeText: /^\s*\/\*(\*|\!)(?!\/)([^\*]|\*(?!\/))*$/,
- action: { indentAction, appendText: " * " },
+ onEnterRules = [
+ ...onEnterRules,
+ {
+ // Doc single-line comment
+ // e.g. ///|
+ beforeText: /^\s*\/{3}.*$/,
+ action: { indentAction, appendText: "/// " },
+ },
+ {
+ // Parent doc single-line comment
+ // e.g. //!|
+ beforeText: /^\s*\/{2}\!.*$/,
+ action: { indentAction, appendText: "//! " },
+ },
+ {
+ // Begins an auto-closed multi-line comment (standard or parent doc)
+ // e.g. /** | */ or /*! | */
+ beforeText: /^\s*\/\*(\*|\!)(?!\/)([^\*]|\*(?!\/))*$/,
+ afterText: /^\s*\*\/$/,
+ action: {
+ indentAction: vscode.IndentAction.IndentOutdent,
+ appendText: " * ",
},
- {
- // Continues a multi-line comment
- // e.g. * ...|
- beforeText: /^(\ \ )*\ \*(\ ([^\*]|\*(?!\/))*)?$/,
- action: { indentAction, appendText: "* " },
- },
- {
- // Dedents after closing a multi-line comment
- // e.g. */|
- beforeText: /^(\ \ )*\ \*\/\s*$/,
- action: { indentAction, removeText: 1 },
- },
- ],
- });
- }
- if (!this.typingContinueCommentsOnNewline && this.configureLang) {
- this.configureLang.dispose();
- this.configureLang = undefined;
+ },
+ {
+ // Begins a multi-line comment (standard or parent doc)
+ // e.g. /** ...| or /*! ...|
+ beforeText: /^\s*\/\*(\*|\!)(?!\/)([^\*]|\*(?!\/))*$/,
+ action: { indentAction, appendText: " * " },
+ },
+ {
+ // Continues a multi-line comment
+ // e.g. * ...|
+ beforeText: /^(\ \ )*\ \*(\ ([^\*]|\*(?!\/))*)?$/,
+ action: { indentAction, appendText: "* " },
+ },
+ {
+ // Dedents after closing a multi-line comment
+ // e.g. */|
+ beforeText: /^(\ \ )*\ \*\/\s*$/,
+ action: { indentAction, removeText: 1 },
+ },
+ ];
}
+
+ this.configureLang = vscode.languages.setLanguageConfiguration("rust", {
+ onEnterRules,
+ });
}
// We don't do runtime config validation here for simplicity. More on stackoverflow: