#๐Ÿ”’ How do I add syntax highlighting to my IDE?

20 messages ยท Page 1 of 1 (latest)

solemn jettyBOT
#

@warped oar

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

inland furnace
#

What IDE? Any IDe configured for Python should have highlighting by default.

warped oar
#

i'm making my own using tkinter

inland furnace
#

Oh, then if you wanted to implement it yourself, you'd probably want to use some tool to parse the code to a AST, then color the words based on how they're interpreted.

warped oar
#

thank you

tulip barn
#

There's plenty of libraries that do IDE things. What language are you using?

warped oar
#

python

tulip barn
#

I suggest using tree sitter for syntax highlighting.

warped oar
#

i'll check that out, thank you ๐Ÿ™‚

inland furnace
#

That's probably a better answer, but since I already wrote this example, I'll just share it:

#

!e

import ast

result = ast.parse("""
def func():
    print(1) 
""")

print(result.body[0])
print(result.body[0].__dict__)
print(result.body[0].body[0].value)
print(result.body[0].body[0].value.func.id)
print(result.body[0].body[0].value.args[0].value)
solemn jettyBOT
# inland furnace !e ```python import ast result = ast.parse(""" def func(): print(1) """) ...

:white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | <ast.FunctionDef object at 0x7f0b45029910>
002 | {'name': 'func', 'args': <ast.arguments object at 0x7f0b450298d0>, 'body': [<ast.Expr object at 0x7f0b45029950>], 'decorator_list': [], 'returns': None, 'type_comment': None, 'type_params': [], 'lineno': 2, 'col_offset': 0, 'end_lineno': 3, 'end_col_offset': 12}
003 | <ast.Call object at 0x7f0b44ee5710>
004 | print
005 | 1
inland furnace
#

You can see the different parts can be picked out be navigating the tree, and the types of each node can help determine what color the corresponding text should be.

warped oar
#

interesting

#

both of these have been really helpful ๐Ÿ™‚

tulip barn
#

FYI AST is usually consumed with match or ast.walk

#

!pip libcst should be used instead of ast to preserve source whitespace, comments, presedense parentheses, etc.

solemn jettyBOT
#

A concrete syntax tree with AST-like properties for Python 3.0 through 3.13 programs.

Released on <t:1731966486:D>.

solemn jettyBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.