#๐ How do I add syntax highlighting to my IDE?
20 messages ยท Page 1 of 1 (latest)
@warped oar
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.
What IDE? Any IDe configured for Python should have highlighting by default.
i'm making my own using tkinter
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.
thank you
There's plenty of libraries that do IDE things. What language are you using?
python
I suggest using tree sitter for syntax highlighting.
i'll check that out, thank you ๐
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)
: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
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.
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.
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.