Skip to content
This repository was archived by the owner on Jan 19, 2025. It is now read-only.

Commit 0dc618d

Browse files
authored
feat: mutable model for package data (#344)
* feat: start building mutable model * fix: use inheritance rather than delegation. Passed delegate list might not always be empty. * fix(partial): update references on container of new value * fix(partial): update references on container of new value (2) * test: TreeNode * test: ContainmentReference * test: ContainmentList * feat: mutable model for Python AST * fix: test
1 parent 3a4d41f commit 0dc618d

File tree

7 files changed

+1056
-0
lines changed

7 files changed

+1056
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
@file:Suppress("unused")
2+
3+
package com.larsreimann.api_editor.mutable_model
4+
5+
import com.larsreimann.api_editor.model.Boundary
6+
import com.larsreimann.api_editor.model.EditorAnnotation
7+
import com.larsreimann.api_editor.model.PythonEnumInstance
8+
import com.larsreimann.api_editor.model.PythonFromImport
9+
import com.larsreimann.api_editor.model.PythonImport
10+
import com.larsreimann.api_editor.model.PythonParameterAssignment
11+
12+
private sealed class PythonAstNode : TreeNode()
13+
14+
private sealed class PythonDeclaration(
15+
var name: String,
16+
val annotations: MutableList<EditorAnnotation> = mutableListOf()
17+
) : PythonAstNode() {
18+
19+
/**
20+
* Returns the qualified name of the declaration.
21+
*/
22+
fun qualifiedName(): String {
23+
return ancestorsOrSelf()
24+
.filterIsInstance<PythonDeclaration>()
25+
.toList()
26+
.asReversed()
27+
.joinToString(separator = ".") { it.name }
28+
}
29+
}
30+
31+
private class PythonPackage(
32+
var distribution: String,
33+
name: String,
34+
var version: String,
35+
modules: List<PythonModule> = emptyList(),
36+
annotations: MutableList<EditorAnnotation> = mutableListOf()
37+
) : PythonDeclaration(name, annotations) {
38+
39+
val modules = ContainmentList(modules)
40+
41+
override fun children() = sequence {
42+
yieldAll(modules)
43+
}
44+
}
45+
46+
private class PythonModule(
47+
name: String,
48+
val imports: MutableList<PythonImport> = mutableListOf(),
49+
val fromImports: MutableList<PythonFromImport> = mutableListOf(),
50+
classes: List<PythonClass> = emptyList(),
51+
enums: List<PythonEnum> = emptyList(),
52+
functions: List<PythonFunction> = emptyList(),
53+
annotations: MutableList<EditorAnnotation> = mutableListOf()
54+
) : PythonDeclaration(name, annotations) {
55+
56+
val classes = ContainmentList(classes)
57+
val enums = ContainmentList(enums)
58+
val functions = ContainmentList(functions)
59+
60+
override fun children() = sequence {
61+
yieldAll(classes)
62+
yieldAll(enums)
63+
yieldAll(functions)
64+
}
65+
}
66+
67+
private class PythonClass(
68+
name: String,
69+
val decorators: MutableList<String> = mutableListOf(),
70+
val superclasses: MutableList<String> = mutableListOf(),
71+
attributes: List<PythonAttribute> = emptyList(),
72+
methods: List<PythonFunction> = emptyList(),
73+
var description: String = "",
74+
var fullDocstring: String = "",
75+
annotations: MutableList<EditorAnnotation> = mutableListOf()
76+
) : PythonDeclaration(name, annotations) {
77+
78+
val attributes = ContainmentList(attributes)
79+
val methods = ContainmentList(methods)
80+
81+
override fun children() = sequence {
82+
yieldAll(attributes)
83+
yieldAll(methods)
84+
}
85+
}
86+
87+
private class PythonEnum(
88+
name: String,
89+
val instances: MutableList<PythonEnumInstance> = mutableListOf(),
90+
annotations: MutableList<EditorAnnotation> = mutableListOf()
91+
) : PythonDeclaration(name, annotations)
92+
93+
private class PythonFunction(
94+
name: String,
95+
val decorators: MutableList<String> = mutableListOf(),
96+
parameters: List<PythonParameter> = emptyList(),
97+
results: List<PythonResult> = emptyList(),
98+
var isPublic: Boolean = true,
99+
var description: String = "",
100+
var fullDocstring: String = "",
101+
val calledAfter: MutableList<String> = mutableListOf(), // TODO: should be cross-references
102+
var isPure: Boolean = false,
103+
annotations: MutableList<EditorAnnotation> = mutableListOf()
104+
) : PythonDeclaration(name, annotations) {
105+
106+
val parameters = ContainmentList(parameters)
107+
val results = ContainmentList(results)
108+
109+
override fun children() = sequence {
110+
yieldAll(parameters)
111+
yieldAll(results)
112+
}
113+
114+
fun isConstructor() = name == "__init__"
115+
}
116+
117+
private class PythonAttribute(
118+
name: String,
119+
var defaultValue: String = "",
120+
var isPublic: Boolean = true,
121+
var typeInDocs: String = "",
122+
var description: String = "",
123+
var boundary: Boundary? = null,
124+
annotations: MutableList<EditorAnnotation> = mutableListOf()
125+
) : PythonDeclaration(name, annotations)
126+
127+
private class PythonParameter(
128+
name: String,
129+
var defaultValue: String? = null,
130+
var assignedBy: PythonParameterAssignment = PythonParameterAssignment.POSITION_OR_NAME,
131+
var isPublic: Boolean = true,
132+
var typeInDocs: String = "",
133+
var description: String = "",
134+
var boundary: Boundary? = null,
135+
annotations: MutableList<EditorAnnotation> = mutableListOf()
136+
) : PythonDeclaration(name, annotations)
137+
138+
private class PythonResult(
139+
name: String,
140+
var type: String = "",
141+
var typeInDocs: String = "",
142+
var description: String = "",
143+
var boundary: Boundary? = null,
144+
annotations: MutableList<EditorAnnotation> = mutableListOf()
145+
) : PythonDeclaration(name, annotations)
146+
147+
private sealed class PythonStatement : PythonAstNode() // TODO
148+
149+
private class PythonExpressionStatement : PythonStatement() // TODO
150+
151+
private sealed class PythonExpression : PythonAstNode() // TODO
152+
153+
private class PythonCall : PythonStatement() // TODO

0 commit comments

Comments
 (0)