Class: RText::DefaultCompleter

Inherits:
Object
  • Object
show all
Defined in:
lib/rtext/default_completer.rb

Defined Under Namespace

Classes: CompletionOption

Instance Method Summary collapse

Constructor Details

#initialize(language) ⇒ DefaultCompleter

Creates a completer for RText::Language language.



41
42
43
# File 'lib/rtext/default_completer.rb', line 41

def initialize(language)
  @lang = language
end

Instance Method Details

#add_label_options(context, clazz, result, version) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/rtext/default_completer.rb', line 131

def add_label_options(context, clazz, result, version)
  result.concat(@lang.labled_arguments(clazz).
    select{|f| 
      !context.element.eIsSet(f.name)}.collect do |f| 
      CompletionOption.from_text_extra("#{f.name}:", "<#{f.eType.name}>")
  end )
  if version > 0 && !context.position.after_comma &&
      context.element.class.ecore.eAllReferences.any? { |r| r.containment } && !context.position.before_brace
    result << CompletionOption.for_curly_braces(context)
  end
end

#add_value_options(context, result, version) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/rtext/default_completer.rb', line 104

def add_value_options(context, result, version)
  if context.feature.is_a?(RGen::ECore::EAttribute) || !context.feature.containment
    if context.feature.is_a?(RGen::ECore::EReference)
      result.concat(reference_options(context))
      if version > 0 && !context.position.before_bracket && context.feature.upperBound != 1
        result << CompletionOption.for_square_brackets
      end
    elsif context.feature.eType.is_a?(RGen::ECore::EEnum)
      result.concat(enum_options(context))
    elsif context.feature.eType.instanceClass == String
      result.concat(string_options(context))
    elsif context.feature.eType.instanceClass == Integer 
      result.concat(integer_options(context))
    elsif context.feature.eType.instanceClass == Float 
      result.concat(float_options(context))
    elsif context.feature.eType.instanceClass == RGen::MetamodelBuilder::DataTypes::Boolean
      result.concat(boolean_options(context))
    else
      # no options 
    end
  else
    if version > 0 && !context.position.before_bracket && context.feature.upperBound != 1
      result << CompletionOption.for_square_brackets
    end
  end
end

#block_options(context, clazz) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/rtext/default_completer.rb', line 73

def block_options(context, clazz)
  types = []
  labled_refs = []
  if context.feature
    if context.feature.is_a?(RGen::ECore::EReference) && context.feature.containment
      types = @lang.concrete_types(context.feature.eType)
    else
      # invalid, ignore
    end
  else
    # all target types which don't need a label
    # and all lables which are needed by a potential target type
    @lang.containments(clazz).each do |r|
      ([r.eType] + r.eType.eAllSubTypes).select{|t| t.concrete}.each do |t|
        if @lang.labeled_containment?(clazz, r) || @lang.containments_by_target_type(clazz, t).size > 1
          labled_refs << r
        else
          types << t
        end
      end
    end
  end
  types.uniq.
    sort{|a,b| a.name <=> b.name}.collect do |c| 
      class_completion_option(c)
    end +
  labled_refs.uniq.collect do |r|
      CompletionOption.from_text_extra("#{r.name}:", "<#{r.eType.name}>")
    end
end

#boolean_options(context) ⇒ Object



190
191
192
# File 'lib/rtext/default_completer.rb', line 190

def boolean_options(context)
  [true, false].collect{|b| CompletionOption.from_text_extra("#{b}", value_description(context)) }
end

#complete(context, version = 0) ⇒ Object

Provides completion options



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rtext/default_completer.rb', line 47

def complete(context, version=0)
  clazz = context && context.element && context.element.class.ecore
  if clazz
    if context.position.in_block
      block_options(context, clazz)
    elsif !context.problem
      result = []
      add_value_options(context, result, version) if context.feature
      add_label_options(context, clazz, result, version) unless context.position.after_label
      result
    else
      # missing comma, after curly brace, etc.
      if version > 0 && !context.position.before_brace &&
          context.element.class.ecore.eAllReferences.any? { |r| r.containment }
        [CompletionOption.for_curly_braces(context)]
      else
        []
      end
    end
  elsif context
    root_options
  else
    []
  end
end

#enum_options(context) ⇒ Object



154
155
156
157
158
159
160
161
162
163
# File 'lib/rtext/default_completer.rb', line 154

def enum_options(context)
  context.feature.eType.eLiterals.collect do |l|
    lname = l.name
    if lname =~ /^\d|\W/ || lname == "true" || lname == "false"
      lname =  "\"#{lname.gsub("\\","\\\\\\\\").gsub("\"","\\\"").gsub("\n","\\n").
        gsub("\r","\\r").gsub("\t","\\t").gsub("\f","\\f").gsub("\b","\\b")}\""
    end
    CompletionOption.from_text_extra("#{lname}", "<#{context.feature.eType.name}>")
  end
end

#float_options(context) ⇒ Object



184
185
186
187
188
# File 'lib/rtext/default_completer.rb', line 184

def float_options(context)
  default_comp = get_default_value_completion(context)
  return [default_comp] if default_comp
  (0..0).collect{|i| CompletionOption.from_text_extra("#{i}.0", value_description(context)) }
end

#get_default_value_completion(context) ⇒ Object



173
174
175
176
# File 'lib/rtext/default_completer.rb', line 173

def get_default_value_completion(context)
  return nil unless context.feature.defaultValue
  CompletionOption.from_text_extra("#{context.feature.defaultValue}", value_description(context))
end

#integer_options(context) ⇒ Object



178
179
180
181
182
# File 'lib/rtext/default_completer.rb', line 178

def integer_options(context)
  default_comp = get_default_value_completion(context)
  return [default_comp] if default_comp
  (0..0).collect{|i| CompletionOption.from_text_extra("#{i}", value_description(context)) }
end

#reference_options(context) ⇒ Object



150
151
152
# File 'lib/rtext/default_completer.rb', line 150

def reference_options(context)
  []
end

#root_optionsObject



143
144
145
146
147
148
# File 'lib/rtext/default_completer.rb', line 143

def root_options
  @lang.root_classes.
    sort{|a,b| a.name <=> b.name}.collect do |c| 
      class_completion_option(c)
    end 
end

#string_options(context) ⇒ Object



165
166
167
168
169
170
171
# File 'lib/rtext/default_completer.rb', line 165

def string_options(context)
  if @lang.unquoted?(context.feature)
    [ CompletionOption.from_text_extra("#{context.feature.name.gsub(/\W/,"")}", value_description(context)) ]
  else
    [ CompletionOption.from_text_extra("\"\"", value_description(context)) ]
  end
end