Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Orion/Documentation/Developer Guide/Plugging into the editor

< Orion‎ | Documentation‎ | Developer Guide
Revision as of 14:01, 21 June 2011 by Mamacdon.ca.ibm.com (Talk | contribs) (orion.edit.validator)

Overview of contributing services to the Orion editor

The built Orion editor defines a number of services for customizing its appearance and behavior. These services will typically be defined by a plug-in providing editing functionality for different programming languages or file extensions. This section will outline the services that are available for editor customization.

orion.edit.command

The orion.edit.command service is the simplest kind of editor extension. A command service simply provides a function that takes some text as input, performs some operation or transformation on the text, and returns a new text value. The command can also optionally receive and return selection information for changing the editor selection.

Service methods

Implementations of orion.edit.command must define the following function:

run(selectedText, text, selection)
selectedText is a string containing the text that is currently selected in the editor. The text argument provides the entire editor buffer. The selection argument is a selection object with start and end fields.

Service attributes

Implementations of orion.edit.command may define the following attributes:

img
The URL of an icon to associate with the command
name
The command text show to the user
key
An optional key binding for the command

Examples

The following simple example just converts the selected text to upper case. In this example the function return value is a simple string, so this is interpreted by the editor as replacement for the original editor selection. In the service properties, we see the command provides a key binding of Ctrl+U (or Cmd+U on Mac).

 var provider = new eclipse.PluginProvider();
 provider.registerServiceProvider("orion.edit.command", {
   run : function(text) {
     return text.toUpperCase();
   }
 }, {
   name : "UPPERCASE",
   img : "/images/gear.gif",
   key : [ "u", true ]
 });
 provider.connect();

Here is an example of a slightly more complex run function that takes the selection and wraps it in C-style block comments. In this example the function returns a complex object with both text and selection fields. These are interpreted by the editor as the new editor buffer contents, and the new editor selection.

 run : function(selectedText, text, selection) {
   return {text: text.substring(0,selection.start) + "/*" + 
     text.substring(selection.start,selection.end) + "*/" + 
     text.substring(selection.end),
     selection: {start:selection.start,end:selection.end+4}};
 }

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:

getKeywords(prefix, buffer, selection)
When content assist is triggered, the editor calls this function to obtain suggestions from a content assist provider.
Returns Array giving possible strings that may be inserted into the editor.
Alternatively, a Dojo promise may be returned, which allows the suggestions to be computed asynchronously.
prefix String The substring extending from the first non-word character preceding the editing caret 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.
buffer String The entire buffer being edited.
selection orion.textview.Selection The current selection in the editor.

Service attributes

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

name
String Name for the content assist provider.
pattern
String A regular expression pattern matching filenames that this provider can provide content assist for. The provider's getKeywords function will be called only for files that match this pattern.

Examples

var provider = new eclipse.PluginProvider();
provider.registerServiceProvider("orion.edit.contentAssist",
  {
     getKeywords: function(prefix, buffer, selection) {
       return [ "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" ];
     }
  },
  {
    name: "JavaScript content assist",
    pattern: "\\.js$"
  });
provider.connect();

The above example provides content assist suggestions for files whose name ends in .js. It offers JavaScript keywords as suggestions.

orion.edit.highlighter

The orion.edit.highlighter service contributes syntax highlighting rules to the editor. A highlighter service provides a grammar, which is a declarative description of the structure of a language and what CSS classes the different structural pieces correspond to. The service also provides a list of file extensions. When the editor opens a file of a registered extension type, the grammar is applied to the file to obtain the styling.

Service methods

None.

Service attributes

Implementations of orion.edit.highlighter must define the following attributes:

type
String What kind of highlight provider is being registered. For now this should always be "grammar", although future versions may support more.
fileTypes
Array An array of file extensions that this provider will be used for.
grammar
Object An object giving the grammar to be used to assign style classes. This object is expected to be a JavaScript equivalent of the format described here.

Examples

var provider = new eclipse.PluginProvider();
provider.registerServiceProvider("orion.edit.highlighter",
  {
  }, {
    type : "grammar",
    fileTypes : ["html", "htm"],
    grammar: {
      patterns: [
          {  begin: "",
             captures: { "0": "punctuation.definition.comment.html" },
             contentName: "comment.block.html"
          }
      ]
    }
  });
provider.connect();

The above example provides a grammar to be used for .html and .htm files. It will assign the CSS class punctuation-definition-comment-html to the <!-- and --> delimiters, and assign the CSS class comment-block-html to the text inside the delimiters. Consult this reference for a full description of the grammar format.

(Note that some aspects of the grammar format are not supported. See orion.editor.TextMateStyler in the Client API reference for a detailed explanation.)

orion.edit.outline

orion.edit.validator

An orion.edit.validator service provides a function that can check the contents of a file and return a data structure indicating which lines (if any) have errors, along with the reason for the error. The result of this service is used by the Orion UI to create error annotations in the ruler beside each problematic line.

Service methods

Implementations of orion.edit.validator must define the following function:

checkSyntax(title, contents)
title String The path and filename of the file being edited.
contents String The contents of the file being edited.

Returns an Object giving the validation result. The returned object must have an errors property whose value is an array.
Each element of the errors array must have the properties:

reason String A description of the error.
line Number Gives the line number where the error was found. (Line numbers begin counting from 1.)
character Number Gives the column within the line where the error was found. (Not currently displayed in the Orion UI).

Service attributes

Implementations of orion.edit.validator must define the following attributes:

pattern
String A regular expression pattern matching the filenames that this validator is capable of validating.

Examples

var service = {
    checkSyntax: function(title, contents) {
        var errors = [];
        var lines = contents.split(/\r?\n/);
        for (var i=0; i < lines.length; i++) {
          var line = lines[i];
          var match = /\t \t| \t /.exec(line);
          if (match) {
            errors.push({ reason: "Mixed spaces and tabs", line: i+1, character: match.index });
          }
        }

        var result = { errors: errors };
        this.dispatchEvent("syntaxChecked", {title: title, result: result});
        return result;
   },
   dispatchEvent: null /* see below */
}; 

var provider = new eclipse.PluginProvider();
var serviceProvider = provider.registerServiceProvider("orion.edit.validator",
  service,
  {
     pattern: "\\.(txt|js)$"
  });
service.dispatchEvent = serviceProvider.dispatchEvent;
provider.connect();

This example finds lines containing a sequence of space-tab-space or tab-space-tab and produces an error on each such line.

Back to the top