Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Telegram-Mac/ChatInterfaceInputContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Cocoa
import TelegramCore

import Postbox
import FoundationUtils

struct PossibleContextQueryTypes: OptionSet {
var rawValue: Int32
Expand Down Expand Up @@ -91,6 +92,9 @@ func textInputStateContextQueryRangeAndType(_ inputState: ChatTextInputState, in
if maxIndex == inputText.startIndex {
return nil
}
if let commandQueryRange = TelegramCommandAutocomplete.queryRange(in: inputText, cursorUTF16Offset: inputState.selectionRange.lowerBound) {
return (commandQueryRange, [.command], nil)
}
var index = inputText.index(before: maxIndex)

// if inputText.length <= 7, inputText.isSingleEmoji {
Expand Down
3 changes: 3 additions & 0 deletions packages/FoundationUtils/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@ let package = Package(
.target(
name: "FoundationUtils",
dependencies: []),
.testTarget(
name: "FoundationUtilsTests",
dependencies: ["FoundationUtils"]),
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import Foundation

public enum TelegramCommandAutocomplete {
public static func queryRange(in text: String, cursorUTF16Offset: Int) -> Range<String.Index>? {
let cursorUTF16Offset = min(max(0, cursorUTF16Offset), text.utf16.count)
let cursorUTF16Index = text.utf16.index(text.utf16.startIndex, offsetBy: cursorUTF16Offset)
guard let cursorIndex = cursorUTF16Index.samePosition(in: text) else {
return nil
}
guard text.startIndex < cursorIndex, text.first == "/" else {
return nil
}
if cursorIndex < text.endIndex, !text[cursorIndex].isWhitespace {
return nil
}
let commandStartIndex = text.index(after: text.startIndex)
for character in text[commandStartIndex ..< cursorIndex] {
guard isValidCommandCharacter(character) else {
return nil
}
}
return commandStartIndex ..< cursorIndex
}

private static func isValidCommandCharacter(_ character: Character) -> Bool {
if character == "_" {
return true
}
guard let scalar = character.unicodeScalars.first, character.unicodeScalars.count == 1 else {
return false
}
return CharacterSet.alphanumerics.contains(scalar)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import XCTest
@testable import FoundationUtils

final class TelegramCommandAutocompleteTests: XCTestCase {
func testKeepsAutocompleteOpenForAlphanumericCommandQuery() {
let text = "/codex"

let range = TelegramCommandAutocomplete.queryRange(in: text, cursorUTF16Offset: text.utf16.count)

XCTAssertNotNil(range)
XCTAssertEqual(String(text[range!]), "codex")
}

func testKeepsAutocompleteOpenForUnderscoreInCommandQuery() {
let text = "/codex_compact"

let range = TelegramCommandAutocomplete.queryRange(in: text, cursorUTF16Offset: text.utf16.count)

XCTAssertNotNil(range)
XCTAssertEqual(String(text[range!]), "codex_compact")
}

func testRejectsInvalidCommandCharacters() {
let text = "/codex-compact"

let range = TelegramCommandAutocomplete.queryRange(in: text, cursorUTF16Offset: text.utf16.count)

XCTAssertNil(range)
}
}