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.



37
38
39
# File 'lib/rtext/default_completer.rb', line 37

def initialize(language)
  @lang = language
end

Instance Method Details

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



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/rtext/default_completer.rb', line 127

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



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

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



69
70
71
72
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
# File 'lib/rtext/default_completer.rb', line 69

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



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

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



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

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



150
151
152
153
154
155
156
157
158
159
# File 'lib/rtext/default_completer.rb', line 150

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



180
181
182
183
184
# File 'lib/rtext/default_completer.rb', line 180

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



169
170
171
172
# File 'lib/rtext/default_completer.rb', line 169

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



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

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



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

def reference_options(context)
  []
end

#root_optionsObject



139
140
141
142
143
144
# File 'lib/rtext/default_completer.rb', line 139

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



161
162
163
164
165
166
167
# File 'lib/rtext/default_completer.rb', line 161

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