Skip to content

Commit

Permalink
Handle multi-level globs
Browse files Browse the repository at this point in the history
Handle multi-level globs by separating the passed file with `/`. See
tpope/vim-projectionist#14
  • Loading branch information
glittershark committed Jul 23, 2014
1 parent 93693f8 commit 0f8a1aa
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 35 deletions.
17 changes: 12 additions & 5 deletions lib/projector/projections.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ def type?(type)

def file_for(type, file)
return nil unless type? type
glob = @types[type]['glob']
specific_glob = glob.gsub(/(.*)\*(.*)/, "\\1#{file}\\2")
file = Dir.glob(specific_glob)[0]
File.expand_path(file.nil? ? specific_glob : file)

glob = build_glob(@types[type]['glob'], file)
file = Dir.glob(glob)[0]
File.expand_path(file.nil? ? glob : file)
end

def files_for(type)
Expand Down Expand Up @@ -55,8 +55,15 @@ def projections_path
return nil if [false, '/'].include?(File.dirname path)
path = File.expand_path('../../.projections.json', path)
end

path
end

def build_glob(glob, file)
# Split the passed file by `/`, then replace all globs that use `*` or `**` with
# components of the passed file, in order
file_components = file.split('/')
glob_components = glob.split(/\*+/)
glob_components.zip(file_components).flatten.compact.join('')
end
end
end
77 changes: 47 additions & 30 deletions spec/projections_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,44 +101,61 @@
end

describe '#file_for' do
let(:test_dir) { File.join(fixture_folder, 'test') }
let(:test_file) { File.join(test_dir, 'file.rb') }
before do
write_fixtures('test/*.rb' => { 'type' => 'test' })
Dir.mkdir(test_dir) unless Dir.exist? test_dir
File.open(test_file, 'w')
Dir.chdir fixture_folder
@projections.load_file
end

context 'with a valid type' do
subject { @projections.file_for('test', 'file') }
it { should eq test_file }
end
context 'with simple globs' do
let(:test_dir) { File.join(fixture_folder, 'test') }
let(:test_file) { File.join(test_dir, 'file.rb') }
before do
write_fixtures('test/*.rb' => { 'type' => 'test' })
Dir.mkdir(test_dir) unless Dir.exist? test_dir
File.open(test_file, 'w')
Dir.chdir fixture_folder
@projections.load_file
end

context 'without a valid type' do
subject { @projections.file_for('toast', 'file') }
it { should be_nil }
end
context 'with a valid type' do
subject { @projections.file_for('test', 'file') }
it { is_expected.to eq test_file }
end

context "with a file that doesn't exist" do
subject { @projections.file_for('test', 'nonexistent') }
it { should eq File.join(test_dir, 'nonexistent.rb') }
end
context 'without a valid type' do
subject { @projections.file_for('toast', 'file') }
it { is_expected.to be_nil }
end

context 'with other files in the directory' do
let(:other_test_file) { File.join(test_dir, 'abc.rb') }
before do
File.open(other_test_file, 'w')
context "with a file that doesn't exist" do
subject { @projections.file_for('test', 'nonexistent') }
it { is_expected.to eq File.join(test_dir, 'nonexistent.rb') }
end

it 'matches the other file' do
expect(@projections.file_for('test', 'abc')).to eq other_test_file
context 'with other files in the directory' do
let(:other_test_file) { File.join(test_dir, 'abc.rb') }
before do
File.open(other_test_file, 'w')
end

it 'matches the other file' do
expect(@projections.file_for('test', 'abc')).to eq other_test_file
end

it 'matches the old file' do
expect(@projections.file_for('test', 'file')).to eq test_file
end
end
end

it 'matches the old file' do
expect(@projections.file_for('test', 'file')).to eq test_file
context 'with multi-level globs' do
let(:test_dir) { File.join(fixture_folder, 'test', 'foobar') }
let(:test_file) { File.join(test_dir, 'test_file.rb') }
before do
write_fixtures('test/**/test_*.rb' => { 'type' => 'test' })
FileUtils.mkdir_p test_dir unless Dir.exist? test_dir
File.open(test_file, 'w')
Dir.chdir fixture_folder
@projections.load_file
end

subject { @projections.file_for 'test', 'foobar/file' }
it { is_expected.to eq test_file }
end
end
end

0 comments on commit 0f8a1aa

Please sign in to comment.