generated from jetbrains-academy/kotlin-course-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Valerii
committed
Dec 1, 2023
1 parent
8a83727
commit 2db0f16
Showing
12 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
courseSection/courseLesson/Find Number of Classes/src/Task.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
courseSection/courseLesson/Find Number of Classes/task-info.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
21
courseSection/courseLesson/Find Number of Classes/test/Tests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
type: theory | ||
files: | ||
- name: src/Main.kt | ||
visible: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fun main() { | ||
// Write your solution here | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,6 @@ content: | |
- theoryTask | ||
- quizTask | ||
- programmingTask | ||
- Intro to Psi | ||
- PsiTreeUtil | ||
- Find Number of Classes |