override fun getSuggestion()

in src/main/kotlin/org/jetbrains/plugins/feature/suggester/suggesters/SurroundWithSuggester.kt [70:131]


    override fun getSuggestion(action: Action): Suggestion {
        val language = action.language ?: return NoSuggestion
        val langSupport = LanguageSupport.getForLanguage(language) ?: return NoSuggestion
        if (State.surroundingStatement?.isValid == false && action is PsiAction) {
            State.tryToUpdateSurroundingStatement(langSupport, action)
        }
        when (action) {
            is ChildReplacedAction -> {
                @Suppress("ComplexCondition")
                if (langSupport.isIfStatement(action.newChild) && action.oldChild.text == "i" ||
                    langSupport.isForStatement(action.newChild) && action.oldChild.text == "fo" ||
                    langSupport.isWhileStatement(action.newChild) && action.oldChild.text == "whil"
                ) {
                    State.applySurroundingStatementAddition(action.newChild, action.timeMillis)
                }
            }
            is ChildAddedAction -> {
                if (langSupport.isSurroundingStatement(action.newChild)) {
                    State.applySurroundingStatementAddition(action.newChild, action.timeMillis)
                }
            }
            is ChildrenChangedAction -> {
                if (State.surroundingStatement == null) return NoSuggestion
                val textInsertedAction = State.lastTextInsertedAction ?: return NoSuggestion
                val text = textInsertedAction.text
                if (text.contains("{") || text.contains("}")) {
                    val psiFile = action.parent as? PsiFile ?: return NoSuggestion
                    if (State.isOutOfDate(action.timeMillis) || text != "{" && text != "}") {
                        State.reset()
                    } else if (text == "{") {
                        if (State.isLeftBraceAdded) {
                            State.reset()
                        } else if (State.isBraceAddedToStatement(
                                langSupport,
                                psiFile,
                                textInsertedAction.caretOffset
                            )
                        ) {
                            State.applyBraceAddition(action.timeMillis, "{")
                            State.saveFirstStatementInBlock(langSupport)
                        }
                    } else if (text == "}") {
                        if (State.isLeftBraceAdded &&
                            State.isBraceAddedToStatement(langSupport, psiFile, textInsertedAction.caretOffset)
                        ) {
                            State.applyBraceAddition(action.timeMillis, "}")
                            if (State.isStatementsSurrounded(langSupport)) {
                                State.reset()
                                return createSuggestion()
                            }
                        }
                        State.reset()
                    }
                }
            }
            is EditorTextInsertedAction -> {
                State.lastTextInsertedAction = action
            }
            else -> NoSuggestion
        }
        return NoSuggestion
    }