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

Open relative paths under the working directory #2

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
source :rubygems
gem 'rake'
gem 'rr'
gem 'rspec', '>= 2.0.0.beta.16'
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
GEM
remote: http://rubygems.org/
specs:
diff-lcs (1.1.2)
rake (0.8.7)
Expand Down
7 changes: 7 additions & 0 deletions ruby/command-t/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ def get_list_or_string name
end
end

def relative_path_under_working_directory path
# clean up paths for the usual case of opening files under the working
# directory. keeps noise out of the buffer list, status line, & tab line.
path.index(pwd = "#{VIM::pwd}/") == 0 ? path[pwd.length..-1] : path
end

# Backslash-escape space, \, |, %, #, "
def sanitize_path_string str
# for details on escaping command-line mode arguments see: :h :
Expand Down Expand Up @@ -216,6 +222,7 @@ def ensure_appropriate_window_selection
def open_selection selection, options = {}
command = options[:command] || default_open_command
selection = File.expand_path selection, @path
selection = relative_path_under_working_directory selection
selection = sanitize_path_string selection
ensure_appropriate_window_selection
::VIM::command "silent #{command} #{selection}"
Expand Down
76 changes: 76 additions & 0 deletions spec/command-t/controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
require File.expand_path('../spec_helper', File.dirname(__FILE__))
require 'command-t/controller'

module VIM; end

describe CommandT::Controller do
describe 'accept selection' do
before do
stub_finder
stub_match_window 'path/to/selection'
stub_prompt
stub_vim '/working/directory'
end

before do
@controller = CommandT::Controller.new
end

it 'opens relative paths inside the working directory' do
stub(::VIM).evaluate('a:arg').returns('')
@controller.show
mock(::VIM).command('silent e path/to/selection')
@controller.accept_selection
end

it 'opens absolute paths outside the working directory' do
stub(::VIM).evaluate('a:arg').returns('../outside')
@controller.show
mock(::VIM).command('silent e /working/outside/path/to/selection')
@controller.accept_selection
end

it 'does not get confused by common directory prefixes' do
stub(::VIM).evaluate('a:arg').returns('../directory-oops')
@controller.show
mock(::VIM).command('silent e /working/directory-oops/path/to/selection')
@controller.accept_selection
end
end

private

def stub_finder(sorted_matches=[])
finder = Object.new
stub(finder).path = anything
stub(finder).sorted_matches_for(anything, anything).returns(sorted_matches)
stub(CommandT::Finder).new.returns(finder)
end

def stub_match_window(selection)
match_window = Object.new
stub(match_window).matches = anything
stub(match_window).close
stub(match_window).selection.returns(selection)
stub(CommandT::MatchWindow).new.returns(match_window)
end

def stub_prompt(abbrev='')
prompt = Object.new
stub(prompt).focus
stub(prompt).clear!
stub(prompt).abbrev.returns(abbrev)
stub(CommandT::Prompt).new.returns(prompt)
end

def stub_vim(working_directory)
stub($curbuf).number.returns('0')
stub(::VIM).command(/noremap/)
stub(::VIM).command('silent b 0')
stub(::VIM).evaluate(/exists\(.+\)/).returns('0')
stub(::VIM).evaluate('getcwd()').returns(working_directory)
stub(::VIM).evaluate('&buflisted').returns('1')
stub(::VIM).evaluate('&lines').returns('80')
stub(::VIM).evaluate('&term').returns('vt100')
end
end