SwiftcordApp / Swiftcord

A fully native Discord client for macOS built 100% in Swift!
https://swiftcordapp.github.io/Swiftcord/
GNU General Public License v3.0
1.85k stars 84 forks source link

Feature: Implement code block support for messages #140

Closed VishwaiOSDev closed 11 months ago

VishwaiOSDev commented 1 year ago

This feature adds the ability to format code blocks in messages. Messages containing code blocks wrapped in triple backticks (```) will now be displayed with the code block highlighted and formatted properly, without the backticks.

What was the problem?

The problem was that there was no formatting available for code blocks in a given message. The code was being displayed as plain text, which made it difficult to read and understand. The lack of formatting also made it difficult to distinguish between regular text and code blocks.

How do I know it is a working solution (what testing did I do)?

I did some testing with multiple code backticks and less backticks to see if this feature actually formats the message in the View. All of the results were as expected.

Screenshot 2023-02-26 at 8 59 24 PM

Why do I believe it is the right solution?

I believe this is the right solution because it addresses the problem of formatting code blocks in messages in a way that is clear and easy to read. The solution utilizes a custom String extension to find the ranges of backticks in the message and extract the code block content from between those ranges. It then applies the appropriate formatting to the code block using SwiftUI's Text view. I have tested this solution thoroughly and believe it will be effective in enhancing the readability of code blocks in messages.

cryptoAlgorithm commented 1 year ago

This takes a really interesting approach to adding custom parsing styles to messages xD I'll do some testing to ensure it doesn't cause any performance regressions.

VishwaiOSDev commented 1 year ago

This takes a really interesting approach to adding custom parsing styles to messages xD I'll do some testing to ensure it doesn't cause any performance regressions.

Certainly! I agree that a more comprehensive solution to adding custom parsing styles to messages would require careful planning and execution. However, currently in Swiftcord, plain text support for code is lacking and this temporary solution provides a starting point. It's a step towards better functionality, and can be iterated and improved upon gradually.

The issue you mentioned in Discord is now fixed. Kindly review it. Thanks

VishwaiOSDev commented 1 year ago

Previously, I had implemented the logic for handling only one code block in a message. Now, I have updated the code to handle multiple code blocks within a message. Can you please review the updated logic?

Screenshot 2023-02-26 at 9 55 26 PM
IsaacMarovitz commented 1 year ago

Previously, I had implemented the logic for handling only one code block in a message. Now, I have updated the code to handle multiple code blocks within a message. Can you please review the updated logic?

Would code highlighting be something that could be implemented?

Sjmarf commented 1 year ago

You could consider looking at how CodeEdit implemented this - it's under the MIT license https://github.com/CodeEditApp/CodeEditTextView

cryptoAlgorithm commented 1 year ago

You could consider looking at how CodeEdit implemented this - it's under the MIT license https://github.com/CodeEditApp/CodeEditTextView

Oh that looks like a good one, wonder if the same concept could be used for emotes support...

cryptoAlgorithm commented 1 year ago

Would code highlighting be something that could be implemented?

Ideally I would've implemented the functionality added in this PR in another way, instead of using hardcoded "rules" like the code does, as it is fragile and prone to edge cases as seen earlier.