# TextFieldBuffer.ChangeList

Artifact: [androidx.compose.foundation:foundation](https://developer.android.com/jetpack/androidx/releases/compose-foundation) [View Source](https://cs.android.com/search?q=file:androidx/compose/foundation/text/input/TextFieldBuffer.kt+class:androidx.compose.foundation.text.input.TextFieldBuffer.ChangeList) Added in [1.7.0](https://developer.android.com/jetpack/androidx/releases/compose-foundation#1.7.0) Common/All
- Cmn

  ```
  interface TextFieldBuffer.ChangeList
  ```

*** ** * ** ***

The ordered list of non-overlapping and discontinuous changes performed on a `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldBuffer` during the current `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldState#edit(kotlin.Function1)` or `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/InputTransformation#(androidx.compose.foundation.text.input.TextFieldBuffer).transformInput()` operation. Changes are listed in the order they appear in the text, not the order in which they were made. Overlapping changes are represented as a single change.

## Summary

| ### Public functions |
|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/TextRange` | `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldBuffer.ChangeList#getOriginalRange(kotlin.Int)(changeIndex: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Returns the range in the original text that was replaced. | Cmn |
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/text/TextRange` | `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldBuffer.ChangeList#getRange(kotlin.Int)(changeIndex: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Returns the range in the `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldBuffer` that was changed. | Cmn |

| ### Public properties |
|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldBuffer.ChangeList#changeCount()` The number of changes that have been performed. | Cmn |

| ### Extension functions |
|---|---|---|
| `inline https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/compose/foundation/ExperimentalFoundationApi https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldBuffer.ChangeList.https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldBuffer.ChangeList#(androidx.compose.foundation.text.input.TextFieldBuffer.ChangeList).forEachChange(kotlin.Function2)( block: (range: https://developer.android.com/reference/kotlin/androidx/compose/ui/text/TextRange, originalRange: https://developer.android.com/reference/kotlin/androidx/compose/ui/text/TextRange) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html )` Iterates over all the changes in this `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldBuffer.ChangeList`. | Cmn |
| `inline https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/compose/foundation/ExperimentalFoundationApi https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldBuffer.ChangeList.https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldBuffer.ChangeList#(androidx.compose.foundation.text.input.TextFieldBuffer.ChangeList).forEachChangeReversed(kotlin.Function2)( block: (range: https://developer.android.com/reference/kotlin/androidx/compose/ui/text/TextRange, originalRange: https://developer.android.com/reference/kotlin/androidx/compose/ui/text/TextRange) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html )` Iterates over all the changes in this `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldBuffer.ChangeList` in reverse order. | Cmn |

## Public functions

### getOriginalRange

Cmn Added in [1.7.0](https://developer.android.com/jetpack/androidx/releases/compose-foundation#1.7.0)

```
fun getOriginalRange(changeIndex: Int): TextRange
```

Returns the range in the original text that was replaced.

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-index-out-of-bounds-exception/index.html` | If `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldBuffer.ChangeList#getOriginalRange(kotlin.Int)` is not in \[0, `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldBuffer.ChangeList#changeCount()`). |

### getRange

Cmn Added in [1.7.0](https://developer.android.com/jetpack/androidx/releases/compose-foundation#1.7.0)

```
fun getRange(changeIndex: Int): TextRange
```

Returns the range in the `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldBuffer` that was changed.

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-index-out-of-bounds-exception/index.html` | If `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldBuffer.ChangeList#getRange(kotlin.Int)` is not in \[0, `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldBuffer.ChangeList#changeCount()`). |

## Public properties

### changeCount

Cmn Added in [1.7.0](https://developer.android.com/jetpack/androidx/releases/compose-foundation#1.7.0)

```
val changeCount: Int
```

The number of changes that have been performed.

## Extension functions

### TextFieldBuffer.ChangeList.forEachChange

Cmn Artifact: [androidx.compose.foundation:foundation](https://developer.android.com/jetpack/androidx/releases/compose-foundation) [View Source](https://cs.android.com/search?q=file:androidx/compose/foundation/text/input/TextFieldBuffer.kt+function:forEachChange)

```
@ExperimentalFoundationApi
inline fun TextFieldBuffer.ChangeList.forEachChange(
    block: (range: TextRange, originalRange: TextRange) -> Unit
): Unit
```

Iterates over all the changes in this `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldBuffer.ChangeList`.

Changes are iterated by index, so any changes made by `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/package-summary#(androidx.compose.foundation.text.input.TextFieldBuffer.ChangeList).forEachChange(kotlin.Function2)` after the current one will be visited by `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/package-summary#(androidx.compose.foundation.text.input.TextFieldBuffer.ChangeList).forEachChange(kotlin.Function2)`. `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/package-summary#(androidx.compose.foundation.text.input.TextFieldBuffer.ChangeList).forEachChange(kotlin.Function2)` should not make any new changes *before* the current one or changes will be visited more than once. If you need to make changes, consider using `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/package-summary#(androidx.compose.foundation.text.input.TextFieldBuffer.ChangeList).forEachChangeReversed(kotlin.Function2)`.

```kotlin
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.input.TextFieldState
import androidx.compose.foundation.text.input.forEachChange
import androidx.compose.foundation.text.input.rememberTextFieldState
import androidx.compose.material3.Text
import androidx.compose.runtime.remember
import androidx.compose.ui.text.substring

// Print a log message every time the text is changed.
BasicTextField(
    state = rememberTextFieldState(),
    inputTransformation = {
        changes.forEachChange { sourceRange, replacedLength ->
            val newString = asCharSequence().substring(sourceRange)
            println("""$replacedLength characters were replaced with "$newString"""")
        }
    },
)
```

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/package-summary#(androidx.compose.foundation.text.input.TextFieldBuffer.ChangeList).forEachChangeReversed(kotlin.Function2)` |   |

### TextFieldBuffer.ChangeList.forEachChangeReversed

Cmn Artifact: [androidx.compose.foundation:foundation](https://developer.android.com/jetpack/androidx/releases/compose-foundation) [View Source](https://cs.android.com/search?q=file:androidx/compose/foundation/text/input/TextFieldBuffer.kt+function:forEachChangeReversed)

```
@ExperimentalFoundationApi
inline fun TextFieldBuffer.ChangeList.forEachChangeReversed(
    block: (range: TextRange, originalRange: TextRange) -> Unit
): Unit
```

Iterates over all the changes in this `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/TextFieldBuffer.ChangeList` in reverse order.

Changes are iterated by index, so `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/package-summary#(androidx.compose.foundation.text.input.TextFieldBuffer.ChangeList).forEachChangeReversed(kotlin.Function2)` should not perform any new changes before the current one or changes may be skipped. `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/package-summary#(androidx.compose.foundation.text.input.TextFieldBuffer.ChangeList).forEachChangeReversed(kotlin.Function2)` may make non-overlapping changes after the current one safely, such changes will not be visited.

```kotlin
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.input.TextFieldState
import androidx.compose.foundation.text.input.delete
import androidx.compose.foundation.text.input.forEachChange
import androidx.compose.foundation.text.input.forEachChangeReversed
import androidx.compose.foundation.text.input.insert
import androidx.compose.foundation.text.input.rememberTextFieldState
import androidx.compose.material3.Text
import androidx.compose.runtime.remember

// Make a text field behave in "insert mode" -- inserted text overwrites the text ahead of it
// instead of being inserted.
BasicTextField(
    state = rememberTextFieldState(),
    inputTransformation = {
        changes.forEachChangeReversed { range, originalRange ->
            if (!range.collapsed && originalRange.collapsed) {
                // New text was inserted, delete the text ahead of it.
                delete(
                    range.end.coerceAtMost(length),
                    (range.end + range.length).coerceAtMost(length),
                )
            }
        }
    },
)
```

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/foundation/text/input/package-summary#(androidx.compose.foundation.text.input.TextFieldBuffer.ChangeList).forEachChange(kotlin.Function2)` |   |