Skip to content

Instantly share code, notes, and snippets.

@wincent
Last active August 29, 2015 14:05
Show Gist options
  • Save wincent/039b4da304a93a33ae5a to your computer and use it in GitHub Desktop.
Save wincent/039b4da304a93a33ae5a to your computer and use it in GitHub Desktop.
diff --git a/ruby/command-t/controller.rb b/ruby/command-t/controller.rb
index 747d35f..908a743 100644
--- a/ruby/command-t/controller.rb
+++ b/ruby/command-t/controller.rb
@@ -242,6 +242,7 @@ def show
@prompt.focus
register_for_key_presses
set_up_autocmds
+ @active_finder.initial_buffer = @initial_buffer
clear # clears prompt and lists matches
end
diff --git a/ruby/command-t/finder.rb b/ruby/command-t/finder.rb
index bef788b..9cf0939 100644
--- a/ruby/command-t/finder.rb
+++ b/ruby/command-t/finder.rb
@@ -13,6 +13,8 @@ module CommandT
class Finder
include VIM::PathUtilities
+ attr_writer :initial_buffer
+
def initialize(path = Dir.pwd, options = {})
raise RuntimeError, 'Subclass responsibility'
end
diff --git a/ruby/command-t/finder/current_file_tweak.rb b/ruby/command-t/finder/current_file_tweak.rb
index e69de29..4c7bfdf 100644
--- a/ruby/command-t/finder/current_file_tweak.rb
+++ b/ruby/command-t/finder/current_file_tweak.rb
@@ -0,0 +1,23 @@
+# Copyright 2010-2014 Greg Hurrell. All rights reserved.
+# Licensed under the terms of the BSD 2-clause license.
+
+module CommandT
+ class Finder
+ module CurrentFileTweak
+ def sorted_matches_for(str, options = {})
+ matches = super
+
+ # if current buffer is best match, demote it by one notch
+ if @initial_buffer && @initial_buffer.name
+ current = relative_path_under_working_directory(@initial_buffer.name)
+ if matches.length >= 2 && current == matches.first
+ matches.shift
+ matches[1, 0] = current
+ end
+ end
+
+ matches
+ end
+ end # module CurrentFileTweak
+ end # class Finder
+end # module CommandT
diff --git a/ruby/command-t/finder/file_finder.rb b/ruby/command-t/finder/file_finder.rb
index 14055e4..5665a78 100644
--- a/ruby/command-t/finder/file_finder.rb
+++ b/ruby/command-t/finder/file_finder.rb
@@ -3,6 +3,7 @@
require 'command-t/ext' # CommandT::Matcher
require 'command-t/finder'
+require 'command-t/finder/current_file_tweak'
require 'command-t/scanner/file_scanner/ruby_file_scanner'
require 'command-t/scanner/file_scanner/find_file_scanner'
require 'command-t/scanner/file_scanner/watchman_file_scanner'
@@ -10,6 +11,8 @@
module CommandT
class FileFinder < Finder
+ include CurrentFileTweak
+
def initialize(path = Dir.pwd, options = {})
case options.delete(:scanner)
when 'ruby', nil # ruby is the default
@@ -24,6 +27,7 @@ def initialize(path = Dir.pwd, options = {})
raise ArgumentError, "unknown scanner type '#{options[:scanner]}'"
end
+ @initial_buffer = options.delete(:initial_buffer)
@matcher = Matcher.new @scanner, options
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment