 
     return "" if serverSuggestion.match(/^[\s\d]/)
 
-    if kind == 'attribute'
-      createSuggestionForAttribute(name, prefix)
-    else if kind == 'var'
-      createSuggestionForVariable(name)
-    else if kind == 'module'
-      createSuggestionForModule(serverSuggestion, name, desc, prefix, subtype)
-    else if kind == 'callback'
-      createSuggestionForCallback(serverSuggestion, name, kind, signature, mod, desc, spec, prefix, defBefore)
-    else if kind == 'return'
-      createSuggestionForReturn(serverSuggestion, name, kind)
-    else if kind in ['private_function', 'public_function', 'public_macro']
-      createSuggestionForFunction(serverSuggestion, name, kind, signature, "", desc, spec, prefix, pipeBefore, captureBefore)
-    else if kind in ['function', 'macro']
-      createSuggestionForFunction(serverSuggestion, name, kind, signature, mod, desc, spec, prefix, pipeBefore, captureBefore)
-    else
-      console.log("Unknown kind: #{serverSuggestion}")
-      {
-        text: serverSuggestion
-        type: 'exception'
-        iconHTML: '?'
-        rightLabel: kind || 'hint'
-      }
+    suggestion =
+      if kind == 'attribute'
+        createSuggestionForAttribute(name, prefix)
+      else if kind == 'var'
+        createSuggestionForVariable(name)
+      else if kind == 'module'
+        createSuggestionForModule(serverSuggestion, name, desc, prefix, subtype)
+      else if kind == 'callback'
+        createSuggestionForCallback(serverSuggestion, name, kind, signature, mod, desc, spec, prefix, defBefore)
+      else if kind == 'return'
+        createSuggestionForReturn(serverSuggestion, name, kind)
+      else if kind in ['private_function', 'public_function', 'public_macro']
+        createSuggestionForFunction(serverSuggestion, name, kind, signature, "", desc, spec, prefix, pipeBefore, captureBefore)
+      else if kind in ['function', 'macro']
+        createSuggestionForFunction(serverSuggestion, name, kind, signature, mod, desc, spec, prefix, pipeBefore, captureBefore)
+      else
+        console.log("Unknown kind: #{serverSuggestion}")
+        {
+          text: serverSuggestion
+          type: 'exception'
+          iconHTML: '?'
+          rightLabel: kind || 'hint'
+        }
+    suggestion.index = index
+    suggestion
 
   createSuggestionForAttribute = (name, prefix) ->
     if prefix.match(/^@/)

    regex = /[\w0-9\._!\?\:@]+$/
    textBeforeCursor.match(regex)?[0] or ''

  createSuggestion = (serverSuggestion, fields, prefix, pipeBefore, captureBefore, defBefore) ->
    if fields[1] == 'module'
      [name, kind, subtype, desc] = fields
    if fields[1] == 'return'


    sortFunctionByArity = (a, b) ->
      return 0 if !isFunc(a) || !isFunc(b)
      return a.arity - b.arity

    sortByOriginalOrder = (a, b) ->
      return a.index - b.index

    sortFunc = (a, b) ->
      sortKind(a, b) || sortFunctionByType(a, b) || sortFunctionByName(a, b) || sortFunctionByArity(a, b) || sortByOriginalOrder(a, b)

    suggestions.sort(sortFunc)

