rdbende / tkcode

Use Chlorophyll instead
https://github.com/rdbende/chlorophyll
MIT License
19 stars 3 forks source link

How about custom schemes and syntax? (enhancement) #4

Closed whmsft closed 2 years ago

whmsft commented 3 years ago

So I was just thinking about tkcode.. I am using it in my code editor.. I am just thinking is there a way to use custom syntax and schemes?

then users can create the json file with scheme and it would be helpful to both, the user and the creator.. and custom syntax definition? like sublime text, using regex?

rdbende commented 3 years ago

Custom color schemes

You can create and load your own color schemes. Simply specify one when initializing CodeBlock or CodeEditor:

something = CodeBlock(highlighter="your_scheme.json")

You can find an example of how to create these schemes in the schemes folder. Customization support isn't complete right now, because I'm having trouble with the font style.

Here are some ideas for what a color scheme looks like:

You can use the general option to modify the actual text widget. For example, the width of the insertion cursor, the background of the textbox, and anything else you can normally adjust on a text widget.

"general":
{
    "background": "# 343d46",
    "insertwidth": 1,
    "borderwidth": 0
},

In the selection section you can set the foreground and background colors of the selection

"selection":
{
    "background": "# 4e5a65",
    "foreground": "# d8dee9"
},

And in the syntax section, you can specify what color to use for each section. These names are the names of the pygments token, so they are so weird. You can find out what the tokens means in the source code or in the documentation.

"syntax":
{
    "Token.Keyword.Constant": "# c695c6",
    "Token.Name.Decorator": "# 6699cc",
    "Token.Comment": "# a6acb9",
}

little explanation:

Token.Keyword.Constant: True, False, None
Token.Name.Decorator: obviously a decorator
Token.Comment: a comment

Of course, you can not only change the foreground color, but if you specify only one color, not additional parameters, it will apply to it. Other things can be set up like this:

"Token.Literal.Number.Integer":
{
    "foreground": "# f9ae58",
    "background": "# 4e5a65"
},


The custom syntax definitions

I do not plan to implement this feature. It would be a lot of work, and since almost everything can be found among the lexers of Pygments, it wouldn’t really be worth it. However, if only a Pygments lexer is missing from support, you can also use them with

your_code_widget.lexer = pygments.lexers.YourLexer

Although you will be warned that it may not be fully supported. Of course with creating my own regex highlighter the syntax highlighting could be much more unique even per character, but I think Pygments are perfect for this highlight purpose.

rdbende commented 3 years ago

I reopen this issue, because I found a way you can create a custom syntax.

First you make a lexer class, similar what Pygments has:

import re

from pygments.lexer import RegexLexer
from pygments.token import Comment, Name, String, Number, Punctuation

class WhirlDataLexer(RegexLexer):

    name = 'WhirlData'
    aliases = ['whirldata']
    filenames = ['*.whirldata']

    tokens = {
        'root': [
            (r'~.*$', Comment.Single),
            (r'@.*$', Name.Other),
            (r'\'\$.*\'', Name.Variable),
            (r'\d(?:_?\d)*', Number.Integer),
            (r'::', Punctuation),
            (r'\'.*\'', String.Single),
        ],
    }

Then you just import your file, and use the lexer class like this:

code_block.lexer = WhirlDataLexer

image

whmsft commented 3 years ago

I reopen this issue, because I found a way you can create a custom syntax.

First you make a lexer class, similar what Pygments has:

WOAH! great!!