use variable in multi-root workspace folders - visual-studio-code

I'm using VSCode multiroot workspaces with remote ssh and git worktrees instead of git branches.
So when working on different branches, the folder containing the code may change and I need to change working folder.
For example, I need frequently to switch between folder /my/path/to/worktree1 to /my/path/to/worktree2 and so on ...
Then everytime I want to switch from worktree1 to worktree2, I manually edit the workspace file and comment/uncomment some lines
{
"folders": [
{
"name": "worktree1",
"path": "/my/path/to/worktree1"
// "name": "worktree2",
// "path": "/my/path/to/worktree2",
},
{
"name": "/other/directory",
"path": "/other/directory"
},
],
...
}
I was wondering if there was a way to define a variable and use it in the folders section, for example my_current_branch:worktree1 and then use it as
{
"folders": [
{
"name": "$my_current_branch",
"path": "/my/path/to/$my_current_branch"
},
...
],
...
}
I insist on the fact that I want to use it in the folders section and not in others sections where I know it is possible.

Related

VS Code: Add single file (README) to multi-root workspace

You can configure a vs-code multi-root workspace to include several folders. Eg,
{
"folders": [
{
// Source code
"name": "Product",
"path": "vscode"
},
{
// Docs and release notes
"name": "Documentation",
"path": "vscode-docs"
},
{
// Yeoman extension generator
"name": "Extension generator",
"path": "vscode-generator-code"
}
]
}
I would also like to include a README file from a root directory that pertains to all those folders. Is there a way to do this?

Is it possible to specify user specific pre commands in VS Code?

We have a slew of folks doing development through the same GitLab repo. We are using VS Code tasks to execute internal commands. The main command is the same for everyone: internal_command on Windows and internalCommand on Linux.
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label" : "do it",
"type" : "shell",
"windows": {
"command": "internal_command"
},
"linux": {
"command": "internalCommand"
}
}
]
}
This works as expected.
Some users need/want to run a specific command before the main command. For example, one use wants to rename a file, another user wants to change an environment variable, etc...
We don't want to have multiple versions of .vscode/tasks.json cause that is a mess when pushing things to GitLab.
So I am wondering if there is a way to specify user specific tasks in the project's .vscode/tasks.json file?
You can with the help of the extension Command Variable it allows you to use the content of a file as a command in the terminal. The file can also contain Key-Value pairs or be a JSON file.
Say you store this userTask.txt or userTask.json file in the .vscode folder and add the file to the .gitignore file.
With the current version of the extension the file userTask.txt has to exist, I will add an option to supply alternative text in case the file does not exist. You can fill the file with a dummy command like echo No User Task
Set up your task.json like
{
"version": "2.0.0",
"tasks": [
{
"label" : "do it",
"type" : "shell",
"windows": {
"command": "internal_command"
},
"linux": {
"command": "internalCommand"
},
"dependsOrder": "sequence",
"dependsOn": ["userTask"]
},
{
"label" : "userTask",
"type" : "shell",
"command": "${input:getUserTask}"
}
],
"inputs": [
{
"id": "getUserTask",
"type": "command",
"command": "extension.commandvariable.file.content",
"args": {
"fileName": "${workspaceFolder}/.vscode/userTask.txt"
}
}
]
}

How to add configurations to user to a VSCode extension

I am trying to add a simple configuration to my VSCode extension, but I can't find the correct information.
I just want that the user have a string input field, that has a default value, and read it from my typescript code through the vscode library
Can someone provide me and example about which file should I add, where, and how to consume? also if more configurations are needed.
Thanks!
Ok, found
I need to add into my package.json
...
"contributes": {
"commands": [
...
],
"configuration": {
"title": "My Extension",
"properties": {
"myExtension.myProperty": {
"type": "string",
"default": "default",
"description": "description"
}
}
}
},
and consume
vscode.workspace.getConfiguration('myExtension').myProperty;

Writing a vscode plugin for adding snippets but only for file composer.json

Did you know if it's possible to write a vscode plugin to match only a given file?
My plugin already works but add snippets for all .json files. I wish to be able to add these snippets only if the current file is composer.json.
My current package.json (partial) is:
{
"contributes": {
"snippets": [
{
"language": "json",
"path": "./snippets/snippets.code-snippets"
}
]
}
}
The idea is to be able to having something like:
{
"contributes": {
"snippets": [
{
"filename": "composer.json", <-- JUST FOR ILLUSTRATION PURPOSE
"path": "./snippets/snippets.code-snippets"
}
]
}
}
Thanks.

VSCode Running a Script Using Macros

tl;dr:
I wanna run a script through a macro using "macros" extension for VSCode, see the extension here
Is it possible? if yes, then how?
long story:
There is this extension called "powertools" (see it here), and I used it to add a custom button that runs a script when I click it.
I wanna add a functionality to this button, such that each time I click the button, it saves all of my files using this command ID "workbench.action.files.saveAll", and then shall it run the script.
defining the button goes like this:
"ego.power-tools": {
"buttons":
[
{
"text": "Compile Folder",
"tooltip": "Compile all the .Jack files in the current folder.",
"action":
{
"type": "command",
"command": "macros.compile_button_click",
}
}
],
}
And I want the macro to look something like this:
"macros": {
"compile_button_click": [
"workbench.action.files.saveAll",
{
"action":
{
"type": "script",
"script": "compile_folder.js"
}
},
]
}
Thanks in advance!
P.S - It is important that the macro will first save the files, and only then shall it execute the script
THE SOLUTION:
as #rioV8 said, using powertools you can define a command to execute a script, and then you can add the newly defined command to the macros.
THIS IS HOW I DID IT:
"ego.power-tools": {
"buttons":
[
{
"text": "Save & Compile",
"tooltip": "Compile all the .Jack files in the current folder.",
// Running the macro on button click
"action":
{
"type": "command",
"command": "macros.compile_button_click"
}
}
],
"commands": {
// Define a command that runs a script
"compile_folder": {
"script": "compile_folder.js"
}
}
},
"macros": {
// Define a macro command
"compile_button_click": [
"workbench.action.files.saveAll",
"compile_folder"
]
}