|
1 | 1 | # vscode-python-fstring-dsl |
| 2 | + |
2 | 3 | Syntax highlighting for HTML, SQL, JS, CSS in Python f-strings |
3 | 4 |
|
4 | 5 | ## Usage |
5 | | -To trigger the syntax highlight you must wrap your |
6 | | -DSL-string in a specific function (or method) call: `html(...),` `sql(...)`, `js(...)`, `css(...)`. This function may be noop like the Python's `typing.cast()` or do something useful, i.e. trimming whitespaces. |
| 6 | + |
| 7 | +The syntax highlighting is triggered by wrapping the DSL-string in a |
| 8 | +specific function (or method) call: |
| 9 | + |
| 10 | +- `html(...)` |
| 11 | +- `sql(...)` |
| 12 | +- `js(...)` |
| 13 | +- `css(...)` |
| 14 | + |
| 15 | +<!-- like the Python's `typing.cast()` --> |
| 16 | + |
| 17 | +This function may be noop. Or it could do something useful, e.g. trimming whitespaces or logging. |
7 | 18 |
|
8 | 19 | ```python |
9 | 20 | def html(s: str): |
| 21 | + return s.trim() |
| 22 | +``` |
| 23 | + |
| 24 | +```python |
| 25 | +def sql(s: str): |
| 26 | + logging.info(s) |
10 | 27 | return s |
11 | 28 | ``` |
12 | 29 |
|
13 | | -If the string is template (f-string), the internal f-expressions will be syntax highlighted too. |
| 30 | +If the string is the template (f-string), the internal f-expressions will be syntax highlighted too. |
14 | 31 |
|
15 | 32 | ## Sample |
| 33 | + |
16 | 34 |  |
17 | 35 |
|
18 | 36 | This sample shows the well-typed server-side Dialog component with |
19 | | -bootsrap styling, htmx magic and text escaping. |
20 | | - |
| 37 | +bootsrap styling, htmx magic, and text escaping. |
21 | 38 |
|
22 | 39 | ## Note |
23 | 40 |
|
24 | | -- The syntax matching is naive. It will work only for the simple common cases. If something don't color the way it should, the best solution is to simplify the template. |
| 41 | +- The syntax matching is naive. It will work only for the simple common cases. If something don't color the way it should, the best workaround will be the template simplification. |
25 | 42 |
|
26 | | -- This extension deals with the highlighting only, so no Intellisence, hovers, etc. |
| 43 | +- This extension deals with the highlighting only, so no Intellisence, hovers, etc. |
27 | 44 |
|
28 | 45 | - f-strings are not very useful with CSS and JS. These languages are brace-heavy - too many braces to be escaped. |
29 | 46 |
|
30 | | -- To underline the embedded areas as in the sample above, add following to the VSCode settings.json: |
31 | | - ```json |
32 | | - "editor.tokenColorCustomizations": { |
33 | | - "textMateRules": [ |
34 | | - { |
35 | | - "scope": "meta.embedded.inline.f-expression", |
36 | | - "settings": { |
37 | | - "fontStyle": "underline", |
38 | | - }, |
39 | | - }, |
40 | | - ] |
41 | | - }, |
42 | | - ``` |
| 47 | +- Add these lines to the VSCode settings.json to underline the embedded scopes as in the sample above: |
| 48 | + ```json |
| 49 | + "editor.tokenColorCustomizations": { |
| 50 | + "textMateRules": [ |
| 51 | + { |
| 52 | + "scope": "meta.embedded.inline.f-expression", |
| 53 | + "settings": { |
| 54 | + "fontStyle": "underline", |
| 55 | + }, |
| 56 | + }, |
| 57 | + ] |
| 58 | + }, |
| 59 | + ``` |
0 commit comments