Skip to content

Commit

Permalink
Merge pull request #10 from jetbrains-academy/tigina/theory-cleanup
Browse files Browse the repository at this point in the history
Theory Cleanup
  • Loading branch information
tiginamaria authored Jan 26, 2024
2 parents 5b084ef + 46e59bf commit a1f9bd0
Show file tree
Hide file tree
Showing 23 changed files with 115 additions and 28 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ courseVersion=1.1.0
gradleVersion=8.3
jvmVersion=17

kotlin.code.style=official
kotlin.code.style=official
org.gradle.jvmargs=-Xmx2048m
3 changes: 2 additions & 1 deletion introductionSection/section-info.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
custom_name: Introduction
content:
- welcomeLesson
- welcomeLesson
- setupLesson
2 changes: 1 addition & 1 deletion introductionSection/section-remote-info.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
id: 720914665
id: 1117582364
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.jetbrains.academy.plugin.course.dev.setup

fun main() {
print("Time to setup!")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: theory
custom_name: Enable Internal Mode
files:
- name: src/main/kotlin/org/jetbrains/academy/plugin/course/dev/setup/Main.kt
visible: true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
id: 987794995
12 changes: 12 additions & 0 deletions introductionSection/setupLesson/internalModeSetupTask/task.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
To have access to some internal actions you need to enable `Internal Mode`.
Please, follow steps in internal mode instruction configuration [by this link](https://plugins.jetbrains.com/docs/intellij/enabling-internal.html) and enable it in your IDE.

![Internal mode documentation](./../../../common/src/main/resources/images/internal-mode-doc.gif)

**Do not forget to restart you IDE after changing idea.properties file!!!**

Also, following gif will show you how Helena done this in her IDE.

![Internal mode documentation](./../../../common/src/main/resources/images/internal-mode-ide.gif)


3 changes: 3 additions & 0 deletions introductionSection/setupLesson/lesson-info.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
custom_name: Setup before you start
content:
- internalModeSetupTask
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: 1262323620
Original file line number Diff line number Diff line change
@@ -1 +1 @@
id: 963422420
id: 788265721
Original file line number Diff line number Diff line change
@@ -1 +1 @@
id: 1111659873
id: 156535081
2 changes: 1 addition & 1 deletion introductionSection/welcomeLesson/lesson-info.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
custom_name: Introduction to PSI
custom_name: Welcome and good luck!
content:
- helloHelenaTask
- helpJonsiTask
2 changes: 1 addition & 1 deletion introductionSection/welcomeLesson/lesson-remote-info.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
id: 272542520
id: 536458692
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@

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

<div class="hint" title="Which class should I use as aClass parameter?">

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

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
@@ -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>
28 changes: 14 additions & 14 deletions psiSection/introductionToPsiLesson/IntroToPsiLesson/task.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@

PSI stands for Program Structure Interface. It's a part of IntelliJ Platform SDK, please have a brief look into
PSI stands for Program Structure Interface. It's a part of IntelliJ Platform SDK, please have a brief look into
[PSI Documentation](https://plugins.jetbrains.com/docs/intellij/psi.html) on IntelliJ Platform SDK Documentation web page.
As you probably read there, PSI represents the entire structure of the code in a project as a tree of elements,
As you probably read there, PSI 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 [`PSI File`](https://plugins.jetbrains.com/docs/intellij/psi-files.html) element, representing an entire file.
* Each node or [`PSI Element`](https://plugins.jetbrains.com/docs/intellij/psi-elements.html) in the tree represents a syntactic or structural part of the code, like expressions,
* statements, and declarations.
* The root is a [`PSI File`](https://plugins.jetbrains.com/docs/intellij/psi-files.html) element, representing an entire file
* Each node or [`PSI Element`](https://plugins.jetbrains.com/docs/intellij/psi-elements.html) 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.
* 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

Each language (Java/Kotlin/Python) has their own implementations of [`PsiElement`](https://github.com/JetBrains/intellij-community/blob/master/platform/core-api/src/com/intellij/psi/PsiElement.java),
Each language (Java, Kotlin, Python, etc.) has their own implementations of [`PsiElement`](https://github.com/JetBrains/intellij-community/blob/master/platform/core-api/src/com/intellij/psi/PsiElement.java),
so for example for file abstraction there are following options:
* [`com.intellij.psi.PsiFile`](https://github.com/JetBrains/intellij-community/blob/idea/232.10227.8/platform/core-api/src/com/intellij/psi/PsiFile.java): Represents an Java file.
* [`org.jetbrains.kotlin.psi.KtFile`](https://github.com/JetBrains/kotlin/blob/master/compiler/psi/src/org/jetbrains/kotlin/psi/KtFile.kt): Represents a Kotlin file.
* [`com.jetbrains.python.psi.PyFile`](https://github.com/JetBrains/intellij-community/blob/master/python/python-psi-api/src/com/jetbrains/python/psi/PyFile.java#L28): Represents a Python file.
* [`com.intellij.psi.PsiFile`](https://github.com/JetBrains/intellij-community/blob/idea/232.10227.8/platform/core-api/src/com/intellij/psi/PsiFile.java): Represents an Java file
* [`org.jetbrains.kotlin.psi.KtFile`](https://github.com/JetBrains/kotlin/blob/master/compiler/psi/src/org/jetbrains/kotlin/psi/KtFile.kt): Represents a Kotlin file
* [`com.jetbrains.python.psi.PyFile`](https://github.com/JetBrains/intellij-community/blob/master/python/python-psi-api/src/com/jetbrains/python/psi/PyFile.java#L28): Represents a Python file

4 changes: 4 additions & 0 deletions psiSection/introductionToPsiLesson/TryPsiViewerLesson/task.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
Follow a `useful tools` link in [PSI Documentation main page](https://plugins.jetbrains.com/docs/intellij/psi.html)
and find an action which provides an ability to see and analyze PSI tree of opened file.

![View PSI documentation](./../../../common/src/main/resources/images/psi-viewer-doc.gif)

Now, I’d want you, **Jonsi**, to look at to PSI tree of `Person.kt` file
and select PSI elements presented in it?

<div class="hint" title="How to find PSI Viewer?">

Use `Tools | View PSI Structure of Current File...` action to access PSI tree

![View PSI in IDE](./../../../common/src/main/resources/images/psi-viewer-ide.gif)

</div>

0 comments on commit a1f9bd0

Please sign in to comment.