question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Expose API for variable substitution in contributed configuration

See original GitHub issue

So now we have a bunch of variable substitutions we can use in .vscode/tasks.json

${workspaceFolder} - the path of the folder opened in VS Code
${workspaceFolderBasename} - the name of the folder opened in VS Code without any slashes (/)
${file} - the current opened file
${relativeFile} - the current opened file relative to workspaceFolder
${fileBasename} - the current opened file's basename
${fileBasenameNoExtension} - the current opened file's basename with no file extension
${fileDirname} - the current opened file's dirname
${fileExtname} - the current opened file's extension
${cwd} - the task runner's current working directory on startup
${lineNumber} - the current selected line number in the active file
${env:Name} - the `Name` variable from the environment
${config:Name} - example: ${config:editor.fontSize}
${command:CommandID} - example: ${command:explorer.newFolder}

But what if an extension wants to give the user the ability to use substitution for the configuration points it contributes, the extension developer has to implement that all themselves. While it is totally possible to support all of these substitutions, even inlined commands, from an extension the developer has to reinvent the wheel and maintain compatibility with future/past versions of VSCode.

It would be great if there was a way for an extension to make use of the existing variable substitution logic. Unfortunately, this is pretty deeply embedded into vscode and not something made readily accessible to extension developers.

I’m sure that there will be demand for custom substitutions and for limiting what kind of substitutions are allowed.

Really rough, but this is how something like this could look.

./package.json

"contributes": {
  "substitutions": {
    "prompt-debug.colorOfSky": {
      "description": "..."
    }
    "prompt-debug.binaryPath": {
      "description": "..."
    }
  },
  "configuration": {
    "properties": {
      "prompt-debug.autoResolveScript": {
        "description": "A javascript or typescript file which is exports a function named 'autoResolve' that resolves a file path to run.",
        "title": "Auto Resolve Script",
        "type": "string",
        "required": false,
		"substitutions": false, // substitutions disabled (default)
        "substitutions": "*", // allow all registered substitutions
		"substitutions": [
		  "*", // allow all registered substitutions
		  "-command:*" // exclude commands from substitutions
		  "command:explorer.newFolder" // with the exception of the newFolder command
		],
        "substitutions": [
    	  "substitution:prompt-debug.*" // allow all of the substitutions prompt-debug contributes
		  "-substitution:prompt-debug.binaryPath" // with the exception of binaryPath 
		]
	}
}

./src/extension.ts

export function activate(context: vscode.ExtensionContext) {
  vscode.substitutions.registerSubstitution('prompt-debug.colorOfSky', () => (Math.random() > 0.5) ? 'blue' : 'grey');
  vscode.substitutions.registerSubstitution('prompt-debug.binaryPath', async () => {
    const binaryPath = await someAsycStuff();
	if (binaryPath) return binaryPath;
    else return '/usr/bin/derp';
  });
}

Obviously vscode.workspace.getConfiguration().get('prompt-debug.autoResolveScript'); would have to be async because substitutions with commands can take a while to evaluate… I have a few good ideas, but I’m tired of typing so we can bikeshed later if anyone is actually interested in this… lol

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:61
  • Comments:14 (3 by maintainers)

github_iconTop GitHub Comments

7reactions
DominicVonkcommented, Jan 19, 2021

Hi, I created a package to use predefined variables within your settings:

https://www.npmjs.com/package/vscode-variables

I will add other options later

2reactions
rozzzlycommented, Mar 24, 2018

Sorry bot, not quite.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Contribution Points | Visual Studio Code Extension API
Contribute configuration keys that will be exposed to the user. ... variables introduces substitution variables and binds them to commands implemented by ...
Read more >
How to use variable substitution in Frontend js applications ...
The main idea is put all your configurations, settings or properties in one site and all your applications must retrieve this values in...
Read more >
Create or Update All Substitution Variables Defined for the ...
Can be used to create or update substitution variables for the application. Variables in the payload that exit in the application at the...
Read more >
Where variables can be used - GitLab Documentation
GitLab Runner internal variable expansion mechanism · Supported: project/group variables, .gitlab-ci.yml variables, config.toml variables, and variables from ...
Read more >
Substituting variable values | Cloud Build Documentation
Use substitutions in your build config file to substitute specific variables at build time. Substitutions are helpful for variables whose value isn't known ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found