Class: Cocoadex::CompletionHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoadex/tools/completion_helper.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command) ⇒ CompletionHelper

Returns a new instance of CompletionHelper.



6
7
8
# File 'lib/cocoadex/tools/completion_helper.rb', line 6

def initialize(command)
  @command = command
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



4
5
6
# File 'lib/cocoadex/tools/completion_helper.rb', line 4

def command
  @command
end

Class Method Details

.clear_tagsObject

Clear all tags in file



43
44
45
# File 'lib/cocoadex/tools/completion_helper.rb', line 43

def self.clear_tags
  Serializer.write_text(tags_path, "")
end

.generate_tags!Object

Build a tags file from existing kewords



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/cocoadex/tools/completion_helper.rb', line 48

def self.generate_tags!
  logger.info "Generating tags file..."

  datastore = Tokenizer.tokens.sort_by {|k| k.term }
  text = datastore.map {|k| k.term }.join("\n") + "\n"

  # Parse class elements and store tags for scope delimiters
  datastore.select {|k| k.type == :class }.each_slice(50).to_a.each do |batch|
    Tokenizer.untokenize(batch).each do |klass|
      logger.debug("Tagging #{klass.name} properties")
      text << tagify(
        klass.name,
        (klass.properties+klass.methods.to_a),
        Keyword::CLASS_PROP_DELIM)
      text << tagify(
        klass.name,
        klass.class_methods,
        Keyword::CLASS_METHOD_DELIM)
      text << tagify(
        klass.name,
        klass.instance_methods,
        Keyword::INST_METHOD_DELIM)
    end
  end

  Serializer.write_text(tags_path, text.strip)
end

.tagsObject

Tags loaded from file



32
33
34
35
36
37
38
39
40
# File 'lib/cocoadex/tools/completion_helper.rb', line 32

def self.tags
  @tags ||= begin
    if File.exists?(tags_path)
      IO.read(tags_path, :mode => 'rb').split("\n")
    else
      []
    end
  end
end

.tags_pathObject

Tags file location



27
28
29
# File 'lib/cocoadex/tools/completion_helper.rb', line 27

def self.tags_path
  Cocoadex.config_file("tags")
end

Instance Method Details

#matchesObject



10
11
12
13
14
15
16
# File 'lib/cocoadex/tools/completion_helper.rb', line 10

def matches
  scope = Keyword.get_scope(command)

  CompletionHelper.tags.select do |tag|
    tag[0, command.length] == command && Keyword.get_scope(tag) == scope
  end
end

#scope_matchesObject



18
19
20
21
22
23
24
# File 'lib/cocoadex/tools/completion_helper.rb', line 18

def scope_matches
  scope = Keyword.get_scope(command)

  CompletionHelper.tags.select do |tag|
    Keyword.get_scope(tag) == scope
  end
end