Skip to content

Commit

Permalink
editing psi task done
Browse files Browse the repository at this point in the history
  • Loading branch information
Valerii committed Dec 5, 2023
1 parent 2b32e3a commit b0f3619
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 0 deletions.
18 changes: 18 additions & 0 deletions courseSection/psi/editPSI/src/Task.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.psi.PsiFile
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.psi.KtNamedFunction


fun editFunctionName(psiFile: PsiFile, currentName: String, newName: String) {
val project = psiFile.project

WriteCommandAction.runWriteCommandAction(project) {
val functions = PsiTreeUtil.findChildrenOfType(psiFile, KtNamedFunction::class.java)
for (function in functions) {
if (function.name == currentName) {
function.setName(newName)
}
}
}
}
6 changes: 6 additions & 0 deletions courseSection/psi/editPSI/task-info.yaml
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
48 changes: 48 additions & 0 deletions courseSection/psi/editPSI/task.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

This is a task description file.
Its content will be displayed to a learner
in the **Task Description** window.

It supports both Markdown and HTML.
To toggle the format, you can rename **task.md**
to **task.html**, or vice versa.
The default task description format can be changed
in **Preferences | Tools | Education**,
but this will not affect any existing task description files.

The following features are available in
**task.md/task.html** which are specific to the JetBrains Academy plugin:

- Hints can be added anywhere in the task text.
Type "hint" and press Tab.
Hints should be added to an empty line in the task text.
In hints you can use both HTML and Markdown.
<div class="hint">

Text of your hint

</div>

- You may need to refer your learners to a particular lesson,
task, or file. To achieve this, you can use the in-course links.
Specify the path using the `[link_text](course://lesson1/task1/file1)` format.

- You can insert shortcuts in the task description.
While **task.html/task.md** is open, right-click anywhere
on the **Editor** tab and choose the **Insert shortcut** option
from the context menu.
For example: &shortcut:FileStructurePopup;.

- Insert the &percnt;`IDE_NAME`&percnt; macro,
which will be replaced by the actual IDE name.
For example, **%IDE_NAME%**.

- Insert PSI elements, by using links like
`[element_description](psi_element://link.to.element)`.
To get such a link, right-click the class or method
and select **Copy Reference**.
Then press &shortcut:EditorPaste; to insert the link where appropriate.
For example, a [link to the "contains" method](psi_element://java.lang.String#contains).

- You can add link to file using **full path** like this:
`[file_link](file://lesson1/task1/file.txt)`.
24 changes: 24 additions & 0 deletions courseSection/psi/editPSI/test/Tests.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import com.intellij.psi.util.PsiTreeUtil
import com.intellij.testFramework.fixtures.BasePlatformTestCase
import org.jetbrains.kotlin.psi.KtNamedFunction

class Test : BasePlatformTestCase() {

fun testSolution() {
var file = myFixture.configureByText("MyClass.kt", """
fun sayHello() {
println("Hello")
}
fun sayGoodbye() {
println("Goodbye")
}
""".trimIndent())

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

val functions = PsiTreeUtil.findChildrenOfType(file, KtNamedFunction::class.java)
val hasNewFunction = functions.any { it.name == "greetings" }
assertTrue(hasNewFunction)
}
}
1 change: 1 addition & 0 deletions courseSection/psi/lesson-info.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ content:
- Try PSI Viewer
- Accessing PSI Elements
- Count number of classes
- editPSI

0 comments on commit b0f3619

Please sign in to comment.