# orion.edit.contentAssist

The `orion.edit.contentAssist` service contributes content assist providers to the editor. A content assist provider produces suggestions for text that may be inserted into the editor at a given point. Providers are invoked when the user triggers the "content assist" action by pressing Ctrl+Space in the editor.

## Service methods

Implementations of `orion.edit.contentAssist` must define the following function:

computeProposals(buffer, offset, context)
  When content assist is triggered, the editor calls this function to obtain suggestions from a content assist provider.
    buffer `String` The entire buffer being edited.
    offset `Number` Offset in the text buffer at which content assist is being invoked.
    context `Object` Additional contextual information about the content assist invocation. This object has the following properties:
      line `String` Text of the entire line that the editing caret is on.
      prefix `String` The substring extending from the first non-word character preceding the editing caret up to the editing caret. This may give a clue about what the user intended to type, and can be used to narrow down the results to be returned. The prefix may not be appropriate for all types of document, depending on their syntax rules.
      selection `orion.textview.Selection` The current selection in the editor.

Returns an `Array` giving possible completions that may be inserted into the editor. Result elements must be "proposal" objects having the following properties:

  proposal `String` completion text that will be inserted in the editor if chosen. The text is inserted at the offset.
  description A `String` describing the completion. This text will be shown in the content assist popup.
  positions An optional `Array` of positions within the completion proposal that require user input. Supplying this property will cause the editor to enter linked mode, and the user can use the Tab key to iterate through the regions of the proposal that require user input. For example if the completion is a function, the positions could indicate the function arguments that need to be supplied. Entries in this position array must be objects with two integer properties: offset, and length describing the regions requiring user input.
  escapePosition An optional `Number` indicating the offset the cursor should have in the document after the completion is inserted. If this value is not supplied, the cursor will be positioned at the end of the inserted text.

Alternatively, a promise may be returned, which allows the suggestions to be computed asynchronously.

## Service attributes

Implementations of `orion.edit.contentAssist` must define the following attributes:

name
  `String` Name for the content assist provider.
contentType
  `Array` An array of Content Type IDs that this provider can provide content assist for. The provider's `computeProposals` function will be called only for files having one of these content types.

## Examples

The example below provides content assist suggestions for files whose name ends in `.js`. It offers JavaScript keywords as suggestions, by checking them against the prefix provided by the content assist engine.

    var provider = new eclipse.PluginProvider();
    provider.registerServiceProvider("orion.edit.contentAssist",
      {
         computeProposals: function(buffer, offset, context) {
           var keywords = [ "break", "case", "catch", "continue", "debugger", "default", "delete", "do", "else",
                            "finally", "for", "function", "if", "in", "instanceof", "new", "return", "switch",
                            "this", "throw", "try", "typeof", "var", "void", "while", "with" ];
           var proposals = [];
           for (var i=0; i < keywords.length; i++) {
               var keyword = keywords[i];
               if (keyword.indexOf(context.prefix) === 0) {
                   proposals.push({
                       proposal: keyword.substring(context.prefix.length),
                       description: keyword
                   });
               }
            }
           return proposals;
         }
      },
      {
        name: "JavaScript content assist",
        contentType: ["application/javascript"]
      });
    provider.connect();

The example below will provide completion on the character 'a' that will insert an HTML anchor element. After completion the cursor will be positioned within the `href` attribute.

    var provider = new eclipse.PluginProvider();
    provider.registerServiceProvider('orion.edit.contentAssist',
      {
        computeProposals: function(buffer, offset, context) {
          var proposals = [];
          if (context.prefix === 'a') {
            proposals.push({
              proposal: ' href=""></a>',
              description: '<a></a> - HTML anchor element',
              escapePosition: offset+7});
          }
          return proposals;
      },
      {
        name: 'HTML content assist',
        contentType: ['text/html']
      });
    provider.connect();