summaryrefslogtreecommitdiff
path: root/plugins/ide-features-trainer/src/training/dsl/LessonContext.kt
blob: d06e3d4d393e2216d9b04fd013ea15268dbccaa1 (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
// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package training.dsl

import com.intellij.openapi.application.ModalityState
import com.intellij.util.concurrency.annotations.RequiresEdt
import org.intellij.lang.annotations.Language
import org.jetbrains.annotations.Nls
import training.learn.course.KLesson

@LearningDsl
abstract class LessonContext : LearningDslBase {
  /**
   * Start a new task in a lesson context
   */
  @RequiresEdt
  open fun task(taskContent: TaskContext.() -> Unit) = Unit

  /**
   * There will not be any freeze in GUI thread.
   * The continuation of the script will be scheduled with the [delayMillis]
   */
  open fun waitBeforeContinue(delayMillis: Int) = Unit

  /// SHORTCUTS ///

  fun prepareRuntimeTask(modalityState: ModalityState? = ModalityState.any(), preparation: TaskRuntimeContext.() -> Unit) {
    task {
      addFutureStep {
        taskInvokeLater(modalityState) {
          preparation()
          completeStep()
        }
      }
    }
  }

  /** Describe a simple task: just one action required */
  fun actionTask(action: String, @Nls getText: TaskContext.(action: String) -> String) {
    task {
      text(getText(action))
      trigger(action)
      test { actions(action) }
    }
  }

  fun text(@Language("HTML") @Nls text: String) = task { text(text) }

  /**
   * Just shortcut to write action name once
   * @see task
   */
  fun task(action: String, taskContent: TaskContext.(action: String) -> Unit) = task {
    taskContent(action)
  }

  /** Select text in editor */
  fun select(startLine: Int, startColumn: Int, endLine: Int, endColumn: Int) = prepareRuntimeTask {
    select(startLine, startColumn, endLine, endColumn)
  }

  open fun caret(offset: Int) = prepareRuntimeTask {
    caret(offset)
  }

  /** NOTE:  [line] and [column] starts from 1 not from zero. So these parameters should be same as in editors. */
  open fun caret(line: Int, column: Int) = prepareRuntimeTask {
    caret(line, column)
  }

  open fun caret(text: String, select: Boolean = false) = prepareRuntimeTask {
    caret(text, select)
  }

  open fun caret(position: LessonSamplePosition) = prepareRuntimeTask {
    caret(position)
  }

  @JvmOverloads
  open fun prepareSample(sample: LessonSample, checkSdkConfiguration: Boolean = true) {
    prepareRuntimeTask { setSample(sample) }

    if (checkSdkConfiguration) {
      sdkConfigurationTasks()
    }
  }

  internal abstract val lesson: KLesson
}