-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.ts
138 lines (116 loc) · 4.6 KB
/
Parser.ts
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { TextDecoder } from 'util';
import * as vscode from 'vscode';
export interface ModelField {
name: string;
type: string | null;
symbol: FixedDocumentSymbol;
}
export interface ModelClass {
name: string
parentFolderName: string
parentClass: string
fileUri: string
fields: ModelField[]
properties: vscode.DocumentSymbol[]
}
interface FixedDocumentSymbol extends vscode.DocumentSymbol {
location: vscode.Location
}
async function getParentClass(location: vscode.Location, name: string): Promise<string | null> {
let searchString = new RegExp(`class\\s+${name}\\((.*)\\)`);
let raw = await vscode.workspace.fs.readFile(location.uri);
let contents = new TextDecoder().decode(raw);
let scopedContent = contents.split('\n').slice(location.range.start.line, location.range.end.line + 1).join('\n');
let matches = searchString.exec(scopedContent);
if(matches){
return matches![1];
}else{
return null;
}
}
async function getFieldType(location: vscode.Location, name: string): Promise<string | null> {
let searchString = new RegExp(`${name}\\s*=\\s*(.+)\\(`);
let raw = await vscode.workspace.fs.readFile(location.uri);
let contents = new TextDecoder().decode(raw);
let matches = searchString.exec(contents.split('\n')[location.range.start.line]);
if(matches){
return matches[1];
}else{
return null;
}
}
export async function searchForModels(): Promise<ModelClass[]> {
let modelFiles = await vscode.workspace.findFiles("**/*.py");
let results: ModelClass[] = [];
let abstracts: {[name: string]: ModelClass} = {};
for (const uri of modelFiles){
let classes = await vscode.commands.executeCommand<FixedDocumentSymbol[]>("vscode.executeDocumentSymbolProvider", uri);
if(classes){
// Parse all classes
classes.forEach(async c => {
if(c.kind === 4){
// Determine what the class is extending
let parentClass = await getParentClass(c.location, c.name);
if(parentClass === null){
return;
}
if(parentClass === "models.Manager"){
return;
}
// Is this an abstract class?
let abstract = c.children.some(s => {
if(s.kind === 4 && s.name === "Meta"){
return s.children.some(a => a.name === "abstract");
}
return false;
});
// Process all fields
let fields = c.children.filter(f => f.kind === 12) as FixedDocumentSymbol[];
let modelFields: ModelField[] = await Promise.all(fields.map(async (f: FixedDocumentSymbol) => {
let fType = await getFieldType(f.location, f.name);
return {
name: f.name,
type: fType,
symbol: f
};
}));
// Pull it all together
let parsed: ModelClass = {
name: c.name,
parentFolderName: uri.path.split('/')[uri.path.split('/').length - 2],
parentClass: parentClass,
fileUri: uri.fsPath,
fields: modelFields,
properties: c.children.filter(f => f.kind === 5)
};
if(abstract){
abstracts[c.name] = parsed;
}else{
results.push(parsed);
}
}
});
}
}
// Filter out classes that aren't models
results = results.filter(r => {
if(r.parentClass === "models.Model"){
return true;
}else{
return abstracts[r.parentClass] !== undefined;
}
});
// Extend Models with abstract classes
results.forEach(r => {
let curParentClass = r.parentClass;
while(curParentClass !== 'models.Model'){
if(abstracts[curParentClass] === undefined){
break; // Abort if the abstract class doesn't exist
}
r.fields.push(...abstracts[curParentClass].fields);
r.properties.push(...abstracts[curParentClass].properties);
curParentClass = abstracts[curParentClass].parentClass;
}
});
return results;
}