Skip to content

Commit

Permalink
Refactor FindFileDialog and recent files stuff in Project. Also fix #175
Browse files Browse the repository at this point in the history
  • Loading branch information
danlucraft committed Mar 5, 2010
1 parent 4685863 commit a47b4f1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 38 deletions.
46 changes: 29 additions & 17 deletions plugins/project/lib/project.rb
Expand Up @@ -9,6 +9,7 @@

module Redcar
class Project
RECENT_FILES_LENGTH = 20

# this will restore open files unless other files or dirs were passed
# as command line parameters
Expand Down Expand Up @@ -131,11 +132,28 @@ def self.open_dir(path, win = Redcar.app.focussed_window)
storage['last_open_dir'] = path
end

# A list of files previously opened in this session
# A list of files previously opened in this session for a given directory path
#
# @param [String] path a directory path
# @return [Array<String>] an array of paths
def self.recent_files
@recent_files ||= []
def self.recent_files_for(path)
(@recent_files ||= Hash.new {|h,k| h[k] = [] })[path]
end

# The directory path of the currently focussed project, or nil if
# there is no directory open in the focussed window.
#
# @return [String, nil]
def self.focussed_project_path
if tree = focussed_project_tree
tree.tree_mirror.path
end
end

# The Tree object for the currently focussed project tree, or nil
# if there is no directory open in the focussed window.
def self.focussed_project_tree
Redcar.app.focussed_window.treebook.trees.detect {|t| t.tree_mirror.is_a?(Project::DirMirror)}
end

private
Expand Down Expand Up @@ -184,26 +202,20 @@ def self.handle_startup_arguments
def self.init_current_files_hooks
Redcar.app.add_listener(:tab_focussed) do |tab|
if tab and tab.document_mirror.respond_to?(:path)
add_to_recent_files(tab.document_mirror.path)
add_to_recent_files_for(Project.focussed_project_path, tab.document_mirror.path)
end
end
end

def self.add_to_recent_files(new_file)
# sanitize paths for doze
# like "E:\\installs\\ruby191p376\\bin/rdebug.bat" => "E:/installs/ruby191p376/bin/rdebug.bat"
def self.add_to_recent_files_for(directory_path, new_file)
new_file = File.expand_path(new_file)
unless new_file == @last_file
recent_files.delete(new_file)
recent_files << new_file

if @last_file
recent_files.delete(@last_file)
recent_files.unshift(@last_file)
end
if recent_files_for(directory_path).include?(new_file)
recent_files_for(directory_path).delete(new_file)
end
recent_files_for(directory_path) << new_file
if recent_files_for(directory_path).length > RECENT_FILES_LENGTH
recent_files_for(directory_path).shift
end

@last_file = new_file
end

def self.set_tree(win, tree)
Expand Down
36 changes: 15 additions & 21 deletions plugins/project/lib/project/find_file_dialog.rb
Expand Up @@ -45,23 +45,24 @@ def self.storage

def update_list(filter)
if filter.length < 2
paths = Project.recent_files
paths = Project.recent_files_for(Project.focussed_project_path)
else
paths = find_files_from_list(filter, Project.recent_files) + find_files(filter, Redcar.app.focussed_window.treebook.trees.last.tree_mirror.path)
paths = find_files_from_list(filter, Project.recent_files_for(Project.focussed_project_path)) +
find_files(filter, Project.focussed_project_path)
paths.uniq! # in case there's some dupe's between the two lists
end

@last_list = paths
full_paths = paths
display_paths = full_paths.map { |path| display_path(path) }
if display_paths.uniq.length < full_paths.length
# search out and expand duplicates
duplicates = display_paths.duplicates_as_hash
display_paths.each_with_index{|dp, i|
# search out and expand duplicates
duplicates = duplicates_as_hash(display_paths)
display_paths.each_with_index do |dp, i|
if duplicates[dp]
display_paths[i] = display_path(full_paths[i], Redcar.app.focussed_window.treebook.trees.last.tree_mirror.path.split('/')[0..-2].join('/'))
display_paths[i] = display_path(full_paths[i], Project.focussed_project_path.split('/')[0..-2].join('/'))
end
}
end
end
display_paths
end
Expand All @@ -74,11 +75,11 @@ def selected(text, ix, closing=false)
end

private

def remove_from_list(path)
self.class.recent_files.delete(path)
def duplicates_as_hash(enum)
enum.inject(Hash.new(0)) {|h,v| h[v] += 1 }.reject {|k,v| v == 1 }
end

def display_path(path, first_remove_this_prefix = nil)
n = -3
if first_remove_this_prefix && path.index(first_remove_this_prefix) == 0
Expand Down Expand Up @@ -108,7 +109,7 @@ def files(directories)
end
took = Time.now - s
puts "find files (#{directories.length} dirs) took #{took}s"
files.reject!{|f|
files.reject do |f|
begin
File.directory?(f)
rescue Errno::ENOENT
Expand All @@ -117,14 +118,13 @@ def files(directories)
# unicode in them.
true
end
}
files
end
end
end

def find_files_from_list(text, file_list)
re = make_regex(text)
file_list.select{|fn|
file_list.select { |fn|
fn.split('/').last =~ re
}.compact
end
Expand All @@ -137,9 +137,3 @@ def find_files(text, directories)
end
end
end

module Enumerable
def duplicates_as_hash
inject({}) {|h,v| h[v]=h[v].to_i+1; h}.reject{|k,v| v==1}
end
end

0 comments on commit a47b4f1

Please sign in to comment.