> ## Documentation Index
> Fetch the complete documentation index at: https://www.cometchat.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# ShortCut Formatter

> Expand !shortcuts in the CometChat iOS UI Kit v6 composer from the message-shortcuts extension, with a CometChatTextFormatter.

> [accordion] AI Integration Quick Reference
>
> ```json theme={null}
> {
>   "products": ["CometChatSwiftUI", "CometChatUIKit"],
>   "protocol": "CometChatTextFormatter",
>   "tracking character": "!",
>   "extension": "message-shortcuts — CometChat.callExtension(slug: \"message-shortcuts\", type: .get, endPoint: \"v1/fetch\", body: nil)",
>   "composer": "CometChatMessageComposer(textFormatters:) (SwiftUI) / set(textFormatter:) (UIKit)",
>   "prerequisite": "Enable the Message Shortcuts extension in the CometChat dashboard"
> }
> ```

The **Message Shortcuts** extension stores short codes — `!hb` for "Happy birthday! 🎉" — for
your app. This guide builds a formatter that lists the matching shortcuts when the user types
`!`, and puts the expansion into the message when one is picked.

Read [Text Formatter Base Class](https://www.cometchat.com/docs/ui-kit/ios/v6/custom-text-formatter-guide.md) first: this is a
formatter with `!` as its tracking character.

## Steps

### 1. Fetch the shortcuts

The SDK calls back on its own queue, so bridge `callExtension` from a `nonisolated` function.
The extension answers with a `shortcuts` dictionary of code → expansion:

```swift ShortcutsAPI.swift theme={null}
import CometChatSDK

enum ShortcutsAPI {
    nonisolated static func fetch() async -> [String: String] {
        await withCheckedContinuation { continuation in
            CometChat.callExtension(slug: "message-shortcuts", type: .get, endPoint: "v1/fetch", body: nil,
                                    onSuccess: { response in
                                        continuation.resume(returning: response?["shortcuts"] as? [String: String] ?? [:])
                                    },
                                    onError: { _ in continuation.resume(returning: [:]) })
        }
    }
}
```

### 2. Create the ShortCutFormatter class

Fetch once, then filter locally as the user types. A picked item shows its expansion in the
input — `promptText` — and sends it — `underlyingText`:

```swift ShortCutFormatter.swift theme={null}
import CometChatCore
import CometChatSDK

final class ShortCutFormatter: CometChatTextFormatter {
    let formatterState = CometChatTextFormatterState(trackingCharacter: "!")
    private var shortcuts: [String: String]?

    func search(_ queryString: String?) {
        guard let query = queryString else {
            setSuggestionItemList([])
            return
        }
        if let shortcuts {
            show(shortcuts, matching: query)
            return
        }
        setShowLoadingIndicator(true)
        Task {
            let fetched = await ShortcutsAPI.fetch()   // step 1
            shortcuts = fetched
            setShowLoadingIndicator(false)
            show(fetched, matching: query)
        }
    }

    func onScrollToBottom() {}

    private func show(_ shortcuts: [String: String], matching query: String) {
        let items = shortcuts
            .filter { query.isEmpty || $0.key.hasPrefix("!" + query) || $0.key.hasPrefix(query) }
            .sorted { $0.key < $1.key }
            .map { code, expansion in
                SuggestionItem(id: code, name: "\(code) → \(expansion)", promptText: expansion,
                               underlyingText: expansion, hideLeadingIcon: true)
            }
        setSuggestionItemList(items)
    }
}
```

### 3. Override the search method

`search(_:)` above is the whole contract: called with the text after `!` while the user types,
and with `nil` when the query ends — clear the list then. `setShowLoadingIndicator(_:)` shows the
list's spinner during the first fetch.

### 4. Override onScrollToBottom

The extension returns every shortcut at once, so there is nothing more to load:
`onScrollToBottom()` stays empty.

### 5. Integrate with the Message Composer

> [tab] SwiftUI
>
> ```swift ShortcutComposer.swift theme={null}
> import SwiftUI
> import CometChatSwiftUI
> import CometChatSDK
>
> struct ShortcutComposer: View {
>     let user: CometChatSDK.User
>     // Keep mentions: textFormatters replaces the composer's list.
>     @State private var formatters: [any CometChatTextFormatter] = [CometChatMentionsFormatter(), ShortCutFormatter()]
>
>     var body: some View {
>         CometChatMessageComposer(user: user, textFormatters: formatters)
>     }
> }
> ```

> [tab] UIKit
>
> `set(textFormatter:)` appends after the composer's mentions formatter:
>
> ```swift theme={null}
> let composer = CometChatMessageComposer()
> composer.set(user: guideUser)
> composer.set(textFormatter: [ShortCutFormatter()])
> ```

Now typing `!h` lists the shortcuts that start with it; picking "Happy birthday! 🎉" puts it in
the input, and that text is what is sent.

## Next Steps

> [card] [Text Formatter Base Class](https://www.cometchat.com/docs/ui-kit/ios/v6/custom-text-formatter-guide.md)
>
> Every hook a formatter has

> [card] [Mentions Formatter](https://www.cometchat.com/docs/ui-kit/ios/v6/mentions-formatter-guide.md)
>
> The built-in formatter

> [card] [Extensions](https://www.cometchat.com/docs/ui-kit/ios/v6/extensions.md)
>
> The extensions the Kit supports

> [card] [All Guides](https://www.cometchat.com/docs/ui-kit/ios/v6/guide-overview.md)
>
> Browse the other guides


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.