summaryrefslogtreecommitdiff
path: root/plugins/ide-features-trainer/src/training/learn/lesson/general/assistance/LocalHistoryLesson.kt
blob: 90570a4544a0c641ed2f38c7a612751331cd1647 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package training.learn.lesson.general.assistance

import com.intellij.CommonBundle
import com.intellij.history.integration.ui.actions.LocalHistoryGroup
import com.intellij.history.integration.ui.actions.ShowHistoryAction
import com.intellij.icons.AllIcons
import com.intellij.idea.ActionsBundle
import com.intellij.openapi.actionSystem.CommonDataKeys
import com.intellij.openapi.actionSystem.DataProvider
import com.intellij.openapi.actionSystem.IdeActions
import com.intellij.openapi.actionSystem.impl.ActionMenu
import com.intellij.openapi.actionSystem.impl.ActionMenuItem
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.ModalityState
import com.intellij.openapi.application.invokeAndWaitIfNeeded
import com.intellij.openapi.application.invokeLater
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.EditorModificationUtil
import com.intellij.openapi.editor.LogicalPosition
import com.intellij.openapi.editor.ex.EditorEx
import com.intellij.openapi.editor.ex.EditorGutterComponentEx
import com.intellij.openapi.editor.impl.EditorComponentImpl
import com.intellij.openapi.fileEditor.FileDocumentManager
import com.intellij.openapi.progress.runBackgroundableTask
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.Messages
import com.intellij.openapi.ui.popup.Balloon
import com.intellij.openapi.util.io.FileUtil
import com.intellij.ui.components.JBLoadingPanel
import com.intellij.ui.components.JBLoadingPanelListener
import com.intellij.ui.table.JBTable
import com.intellij.ui.tabs.impl.SingleHeightTabs
import com.intellij.util.DocumentUtil
import com.intellij.util.concurrency.annotations.RequiresBackgroundThread
import com.intellij.util.ui.UIUtil
import org.assertj.swing.core.MouseButton
import org.assertj.swing.data.TableCell
import org.assertj.swing.fixture.JTableFixture
import org.jetbrains.annotations.Nls
import training.FeaturesTrainerIcons
import training.dsl.*
import training.dsl.LessonUtil.restoreIfModifiedOrMoved
import training.learn.LearnBundle
import training.learn.LessonsBundle
import training.learn.course.KLesson
import training.learn.course.LessonProperties
import training.learn.course.LessonType
import training.learn.lesson.LessonManager
import training.ui.LearningUiHighlightingManager
import training.ui.LearningUiHighlightingManager.HighlightingOptions
import training.ui.LearningUiUtil
import training.util.LessonEndInfo
import java.awt.Component
import java.awt.Point
import java.awt.Rectangle
import java.util.concurrent.CompletableFuture
import javax.swing.JFrame

class LocalHistoryLesson : KLesson("CodeAssistance.LocalHistory", LessonsBundle.message("local.history.lesson.name")) {
  override val languageId = "yaml"
  override val lessonType = LessonType.SCRATCH
  override val properties = LessonProperties(availableSince = "212.5284")

  private val lineToDelete = 14
  private val revisionInd = 2

  private val sample = parseLessonSample("""
    cat:
      name: Pelmen
      gender: male
      breed: sphinx
      fur_type: hairless
      fur_pattern: solid
      fur_colors: [ white ]
      tail_length: long
      eyes_colors: [ green ]
    
      favourite_things:
        - three plaids
        - pile of clothes
        - castle of boxes
        - toys scattered all over the place
    
      behavior:
        - play:
            condition: boring
            actions:
              - bring one of the favourite toys to the human
              - run all over the house
        - eat:
            condition: want to eat
            actions:
              - shout to the whole house
              - sharpen claws by the sofa
              - wake up a human in the middle of the night""".trimIndent())

  private val textToDelete = """
    |    - play:
    |        condition: boring
    |        actions:
    |          - bring one of the favourite toys to the human
    |          - run all over the house
  """.trimMargin()

  private val textAfterDelete = """
    |  behavior:
    |
    |    - eat:
  """.trimMargin()

  private val textToAppend = """
    |    - sleep:
    |        condition: want to sleep
    |        action:
    |          - bury himself in a human's blanket
    |          - bury himself in a favourite plaid
  """.trimMargin()

