Skip to content

Commit bc19ddf

Browse files
committed
Prefer project root containing vendor directory
Previously when setting b:composer_root, the closest composer.json would be used. Now, the closest composer.json with sibling vendor/autoload.php is used. If none is found, falls back to the closest composer.json. The rationale is that it is not useful to scope :Composer and the navigation commands (:Ecomposer et al.) to an installed dependency, and <Plug>(composer-find) was broken when jumping from a dependency. Fixes #5.
1 parent 7fa2800 commit bc19ddf

File tree

11 files changed

+48
-4
lines changed

11 files changed

+48
-4
lines changed

plugin/composer.vim

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ let g:loaded_composer = 1
6262
" Detection {{{
6363

6464
""
65-
" Determine whether the current or supplied [path] belongs to a Composer project
65+
" Determine whether the current or supplied [path] belongs to a Composer
66+
" project, and set b:composer_root to the path of the project root.
6667
function! s:composer_detect(...) abort
6768
if exists('b:composer_root')
6869
return 1
@@ -74,10 +75,19 @@ function! s:composer_detect(...) abort
7475
let fn = fnamemodify(fn, ':h')
7576
endif
7677

77-
let composer_json = findfile('composer.json', escape(fn, ', ') . ';')
78+
let candidates = findfile('composer.json', escape(fn, ', ') . ';', -1)
7879

79-
if !empty(composer_json)
80-
let b:composer_root = fnamemodify(composer_json, ':p:h')
80+
for json in candidates
81+
let root = fnamemodify(json, ':p:h')
82+
83+
if filereadable(root.'/vendor/autoload.php')
84+
let b:composer_root = root
85+
return 1
86+
endif
87+
endfor
88+
89+
if !empty(candidates)
90+
let b:composer_root = fnamemodify(candidates[0], ':p:h')
8191
return 1
8292
endif
8393
endfunction

t/activation.vim

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@ describe 's:composer_detect()'
3737
Expect b:composer_root == g:fixtures . 'project-composer'
3838
end
3939
end
40+
41+
context 'in a dependency of a composer project'
42+
it 'sets b:composer_root to the root project'
43+
execute 'edit' g:fixtures . 'project-composer/vendor/foo/bar/index.php'
44+
Expect exists('b:composer_root') to_be_true
45+
Expect b:composer_root == g:fixtures . 'project-composer'
46+
end
47+
end
48+
49+
context 'in a composer project without a vendor autoload'
50+
it 'sets b:composer_root'
51+
execute 'edit' g:fixtures . 'project-uninstalled/index.php'
52+
Expect exists('b:composer_root') to_be_true
53+
Expect b:composer_root == g:fixtures . 'project-uninstalled'
54+
end
55+
end
4056
end
4157

4258
describe 'composer#buffer_setup()'
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
// Dummy PHP file

t/fixtures/project-composer/vendor/foo/bar/composer.json

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
// Dummy PHP file
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
// Dummy PHP file

t/fixtures/project-phar/vendor/foo/bar/composer.json

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
// Dummy PHP file

t/fixtures/project-uninstalled/composer.json

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
// Dummy PHP file

0 commit comments

Comments
 (0)