Skip to content

Add relative directory/filepath support to snippets #37570

Closed

Description

  • VSCode Version: Code 1.17.1 (1e9d365, 2017-10-10T14:24:41.632Z)
  • OS Version: Windows_NT ia32 6.3.9600

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.

Activity

  1. jrieken commented on Nov 3, 2017

    @jrieken
    Contributor

    We have added Variable Transformations for thing like this. Please give it a try

  2. added
    under-discussion
    on Nov 3, 2017
  3. mdickin commented on Nov 3, 2017

    @mdickin
    Author

    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.

  4. mdickin commented on Nov 3, 2017

    @mdickin
    Author

    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 ../../../

  5. jrieken commented on Nov 3, 2017

    @jrieken
    Contributor

    how can I know where the project begins?

    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

  6. mdickin commented on Nov 4, 2017

    @mdickin
    Author

    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.

  7. Spielberg commented on Feb 16, 2018

    @Spielberg

    Any idea how to make this works using variable transformations?

  8. DVDima commented on Feb 20, 2018

    @DVDima

    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');",

  9. neminovno commented on Apr 16, 2018

    @neminovno

    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.

  10. 18 remaining items

  11. added a commit that references this issue on Jan 14, 2021
    98038a8
  12. added
    unreleased
    on Jan 14, 2021
  13. added
    verification-needed
    and removed
    unreleased
    on Jan 14, 2021
  14. added a commit that references this issue on Jan 15, 2021
    b452699
  15. added
    verified
    and removed
    verification-needed
    on Jan 26, 2021
  16. aeschli commented on Jan 26, 2021

    @aeschli
    Contributor

    Verified that the RELATIVE_FILEPATH works

  17. added
    on-release-notes
    on Jan 28, 2021
  18. locked and limited conversation to collaborators on Feb 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

feature-requesthelp wantedon-release-notessnippetsverified

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions