Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help projectionist find .projections.json in new directories #105

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tandrewnichols
Copy link

I have an auto-mkdir thing for writing files in directories that don't exist, and projection templates are not applied to files in non-existent directories (and actually, vim-projectionist just fails to find .projections.json in this case). They are if I mkdir first though.

Example:

Projection

{
  "foo/*.js": {
    "template": [
      "blah"
    ]
  }
}

If I do vim foo/bar.js the template is used; if I do mkdir foo/bar/ && vim foo/bar/baz.js the template is used. But if I do vim foo/bar/baz.js by itself I don't get the template.

Here's why it matters: angular and react and some other frameworks advocate a directory-per-component structure, so any new component is in a new directory. Your structure ends up looking like:

components/
  foo/
    foo.js
    foo.html
    foo.test.js

so every time you create a new component you're creating a new directory. Which means I either never get the benefit of auto-mkdir (and have to type mkdir every time . . . I know, I know, first world problems) or I never get the benefit of a projection template.

This turned out to be because expand('<afile>:p') doesn't result in an absolute path in non-existent directories. This change fixes it by detecting non-absolute paths and prefixing them with cwd.

Thoughts?

@tpope
Copy link
Owner

tpope commented May 14, 2018

Off the top of my head, this will have unintended consequences for unnamed buffers, URLs (ranging from http:// to fugitive://), and all things Windows.

My larger thoughts are that auto-mkdir just can't be done well in practice. If you do it on BufNewFile, every typo creates a new directory. If you do it on BufWritePre, the buffer is in many ways broken before the first save. Doing :e foo/bar|cd /tmp|w will create /tmp/foo/bar, almost certainly not what you intended.

My preferred solution to this is to have a command or command variant for creating the directory and file in one go. For example, in rails.vim, you can do :Emodel foo/bar! and mkdir app/models/foo will happen automatically if necessary. I haven't added something like that to Projectionist yet because the ! syntax is weird, and I haven't needed it enough to come up with an alternative. I'm thinking something like ++mkdir, which matches Vim's ++opt syntax, although it's a bit verbose.

@tpope
Copy link
Owner

tpope commented May 14, 2018

@tandrewnichols
Copy link
Author

Somehow that link isn't going to the right thing for me. When I follow it, the version of autoload/projectionist.vim doesn't even have 688 lines. That aside, yeah, I figured there would be cases where my solution wasn't sufficient. Just wanted to get the conversation started. So you're saying this is already supported via :E<type> thingname? (like in my case, something like :Ecomponent foo/foo.js?)

@tandrewnichols
Copy link
Author

@tpope
Copy link
Owner

tpope commented May 15, 2018

Yes that.

I have an auto-mkdir thing for writing files in directories that don't exist, and projection templates are not applied to files in non-existent directories (and actually, vim-projectionist just fails to find .projections.json in this case). They are if I `mkdir` first though.

Example:

**Projection**

```
{
  "foo/*.js": {
    "template": [
      "blah"
    ]
  }
}
```

If I do `vim foo/bar.js` the template is used; if I do `mkdir foo/bar/ && vim foo/bar/baz.js` the template is used. But if I do `vim foo/bar/baz.js` by itself I don't get the template.

Here's why it matters: angular and react and some other frameworks advocate a directory-per-component structure, so any new component is in a new directory. Your structure ends up looking like:

```
components/
  foo/
    foo.js
    foo.html
    foo.test.js
```

so every time you create a new component you're creating a new directory. Which means I either _never_ get the benefit of auto-mkdir (and have to type `mkdir` every time . . . I know, I know, first world problems) or I _never_ get the benefit of a projection template.

This turned out to be because `expand('<afile>:p')` doesn't result in an absolute path in non-existent directories. This change fixes it by detecting non-absolute paths and prefixing them with cwd.

Thoughts?
matthewd added a commit to matthewd/vim-projectionist that referenced this pull request Apr 12, 2020
Help projectionist find .projections.json in new directories

* github.com:tpope/vim-projectionist:
  Help projectionist find .projections.json in new directories
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants