You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add relative directory/filepath support to snippets #37570
Currently, snippets allow you to resolve the current directory or filepath with TM_DIRECTORY and TM_FILEPATH respectively. It would be great to be able to get the path for these relative to the root folder, whether directly (e.g. TM_DIRECTORY_REL) or by providing a variable with the root folder.
I'm trying to write a snippet to create a new C# file. Visual Studio will auto-populate the file with namespace Directory.Structure.Here, but that doesn't seem possible in Visual Studio Code right now.
Reacted by Ash Clarke, m1ke-van-dyke, Spielberg, neminovno, Jesse, Will 保哥, srx4444, Greg McGrath, Will Madden, snoosmoomrik and 21 more
I'm not sure how that would help in this case. If TM_DIRECTORY resolves to C:\dev\projects\my_projects\secret_projects\secret_project1\features\secret_feature, how can I know where the project begins? If I have the project root at secret_project1, I would expect the namespace to be secret_project1.features.secret_feature, but I don't see any way of reliably generating that without making assumptions about base folder structure.
Reacted by still-dreaming-1, Michael Johnston and Peter Hedberg
I could also see this used for other situations, like generating a JavaScript file that imports a common file, like const myCommonLib = require('../../../common.js');. In this case, if I had the relative directory, I could easily use the Variable Transformations to generate ../../../
Well, how would we know? Do you suggest to take the current folder? What if your project start one level deeper, like src and test? I like the idea of having a variable that resolves the active (workspace) folder but I don't know if it will help you. Tho, it might make it easier to craft a snippet with transforms
Reacted by still-dreaming-1, Michael Johnston, Sam Cassar and Peter Hedberg
Spielberg (@Spielberg), if you hardcode root foder, you can use the next transformation: "const myCommonLib = require('${TM_DIRECTORY/.*src(\\\\[^\\\\]*)(\\\\[^\\\\]*)?(\\\\[^\\\\]*)?(\\\\[^\\\\]*)?/${1:+../}${2:+../}${3:+../}${4:+../}/}common.js');",
Johannes Rieken (@jrieken) "variable that resolves the active (workspace) folder" is definitely missing. How about 'custom' variable that could read string from another file? Usually, if source root is not the relative folder, it is written in some (json, xml..) file in relative folder. Beside resolving this issue, probably that new variable would provide many more options in snippets.
Activity
jrieken commentedon Nov 3, 2017
We have added Variable Transformations for thing like this. Please give it a try
mdickin commentedon Nov 3, 2017
I'm not sure how that would help in this case. If
TM_DIRECTORYresolves toC:\dev\projects\my_projects\secret_projects\secret_project1\features\secret_feature, how can I know where the project begins? If I have the project root atsecret_project1, I would expect the namespace to besecret_project1.features.secret_feature, but I don't see any way of reliably generating that without making assumptions about base folder structure.mdickin commentedon Nov 3, 2017
I could also see this used for other situations, like generating a JavaScript file that imports a common file, like
const myCommonLib = require('../../../common.js');. In this case, if I had the relative directory, I could easily use the Variable Transformations to generate../../../jrieken commentedon Nov 3, 2017
Well, how would we know? Do you suggest to take the current folder? What if your project start one level deeper, like
srcandtest? I like the idea of having a variable that resolves the active (workspace) folder but I don't know if it will help you. Tho, it might make it easier to craft a snippet with transformsmdickin commentedon Nov 4, 2017
Sorry, when I said project root, I meant workspace root.
You're right, it wouldn't solve every situation, but I think it could be handy to have.
Spielberg commentedon Feb 16, 2018
Any idea how to make this works using variable transformations?
DVDima commentedon Feb 20, 2018
Spielberg (@Spielberg), if you hardcode root foder, you can use the next transformation:
"const myCommonLib = require('${TM_DIRECTORY/.*src(\\\\[^\\\\]*)(\\\\[^\\\\]*)?(\\\\[^\\\\]*)?(\\\\[^\\\\]*)?/${1:+../}${2:+../}${3:+../}${4:+../}/}common.js');",neminovno commentedon Apr 16, 2018
Johannes Rieken (@jrieken) "variable that resolves the active (workspace) folder" is definitely missing. How about 'custom' variable that could read string from another file? Usually, if source root is not the relative folder, it is written in some (json, xml..) file in relative folder. Beside resolving this issue, probably that new variable would provide many more options in snippets.
18 remaining items
Merge pull request #114208 from gjsjohnmurray/fix-37570
fix #37570 add RELATIVE_FILEPATH snippet variable
aeschli commentedon Jan 26, 2021
Verified that the RELATIVE_FILEPATH works