Related
i am a newcomer to this site and i'm not english so excuse me if this post isn't as good as you expected. Let's get to the point: to give you an example of what i mean with "run button", take the python one for example.
Once you install python extension in visual studio code, you can press a button in the right top of the screen, and then vsc automatically executes a command in a shell running python on the active .py file.
Given this example, I'd like to know how to make a button for vsc that automatically creates a new terminal, executes a command which i would insert in the making of the button and nothing else, of course the terminal shall not disappear after giving the desired output.
Just to give you some more info, i'm on ubuntu, if that helps anyhow.
Feel free to tell me whether i should edit this question in anyway.
Install the Task Runner extension. Now, every task you add to tasks.json will be displayed as a button, in the 'Task runner' panel (which is normally on the same tab as the file explorer).
Here's a decent tasks.json template:
{
"version": "2.0.0",
"tasks": [
{
"label": "COMMAND NAME",
"type": "shell",
"command": "YOUR COMMAND HERE",
"presentation": {"echo": true, "reveal": "always", "focus": false, "panel": "shared", "showReuseMessage": false, "clear": true},
},
// More tasks here, if you need them.
]
}
Put this file in your project directory, in a directory called .vscode. Open the project directory with "File" -> "Open Folder".
I know you did not ask for debugging, but you can use the debugger launch mechanism on arbitrary programs. You can define launch.json to implement any number of launch configurations. The currently selected configuration will display a button you can click on.
https://code.visualstudio.com/docs/editor/debugging
Install Code Runner extension in VS Code
you can add the Code Runner extension.
This will show a run button on top of the editor panel
Add Code Runner Extension and you will see a RUN BUTTON on the Top Right in VS code. Like this
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
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
As the title said, can I populate automatically c_cpp_properties.json from an existing Makefile?
Edit:
For others trying to import makefiles, I have found a set of scripts that perform exactly what I wanted to achieve, the management of STM32 embedded projects through VS Code. If you dig into the scripts folder you can find the make parser and the VSCode project files populator.
Here the repo, enjoy!
This answer may not entirely give you what you want, but hopefully it helps you setting up your VS Code environment.
In your question title, you mention "Makefile projects", which is an indication that you have the wrong impression that (GNU) Makefiles capture project settings in a way similar to Visual Studio project files do. Makefiles do not work like that and the short answer to your question is No, there does not seem to be a way to "import" Makefiles into VS Code and have it automatically set values in your c_cpp_properties.json file.
Having said that, there are some other mechanisms that may help you getting started with VS Code in your situation, depending on the other elements you have in your toolchain.
VS Code works with the premise that it opens one or more directories in your file system. When you do so, it will automatically scan all its subdirectories, display the tree and do linting (if enabled) to detect problems. Indeed, include directories need to be added manually to your c_cpp_properties.json file but to add all your subdirectories recursively to the list of include directories, you can use the expression
"${workspaceRoot}/**"
as one of the elements in your includePath setting. See the blog post Visual Studio Code C/C++ extension May 2018 Update – IntelliSense configuration just got so much easier! for more details. That often resolves a lot of the missing symbols. If you are using external or 3rd party libraries as well, then you have to add those to your includePath setting manually, there is no way around that.
Additionally, you can create VS Code Tasks to execute your make command to your liking. This is explained here: Integrate with External Tools via Tasks. The Microsoft C/C++ Extension comes with a set of so-called problemMatchers that parse the output of your command and can interpret that for certain known compilers. You will see hyperlinks for errors and warning to navigate directly to the associated location in your code.
An example of such a Task may look like this:
{
"label": "Build All Debug",
"type": "shell",
"command": "make -f path/to/Makefile DEBUG=1",
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"reveal": "always",
"panel": "new"
},
"problemMatcher": [
"$gcc"
]
}
Visual Studio Code's Intellisense extension for C/C++ supports a compile_commands.json database. Some makefile utilities can generate these databases by themselves, but I don't have any experience using them to do so. With GNU make, you'll need some kind of helper script or tool. I used the python package compiledb:
sudo pip install compiledb
cd projectfolder
make clean
compiledb make
After running that, you should end up with a compile_commands.json file. Then, in VS Code, you need to edit c_cpp_properties.json for the workspace in question. One way to open it is by clicking the configuration name in the lower right corner of the status bar then clicking "Edit Configurations (json)". Or, you can open the command palatte (CTRL + SHIFT + P on my system), and entering C/C++: Edit configurations (JSON)
Then, in configuration you are using, add the property "compileCommands": "${workspaceFolder}/compile_commands.json"
Full example:
{
"configurations": [
{
"name": "geany",
"includePath": [
"${workspaceFolder}/**"
],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64",
"browse": {
"path": [
"${workspaceFolder}/**"
],
"limitSymbolsToIncludedHeaders": true
},
"compileCommands": "${workspaceFolder}/compile_commands.json"
}
],
"version": 4
}
Now, Visual Studio code's Intellisense analysis should be using the exactly correct include directories, define directives, and other command line compiler flags unique to each individual source file.
For others trying to import makefiles, I have found a set of scripts that perform exactly what I wanted to achieve, the management of STM32 embedded projects through VS Code. If you dig into the scripts folder you can find the make parser and the VSCode project files populator.
Here the repo, enjoy!
GNU Make is commonly used to compile C projects, but it can produce any sort of output files you want.
You can create a script for this purpose, or you can do it from another makefile, eg: VSCode.mk that you include in your top level Makefile.
This solution is using One Shell but if that is not viable a separate script would look nicer.
The # makes your output nicer.
Most likely you can't safely copy paste this code because Make uses literal tabs, not whitespace.
.ONESHELL:
SOURCE_DIRECTORY := src
DEFINED_VALUE := 1
# Recipe:
.vscode/c_cpp_properties.json:
#
cat << EOF > "$#"
{
"configurations": [
{
"name": "Linux",
"includePath": [
"\$${workspaceFolder}/$(SOURCE_DIRECTORY)*"
],
"defines": [
"DEFINED_VALUE=$(DEFINED_VALUE)"
],
"compilerPath": "$(CC)",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64",
"browse" : {
"limitSymbolsToIncludedHeaders" : true
}
}
],
"version": 4
}
EOF
With Visual Studio Code (not the traditional Visual Studio IDEs), can you run PowerShell in the Command Palette? I ask because I commonly use it in the full IDE.
I have not seen PowerShell mentioned in the documentation, other than for basic syntax highlighting. I have tried it with no success. Is it unsupported, or is it an optional feature I can configure somehow?
Note: to those voting up the PowerGUI answer, it is not correct as it references the wrong edition of Visual Studio for this question. It is helpful if you are using the full IDE, but not the new editor named: Code.
This was the first problem I wanted to solve in VSCode. I did not find a way to
type PowerShell commands but I have found a way to create and run PowerShell tasks.
In the command palette type and choose Tasks: Configure Task Runner. It will
show you a json file configured for the default task runner.
I replaced the content with this
{
"version": "0.1.0",
"command": "PowerShell.exe",
"isShellCommand": true,
"args": [
"-NoProfile",
"Invoke-Build.ps1"
],
"tasks": [
{
"taskName": "Build",
"isBuildCommand": true,
"showOutput": "always"
},
{
"taskName": "Test",
"isTestCommand": true,
"showOutput": "always"
},
{
"taskName": "Build, Test",
"showOutput": "always"
}
]
}
As a result in the command palette I can choose and run my predefined tasks (Build, Test, and combined Build, Test). I can add other tasks and probably bind them to some hotkeys. This is not exactly what I would like to have in VSCode for PowerShell but for the preview it is at least something.
P.S. This just my first experiment that somewhat worked. It is not perfect, more likely. There are many configuration parameters for this json file that I have not tried yet.
With version 0.10.1 of Visual Studio Code, you can run and debug PowerShell. Here is a sample launch.json that I use:
{
"version": "0.2.0",
"configurations": [
{
"name": "PowerShell",
"type": "PowerShell",
"program": "MyScript.ps1",
"args": "-Verbose"
}
]
}
Unfortunately, I cannot get the args to work (for more details, see: https://github.com/PowerShell/vscode-powershell/issues/24). Another problem I have is that Read-Host does not work with VS Code (for more details, see: https://github.com/PowerShell/vscode-powershell/issues/29). Definitely some rough edges.
Here's how you can configure the Powershell task to execute the currently opened .ps1 file without any Invoke-Build dependency:
{
"version": "0.1.0",
"command": "PowerShell.exe",
"isShellCommand": true,
"args": [
"${file}"
],
"tasks": [
{
"taskName": "Build",
"isBuildCommand": true,
"showOutput": "always"
},
{
"taskName": "Test",
"isTestCommand": true,
"showOutput": "always"
},
{
"taskName": "Build, Test",
"showOutput": "always"
}
]
}
Note: This is just a slight modification of Roman's answer (My edit to his answer was rejected).
Open the Debug view, in the View Bar select Debug from the View menu or press Ctrl + Shift + D.
In the Launch Configuration dropdown (shown in the following screenshot), select Add Configuration...
The launch.json configuration will open in the editor, type PowerShell and select the debug configurations you want, as shown in the following screenshot.
Save the launch.json file and select the debug configuration you want from the Launch Configuration dropdown:
Now you can debug PowerShell scripts through VSCode.
The Scripting Guys wrote a comprehensive 2 part blog post on this topic, like everything else they write, it's well worth a read if you're new to VSCode and PowerShell.
https://blogs.technet.microsoft.com/heyscriptingguy/2017/02/06/debugging-powershell-script-in-visual-studio-code-part-1/
https://blogs.technet.microsoft.com/heyscriptingguy/2017/02/13/debugging-powershell-script-in-visual-studio-code-part-2/
EDIT: I am aware that this question is several years old, but I came across it before I found any up to date information on how to set up PowerShell debugging in VSCode
As of version 0.10.3 there is now a PowerShell extension written by Microsoft that will enable writing PowerShell with IntelliSense and highlighting in Visual Studio Code
http://blogs.msdn.com/b/powershell/archive/2015/11/17/announcing-windows-powershell-for-visual-studio-code-and-more.aspx
At least in V1.4 (July 2016) this has been made a lot simpler.
The relevant documentation can be found here:
https://code.visualstudio.com/docs/editor/integrated-terminal
Essentially all you're doing is adding an entry to the settings.json file for User Settings.
Adding the following value:
// 64-bit PowerShell if available, otherwise 32-bit
"terminal.integrated.shell.windows":"C:\\Windows\\sysnative\\WindowsPowerShell\\v1.0\\powershell.exe"
IMG: Shows the User Settings settings.json file and the successful Integrated Console showing the PowerShell prompt:
An "Integrated Terminal" is now integrated into Visual Studio Code. This is a PowerShell Terminal in Windows. (Likely BASH in Linux / MacOS). The terminal appears in a panel at the bottom of the code editor, not in the Command Pallete.
Keyboard Shortcut to open Terminal: CTRL + `
Sources:
https://code.visualstudio.com/shortcuts/keyboard-shortcuts-windows.pdf
https://code.visualstudio.com/docs/getstarted/keybindings
I confirmed in version 1.19.2. Not sure when the feature was first integrated.
There is a much easier way to run PowerShell, no configuration needed:
Install the Code Runner Extension
Open the PowerShell code file in Text Editor, then use shortcut Ctrl+Alt+N, or press F1 and then select/type Run Code, or right click the Text Editor and then click Run Code in context menu, the code will run and the output will be shown in the Output Window.
Besides, you could select part of the PowerShell code and run the code snippet. Very convenient!
PowerGUI tool provides a nice interface to Visual Studio. The goal of this extension is to bring PowerShell development into Visual Studio.
Here is how it looks -
Along with IntelliSense, the PowerGUI Visual Studio Extension provides the following features and more to make it easier to work with PowerShell.
PowerShell file and project types: You can create/edit PowerShell code files and assemble them into projects with more than one file.
PowerShell code snippets: The code snippet feature can be used for PowerShell code.
PowerShell console window: This feature provides the PowerShell console environment within Visual Studio IDE. This allows you to run commands or view output of scripts. Figure B shows the console window opened in the IDE.
PowerShell debugging: This feature is why I installed the extension; it provides a way to debug scripts within Visual Studio. It's a straight-forward way to locate syntax or logical problems in a script.
Syntax highlighting and script analysis: These are more Visual Studio features made available to PowerShell development.
In order to install the PowerGUI Visual Studio Extension, PowerGUI must be installed, with the caveat that the correct version of each product is installed.
Download Here
It also provides Debugging facility which is the best part I like as a developer.
Thanks!