Skip to content

Commit

Permalink
psi theory + coding task
Browse files Browse the repository at this point in the history
  • Loading branch information
Valerii committed Dec 1, 2023
1 parent 8a83727 commit 2db0f16
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 0 deletions.
4 changes: 4 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ plugins {
val kotlinVersion = "1.9.0"
id("org.jetbrains.kotlin.jvm") version kotlinVersion apply false
id("io.gitlab.arturbosch.detekt") version "1.23.1"
id("org.jetbrains.intellij") version "1.16.1"
}



val detektReportMerge by tasks.registering(io.gitlab.arturbosch.detekt.report.ReportMergeTask::class) {
output.set(rootProject.buildDir.resolve("reports/detekt/merge.sarif"))
}
Expand All @@ -26,6 +29,7 @@ allprojects {
}
}


tasks {
wrapper {
gradleVersion = gradleProperties("gradleVersion").get()
Expand Down
12 changes: 12 additions & 0 deletions courseSection/courseLesson/Find Number of Classes/src/Task.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import com.intellij.openapi.vfs.VirtualFile

class FileAnalyzer(val project: Project) {

private val psiManager = PsiManager.getInstance(project)

fun classCounter(file: VirtualFile): Int {
// get psiFile from virtualFile
val classes = // get classes from psiFile
return classes
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: edu
files:
- name: src/Task.kt
visible: true
- name: test/Tests.kt
visible: false
6 changes: 6 additions & 0 deletions courseSection/courseLesson/Find Number of Classes/task.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

You need to implement a function classCounter which will count number of classes in the kotlin file.
<div class="hint">
[Try to find method in official docs to get the psi file](https://plugins.jetbrains.com/docs/intellij/psi-files.html)
</div>

21 changes: 21 additions & 0 deletions courseSection/courseLesson/Find Number of Classes/test/Tests.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import org.junit.Assert
import org.junit.Test
import com.intellij.testFramework.fixtures.BasePlatformTestCase
import org.junit.jupiter.api.Assertions.assertEquals

class Test {
@Test fun testSolution() {
val file = myFixture.configureByText("MyClass.java", """
public class MyClass1 {
public void method1() {}
}
class MyClass2 {
public void method2() {}
}
""".trimIndent())

val stats = FileAnalyzer(myFixture.project).classCounter(file.virtualFile)
assertEquals(2, stats)
}
}
4 changes: 4 additions & 0 deletions courseSection/courseLesson/Intro to Psi/src/Main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.jetbrains.academy.kotlin.template
fun main() {
// Write your solution here
}
4 changes: 4 additions & 0 deletions courseSection/courseLesson/Intro to Psi/task-info.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type: theory
files:
- name: src/Main.kt
visible: true
33 changes: 33 additions & 0 deletions courseSection/courseLesson/Intro to Psi/task.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

# Intro to Psi

---
PSI stands for Program Structure Interface. It's a part of IntelliJ Platform SDK.
It represents the entire structure of the code in a project as a tree of elements, called a PSI tree.

**PSI Tree Structure**
* The root is a **PsiFile** element, representing an entire file.
* Each node/element in the tree represents a syntactic or structural part of the code, like expressions, statements, and declarations.

**Types of PSI Elements**
* PsiFile: Represents an entire file.
* PsiClass: Represents a class in the code.
* PsiMethod: Represents a method.
* PsiVariable: Represents a variable.
* PsiExpression: Represents an expression, and so on.

**Accessing PSI Elements**

**PsiTreeUtil** is a utility class in the IntelliJ Platform SDK that provides methods for navigating and querying the PSI tree of a project.

It's a part of the `com.intellij.psi.util package`.

The **findChildrenOfType** method is used to find all children of a specified type within a given PSI element.
It's particularly useful when you need to locate all instances of a particular element type, such as classes, methods, or variables, within a file or a code block.

Syntax:
`public static @Unmodifiable @NotNull <T extends PsiElement> Collection<T> findChildrenOfType(@Nullable PsiElement element, @NotNull Class<? extends T> aClass)`

**Parameters**
* element: The PSI element within which to search for children. This could be a PsiFile, a PsiClass, or any other PSI element.
* aClass: The class type of the elements you are searching for. For example, PsiClass.class to find all classes.
3 changes: 3 additions & 0 deletions courseSection/courseLesson/PsiTreeUtil/src/Main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fun main() {
// Write your solution here
}
16 changes: 16 additions & 0 deletions courseSection/courseLesson/PsiTreeUtil/task-info.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
type: choice
is_multiple_choice: false
options:
- text: PsiFile
is_correct: false
- text: PsiMethod
is_correct: true
- text: PsiVariable
is_correct: false
- text: PsiClass
is_correct: false
message_incorrect: Almost there! The key is to match the element type you're searching for with the corresponding PSI class. In PsiTreeUtil.findChildrenOfType, the class type parameter determines what kind of PSI elements you'll find.
files:
- name: src/Main.kt
visible: true

1 change: 1 addition & 0 deletions courseSection/courseLesson/PsiTreeUtil/task.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
If you want to find all method declarations within a class using PsiTreeUtil, which class type should you specify in the findChildrenOfType method?
3 changes: 3 additions & 0 deletions courseSection/courseLesson/lesson-info.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ content:
- theoryTask
- quizTask
- programmingTask
- Intro to Psi
- PsiTreeUtil
- Find Number of Classes

0 comments on commit 2db0f16

Please sign in to comment.