|
1 | 1 | import { OnTypeFormattingEditProvider, FormattingOptions, TextEdit, CancellationToken, TextDocument } from 'vscode'; |
2 | 2 | import { Position } from 'vscode'; |
3 | | -import { CodeBlockFormatProvider } from './contracts'; |
4 | | -import { ElseFormatProvider } from './elseFormatProvider'; |
| 3 | +import { CodeBlockFormatProvider } from './codeBlockFormatProvider'; |
| 4 | +import { IF_REGEX, ELIF_REGEX, ELSE_REGEX, FOR_IN_REGEX, ASYNC_FOR_IN_REGEX, WHILE_REGEX } from './contracts'; |
| 5 | +import { TRY_REGEX, FINALLY_REGEX, EXCEPT_REGEX } from './contracts'; |
| 6 | +import { DEF_REGEX, ASYNC_DEF_REGEX, CLASS_REGEX } from './contracts'; |
5 | 7 |
|
6 | 8 | export class BlockFormatProviders implements OnTypeFormattingEditProvider { |
7 | 9 | private providers: CodeBlockFormatProvider[]; |
8 | 10 | constructor() { |
9 | 11 | this.providers = []; |
10 | | - this.providers.push(new ElseFormatProvider()); |
| 12 | + const boundaryBlocks = [ |
| 13 | + DEF_REGEX, |
| 14 | + ASYNC_DEF_REGEX, |
| 15 | + CLASS_REGEX |
| 16 | + ]; |
| 17 | + |
| 18 | + const elseParentBlocks = [ |
| 19 | + IF_REGEX, |
| 20 | + ELIF_REGEX, |
| 21 | + FOR_IN_REGEX, |
| 22 | + ASYNC_FOR_IN_REGEX, |
| 23 | + WHILE_REGEX, |
| 24 | + TRY_REGEX, |
| 25 | + EXCEPT_REGEX |
| 26 | + ]; |
| 27 | + this.providers.push(new CodeBlockFormatProvider(ELSE_REGEX, elseParentBlocks, boundaryBlocks)); |
| 28 | + |
| 29 | + const elifParentBlocks = [ |
| 30 | + IF_REGEX, |
| 31 | + ELIF_REGEX |
| 32 | + ]; |
| 33 | + this.providers.push(new CodeBlockFormatProvider(ELIF_REGEX, elifParentBlocks, boundaryBlocks)); |
| 34 | + |
| 35 | + const exceptParentBlocks = [ |
| 36 | + TRY_REGEX, |
| 37 | + EXCEPT_REGEX |
| 38 | + ]; |
| 39 | + this.providers.push(new CodeBlockFormatProvider(EXCEPT_REGEX, exceptParentBlocks, boundaryBlocks)); |
| 40 | + |
| 41 | + const finallyParentBlocks = [ |
| 42 | + TRY_REGEX, |
| 43 | + EXCEPT_REGEX |
| 44 | + ]; |
| 45 | + this.providers.push(new CodeBlockFormatProvider(FINALLY_REGEX, finallyParentBlocks, boundaryBlocks)); |
11 | 46 | } |
12 | 47 | provideOnTypeFormattingEdits(document: TextDocument, position: Position, ch: string, options: FormattingOptions, token: CancellationToken): TextEdit[] { |
13 | 48 | if (position.line === 0) { |
14 | 49 | return []; |
15 | 50 | } |
16 | 51 |
|
17 | | - const currentLine = document.lineAt(position.line).text; |
18 | | - const provider = this.providers.find(provider => provider.canProvideEdits(currentLine)); |
| 52 | + const currentLine = document.lineAt(position.line); |
| 53 | + const prevousLine = document.lineAt(position.line - 1); |
| 54 | + |
| 55 | + // We're only interested in cases where the current block is at the same indentation level as the previous line |
| 56 | + // E.g. if we have an if..else block, generally the else statement would be at the same level as the code in the if... |
| 57 | + if (currentLine.firstNonWhitespaceCharacterIndex !== prevousLine.firstNonWhitespaceCharacterIndex) { |
| 58 | + return []; |
| 59 | + } |
| 60 | + |
| 61 | + const currentLineText = currentLine.text; |
| 62 | + const provider = this.providers.find(provider => provider.canProvideEdits(currentLineText)); |
19 | 63 | if (provider) { |
20 | 64 | return provider.provideEdits(document, position, ch, options, currentLine); |
21 | 65 | } |
|
0 commit comments