forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblockFormatProvider.ts
More file actions
69 lines (60 loc) · 2.69 KB
/
blockFormatProvider.ts
File metadata and controls
69 lines (60 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { OnTypeFormattingEditProvider, FormattingOptions, TextEdit, CancellationToken, TextDocument } from 'vscode';
import { Position } from 'vscode';
import { CodeBlockFormatProvider } from './codeBlockFormatProvider';
import { IF_REGEX, ELIF_REGEX, ELSE_REGEX, FOR_IN_REGEX, ASYNC_FOR_IN_REGEX, WHILE_REGEX } from './contracts';
import { TRY_REGEX, FINALLY_REGEX, EXCEPT_REGEX } from './contracts';
import { DEF_REGEX, ASYNC_DEF_REGEX, CLASS_REGEX } from './contracts';
export class BlockFormatProviders implements OnTypeFormattingEditProvider {
private providers: CodeBlockFormatProvider[];
constructor() {
this.providers = [];
const boundaryBlocks = [
DEF_REGEX,
ASYNC_DEF_REGEX,
CLASS_REGEX
];
const elseParentBlocks = [
IF_REGEX,
ELIF_REGEX,
FOR_IN_REGEX,
ASYNC_FOR_IN_REGEX,
WHILE_REGEX,
TRY_REGEX,
EXCEPT_REGEX
];
this.providers.push(new CodeBlockFormatProvider(ELSE_REGEX, elseParentBlocks, boundaryBlocks));
const elifParentBlocks = [
IF_REGEX,
ELIF_REGEX
];
this.providers.push(new CodeBlockFormatProvider(ELIF_REGEX, elifParentBlocks, boundaryBlocks));
const exceptParentBlocks = [
TRY_REGEX,
EXCEPT_REGEX
];
this.providers.push(new CodeBlockFormatProvider(EXCEPT_REGEX, exceptParentBlocks, boundaryBlocks));
const finallyParentBlocks = [
TRY_REGEX,
EXCEPT_REGEX
];
this.providers.push(new CodeBlockFormatProvider(FINALLY_REGEX, finallyParentBlocks, boundaryBlocks));
}
provideOnTypeFormattingEdits(document: TextDocument, position: Position, ch: string, options: FormattingOptions, token: CancellationToken): TextEdit[] {
if (position.line === 0) {
return [];
}
const currentLine = document.lineAt(position.line);
const prevousLine = document.lineAt(position.line - 1);
// We're only interested in cases where the current block is at the same indentation level as the previous line
// 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...
if (currentLine.firstNonWhitespaceCharacterIndex !== prevousLine.firstNonWhitespaceCharacterIndex) {
return [];
}
const currentLineText = currentLine.text;
const provider = this.providers.find(provider => provider.canProvideEdits(currentLineText));
if (provider) {
return provider.provideEdits(document, position, ch, options, currentLine);
}
return [];
}
}