Repository navigation
Send typed code blocks that touch letters or brackets - #468
Open
SzymonMrozek wants to merge 3 commits into
Open
SzymonMrozek wants to merge 3 commits into
SzymonMrozek wants to merge 3 commits into
Conversation
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.
1 task done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 blocka```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 byisInsideTypedCodeto keep--and-as typed in code, #367) and the share extension'sSEUIInputView.swift. All three change.Steps:
```abc```)and press Return.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
aandc):?added in the edit box, then Return. The two blocks merge into one.After
The same three messages, sent at 14:22: each has
abcas 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.```abc```text, Returnabcas a block, thentextabcas a block, thentext(```abc```), Return(,abcas a block,)(,abcas a block,)```a```b ```c```, Returnaandca```b ```caandc, withbbetweenaandc: ↑ to edit, one character added at the end, Returna```b ```cMessages 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
subInputStateand the markdown pattern fromChatTextInputState.swiftandNSAttributedString.trimmedfrom 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.subInputStatetiming, same machine, Release build of the test:```arepeated to 4096 characters```a`` repeated to 4096 characters``` xrepeated to 100,000 charactersLimitations:
A
```inside a word now opens a block when a later```closes it:a```b```cis sent asa,bas a block,c.isInsideTypedCodefollows 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.