  override val lessonContent: LessonContext.() -> Unit = {
    prepareSample(sample)

    caret(textToDelete, select = true)

    prepareRuntimeTask(ModalityState.NON_MODAL) {
      FileDocumentManager.getInstance().saveDocument(editor.document)
    }

    val localHistoryActionText = ActionsBundle.groupText("LocalHistory").dropMnemonic()
    task {
      text(LessonsBundle.message("local.history.remove.code",
                                 strong(localHistoryActionText),
                                 action(IdeActions.ACTION_EDITOR_BACKSPACE)))
      stateCheck {
        editor.document.charsSequence.contains(textAfterDelete)
      }
      restoreIfModifiedOrMoved()
      test { invokeActionViaShortcut("DELETE") }
    }

    setEditorHint(LessonsBundle.message("local.history.editor.hint"))

    waitBeforeContinue(500)

    prepareRuntimeTask {
      if (!TaskTestContext.inTestMode) {
        val userDecision = Messages.showOkCancelDialog(
          LessonsBundle.message("local.history.dialog.message"),
          LessonsBundle.message("recent.files.dialog.title"),
          CommonBundle.message("button.ok"),
          LearnBundle.message("learn.stop.lesson"),
          FeaturesTrainerIcons.Img.PluginIcon
        )
        if (userDecision != Messages.OK) {
          LessonManager.instance.stopLesson()
        }
      }
    }

    modifyFile()

    lateinit var invokeMenuTaskId: TaskContext.TaskId
    task {
      invokeMenuTaskId = taskId
      text(LessonsBundle.message("local.history.imagine.restore", strong(ActionsBundle.message("action.\$Undo.text"))))
      text(LessonsBundle.message("local.history.invoke.context.menu", strong(localHistoryActionText)))
      triggerAndBorderHighlight().component { ui: EditorComponentImpl -> ui.editor == editor }
      triggerAndFullHighlight().component { ui: ActionMenu ->
        isClassEqual(ui.anAction, LocalHistoryGroup::class.java)
      }
      test {
        ideFrame { robot().rightClick(editor.component) }
      }
    }

    task("LocalHistory.ShowHistory") {
      val showHistoryActionText = ActionsBundle.actionText(it).dropMnemonic()
      text(LessonsBundle.message("local.history.show.history", strong(localHistoryActionText), strong(showHistoryActionText)))
      triggerAndFullHighlight { clearPreviousHighlights = false }.component { ui: ActionMenuItem ->
        isClassEqual(ui.anAction, ShowHistoryAction::class.java)
      }
      trigger(it)
      restoreByUi()
      test {
        ideFrame {
          jMenuItem { item: ActionMenu -> isClassEqual(item.anAction, LocalHistoryGroup::class.java) }.click()
          jMenuItem { item: ActionMenuItem -> isClassEqual(item.anAction, ShowHistoryAction::class.java) }.click()
        }
      }
    }

    var revisionsTable: JBTable? = null
    task {
      triggerAndBorderHighlight().componentPart { ui: JBTable ->
        if (checkInsideLocalHistoryFrame(ui)) {
          revisionsTable = ui
          ui.getCellRect(revisionInd, 0, false)
        }
        else null
      }
    }

    lateinit var selectRevisionTaskId: TaskContext.TaskId
    task {
      selectRevisionTaskId = taskId
      text(LessonsBundle.message("local.history.select.revision", strong(localHistoryActionText), strong(localHistoryActionText)))
      val step = CompletableFuture<Boolean>()
      addStep(step)
      triggerUI { clearPreviousHighlights = false }.component l@{ ui: JBLoadingPanel ->
        if (!checkInsideLocalHistoryFrame(ui)) return@l false
        ui.addListener(object : JBLoadingPanelListener {
          override fun onLoadingStart() {
            // do nothing
          }

          override fun onLoadingFinish() {
            val revisions = revisionsTable ?: return
            if (revisions.selectionModel.selectedIndices.let { it.size == 1 && it[0] == revisionInd }) {
              ui.removeListener(this)
              step.complete(true)
            }
          }
        })
        true
      }
      restoreByUi(invokeMenuTaskId, delayMillis = defaultRestoreDelay)
      test {
        ideFrame {
          Thread.sleep(1000)
          val table = revisionsTable ?: error("revisionsTable is not initialized")
          JTableFixture(robot(), table).click(TableCell.row(revisionInd).column(0), MouseButton.LEFT_BUTTON)
        }
      }
    }

    task {
      triggerAndBorderHighlight().componentPart { ui: EditorGutterComponentEx -> findDiffGutterRect(ui) }
    }

    task {
      text(LessonsBundle.message("local.history.restore.code", icon(AllIcons.Diff.ArrowRight)))
      text(LessonsBundle.message("local.history.restore.code.balloon"),
        LearningBalloonConfig(Balloon.Position.below, 0, cornerToPointerDistance = 50))
      stateCheck {
        editor.document.charsSequence.contains(textToDelete)
      }
      restoreByUi(invokeMenuTaskId)
      restoreState(selectRevisionTaskId) l@{
        val revisions = revisionsTable ?: return@l false
        revisions.selectionModel.selectedIndices.let { it.size != 1 || it[0] != revisionInd }
      }
      test {
        ideFrame {
          val gutterComponent = previous.ui as? EditorGutterComponentEx ?: error("Failed to find gutter component")
          val gutterRect = findDiffGutterRect(gutterComponent) ?: error("Failed to find required gutter")
          robot().click(gutterComponent, Point(gutterRect.x + gutterRect.width / 2, gutterRect.y + gutterRect.height / 2))
        }
      }
    }

    task {
      before { LearningUiHighlightingManager.clearHighlights() }
      text(LessonsBundle.message("local.history.close.window", action("EditorEscape")))
      stateCheck {
        val focusedEditor = focusOwner as? EditorComponentImpl
        // check that it is editor from main IDE frame
        focusedEditor != null && UIUtil.getParentOfType(SingleHeightTabs::class.java, focusedEditor) != null
      }
      test {
        invokeActionViaShortcut("ESCAPE")
        // sometimes some small popup appears at mouse position so the first Escape may close just that popup
        invokeActionViaShortcut("ESCAPE")
      }
    }

    setEditorHint(null)

    text(LessonsBundle.message("local.history.congratulations"))
  }

