forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix indentation after string literals containing escaped characters (#…
…6440) * Update indent regex * Prettier formatting * Add regex tests * Undo prettier changes * Forgot some * One more * Simplify & fix tests * More descriptive comment * Move language config to another file * Remove unneeded tslint disable rule
- Loading branch information
1 parent
0eb059b
commit d69efae
Showing
4 changed files
with
61 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fix indentation after string literals containing escaped characters. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
'use strict'; | ||
|
||
import { IndentAction, languages } from 'vscode'; | ||
import { PYTHON_LANGUAGE } from '../common/constants'; | ||
|
||
export const MULTILINE_SEPARATOR_INDENT_REGEX = /^(?!\s+\\)[^#\n]+\\$/; | ||
|
||
export function setLanguageConfiguration() { | ||
// Enable indentAction | ||
languages.setLanguageConfiguration(PYTHON_LANGUAGE, { | ||
onEnterRules: [ | ||
{ | ||
beforeText: /^\s*(?:def|class|for|if|elif|else|while|try|with|finally|except|async)\b.*:\s*/, | ||
action: { indentAction: IndentAction.Indent } | ||
}, | ||
{ | ||
beforeText: MULTILINE_SEPARATOR_INDENT_REGEX, | ||
action: { indentAction: IndentAction.Indent } | ||
}, | ||
{ | ||
beforeText: /^\s*#.*/, | ||
afterText: /.+$/, | ||
action: { indentAction: IndentAction.None, appendText: '# ' } | ||
}, | ||
{ | ||
beforeText: /^\s+(continue|break|return)\b.*/, | ||
afterText: /\s+$/, | ||
action: { indentAction: IndentAction.Outdent } | ||
} | ||
] | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
'use strict'; | ||
|
||
import { expect } from 'chai'; | ||
|
||
import { MULTILINE_SEPARATOR_INDENT_REGEX } from '../../client/language/languageConfiguration'; | ||
|
||
suite('Language configuration regexes', () => { | ||
test('Multiline separator indent regex should not pick up strings with no multiline separator', async () => { | ||
const result = MULTILINE_SEPARATOR_INDENT_REGEX.test('a = "test"'); | ||
expect (result).to.be.equal(false, 'Multiline separator indent regex for regular strings should not have matches'); | ||
}); | ||
test('Multiline separator indent regex should not pick up strings with escaped characters', async () => { | ||
const result = MULTILINE_SEPARATOR_INDENT_REGEX.test('a = \'hello \\n\''); | ||
expect (result).to.be.equal(false, 'Multiline separator indent regex for strings with escaped characters should not have matches'); | ||
}); | ||
test('Multiline separator indent regex should pick up strings ending with a multiline separator', async () => { | ||
const result = MULTILINE_SEPARATOR_INDENT_REGEX.test('a = \'multiline \\'); | ||
expect (result).to.be.equal(true, 'Multiline separator indent regex for strings with newline separator should have matches'); | ||
}); | ||
}); |