in intellij-plugin/educational-core/src/com/jetbrains/edu/coursecreator/actions/studyItem/CCCreateTask.kt [72:131]
override fun initItem(holder: CourseInfoHolder<Course>, parentItem: StudyItem?, item: Task, info: NewStudyItemInfo) {
require(parentItem is Lesson) {
"parentItem should be Lesson, found `$parentItem`"
}
item.parent = parentItem
item.addDefaultTaskDescription()
val course = holder.course
if (parentItem is FrameworkLesson) {
val prevTask = parentItem.taskList.getOrNull(info.index - 2)
val prevTaskDir = prevTask?.getDir(holder.courseDir)
if (prevTask == null || prevTaskDir == null) {
initTask(course, item, info)
return
}
FileDocumentManager.getInstance().saveAllDocuments()
// We can't just copy text from course objects because they can contain outdated text
// in reason that we don't synchronize them with files system
// So we need to load actual files text from filesystem
val newTaskFiles = LinkedHashMap<String, TaskFile>()
val testDirs = course.testDirs
val defaultTestFileName = course.configurator?.testFileName ?: ""
val needCopyTests = CCSettings.getInstance().copyTestsInFrameworkLessons
for ((path, file) in prevTask.taskFiles) {
val shouldCopyTaskFile = course.configurator?.courseBuilder?.shouldCopyTaskFile(path) ?: true
if (shouldCopyTaskFile && (needCopyTests || !(testDirs.any { path.startsWith(it) } || path == defaultTestFileName))) {
newTaskFiles[path] = file.copyForNewTask(prevTaskDir, item)
}
}
item.taskFiles = newTaskFiles
if (!needCopyTests) {
initTask(course, item, info, withSources = false)
}
// copy propagatable property for all files
// there may be test files that will not be copied because property `copyTestsInFrameworkLessons` is false,
// but there may be a corresponding test file in the previous task
// in order to avoid this situation, `isPropagatable` is copied for all files
for ((name, taskFile) in item.taskFiles) {
val correspondingFile = prevTask.taskFiles[name] ?: continue
taskFile.isPropagatable = correspondingFile.isPropagatable
}
// If we insert new task between `task1` and `task2`
// we should change target of all placeholder dependencies of `task2` from task file of `task1`
// to the corresponding task file in new task
parentItem.taskList.getOrNull(info.index - 1)
?.placeholderDependencies
?.forEach { dependency ->
if (dependency.resolve(course)?.taskFile?.task == prevTask) {
val placeholder = dependency.answerPlaceholder
placeholder.placeholderDependency = dependency.copy(taskName = item.name)
}
}
item.init(parentItem, false)
} else {
initTask(course, item, info)
}
}