Class: VimMate::ListedTree

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/vimmatelib/files.rb

Overview

A tree of files and directory. Can signal added and removed files.

Instance Method Summary collapse

Constructor Details

#initialize(exclude_file_list = []) ⇒ ListedTree

Create a ListedTree which contains ListedFile and ListedDirectory



177
178
179
180
181
182
183
184
185
# File 'lib/vimmatelib/files.rb', line 177

def initialize(exclude_file_list = [])
  @paths = Set.new
  @refresh_signal = Set.new
  @signal_method = method(:signal)
  @exclude_file_list = exclude_file_list
  @too_many_files_signal = Set.new
  @warn_too_many_files = false
  @warn_files_count = 0
end

Instance Method Details

#add_path(path) ⇒ Object

Add a path: a file or a directory. If it’s a directory, all files within this directory are also added



195
196
197
198
199
200
201
202
203
204
# File 'lib/vimmatelib/files.rb', line 195

def add_path(path)
  return unless File.exist? path
  return if @exclude_file_list.any? {|f| path[-(f.size+1)..-1] == "/#{f}" }
  @paths << if File.directory? path
              ListedDirectory.new(path, @exclude_file_list, &@signal_method)
            else
              ListedFile.new(path, &@signal_method)
            end
  self
end

#add_refresh_signal(&block) ⇒ Object

Add a block that will be called when a file is added or removed. The block take 2 arguments: method and file:

method: :add, :remove or :refresh
file: the ListedFile or ListedDirectory that is affected


232
233
234
# File 'lib/vimmatelib/files.rb', line 232

def add_refresh_signal(&block)
  @refresh_signal << block
end

#add_too_many_files_signal(&block) ⇒ Object

Add a block that will be called when too many files are added. The block takes 1 argument, the number of files that where added.



238
239
240
# File 'lib/vimmatelib/files.rb', line 238

def add_too_many_files_signal(&block)
  @too_many_files_signal << block
end

#each(&block) ⇒ Object

Yield each files and directory at the root of the tree



188
189
190
191
# File 'lib/vimmatelib/files.rb', line 188

def each(&block)
  @paths.each(&block)
  self
end

#initial_addObject

Indicates that the initial file adding is going on. Used to warn if there are too many files



208
209
210
211
212
213
214
215
216
217
# File 'lib/vimmatelib/files.rb', line 208

def initial_add
  begin
    @warn_files_count = 0
    @warn_too_many_files = true
    yield
  ensure
    @warn_files_count = 0
    @warn_too_many_files = false
  end
end

#refreshObject

Refresh the files from the tree. Inexistent files are removed and new files are added



221
222
223
224
225
226
# File 'lib/vimmatelib/files.rb', line 221

def refresh
  each do |path|
    path.refresh
  end
  self
end