2

I asked a similar question to this a couple weeks ago, but I think because it was so specific and lengthy, I did not get an answer. See original post. I'll try and ask a more direct question this time--

I'm currently writing a VS Code extension. Is there a function built into the VS Code API that when given a string as input, allows for evaluation of environment variables, like those used in the tasks.json file? Full list of variables can be found here: https://code.visualstudio.com/docs/editor/variables-reference.

Such a function would take in a string containing environment variables and information about the workspace, then evaluate the environment variables in it. Example: "${workspaceFolder}_${fileBasenameNoExt}.txt""myWokspaceFolderName_myFileName.txt

I know VS Code has the functionality built in (as it is used in tasks) but I don't know how they do it and/or if I am able to use that functionality without having to rebuild it for my extension. What's the best way to implement this functionality in my extension?

Antyos
  • 455
  • 5
  • 11
  • I'm looking for the same answer. – ATL_DEV Jun 05 '22 at 14:50
  • @ATL_DEV I ended up implementing my own variable resolver. See: https://github.com/Antyos/vscode-openscad/blob/43e709478ff68f3b02c878a458cc2264fa2e1630/src/variableResolver.ts – Antyos Jun 06 '22 at 18:21
  • I added an answer. I hope I understood your question as it seemed identical to mine. I think your code might be better though. – ATL_DEV Jun 13 '22 at 19:15

1 Answers1

0

One easy way to check a variable's runtime value is to create a VS Code task to output the variable value to the console. For example, to see the resolved value for ${workspaceFolder}, you can create and run (Terminal > Run Task) the following simple 'echo' task in tasks.json:

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "echo",
      "type": "shell",
      "command": "echo ${workspaceFolder}"
    }
  ]
}
ATL_DEV
  • 9,256
  • 11
  • 60
  • 102
  • My question was not about seeing the output of the variables but about being able to use the variable resolver used by `tasks.json` in other parts of a VS Code extension. My use case was I had a config in one of my extensions that defines the file name format when exporting files and I wanted the user to be able to use the same variables as `tasks.json`. At the time of asking the question, VS Code did not (and still may not) reveal that part of its API, so I think I just copied from its source code. – Antyos Jun 15 '22 at 16:54
  • OK. So you want to resolve them programmatically. I believe VSCode is open source, so you might be able to find the API function responsible for resolving variable. – ATL_DEV Jun 15 '22 at 17:00
  • That's precisely what I did! (I forgot to add an answer to my own question). Take a look at: https://github.com/Antyos/vscode-openscad/blob/43e709478ff68f3b02c878a458cc2264fa2e1630/src/variableResolver.ts – Antyos Jun 15 '22 at 17:17
  • Is there a reason you couldn't import the resolver service and use it? – ATL_DEV Jun 15 '22 at 17:20
  • I can't remember my specific reasoning from 2 years ago, but if I recall correctly, at the time it did not seem that VS Code exposed the variable resolver in their extension API, and I was unable to find a way to import it. – Antyos Jun 15 '22 at 19:37