in intellij-plugin/features/ai-hints-core/src/com/jetbrains/edu/aiHints/core/HintsLoader.kt [55:106]
fun getHint(task: Task) {
scope.launch(Dispatchers.Default) {
if (!mutex.tryLock()) {
withContext(Dispatchers.EDT) {
ErrorHintInlineBanner(project, task, EduAIHintsCoreBundle.message("action.Educational.Hints.GetHint.already.in.progress")).display()
}
return@launch
}
try {
val launchId = launchId.incrementAndGet()
val taskProcessor = TaskProcessor(task)
val taskFile = project.selectedTaskFile ?: error("TaskFile for ${task.name} not found")
val taskVirtualFile = taskFile.getVirtualFile(project) ?: error("VirtualFile for ${taskFile.name} not found")
val taskFileText = taskVirtualFile.getTextFromTaskTextFile() ?: error("TaskFile text for ${taskFile.name} not found")
val hintsAssistant = AiHintsAssistant.getAssistant(taskProcessor, AiCodeHintGenerator(taskProcessor), AiTextHintGenerator())
val hint = withBackgroundProgress(project, EduAIHintsCoreBundle.message("action.Educational.Hints.GetHint.progress.text"), cancellable = true) {
withContext(Dispatchers.IO) {
hintsAssistant.getHint(taskProcessor.getFullTaskFileText())
}
}.getOrElse {
withContext(Dispatchers.EDT) {
val errorMessage = AiAssistantException.get(it).message
ErrorHintInlineBanner(project, task, errorMessage) { getHint(task) }
.addFeedbackLink(task, taskFileText, errorMessage)
.display()
}
return@launch
}
val codeHint = hint.codeHint
if (codeHint != null) {
withContext(Dispatchers.EDT) {
val highlighter = highlightFirstCodeDiffPositionOrNull(project, taskVirtualFile, taskFileText, codeHint.code)
CodeHintInlineBanner(project, task, hint.textHint.text, highlighter)
.addCodeHint { showInCodeAction(project, launchId, taskVirtualFile, taskFileText, codeHint.code) }
.addFeedbackLikenessButtons(task, taskFileText, hint.textHint, codeHint)
.display()
}
return@launch
}
withContext(Dispatchers.EDT) {
TextHintInlineBanner(project, task, hint.textHint.text)
.addFeedbackLikenessButtons(task, taskFileText, hint.textHint)
.display()
}
}
finally {
mutex.unlock()
}
}
}