<commit_before>package org.scalaide.extensions
package autoedits

import org.eclipse.jface.text.{Document => EDocument}
import org.scalaide.core.internal.text.TextDocument
import org.scalaide.core.text.CursorUpdate
import org.scalaide.core.text.Document
import org.scalaide.core.text.TextChange
import org.scalaide.core.ui.TextEditTests

abstract class AutoEditTests extends TextEditTests {

  def autoEdit(doc: Document, change: TextChange): AutoEdit

  private var udoc: EDocument = _

  override def runTest(source: String, operation: Operation) = {
    udoc = new EDocument(source)
    operation.execute()
  }

  override def source = udoc.get()

  case class Add(text: String) extends Operation {
    override def execute() = {
      val change = TextChange(caretOffset, caretOffset, text)
      val appliedChange = autoEdit(new TextDocument(udoc), change).perform()

      appliedChange match {
        case Some(TextChange(start, end, text)) =>
          udoc.replace(start, end-start, text)

        case Some(CursorUpdate(TextChange(start, end, text), cursorPos, _)) =>
          udoc.replace(start, end-start, text)
          caretOffset = cursorPos

        case Some(o) =>
          throw new AssertionError(s"Invalid change object '$o'.")

        case None =>
          udoc.replace(change.start, change.end-change.start, change.text)
          caretOffset += 1
      }
    }
  }
}<commit_msg>Add `Remove` operation to `AutoEditTests`<commit_after>package org.scalaide.extensions
package autoedits

import org.eclipse.jface.text.{Document => EDocument}
import org.scalaide.core.internal.text.TextDocument
import org.scalaide.core.{text => impl}
import org.scalaide.core.text.CursorUpdate
import org.scalaide.core.text.Document
import org.scalaide.core.text.TextChange
import org.scalaide.core.ui.TextEditTests
import org.scalaide.extensions.AutoEdit

abstract class AutoEditTests extends TextEditTests {

  def autoEdit(doc: Document, change: TextChange): AutoEdit

  private var udoc: EDocument = _

  override def runTest(source: String, operation: Operation) = {
    udoc = new EDocument(source)
    operation.execute()
  }

  override def source = udoc.get()

  abstract class AutoEditOperation extends Operation {
    def applyChange(change: TextChange): Unit = {
      val appliedChange = autoEdit(new TextDocument(udoc), change).perform()

      appliedChange match {
        case Some(TextChange(start, end, text)) =>
          udoc.replace(start, end-start, text)

        case Some(CursorUpdate(TextChange(start, end, text), cursorPos, _)) =>
          udoc.replace(start, end-start, text)
          caretOffset = cursorPos

        case Some(o) =>
          throw new AssertionError(s"Invalid change object '$o'.")

        case None =>
          udoc.replace(change.start, change.end-change.start, change.text)
          change match {
            case impl.Add(_, text) =>
              caretOffset += text.size
            case impl.Remove(start, end) =>
              caretOffset += start-end
          }
      }
    }
  }

  case class Add(text: String) extends AutoEditOperation {
    override def execute() = {
      val change = impl.Add(caretOffset, text)
      applyChange(change)
    }
  }

  case class Remove(text: String) extends AutoEditOperation {
    override def execute() = {
      val actual = udoc.get().substring(caretOffset-text.length(), caretOffset)
      require(text == actual, "removeable content does not equal to the declared content")

      val change = impl.Remove(caretOffset-text.length(), caretOffset)
      applyChange(change)
    }
  }
}
{'fraction_non_alphanumeric': 0.10472222222222222, 'fraction_numerical': 0.0002777777777777778, 'mean_word_length': 3.196969696969697, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2565927', 'n_tokens_mistral': 1061, 'n_tokens_neox': 1020, 'n_words': 255}