Skip to content

Send typed code blocks that touch letters or brackets - #468

Open
SzymonMrozek wants to merge 3 commits into
TelegramMessenger:masterfrom
SzymonMrozek:fix/typed-code-fence-boundaries
Open

SzymonMrozek wants to merge 3 commits into
TelegramMessenger:masterfrom
SzymonMrozek:fix/typed-code-fence-boundaries

Conversation

@SzymonMrozek

@SzymonMrozek SzymonMrozek commented Nov 23, 2026 •

Copy link
Copy Markdown

Depends on #276, which builds on #269, and should be merged after them. Until then the diff also shows their commits.

Key change

A typed code block is now sent as code when a fence touches a letter, a bracket or any other character except a backtick. Before, hey```abc```, ```abc```text, ```abc```) and (```abc```) were sent as typed, backticks included, and ```a```b ```c``` became one block a```b ```c. Fences next to a space, a line break or .,:?!; work as before. Single backticks, **, __, ~~ and || keep their rules.

Reason

The markdown pattern required the start of a line, a space or a line break before an opening fence, and a space, a line break, .,:?!; or the end of a line after a closing fence. When a closing fence was followed by anything else, the lazy code group kept extending to the next fence that qualified, so the block was either sent as text or swallowed everything up to the next block. Editing hits the same pattern: #269 notes that a block followed directly by a letter or a bracket can't be read back on save. With this change it can.

An opening fence may now follow any character except a backtick, and a closing fence may be followed by any character except a backtick, so a run of backticks is never split between a fence and the surrounding text. The fence lengths from #276 are unchanged.

The pattern exists in three places that must stay equal: ChatTextInputState.swift, ChatTextInputAttribute.swift (used by isInsideTypedCode to keep -- and - as typed in code, #367) and the share extension's SEUIInputView.swift. All three change.

Steps:

  1. In any chat, type ```abc```) and press Return.
  2. The message is sent as typed, with the backticks.

Screenshot/video result (before/after)

Before

Saved Messages after the three typed messages: the first two are sent as typed, the third is one block a```b ```c.

Editing the third message as sent by the After build (blocks a and c): ? added in the edit box, then Return. The two blocks merge into one.

After

The same three messages, sent at 14:22: each has abc as a block, and the third has two blocks.

The same message with ! added in the edit box, then Return. Both blocks stay.

Confirmation

Tested on macOS 26.6.2 (25G83), Debug build 13.2.0 (283467) built with Xcode 27.2, production account, Saved Messages. Before: #276 (4a265a33). After: this change on top of it. Both builds had local-only edits that are not in this PR: the Xcode 27 changes from XCODE27.md, the SDK path in the dav1d meson files, and a separate bundle identifier, app group and update feed so the build doesn't share data with an installed Telegram.

Case Expected Before After
```abc```text, Return abc as a block, then text Sent as typed abc as a block, then text
(```abc```), Return (, abc as a block, ) Sent as typed (, abc as a block, )
```a```b ```c```, Return Two blocks, a and c One block a```b ```c Two blocks, a and c, with b between
The message with blocks a and c: ↑ to edit, one character added at the end, Return Both blocks stay Blocks merge into a```b ```c Both blocks stay

Messages were typed key by key and sent with Return. The edit case ran in the After build first, then in the Before build on the same message.

A local test script compiles subInputState and the markdown pattern from ChatTextInputState.swift and NSAttributedString.trimmed from TGUIKit with small stubs, checks that the three copies of the pattern are equal, and checks the text and entities of 30 typed messages: 7 about fences next to other characters, 12 about a block's first line (#467), and 11 that must not change (inline code, **bold**, a bold attribute inside and before a block, unclosed fences, the fence lengths from #276). On #276, all 7 fail. With this change all 7 pass and the other 11 are unchanged.

subInputState timing, same machine, Release build of the test:

Text #276 This change
4096 backticks 0.9 ms 1.3 ms
```a repeated to 4096 characters 1.3 ms 1.3 ms
```a `` repeated to 4096 characters 0.6 ms 3.0 ms
100,000 letters and backticks 13.1 ms 13.6 ms
``` x repeated to 100,000 characters 24.0 ms 58.5 ms

Limitations:

  • A ``` inside a word now opens a block when a later ``` closes it: a```b```c is sent as a, b as a block, c.

  • isInsideTypedCode follows the pattern, so -- and - also stay as typed inside such blocks.

  • One pass per build, Saved Messages only.

  • The share extension's parser has the same pattern but wasn't run.

  • I confirm that I have tested the affected behavior and verified that it works properly, or verified the content and links for a documentation-only change.

null00x and others added 3 commits November 18, 2026 02:50
The edit box shows code blocks as ``` fences. A block without a
language got the fence right before its first line, so saving the edit
read that line as the language: "a = 1\nb = 2" came back as "b = 2"
labelled "a = 1". The edit box now ends the opening fence line with a
line break for blocks with a language or more than one line, and the
save path drops the line break that ends an opening fence line, so the
text and entities come back exactly as they were. A typed ``` fence
works the same way: the line break after it is no longer sent as the
first character of the code.
The edit box writes code blocks between backtick fences, and saving
read a fence as three or four backticks, with a closing fence that
didn't have to match the opening one. A backtick at the end of the
code joined the closing fence, so "name = `x`" came back as
"name = `x", and code that starts with four backticks lost one too.
The fence is now one backtick longer than the longest run of
backticks in the code, at least three, and the closing fence must
match the opening one. A fence of three or four backticks is read as
before and a longer one is taken whole, so saving stays as fast as
before on long runs of backticks. Code that starts with a backtick
gets the line break after the opening fence that TelegramMessenger#269 adds for other
blocks, and a first line that starts with a backtick is no longer
read as a language. Formatting after a block moves by the fences'
real length, and the share extension's copy of the parser gets the
same pattern and offsets.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants