Using Rust Cargo TOML variables within VSCode? - visual-studio-code

I need to prepare a number of exercises for students learning Rust. I've found the use of ${workspaceFolderBasename} within the program field of my launch.json to be quite useful, as in:
"program": "${workspaceFolder}/target/debug/${workspaceFolderBasename}.exe"
This allows me to execute the program using CTRL+F5. However, this only works while the resulting executable has the same name as the workspace folder (i.e. ${workspaceFolderBasename}). I note that the name field in my project's Cargo.toml file has a name field under [package] which controls the name of the resulting executable. Is there a way to use this field within launch.json?

I want to propose a different approach. What if you define cargo run as a task in your tasks.json with a shortcut and let cargo handle the naming. That also has the advantage that, if students make a change ,cargo will compile it automatically when running cargo run.
This is how to do it:
Create a .vscode folder on project directory.
Create a tasks.json file in it.
In the tasks.json file, my content is like this:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "run",
"type": "shell",
"command": "cargo run",
"problemMatcher": []
}
]
}
Now when you run the command tasks: run task on the command palette, you will see a new task called run. Clicking it will run cargo run on the workspace folder.
The documentation can be found in https://code.visualstudio.com/docs/editor/tasks .
Sidenote: I checked the VsCode Rust Plugin and it seems that they already have that functionality, so just let your students install that extension and they can run all the cargo tasks without doing the above steps.

Related

How to open file using vscode tasks in remote vscode

I'd like to be able to use the new vscode tasks feature to open a file in remote vscode. This is useful because I have a bunch of symlinked log files in tmp that I would like to open, and I don't want to type their whole name every time. Ideally it would be something like this:
{
"label": "$(play) open log file",
"type": "shell",
"command": "code /tmp/logfile.INFO",
"group": "build",
},
If I run the same command in the normal vscode terminal, it opens the file correctly. If I run it in the task, the environment doesn't have vscode's bin in the path. If I change code in the task to use the direct path of the remove vscode, it then complains saying "Command is only available in WSL or inside a Visual Studio Code terminal."
I'm unclear if this fails because it is remote vscode or if it fails because it's not possible to open code in a task. Either way it'd be nice to do so :)

How to run Lua script from within VS Code

Have been using Notepad++ for awhile now, and adding scripts via Lua extensions. Now, I would like to get my feet wet using VS Code and was wondering what sort of extensibility I could leverage in that environment? Possible to run the same Lua scripts, for instance? Or are there other avenues I should consider? Thanks for any insights!
Please install the vs-code extensions "LuaDebug" and "extensionPath" provided by actboy168.
Add a file named .vscode/launch.json with the content:
{
"version": "0.2.0",
"configurations": [
{
"type": "lua",
"request": "launch",
"name": "Launch",
"program": "${workspaceFolder}/src/main.lua"
}
]
}
if your source to be executed is src/main.lua.
Then hit F5 or "Run"->"Start Debugging", select the lauch configuration and happy debugging.
An example is also shipped by the author of the plug-in (actboy128), which can be found here: https://github.com/actboy168/luamake
Please do not try to build it, simply load it in VS Code and debug it!
I'm using the following settings to run the interpreter from VSCode using the menu option "Terminal|Run Build Task":
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Run Lua",
"type": "shell",
"command": "lua54",
"args": ["${file}"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
The file contains this settings must be named as "tasks.json". I suggest you to read the VSCode in order to learn how to create it properly.
NOTE: in the command property, where you read "lua54", you must write the name of the lua interpreter executable without the extension.
You would have to configure a task file to run lua from VS code and install lua on your pc if it's isn't done yet.
In order to debug lua you would have to install the extension lua debug which will add a ton of useful feature to help you debug lua such as breakpoint.
Your question seems to touch on several topics:
How to run a Lua script from VS Code: other answers here have already addressed it.
How to extend VS Code: pull up the extension view in VS Code, find an extension you like, and click install. More info here:
https://code.visualstudio.com/docs/editor/extension-marketplace
How to author VS Code extensions: they are written in TypeScript or JavaScript rather than Lua. More info here:
https://code.visualstudio.com/api/get-started/your-first-extension
you can type the following in cmd and it will run the command. Instead of installing extensions.
"C:\Program Files\LOVE\love.exe" "D:\game"
--> this is the path where your main.lua file is

Is it possible to create a VS Code extension that runs a specific gulp task?

Since I'm a lazy programmer, I've built a gulp task that does some minor tasks for me but I'd like to turn this into an extension so I can install it on my computer and it just "works". Like I install it, boot up VS code and the tasks execute and start watching my code automatically.
I haven't found much things online but then again I could be googling the wrong things. Anything helps - thanks!
Have a look at run task on folderOpen option: vscode docs.
This works for me:
// in tasks.json
{
"label": "Tasks: copy3",
"type": "shell",
"command": "gulp",
"args": [
"copy3",
"--file",
"${fileBasename}"
],
"problemMatcher": [],
"runOptions": {
"runOn": "folderOpen"
}
},
Now the copy3 task in gulpfile.js will automatically be run when that folder (or workspace) is opened. You may have to select Tasks: Allow Automatic Tasks in Folder first in the command palette. And definitely reload.
No extension - it is built-in behaviour more recently. See run task on folder open.
Looks like Yuki Doi had you to it with "Blade Runner". This extension does exactly what you're looking for, it automatically runs build task when a folder is opened.
Related question

VisualStudio Code Tasks and Variables

Visual studio 2010 had an option when creating external tools to define an initial directory like $(ItemDir) which would be based on the file currently in focus. I am trying to do something similar in visual studio code but I cannot find an equivalent environment variable to do the same thing. Any suggestions would be much appreciated.
Just to formalize the info into an answer:
Here is a list of predefined variables that can be used in tasks: Variables reference
and here is more info on using variables in tasks : Variable substitution
And this is really interesting from the last link above:
Similarly, you can reference your project's configuration settings by
prefixing the name with ${config:.
For example,
${config:python.pythonPath} returns the Python extension setting
pythonPath.
Below is an example of a custom task configuration which
executes autopep8 on the current file using your project's selected
Python executable:
{
"label": "autopep8 current file",
"type": "process",
"command": "${config:python.pythonPath}",
"args": [
"-m",
"autopep8",
"-i",
"${file}"
]
}
From the first link we see that you can even incorporate commands (like those used in keybindings and the command palette) into your tasks:
Settings and command variables
You can reference VS Code settings and
commands using the following syntax:
${config:Name} - example: ${config:editor.fontSize}
${command:CommandID} - example: ${command:explorer.newFolder}
Finally, related but because some people might end up here searching for these, there are variables that can be used in snippets (but not tasks). See Snippet Variables

Can VSCode (Visual Studio Code) be used to run elm-make?

I am new to VSCode.
I am new to ELM.
I am perfectly capable of using VIM and command line tools to create an ELM Project, but I want to utilize an IDE. I have chosen VSCode on advice from the internet since it seems to pick up some nice pieces of VIM.
So now I have a few .elm files.
Main.elm
View.elm
Model.elm
I want to run elm-make on Model.elm to make sure it has no errors.
Then I want to run elm-make on Main.elm to create an index.html so I can view my project.
I think this is a pretty simple question for people familiar with how to customize VSCode, but as I stated previously, I am new to VSCode.
Try setting up a task for elm-make:
Create a ./vscode/tasks.json with the contents:
{
"version": "0.1.0",
"tasks": [
{
"taskName": "elm make",
"isBuildCommand": true,
"command": "elm-make",
"args": ["./main.elm"],
"isShellCommand": true
}
]
}
You can then use the build command to run the task, or run the task individually.
You may also want to look into the elm extension: https://marketplace.visualstudio.com/items?itemName=sbrink.elm