override fun preprocessEnter()

in plugin-bazel/src/main/kotlin/org/jetbrains/bazel/languages/projectview/formatting/ProjectViewEnterHandler.kt [65:109]


  override fun preprocessEnter(
    file: PsiFile,
    editor: Editor,
    caretOffset: Ref<Int>,
    caretAdvance: Ref<Int>,
    dataContext: DataContext,
    originalHandler: EditorActionHandler?,
  ): EnterHandlerDelegate.Result {
    var offset = caretOffset.get()
    var file = file // now it's mutable
    var editor = editor

    if (editor is EditorWindow) {
      file = InjectedLanguageManager.getInstance(file.project).getTopLevelFile(file)
      editor = InjectedLanguageEditorUtil.getTopLevelEditor(editor)
      offset = editor.caretModel.offset
    }

    if (!isApplicable(file, dataContext)) {
      return EnterHandlerDelegate.Result.Continue
    }
    val document = editor.document
    PsiDocumentManager.getInstance(file.project).commitDocument(document)
    editor.caretModel.moveToOffset(offset)

    // remove indent if enter is pressed on the blank line
    if (isBlankLine(offset, document)) {
      val lineSeparator = FileDocumentManager.getInstance().getLineSeparator(file.virtualFile, file.project)
      editor.document.insertString(offset, lineSeparator)
      editor.caretModel.moveToOffset(offset + lineSeparator.length)
      return EnterHandlerDelegate.Result.Stop
    }
    if (!insertIndent(file, offset)) {
      return EnterHandlerDelegate.Result.Continue
    }

    originalHandler?.execute(editor, editor.caretModel.currentCaret, dataContext)
    val position = editor.caretModel.logicalPosition
    if (position.column < INDENT) {
      val spacesPadding = StringUtil.repeat(" ", INDENT - position.column)
      document.insertString(editor.caretModel.offset, spacesPadding)
    }
    editor.caretModel.moveToLogicalPosition(LogicalPosition(position.line, INDENT))
    return EnterHandlerDelegate.Result.Stop
  }