-
-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathget_body.ts
More file actions
68 lines (50 loc) · 1.62 KB
/
get_body.ts
File metadata and controls
68 lines (50 loc) · 1.62 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
import { blankLine, indentationOf, preprocessLines } from "./utilities";
export function getBody(docstringType: string, document: string, linePosition: number): string[] {
const lines = document.split("\n");
let regex = '\s*def \w+'
if (docstringType === 'module') {
return lines;
}
else if (docstringType === 'class') {
regex = '.';
}
let currentLineNum = linePosition;
const originalIndentation = getBodyBaseIndentation(lines, linePosition, regex);
if (originalIndentation === 0) {
return [];
}
return populateBody(currentLineNum, lines, originalIndentation);
}
function populateBody(currentLineNum: number, lines: string[], originalIndentation: number) {
const body = [];
while (currentLineNum < lines.length) {
const line = lines[currentLineNum];
if (blankLine(line)) {
currentLineNum++;
continue;
}
if (indentationOf(line) < originalIndentation) {
break;
}
body.push(line);
currentLineNum++;
}
return preprocessLines(body);
}
function getBodyBaseIndentation(lines: string[], linePosition: number, regex: string): number {
let currentLineNum = linePosition;
//const functionDefRegex = /\s*def \w+/;
const functionDefRegex = RegExp(regex)
while (currentLineNum < lines.length) {
const line = lines[currentLineNum];
if (blankLine(line)) {
currentLineNum++;
continue;
}
if (functionDefRegex.test(line)) {
break;
}
return indentationOf(line);
}
return 10000;
}