Skip to content

Commit

Permalink
add ouw own realization of functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Valerii committed Jan 16, 2024
1 parent c35a2fe commit 6715358
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class ComplexMethods {

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,8 @@
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

fun authorCountKtClasses(psiFile: PsiFile) =
PsiTreeUtil.findChildrenOfType(psiFile, KtClass::class.java).toList().size
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ 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 java.awt.BorderLayout
import java.awt.FlowLayout
import java.awt.event.ActionListener
Expand Down Expand Up @@ -68,10 +69,11 @@ class DemoPanelFactory : ToolWindowFactory {

psiFile?.let {
val result = countKtClasses(it)
val authorResult = authorCountKtClasses(it)

// Display the result in the table
tableModel.setRowCount(0)
tableModel.addRow(arrayOf("Class", result.toString(), 2))
tableModel.addRow(arrayOf("Class", result.toString(), authorResult.toString()))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ files:
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Test : BasePlatformTestCase() {
val file = myFixture.configureByText("MyClass.kt", fileContent)

val psiClassesCount = countKtClasses(file)
assertEquals("For the Kotlin file with content $fileContent the function countKtClasses should return 2, but currently it returns $psiClassesCount", 2, psiClassesCount)
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)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
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 authorCountKtFunctions(psiFile: PsiFile) =
PsiTreeUtil.findChildrenOfType(psiFile, KtNamedFunction::class.java).toList().size
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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.authorCountKtFunctions
import org.jetbrains.academy.plugin.course.dev.access.countKtFunctions
import java.awt.BorderLayout
import java.awt.FlowLayout
Expand Down Expand Up @@ -67,10 +68,10 @@ class DemoPanelFactory : ToolWindowFactory {

psiFile?.let {
val result = countKtFunctions(it)

val authorResult = authorCountKtFunctions(it)
// Display the result in the table
tableModel.setRowCount(0)
tableModel.addRow(arrayOf("Class", result.toString(), 2))
tableModel.addRow(arrayOf("Function", result.toString(), authorResult.toString()))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ files:
visible: false
- name: test/org/jetbrains/academy/plugin/course/dev/access/Tests.kt
visible: false
- name: src/main/kotlin/org/jetbrains/academy/plugin/course/dev/access/AuthorElementsCounter.kt
visible: false
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ object MyObject {
val file = myFixture.configureByText("MyClass.kt", fileContent)

val psiFunctionsCount = countKtFunctions(file)
assertEquals("For the Kotlin file with content $fileContent the function countKtClasses should return 6, but currently it returns $psiFunctionsCount", 6, psiFunctionsCount)
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
Expand Up @@ -28,6 +28,7 @@ class Test : BasePlatformTestCase() {
val file = myFixture.configureByText("Person.kt", fileContent)
val classes = PsiTreeUtil.findChildrenOfType(file, KtClass::class.java)
for (ktClass in classes) {
sortMethods(ktClass)
val methods = ktClass.declarations.filterIsInstance<KtNamedFunction>()
val sorted = methods.sortedBy { it.name }
assertEquals(
Expand Down

0 comments on commit 6715358

Please sign in to comment.