  override fun onLessonEnd(project: Project, lessonEndInfo: LessonEndInfo) {
    if (!lessonEndInfo.lessonPassed) return
    ApplicationManager.getApplication().executeOnPooledThread {
      val editorComponent = LearningUiUtil.findComponentOrNull(project, EditorComponentImpl::class.java) { editor ->
        UIUtil.getParentOfType(SingleHeightTabs::class.java, editor) != null
      } ?: error("Failed to find editor component")
      invokeLater {
        val lines = textToDelete.lines()
        val rightColumn = lines.maxOf { it.length }
        LearningUiHighlightingManager.highlightPartOfComponent(editorComponent, HighlightingOptions(highlightInside = false)) {
          val editor = editorComponent.editor
          val textToFind = lines[0].trim()
          val offset = editor.document.charsSequence.indexOf(textToFind)
          if (offset == -1) error("Failed to find '$textToFind' in the editor")
          val leftPosition = editor.offsetToLogicalPosition(offset)
          val leftPoint = editor.logicalPositionToXY(leftPosition)
          val rightPoint = editor.logicalPositionToXY(LogicalPosition(leftPosition.line, rightColumn))
          Rectangle(leftPoint.x - 3, leftPoint.y, rightPoint.x - leftPoint.x + 6, editor.lineHeight * lines.size)
        }
      }
    }
  }

  private fun isClassEqual(value: Any, expectedClass: Class<*>): Boolean {
    return value.javaClass.name == expectedClass.name
  }

  private fun findDiffGutterRect(ui: EditorGutterComponentEx): Rectangle? {
    val editor = CommonDataKeys.EDITOR.getData(ui as DataProvider) ?: return null
    val offset = editor.document.charsSequence.indexOf(textToDelete)
    return if (offset != -1) {
      val lineIndex = editor.document.getLineNumber(offset)
      invokeAndWaitIfNeeded {
        val y = editor.visualLineToY(lineIndex)
        Rectangle(ui.width - ui.whitespaceSeparatorOffset, y, ui.width - 26, editor.lineHeight)
      }
    }
    else null
  }

  private fun TaskRuntimeContext.checkInsideLocalHistoryFrame(component: Component): Boolean {
    val frame = UIUtil.getParentOfType(JFrame::class.java, component)
    return frame?.title == FileUtil.toSystemDependentName(virtualFile.path)
  }

  // If message is null it will remove the existing hint and allow file modification
  private fun LessonContext.setEditorHint(@Nls message: String?) {
    prepareRuntimeTask {
      EditorModificationUtil.setReadOnlyHint(editor, message)
      (editor as EditorEx).isViewer = message != null
    }
  }

  private fun LessonContext.modifyFile() {
    task {
      addFutureStep {
        val editor = this.editor
        runBackgroundableTask(LessonsBundle.message("local.history.file.modification.progress"), project, cancellable = false) {
          val document = editor.document
          invokeAndWaitIfNeeded { FileDocumentManager.getInstance().saveDocument(document) }
          removeLineWithAnimation(editor)
          invokeAndWaitIfNeeded { FileDocumentManager.getInstance().saveDocument(document) }
          Thread.sleep(50)
          insertStringWithAnimation(editor, textToAppend, editor.document.textLength)
          taskInvokeLater {
            editor.caretModel.moveToOffset(document.textLength)
            FileDocumentManager.getInstance().saveDocument(document)
            completeStep()
          }
        }
      }
    }
  }

  @RequiresBackgroundThread
  private fun removeLineWithAnimation(editor: Editor) {
    val document = editor.document
    val startOffset = document.getLineStartOffset(lineToDelete)
    val endOffset = document.getLineEndOffset(lineToDelete)
    for (ind in endOffset downTo startOffset) {
      invokeAndWaitIfNeeded {
        DocumentUtil.writeInRunUndoTransparentAction {
          editor.caretModel.moveToOffset(ind)
          document.deleteString(ind - 1, ind)
        }
      }
      Thread.sleep(10)
    }
  }

  @RequiresBackgroundThread
  private fun insertStringWithAnimation(editor: Editor, text: String, offset: Int) {
    val document = editor.document
    for (ind in text.indices) {
      invokeAndWaitIfNeeded {
        DocumentUtil.writeInRunUndoTransparentAction {
          document.insertString(offset + ind, text[ind].toString())
          editor.caretModel.moveToOffset(offset + ind)
        }
      }
      Thread.sleep(10)
    }
  }

  override val suitableTips = listOf("local_history")

  override val helpLinks: Map<String, String> get() = mapOf(
    Pair(LessonsBundle.message("local.history.help.link"),
         LessonUtil.getHelpLink("local-history.html")),
  )
}