Skip to content

Commit d71b8d5

Browse files
committed
feat(pair): add LayeredArch and ProjectPackageTree classes
Add the `LayeredArch` and `ProjectPackageTree` classes to the `cc.unitmesh.devti.pair.arch` package.
1 parent 55a103e commit d71b8d5

File tree

9 files changed

+162
-18
lines changed

9 files changed

+162
-18
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package cc.unitmesh.idea.provider
2+
3+
import cc.unitmesh.devti.pair.arch.ProjectPackageTree
4+
import cc.unitmesh.devti.provider.architecture.LayeredArchProvider
5+
import com.intellij.openapi.project.Project
6+
import com.intellij.openapi.projectRoots.JavaSdkType
7+
import com.intellij.openapi.roots.ProjectRootManager
8+
import com.intellij.psi.JavaPsiFacade
9+
10+
class JavaLayeredArchProvider : LayeredArchProvider {
11+
private val layeredArch = ProjectPackageTree()
12+
override fun isApplicable(project: Project): Boolean {
13+
val projectSdk = ProjectRootManager.getInstance(project).projectSdk ?: return false
14+
15+
return projectSdk.sdkType is JavaSdkType
16+
}
17+
18+
override fun getLayeredArch(project: Project): ProjectPackageTree {
19+
val psiFacade = JavaPsiFacade.getInstance(project)
20+
21+
val projectRootManager = ProjectRootManager.getInstance(project)
22+
val contentRoots = projectRootManager.contentRoots
23+
for (contentRoot in contentRoots) {
24+
// todo implement this
25+
val psiPackage = psiFacade.findPackage(contentRoot.url)
26+
if (psiPackage != null) {
27+
layeredArch.addPackage(psiPackage.qualifiedName)
28+
}
29+
}
30+
31+
return layeredArch
32+
}
33+
}

java/src/main/resources/cc.unitmesh.idea.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@
5252
language="JAVA"
5353
implementationClass="cc.unitmesh.idea.provider.JavaCustomPromptProvider" />
5454

55+
<layeredArchProvider
56+
implementation="cc.unitmesh.idea.provider.JavaLayeredArchProvider" />
57+
5558
<autoDevIntention>
5659
<className>cc.unitmesh.idea.actions.AutoCrudAction</className>
5760
<bundleName>messages.AutoDevBundle</bundleName>

src/233/main/resources/META-INF/autodev-core.xml

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@
5050
icon="cc.unitmesh.devti.AutoDevIcons.AI_COPILOT"
5151
factoryClass="cc.unitmesh.devti.gui.AutoDevToolWindowFactory"/>
5252

53-
<!-- <toolWindow id="AutoDevPair"-->
54-
<!-- doNotActivateOnStart="true"-->
55-
<!-- anchor="right"-->
56-
<!-- secondary="true"-->
57-
<!-- canCloseContents="true"-->
58-
<!-- icon="cc.unitmesh.devti.AutoDevIcons.AI_PAIR"-->
59-
<!-- factoryClass="cc.unitmesh.devti.gui.AutoDevPairToolWindowFactory"/>-->
53+
<toolWindow id="AutoDev Pair"
54+
doNotActivateOnStart="true"
55+
anchor="left"
56+
secondary="true"
57+
canCloseContents="false"
58+
icon="cc.unitmesh.devti.AutoDevIcons.AI_PAIR"
59+
factoryClass="cc.unitmesh.devti.gui.AutoDevPairToolWindowFactory"/>
6060

6161
<notificationGroup id="AI notification group" displayType="STICKY_BALLOON" bundle="messages.AutoDevBundle"
6262
key="name"/>
@@ -135,6 +135,10 @@
135135
interface="cc.unitmesh.devti.provider.DevFlowProvider"
136136
dynamic="true"/>
137137

138+
<extensionPoint qualifiedName="cc.unitmesh.layeredArchProvider"
139+
interface="cc.unitmesh.devti.provider.architecture.LayeredArchProvider"
140+
dynamic="true" />
141+
138142
<!-- TODO: find better way to share context -->
139143
<extensionPoint qualifiedName="cc.unitmesh.contextPrompter"
140144
interface="cc.unitmesh.devti.provider.ContextPrompter"
Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,44 @@
11
package cc.unitmesh.devti.gui
22

3+
import cc.unitmesh.devti.provider.architecture.LayeredArchProvider
4+
import com.intellij.openapi.Disposable
35
import com.intellij.openapi.project.Project
6+
import com.intellij.openapi.ui.NullableComponent
7+
import com.intellij.openapi.ui.SimpleToolWindowPanel
48
import com.intellij.openapi.wm.ToolWindow
59
import com.intellij.openapi.wm.ToolWindowFactory
610
import com.intellij.ui.dsl.builder.panel
7-
import javax.swing.JComponent
811

