Nheko-Reborn / nheko

Desktop client for Matrix using Qt and C++20.
https://nheko-reborn.github.io/
GNU General Public License v3.0
1.92k stars 201 forks source link

please add code snippet syntax highlighting #248

Open AlexCzar opened 4 years ago

AlexCzar commented 4 years ago

Describe the solution you'd like I'd like multi-line code blocks to be syntax highlighted, it would be nice (but not absolutely necessary) if highlighting theme could be chosen in settings.

Describe alternatives you've considered The only alternative solution for now is to use Riot web client.

deepbluev7 commented 4 years ago

This is actually pretty complicated, since Qt by itself doesn't support this. We would need to write our own custom renderer for text elements or wait for Qt to add a more extensible API like https://bugreports.qt.io/browse/QTBUG-85651. I doubt we'll see something like this soon, the closest thing we could probably get in the near future would be a background color. Alternatively we could split the rendering into multiple text elements, but that would complicate our logic a lot too.

AlexCzar commented 4 years ago

Found this:

Accepted answer: Qt Quick's TextEdit item exposes a textDocument property, of type QQuickTextDocument. This is explicitly exposed so you can use QSyntaxHighlighter directly with the document.

QtQuick textEdit documentation for Qt 5

at https://stackoverflow.com/questions/14791360/qt5-syntax-highlighting-in-qml

other answers are interesting as well.

deepbluev7 commented 4 years ago

I don't think it is that simple, since we don't want to syntax highlight the full TextEdit, but only the <pre> tags within it. We also need to guess the programming language, etc.

tastytea commented 3 years ago

The language is sent as meta datum, Nheko at least sends <code class="language-shell"> if you type “``` shell”.

Akselmo commented 2 years ago

Could this be done using treesitter? https://tree-sitter.github.io/tree-sitter/syntax-highlighting