From 6967ff2357b48172e727b45c19216d078c2a8d51 Mon Sep 17 00:00:00 2001 From: Peter Law Date: Sat, 27 Feb 2021 09:26:08 +0000 Subject: [PATCH] Don't outdent on enter after raise with arguments This changes the behaviour of pressing enter after a raise statement which involves an explicit value so that the cursor position is no longer outdented. Previously this worked well for things like: raise raise e but less well for things like: raise Exception(|) which wouuld end up like: raise Exception( |) With this change applied the latter case will end up like: raise Exception( | ) which matches the behaviour of 'return'. The first case (without a value) is unaffected, though the case of: raise e will, just like 'return' now require the user to explicitly outdent after pressing enter. It is expected that this is an acceptable trade-off, especially as it is already the case for return statements. Fixes https://github.com/microsoft/vscode-python/issues/10583. --- news/2 Fixes/10583.md | 4 ++++ src/client/language/languageConfiguration.ts | 10 ++++++++-- src/test/language/languageConfiguration.unit.test.ts | 7 +++++-- 3 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 news/2 Fixes/10583.md diff --git a/news/2 Fixes/10583.md b/news/2 Fixes/10583.md new file mode 100644 index 000000000000..3052bb2a28ea --- /dev/null +++ b/news/2 Fixes/10583.md @@ -0,0 +1,4 @@ +Make on-enter behaviour after `raise` much more like that of `return`, fixing +handling in the case of pressing enter to wrap the parentheses of an exception +call. +(thanks [PeterJCLaw](https://github.com/PeterJCLaw)) diff --git a/src/client/language/languageConfiguration.ts b/src/client/language/languageConfiguration.ts index 8dfe6dc0174b..13434a9ecf55 100644 --- a/src/client/language/languageConfiguration.ts +++ b/src/client/language/languageConfiguration.ts @@ -76,14 +76,20 @@ export function getLanguageConfiguration(): LanguageConfiguration { }, // outdent on enter (block-ending statements) { + /** + * This does not handle all cases. Notable omissions here are + * "return" and "raise" which are complicated by the need to + * only outdent when the cursor is at the end of an expression + * rather than, say, between the parentheses of a tail-call or + * exception construction. (see issue #10583) + */ beforeText: verboseRegExp(` ^ (?: (?: \\s* (?: - pass | - raise \\s+ [^#\\s] [^#]* + pass ) ) | (?: diff --git a/src/test/language/languageConfiguration.unit.test.ts b/src/test/language/languageConfiguration.unit.test.ts index 87f89ae9221a..a9f74dff2c1a 100644 --- a/src/test/language/languageConfiguration.unit.test.ts +++ b/src/test/language/languageConfiguration.unit.test.ts @@ -10,7 +10,8 @@ import { getLanguageConfiguration } from '../../client/language/languageConfigur const NEEDS_INDENT = [ /^break$/, /^continue$/, - /^raise$/, // only re-raise + // raise is indented unless it's the only thing on the line + /^raise\b/, /^return\b/, ]; const INDENT_ON_ENTER = [ @@ -37,7 +38,9 @@ const DEDENT_ON_ENTER = [ ///^return\b/, /^break$/, /^continue$/, - /^raise\b/, + // For now we are mostly ignoring "return". + // See https://github.com/microsoft/vscode-python/issues/10583. + /^raise$/, /^pass\b/, ];