Skip to content

Commit

Permalink
Add theory to PSI access and edit lessons
Browse files Browse the repository at this point in the history
  • Loading branch information
tiginamaria committed Jan 16, 2024
1 parent 03ad6ac commit 047b1f0
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 6 deletions.
1 change: 1 addition & 0 deletions introductionSection/setupLesson/lesson-remote-info.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
id: 1521434296
1 change: 1 addition & 0 deletions introductionSection/welcomeLesson/lesson-remote-info.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
id: 2136442658
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

You need to implement a function `countKtClasses` which will
Using your newly gained knowledge about `PsiTreeUtil.findChildrenOfType` method, implement a function `countKtClasses` which will
count number of kotlin classes declared in the given kotlin PSI file.

<div class="hint" title="Which class should I use as aClass parameter?">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package org.jetbrains.academy.plugin.course.dev.access

fun main() {
TODO()
}
Original file line number Diff line number Diff line change
@@ -1 +1,22 @@
TODO
All operations with code like adding, deleting or moving methods, renaming variable, you name it, are implemented as PSI editions.
Please skim the [Modify the PSI](https://plugins.jetbrains.com/docs/intellij/modifying-psi.html) documentation.

Methods to edit PSI Element:
* [`PsiElement.add()`](https://github.com/JetBrains/intellij-community/blob/19d9a1cc2d9c14df9c3bdee391e9e4795ac25cb9/platform/core-api/src/com/intellij/psi/PsiElement.java#L302) - to add a child to PSI Element into tree. To specify the place in children list use [`PsiElement.addBefore()`](https://github.com/JetBrains/intellij-community/blob/19d9a1cc2d9c14df9c3bdee391e9e4795ac25cb9/platform/core-api/src/com/intellij/psi/PsiElement.java#L312C14-L312C23) and [`PsiElement.addAfter()`](https://github.com/JetBrains/intellij-community/blob/19d9a1cc2d9c14df9c3bdee391e9e4795ac25cb9/platform/core-api/src/com/intellij/psi/PsiElement.java#L322)
* [`PsiElement.delete()`](https://github.com/JetBrains/intellij-community/blob/19d9a1cc2d9c14df9c3bdee391e9e4795ac25cb9/platform/core-api/src/com/intellij/psi/PsiElement.java#L373) - to delete PSI Element from tree
* [`PsiElement.replace()`](https://github.com/JetBrains/intellij-community/blob/19d9a1cc2d9c14df9c3bdee391e9e4795ac25cb9/platform/core-api/src/com/intellij/psi/PsiElement.java#L402) - to replace PSI Element in tree

Also, you create PSI Elements by using:
* [`PsiElement.copy()`](https://github.com/JetBrains/intellij-community/blob/19d9a1cc2d9c14df9c3bdee391e9e4795ac25cb9/platform/core-api/src/com/intellij/psi/PsiElement.java#L293) - to copy PSI Element subtree

Moreover, there are some PisElement-specific methods like [`KtNamedFunction.setName()`](https://github.com/JetBrains/intellij-community/blob/bf3083ca66771e038eb1c64128b4e508f52acfad/platform/core-api/src/com/intellij/psi/PsiNamedElement.java#L39) as well as all `PsiNamedElement` inheritors.

**IMPORTANT!**

Every PSI modifications need to be wrapped in a [write action and in command](https://github.com/JetBrains/intellij-community/blob/master/platform/core-api/src/com/intellij/openapi/command/WriteCommandAction.java)

```kotlin
WriteCommandAction.runWriteCommandAction(project) {
// Here you can modify PSI Elements
}
```
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
TODO
Sometimes, switching between Python and Kotlin leeds to following code style mistake:

Python developers usually use `snake_case` for methods naming like
```python
def sort_values(values):
```
While in Kotlin or Java `camelCase` is commonly used
```Kotlin
fun sortValues(values: List<Int>) {...}
```

**Your task will be** to help such multi-language programmers and
implement method which renames `snake_case` named method to `camelCase`.

So before your method invocation method looked like:
```Kotlin
fun sort_values(values: List<Int>) {...}
```
but after
```Kotlin
fun sortValues(values: List<Int>) {...}
```

<div class="hint" title="How to get project for WriteActionCommand?">

Every PSI element has link to project, just try `psiElement.project`
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fun sortMethods(psiFile: PsiFile) {
WriteCommandAction.runWriteCommandAction(project) {
val classes = PsiTreeUtil.findChildrenOfType(psiFile, KtClass::class.java)

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
TODO
Sometimes, you want to make you code more searchable and sort methods by the alphabetical order.
Now you can do it automatically, just several lines of code!
Implement method which takes PSI class and orders all inner methods in alphabetical order.


<div class="hint" title="Where to start?">

Get list of class methods, **copy** them and sort list of copies by name. Then use **replace** original methods one by one with copy which should stand in this position in sorted order.
</div>

0 comments on commit 047b1f0

Please sign in to comment.