912
class AutoDevPairToolWindowFactory : ToolWindowFactory {
1013
override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) {
11-
val panel = AutoDevPairToolWindow(project)
14+
val disposable = toolWindow.disposable
15+
val panel = AutoDevPairToolWindow(project, disposable)
16+
1217
val contentManager = toolWindow.contentManager
18+
1319
val content = contentManager.factory.createContent(panel, "", false)
1420
contentManager.addContent(content)
1521
}
1622
}
1723

18-
class AutoDevPairToolWindow(val project: Project) : JComponent() {
24+
class AutoDevPairToolWindow(val project: Project, val disposable: Disposable) : SimpleToolWindowPanel(true, true),
25+
NullableComponent {
1926
init {
20-
// A table like todo list
21-
panel {
27+
val layeredArch = LayeredArchProvider.find(project)?.getLayeredArch(project)
28+
val panel = panel {
2229
row {
2330
label("Hello World")
2431
}
32+
row {
33+
// show a tree in a table
34+
label(layeredArch.toString())
35+
}
2536
}
37+
38+
setContent(panel)
39+
}
40+
41+
override fun isNull(): Boolean {
42+
return false
2643
}
2744
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cc.unitmesh.devti.pair.arch
2+
3+
data class LayeredArch(val layers: List<Layer>) {
4+
fun getLayerByName(name: String): Layer? {
5+
return layers.find { it.name == name }
6+
}
7+
}
8+
9+
data class Layer(val name: String, val packages: List<String>) {
10+
fun getPackageByName(name: String): String? {
11+
return packages.find { it == name }
12+
}
13+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package cc.unitmesh.devti.pair.arch
2+
3+
class ProjectPackageTree {
4+
private var tree: TreeNode? = null
5+
6+
private fun getTree(): TreeNode {
7+
if (tree == null) {
8+
tree = TreeNode("root")
9+
}
10+
return tree!!
11+
}
12+
13+
fun setTree(tree: TreeNode) {
14+
this.tree = tree
15+
}
16+
17+
fun addPackage(packageName: String) {
18+
val tree = getTree()
19+
val packages = packageName.split(".")
20+
var node = tree
21+
for (pkg in packages) {
22+
val child = node.children.find { it.name == pkg }
23+
if (child != null) {
24+
node = child
25+
} else {
26+
val newNode = TreeNode(pkg)
27+
node.addChild(newNode)
28+
node = newNode
29+
}
30+
}
31+
}
32+
33+
fun print(): String {
34+
return getTree().print()
35+
}
36+
}
37+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package cc.unitmesh.devti.pair.arch
2+
3+
class TreeNode(val name: String, val children: MutableList<TreeNode> = mutableListOf()) {
4+
fun addChild(child: TreeNode) {
5+
children.add(child)
6+
}
7+
8+
fun print(): String {
9+
val sb = StringBuilder()
10+
sb.append(name)
11+
sb.append("\n")
12+
for (child in children) {
13+
sb.append(child.print())
14+
}
15+
return sb.toString()
16+
}
17+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package cc.unitmesh.devti.provider.architecture
2+
3+
import cc.unitmesh.devti.pair.arch.ProjectPackageTree
4+
import com.intellij.openapi.extensions.ExtensionPointName
5+
import com.intellij.openapi.project.Project
6+
7+
interface LayeredArchProvider {
8+
fun isApplicable(project: Project): Boolean
9+
fun getLayeredArch(project: Project): ProjectPackageTree
10+
11+
companion object {
12+
13+
private val EP_NAME = ExtensionPointName<LayeredArchProvider>("cc.unitmesh.layeredArchProvider")
14+
15+
fun find(project: Project): LayeredArchProvider? {
16+
val providers = EP_NAME.extensionList.filter {
17+
it.isApplicable(project)
18+
}
19+
20+
return providers.firstOrNull()
21+
}
22+
}
23+
}
24+

src/main/kotlin/cc/unitmesh/devti/provider/builtin/LanguageContextProvider.kt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@ class LanguageContextProvider : ChatContextProvider {
1515
override suspend fun collect(project: Project, creationContext: ChatCreationContext): List<ChatContextItem> {
1616
val language = AutoDevSettingsState.getInstance().language
1717

18-
return listOf(
19-
ChatContextItem(
20-
LanguageContextProvider::class,
21-
"You MUST Use $language to reply me!"
22-
)
23-
)
18+
val text = "You MUST Use $language to reply me!"
19+
return listOf(ChatContextItem(LanguageContextProvider::class, text))
2420
}
2521
}

0 commit comments

Comments
 (0)