Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Valerii committed Dec 27, 2023
1 parent 8f54dc1 commit fd56d76
Show file tree
Hide file tree
Showing 24 changed files with 100 additions and 84 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
type: theory
custom_name: Accessing PSI Elements
files:
- name: src/jetbrains/plugindev/course/access/Main.kt
visible: true
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package jetbrains.plugindev.course.addpsi
package org.jetbrains.academy.ij.plugin.course.annotations

import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.project.Project
Expand All @@ -8,27 +8,6 @@ import com.intellij.psi.PsiMethod
import com.intellij.psi.PsiType
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.psi.util.TypeConversionUtil
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.KtNamedFunction


// type your solution here

fun sortMethods(psiFile: PsiFile) {
val project = psiFile.project
WriteCommandAction.runWriteCommandAction(project) {
val classes = PsiTreeUtil.findChildrenOfType(psiFile, KtClass::class.java)

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)
}
}
}
}

fun addAnnotations(psiFile: PsiFile){
val project = psiFile.project
Expand Down
11 changes: 11 additions & 0 deletions courseSection/psi/AddAnnotations/task-info.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
type: edu
custom_name: Psi Manipulation Advanced
files:
- name: src/org/jetbrains/academy/ij/plugin/course/annotations/Task.kt
visible: true
placeholders:
- offset: 871
length: 247
placeholder_text: TODO()
- name: test/org/jetbrains/academy/ij/plugin/course/annotations/Tests.kt
visible: false
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,42 +1,14 @@
package jetbrains.plugindev.course.addpsi
package org.jetbrains.academy.ij.plugin.course.annotations


import com.intellij.ide.highlighter.JavaFileType
import com.intellij.psi.PsiClass
import com.intellij.psi.PsiMethod
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.testFramework.fixtures.BasePlatformTestCase
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.KtNamedFunction

class Test : BasePlatformTestCase() {

fun testSolution() {
var file = myFixture.configureByText("MyClass.kt", """
class MyClass {
fun abcd() {
println("Hello")
}
fun zhas() {
println("Goodbye")
}
fun mid() {
println("Goodbye")
}
}
""".trimIndent())

sortMethods(file)
val classes = PsiTreeUtil.findChildrenOfType(file, KtClass::class.java)

for (ktClass in classes){
val methods = ktClass.declarations.filterIsInstance<KtNamedFunction>()
val sorted = methods.sortedBy { it.name }
assertEquals(sorted, methods)
}
}
class Test : BasePlatformTestCase() {

fun testAnnotation() {
val javaFile = myFixture.configureByText(
Expand All @@ -46,7 +18,6 @@ class Test : BasePlatformTestCase() {
}
class SubClass extends SuperClass {
public void testMethod() {}
}
""".trimIndent())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
type: edu
custom_name: Count number of classes
files:
- name: src/jetbrains/plugindev/course/classcounter/Task.kt
visible: true
placeholders:
- offset: 160
length: 85
- offset: 211
length: 81
placeholder_text: TODO()
- name: test/jetbrains/plugindev/course/classcounter/Tests.kt
visible: false
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

You need to implement a function `countKtClasses` which will count number of kotlin classes declared in the given kotlin PSI file.
<div class="hint">

Try to use `KtClass::class.java` value for aClass parameter for `findChildrenOfType`
</div>

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package jetbrains.plugindev.course.editpsi

import com.intellij.openapi.command.WriteCommandAction
import org.jetbrains.kotlin.psi.KtNamedFunction


fun editFunctionName(function: KtNamedFunction, newName: String) {
val project = function.project

WriteCommandAction.runWriteCommandAction(project) {
function.setName(newName)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
type: edu
custom_name: Rename function using PSI
files:
- name: src/jetbrains/plugindev/course/editpsi/Task.kt
visible: true
placeholders:
- offset: 220
length: 127
placeholder_text: TODO()
- name: test/jetbrains/plugindev/course/editpsi/Tests.kt
visible: false
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ class Test : BasePlatformTestCase() {
}
""".trimIndent())

editFunctionName(file, "sayHello", "greetings")

val functions = PsiTreeUtil.findChildrenOfType(file, KtNamedFunction::class.java)
val functionToRename = functions.find { it.name == "sayHello" }
if (functionToRename != null) {
editFunctionName(functionToRename, "greetings")
}

val hasNewFunction = functions.any { it.name == "greetings" }
assertTrue(hasNewFunction)
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
type: theory
custom_name: What is PSI?
files:
- name: src/Person.kt
visible: true
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
type: edu
custom_name: Psi Manipulation Basics
files:
- name: src/jetbrains/plugindev/course/addpsi/Task.kt
visible: true
placeholders:
- offset: 228
length: 387
placeholder_text: TODO()
- name: test/jetbrains/plugindev/course/addpsi/Tests.kt
visible: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package jetbrains.plugindev.course.addpsi

import com.intellij.ide.highlighter.JavaFileType
import com.intellij.psi.PsiClass
import com.intellij.psi.PsiMethod
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.testFramework.fixtures.BasePlatformTestCase
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.psi.KtNamedFunction

class Test : BasePlatformTestCase() {

fun testSolution() {
var file = myFixture.configureByText("Person.kt", """
class Person {
fun name() {
println("My name is John")
}
fun surname() {
println("My surname is Johnson")
}
fun age() {
println("I am 18 years old")
}
}
""".trimIndent())

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(sorted, methods)
}
}


}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
type: choice
custom_name: Try Psi Viewer
is_multiple_choice: true
options:
- text: org.jetbrains.kotlin.psi.KtClass
Expand All @@ -13,3 +14,4 @@ files:
- name: src/Person.kt
visible: true
local_check: true

File renamed without changes.

This file was deleted.

13 changes: 7 additions & 6 deletions courseSection/psi/lesson-info.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
custom_name: PSI
content:
- What is PSI
- Try PSI Viewer
- Accessing PSI Elements
- classCounter
- editPSI
- addPSI
- IntroToPsi
- TryPsiViewer
- AccessingPsiElements
- ClassCounter
- EditPSI
- SortMethods
- AddAnnotations

0 comments on commit fd56d76

Please sign in to comment.