The MATLAB Blog

Practical Advice for People on the Leading Edge

Do you use Visual Studio Code? MATLAB is now there too.

Along with many other developers, it was love at first sight for me when I first experienced Visual Studio Code. Highly customisable, easy to use and available for all 3 major operating systems; it took hardly any time at all for me to switch from using a plethora of editors across all my machines to using just two – The MATLAB Editor and Visual Studio Code.

MathWorks releases MATLAB extension for Visual Studio Code

For the impatient among you, let’s get straight to the point. MathWorks now support Visual Studio Code and have made the official MATLAB Extension available via the Visual Studio Marketplace. You can also find it on GitHub at https://github.com/mathworks/matlab-extension-for-vscode
The extension provides a set of functionality that makes it easier to edit and develop MATLAB code in Visual Studio Code. I’ll explore some of this functionality in detail later. We’ve built this as part of a project that aims to meet MATLAB users wherever they are; and a lot of MATLAB users are in Visual Studio Code.
This is version 1 of an integration that intend to develop further and so we welcome your feedback. If you encounter a technical issue or have an enhancement request, create an issue on GitHub or contact MathWorks at https://www.mathworks.com/support/contact_us.html.
With that out of the way, I invite you to settle in and explore some of the details with me.

Features of the MATLAB extension for VS Code

The extension has a set of basic features, which work whether or not you have MATLAB installed on your system, and a set of advanced features that require MATLAB.

Basic features (MATLAB not required)

Everybody gets the following basic features
  • Syntax highlighting
  • Code snippets
  • Commenting
  • Code folding
The simplest thing expected of any language extension for Visual Studio Code is syntax highlighting. Below, you can see how a MATLAB class definition appeared in my Visual Studio Code running in Dark Mode, both with (bottom) and without (top) the extension activated.
Code Snippets in Visual Studio Code are templates for common code patterns such as for-loops, function definitions or conditional blocks. Not only are they convenient time-savers that can reduce the number of keystrokes required as you code but they are also useful if, like me, you switch between programming languages a lot and muddle the syntax for some constructs in your head. The MATLAB extension supports a set of these code snippets and more can be added in the future. Pressing CRTL and Space will bring up the list that you can choose from using either the mouse or keyboard.
code_snippet1.gif
Alternatively, just start typing and use the keyboard to select the relevant snippet
code_snippet2.gif
Commenting a line is achieved with CTRL (or Command on Mac) and / after placing the cursor anywhere on the line you wish to comment out. You can also select a section of text and use CTRL and / to comment it all.
commenting.gif
The final feature that’s available to everyone is code folding – useful for summarising and navigating large code files
coldfolding.gif

Advanced features (MATLAB Install required)

For those who do have MATLAB R2021a or later installed, there is a lot more available because of the additional infrastructure that MATLAB provides. For example:
  • Automatic code completion
  • Source code formatting (document formatting)
  • Code navigation
  • Code analysis, such as continuous code checking and automatic fixes
My favourite feature is the code analysis – something that I rely on hugely when using the MATLAB Editor. It’s great to have this available in Visual Studio Code too. It gives coding advice as you go. For example, in the example below, I copied and pasted something from some ancient MATLAB code that is no longer recommended.
code_analyze.gif
The code analyzer recognises this and underlines it for me. When I hover over the underline, it tells me why it chose to bring this to my attention and advises what I might be able to do about it.
We can also see code completion in action in the above example. When I start typing rng, it shows me that the function signature is rng(seed,generator). As I continue typing it shows me the possible options for generator.
This stuff requires a headless MATLAB to be running in the background. By default, this is launched as soon as the extension starts but this can be controlled via the extension settings. You can even switch it off altogether if you don’t want this functionality

What’s missing from this version of the extension?

Version 1 of the extension is focused on providing a feature-rich MATLAB code editing experience in Visual Studio Code. As such, it doesn’t include things such as the ability to execute MATLAB code or debugging support. However, version 1 is just the beginning and these things are on the radar. If there is anything you would like to see, do let us know by creating an issue on GitHub or contact MathWorks at https://www.mathworks.com/support/contact_us.html.

Pull Requests are welcome

The extension’s source code is available on GitHub under the MIT license but you might notice that some of the source code is in the form of .p files that are not human-readable by design. This is because those files make use of internal APIs that we are not ready to expose. Our plan is to create documented versions of those APIs and as we do so, we’ll replace the .p with .m.
All of this will help us in our aim developing this extension in collaboration with the community. As such, Pull Requests are welcome and details about the process can be found in the Contributing.md file in the GitHub repo.

Thank you Xavier Hahn!

While on the subject of community, I’d like to give a shoutout to Xavier Hahn who has been maintaining the most popular MATLAB extension for Visual Studio Code with over half a million installs. Lately, he was unable to devote time to develop it further and some users expressed interest in MathWorks stepping in.
“Why build something new? Why not just take over Xavier’s project?” I hear you asking. Good question. I asked it too when this project was announced internally, and the answer is that we have chosen to adopt a different architecture. Our extension leverages Microsoft's Language Server Protocol (LSP). This will result in a MATLAB language server that can be reused between IDEs that provide pluggable language support through the LSP interface.
We’ve been discussing the transition with Xavier, and he decided he would advise his users to migrate to this new extension and would deprecate his project. Thank you, Xavier, for your support and vision over the years. Without the success of his project among MATLAB users, we would have not come to embrace the idea of supporting this very popular IDE.

Over to you

So there we have it. Hot on the heels of our new Jupyter Kernel, we have a new Visual Studio Code Extension. Try it out, let us know what you think and enjoy!
|
  • print

评论

要发表评论,请点击 此处 登录到您的 MathWorks 帐户或创建一个新帐户。