Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UI content #7

Merged
merged 19 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class FirstClass {
tiginamaria marked this conversation as resolved.
Show resolved Hide resolved
// Class content
}

class SecondClass {
// Class content
}

class ThirdClass {
// Class content
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class OuterClass {
class NestedClass {
// Nested class content
}

inner class InnerClass {
// Inner class content
}
}

class AnotherClass {
// Class content
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class ComplexMethods {
tiginamaria marked this conversation as resolved.
Show resolved Hide resolved

fun calculate(value: Int): Int {
fun innerFunction(x: Int): Int {
return x * x
}
return innerFunction(value)
}

companion object {
fun staticMethod() {
println("Static method in companion object")
}
}
}

fun anotherTopLevelFunction() {
println("Another top-level function")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class MultipleMethods {
fun add(a: Int, b: Int): Int {
return a + b
}

fun subtract(a: Int, b: Int): Int {
return a - b
}

private fun privateMethod() {
println("This is a private method")
}
}

fun topLevelFunctionOne() {
println("This is a top-level function")
}

fun topLevelFunctionTwo() {
println("This is another top-level function")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fun main(args: Array<String>) {
tiginamaria marked this conversation as resolved.
Show resolved Hide resolved
println("Hello World!")

// Try adding program arguments via Run/Debug configuration.
// Learn more about running applications: https://www.jetbrains.com/help/idea/running-applications.html.
println("Program arguments: ${args.joinToString()}")
}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.jetbrains.academy.plugin.course.dev.access

import com.intellij.psi.PsiFile
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.KtNamedFunction

fun authorCountKtClasses(psiFile: PsiFile) =
PsiTreeUtil.findChildrenOfType(psiFile, KtClass::class.java).toList().size

fun authorCountKtFunctions(psiFile: PsiFile) =
PsiTreeUtil.findChildrenOfType(psiFile, KtNamedFunction::class.java).toList().size
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.jetbrains.academy.plugin.course.dev.access


import com.intellij.psi.PsiFile
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.psi.KtNamedFunction

fun countKtFunctions(psiFile: PsiFile) =
tiginamaria marked this conversation as resolved.
Show resolved Hide resolved
PsiTreeUtil.findChildrenOfType(psiFile, KtNamedFunction::class.java).toList().size
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package org.jetbrains.academy.plugin.course.dev.ui

import com.intellij.notification.NotificationGroupManager
import com.intellij.notification.NotificationType
import com.intellij.openapi.fileEditor.FileEditorManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.wm.ToolWindow
import com.intellij.openapi.wm.ToolWindowFactory
import com.intellij.psi.PsiDocumentManager
import com.intellij.ui.components.JBPanel
import com.intellij.ui.components.JBScrollPane
import com.intellij.ui.table.JBTable
import com.intellij.util.ui.JBUI
import org.jetbrains.academy.plugin.course.dev.access.countKtClasses
import org.jetbrains.academy.plugin.course.dev.access.authorCountKtClasses
import org.jetbrains.academy.plugin.course.dev.access.authorCountKtFunctions
import org.jetbrains.academy.plugin.course.dev.access.countKtFunctions
import java.awt.BorderLayout
import java.awt.FlowLayout
import java.awt.event.ActionListener
import javax.swing.JButton
import javax.swing.table.DefaultTableModel

class DemoPanelFactory : ToolWindowFactory {
tiginamaria marked this conversation as resolved.
Show resolved Hide resolved
companion object {
private const val DEMO_PLUGIN_NOTIFICATION = "IdeDevCourseDemo"
tiginamaria marked this conversation as resolved.
Show resolved Hide resolved
}


// TODO use UI bundle instead of a string here?
private val runTaskMethodButton = JButton("Run task function").apply {
tiginamaria marked this conversation as resolved.
Show resolved Hide resolved
isBorderPainted = false
isVisible = true
}

lateinit var demoWindow: DemoPanelWindow
private val tableModel = DefaultTableModel()
private val resultsTable = JBTable(tableModel)
override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) {
tiginamaria marked this conversation as resolved.
Show resolved Hide resolved
demoWindow = project.getService(DemoPanelService::class.java).demoWindow
demoWindow.jComponent.size = JBUI.size(toolWindow.component.width, toolWindow.component.height)
tableModel.addColumn("PSI Element")
tableModel.addColumn("Your Output")
tableModel.addColumn("Expected Output")
val buttonPanel = JBPanel<JBPanel<*>>(FlowLayout()).apply {
add(runTaskMethodButton)
}
// Wrapping the resultsTable in a JScrollPane
val scrollPane = JBScrollPane(resultsTable)
scrollPane.preferredSize = JBUI.size(400, 100) // Set preferred size for scrollPane

val panel = JBPanel<JBPanel<*>>(BorderLayout()).apply {
add(buttonPanel, BorderLayout.SOUTH)
add(scrollPane, BorderLayout.CENTER)
}
toolWindow.component.add(panel)

// TODO add another listener? This example as a demo for now
runTaskMethodButton.setListener {
NotificationGroupManager.getInstance().getNotificationGroup(DEMO_PLUGIN_NOTIFICATION)
.createNotification("This panel demonstrates how the plugin works. The current task is a theory task, the plugin does nothing.", NotificationType.INFORMATION)
.notify(project)
}

runTaskMethodButton.addActionListener {
// Creating a virtual file with Kotlin content
val editor = FileEditorManager.getInstance(project).selectedTextEditor
val document = editor?.document

val psiFile = document?.let { PsiDocumentManager.getInstance(project).getPsiFile(it) }

psiFile?.let {
tiginamaria marked this conversation as resolved.
Show resolved Hide resolved
val classResult = countKtClasses(it)
val classAuthorResult = authorCountKtClasses(it)

val functionResult = countKtFunctions(it)
val functionAuthorResult = authorCountKtFunctions(it)

// Display the result in the table
tableModel.setRowCount(0)
tableModel.addRow(arrayOf("Class", classResult.toString(), classAuthorResult.toString()))
tiginamaria marked this conversation as resolved.
Show resolved Hide resolved
tableModel.addRow(arrayOf("Function", functionResult.toString(), functionAuthorResult.toString()))
}
}
}

private fun JButton.setListener(listener: ActionListener) {
actionListeners.forEach {
removeActionListener(it)
}
addActionListener(listener)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.jetbrains.academy.plugin.course.dev.ui

import com.intellij.openapi.Disposable
import com.intellij.openapi.components.Service
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.project.Project

@Service(Service.Level.PROJECT)
class DemoPanelService : Disposable {
tiginamaria marked this conversation as resolved.
Show resolved Hide resolved
val demoWindow = DemoPanelWindow(this)
override fun dispose() {
logger<Project>().info("Jcef window service was disposed")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.jetbrains.academy.plugin.course.dev.ui

import com.intellij.openapi.util.Disposer
import com.intellij.ui.jcef.JBCefBrowser
import javax.swing.JComponent

class DemoPanelWindow(service: DemoPanelService) {
tiginamaria marked this conversation as resolved.
Show resolved Hide resolved
private val windowBrowser: JBCefBrowser = JBCefBrowser()

val jComponent: JComponent
get() = windowBrowser.component

init {
// TODO load the panel's content
Disposer.register(service, windowBrowser)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<idea-plugin>
<id>jetbrains.academy.plugin.course.dev.ui.demo</id>
<name>IDE Development Course Demo</name>
<vendor>JetBrains</vendor>

<depends>com.intellij.modules.platform</depends>
<depends>org.jetbrains.kotlin</depends>

<extensions defaultExtensionNs="com.intellij">
<notificationGroup id="IdeDevCourseDemo" displayType="BALLOON"/>

<toolWindow doNotActivateOnStart="true"
factoryClass="org.jetbrains.academy.plugin.course.dev.ui.DemoPanelFactory"
id="IDECourseDemo" anchor="right"/>
</extensions>
</idea-plugin>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
type: edu
custom_name: Count Number of specific Elements
files:
- name: test/org/jetbrains/academy/plugin/course/dev/access/Tests.kt
visible: false
- name: src/main/kotlin/org/jetbrains/academy/plugin/course/dev/access/ClassCounter.kt
visible: true
- name: src/main/kotlin/org/jetbrains/academy/plugin/course/dev/ui/DemoPanelService.kt
visible: false
- name: src/main/kotlin/org/jetbrains/academy/plugin/course/dev/ui/DemoPanelWindow.kt
visible: false
- name: src/main/kotlin/org/jetbrains/academy/plugin/course/dev/ui/DemoPanelFactory.kt
visible: false
- name: src/main/resources/META-INF/plugin.xml
visible: false
- name: src/main/kotlin/org/jetbrains/academy/plugin/course/dev/access/AuthorElementsCounter.kt
visible: false
- name: src/main/kotlin/org/jetbrains/academy/plugin/course/dev/access/FunctionCounter.kt
visible: true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
id: 1455788717
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.jetbrains.academy.plugin.course.dev.access

import com.intellij.testFramework.fixtures.BasePlatformTestCase

fun getResourceFileContent(path: String): String {
val inputStream = Test::class.java.getResourceAsStream(path)
return inputStream?.bufferedReader()?.use { it.readText() } ?: ""
}

class Test : BasePlatformTestCase() {

fun testSolution() {
// TODO: add more test cases
val path = "/sampleProject/src/main/kotlin/AccessingPsiElements/ClassCounter/MultipleClasses.kt"
val fileContent = getResourceFileContent(path)
val file = myFixture.configureByText("MyClass.kt", fileContent)

val psiClassesCount = countKtClasses(file)
val authorClassesCount = authorCountKtClasses(file)
assertEquals("For the Kotlin file with content $fileContent the function countKtClasses should return 2, but currently it returns $psiClassesCount", authorClassesCount, psiClassesCount)
}
}

class FunctionCounterTest : BasePlatformTestCase() {

fun testSolution() {
// TODO: add more test cases
val path = "/sampleProject/src/main/kotlin/AccessingPsiElements/FunctionCounter/MultipleMethods.kt"
tiginamaria marked this conversation as resolved.
Show resolved Hide resolved
val fileContent = getResourceFileContent(path)
val file = myFixture.configureByText("MyClass.kt", fileContent)

val psiFunctionsCount = countKtFunctions(file)
val authorFunctionsCount = authorCountKtFunctions(file)
assertEquals("For the Kotlin file with content $fileContent the function countKtClasses should return 6, but currently it returns $psiFunctionsCount", authorFunctionsCount, psiFunctionsCount)
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
id: 1815927707
id: 1094497733
3 changes: 2 additions & 1 deletion courseSection/accessingPsiElementsLesson/lesson-info.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
type: framework
custom_name: Accessing PSI Elements
content:
- accessingPsiElementsTheoryTask
- accessingPsiElementsClassProgrammingTask
- accessingPsiElementsProgrammingTask
Original file line number Diff line number Diff line change
@@ -1 +1 @@
id: 873950298
id: 139943943
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
package org.jetbrains.academy.plugin.course.dev.access

import com.intellij.openapi.command.WriteCommandAction
import com.intellij.psi.PsiFile
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.KtNamedFunction

fun sortMethods(psiFile: PsiFile) {
val project = psiFile.project
fun sortMethods(ktClass: KtClass) {
val project = ktClass.project
WriteCommandAction.runWriteCommandAction(project) {
val classes = PsiTreeUtil.findChildrenOfType(psiFile, KtClass::class.java)
val methods = ktClass.declarations.filterIsInstance<KtNamedFunction>()
val sortedMethods = methods.sortedBy { it.name }.map { it.copy() as KtNamedFunction }

for (ktClass in classes){
val methods = ktClass.declarations.filterIsInstance<KtNamedFunction>()
val sortedMethods = methods.sortedBy { it.name }.map { it.copy() as KtNamedFunction }

methods.zip(sortedMethods).forEach { (original, sortedCopy) ->
original.replace(sortedCopy)
}
methods.zip(sortedMethods).forEach { (original, sortedCopy) ->
original.replace(sortedCopy)
}
}
}
Loading
Loading