fun getPlaceholders()

in intellij-plugin/educational-core/testSrc/com/jetbrains/edu/learning/EduTestCase.kt [336:382]


    fun getPlaceholders(document: Document, useLength: Boolean): List<AnswerPlaceholder> {
      return WriteCommandAction.writeCommandAction(null).compute<List<AnswerPlaceholder>, RuntimeException> {
        val placeholders = mutableListOf<AnswerPlaceholder>()
        val openingTagRx = "<placeholder( taskText=\"(.+?)\")?( possibleAnswer=\"(.+?)\")?( hint=\"(.+?)\")?( hint2=\"(.+?)\")?>"
        val closingTagRx = "</placeholder>"
        val text = document.charsSequence
        val openingMatcher = Pattern.compile(openingTagRx).matcher(text)
        val closingMatcher = Pattern.compile(closingTagRx).matcher(text)
        var pos = 0
        while (openingMatcher.find(pos)) {
          val answerPlaceholder = AnswerPlaceholder()
          val taskText = openingMatcher.group(2)
          if (taskText != null) {
            answerPlaceholder.placeholderText = taskText
            answerPlaceholder.length = taskText.length
          }
          var possibleAnswer = openingMatcher.group(4)
          if (possibleAnswer != null) {
            answerPlaceholder.possibleAnswer = possibleAnswer
          }
          answerPlaceholder.offset = openingMatcher.start()
          if (!closingMatcher.find(openingMatcher.end())) {
            LOG.error("No matching closing tag found")
          }
          var length: Int
          if (useLength) {
            answerPlaceholder.placeholderText = text.substring(openingMatcher.end(), closingMatcher.start())
            answerPlaceholder.length = closingMatcher.start() - openingMatcher.end()
            length = answerPlaceholder.length
          }
          else {
            if (possibleAnswer == null) {
              possibleAnswer = document.getText(TextRange.create(openingMatcher.end(), closingMatcher.start()))
              answerPlaceholder.possibleAnswer = possibleAnswer
              answerPlaceholder.length = possibleAnswer.length
            }
            length = answerPlaceholder.possibleAnswer.length
          }
          document.deleteString(closingMatcher.start(), closingMatcher.end())
          document.deleteString(openingMatcher.start(), openingMatcher.end())
          FileDocumentManager.getInstance().saveDocument(document)
          placeholders.add(answerPlaceholder)
          pos = answerPlaceholder.offset + length
        }
        placeholders
      }
    }