I would like to use tokenColor in Visual Studio Code to highlight the initial binding of a variable in python. That is to say, the first time a variable name appears in its scope, I would like it to have a different color. This would usually be where the keyword "let" or "var" would be used in JavaScript. How would I go about adding this to VSCode?
This is not possible using a simple language grammar as it requires understanding the structure of the program itself (i.e. understanding what are initial bindings vs re-assignments). This type of highlighting is called semantic highlighting.
As of VS Code 1.29, you can implement a custom version of semantic coloring using decorators. A proper semantic highlighting api is tracked by this issue
Related
In RStudio, function variables, parameters or arguments are displayed by pressing tab.
While VSCode has a lot of features, I cannot find a similar one for Python.
I found a way for VSCode to show me the definition of the function while hovering in the function itself, but there are no autocompletion for the actual variables of that function (nor suggestions while writing). Besides, the tooltips close itself as soon as I start typing the variables.
Is there a way to get something more similar regarding autocompletion and suggestion of function variables in VSCode while using Python?
Thanks.
According to your description, it is recommended that you use the extension "Pylance", which provides outstanding language service functions.
Its 'Docstrings' and 'auto-completion' functions show us the function parameters and will not close the prompt when inputting:
Part of its function introduction:
The vscode will record all functions, classes, variables in a table?
Or How does vscode can find definitions and references?
I want to know how VScode build this "Go To Definition" function?
Go to Definition:
If a language supports it, you can go to the definition of a symbol by pressing F12.
If you press Ctrl and hover over a symbol, a preview of the declaration will appear:
Tip: You can jump to the definition with Ctrl+Click or open the definition to the side with Ctrl+Alt+Click.
Go to Type Definition:
Some languages also support jumping to the type definition of a symbol by running the Go to Type Definition command from either the editor context menu or the Command Palette. This will take you to the definition of the type of a symbol. The command editor.action.goToTypeDefinition is not bound to a keyboard shortcut by default but you can add your own custom keybinding.
the explanation is copy pasted from this page:
this
This is a very complicated process and there is a ton of research on software engineering related to this area. Most IDEs/Extensions usually do a best-effort job, and that is good enough for various use cases. It also helps if one follows a widely used build system, and sticks to widely accepted code organization.
Finding definition of a call is a complicated process and usually involve building AST (Abstract Syntax Tree) of the program and finding where a function is defined. There are two important consideration:
type system of the language
organization of code around the build system
For (mostly) statically typed languages like C/C++ etc. finding the function declaration is not too difficult but finding definition usually involves finding where the source file (.c/.cpp) file is? Without the knowledge of build system (cmake, qmake) it is very difficult and hence most IDEs have hard time supporting it.
For languages like rust, where it is customary to follow a rigid system of code organization, simple grepping with some type information is sufficient to get most of the function definitions.
For languages with dynamic type (Python, JavaScript), it is very hard to find the right definition and IDEs/Extensions usually support a well known coding style and module system. TLDR: It helps to follow the crowd in this case. In my experience, PyCharm does a very good job of finding definition of a function invocation.
I know python does not formally support any built in regional code folding syntax. I do know that a few syntaxes exist out in the wild with conventions tied to specific editors and particular comment pairs.
I know of the python source code folding syntax recognized by VS Code which uses the #region keyword
#region MY_CODE_REGION
...
#endregion
I came across another I don't recognize taken from some source over at
https://github.com/gitpython-developers/GitPython/blob/master/git/objects/tree.py#L96-L104
which used the following syntax
#{ MY_CODE_REGION
#} END MY_CODE_REGION
What other editor syntaxes exist for python? Is there a pending PEP open on the subject?
I think #%% is another option for the syntax you are looking for. This is from the Spyder editor as the method for separating sections of code (and it is listed there as the "standard cell divider" syntax)
https://docs.spyder-ide.org/3/editor.html#defining-code-cells
there are also ways to convert this to and from a jupyter notebook
https://stackoverflow.com/a/55920098/13665895
You can use extensions or display helpers in IPython to make whatever syntax highlighting you'd like on output cells.
For some special cell magics, like %%javascript you can also see the input cell itself is rendered with that language's natural syntax highlighting.
How can you cause every input cell to be displayed with some chosen, non-Python syntax highlighting (regardless of any magics used on a cell, regardless of whether the cell embodies Python code, some other language).
In my case I am working with a custom-made cell magic for a proprietary language. The %%javascript syntax highlighting works well for this language, but if I have my own %%proprietarylang magic function, I obviously can't use %%javascript to help me with how the cell is displayed.
Is there a setting I can enable when I launch the notebook, or some property of the ipython object itself that can be programmatically set inside of my magic function, which will cause the same display logic to happen as if it was %%javascript.
I know that general-purpose on-the-fly syntax highlighting is not supported by the notebook currently. I'm asking specifically about how to make use of pre-existing syntax highlighting effects, such as that of %%javascript.
I've seen some documentation referring to IPython.config.cell_magic_highlight but this does not seem to exist anymore. Is there a standard replacement for it?
To replace IPython.config.cell_magic_highlight, you can use something like
import IPython
js = "IPython.CodeCell.config_defaults.highlight_modes['magic_fortran'] = {'reg':[/^%%fortran/]};"
IPython.core.display.display_javascript(js, raw=True)
so cells which begin with %%fortran will be syntax-highlighted like FORTRAN. (However, they will still be evaluated as python if you do only this.)
For recent IPython version, the selected answer no longer works. The 'config_default' property was renamed options_default (Ipython 6.0.0).
The following works:
import IPython
js = "IPython.CodeCell.options_default.highlight_modes['magic_fortran'] = {'reg':[/^%%fortran/]};"
IPython.core.display.display_javascript(js, raw=True)
I am trying to customize the minimap package in emacs.
I'd like to be able to read the name of the functions, class, etc while in the minimap. The effect I am trying to achieve is this (although in python mode): (from www.emacswiki.org/emacs/MiniMap)
As far as I understand the minimap module, there are two way to enlarge the name of function, class, etc.
The first requires semantic overlays, but I am not familiar with the semantic package, and I am not sure how well it supports python.
The second (that I set as the default option from M-x customize-group minimap) should use the normal text height for all the functions - all the text with the face font-lock-function-name-face - but I can't get it to work.
Does anyone know how to use this minimap function?