From f672525897be41a38e31475108fcc5c0b904d14f Mon Sep 17 00:00:00 2001 From: Andrew Nichols Date: Mon, 14 May 2018 16:11:35 -0400 Subject: [PATCH] Help projectionist find .projections.json in new directories 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(':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? --- plugin/projectionist.vim | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/plugin/projectionist.vim b/plugin/projectionist.vim index 290cee4..a10e46b 100644 --- a/plugin/projectionist.vim +++ b/plugin/projectionist.vim @@ -69,13 +69,14 @@ function! s:has(ns, root, requirements) abort endfunction function! ProjectionistDetect(path) abort + let path = a:path + if path !~ '^/' + let path = getcwd() . '/' . path + endif + let b:projectionist = {} unlet! b:projectionist_file - if a:path =~# '^\a[[:alnum:].+-]\+:' - let file = substitute(a:path, '[\/]$', '', '') - else - let file = simplify(fnamemodify(resolve(a:path), ':p:s?[\/]$??')) - endif + let file = simplify(fnamemodify(path, ':p:s?[\/]$??')) let root = file let ns = matchstr(file, '^\a\a\+\ze:')