# 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 where problems are. The result of this service is used by the Orion UI to create annotations in the ruler beside each problematic line, and also to underline the specific portion of the document where the problem occurs.

## Service methods (Orion 4.0)

computeProblems(editorContext, options)
  editorContext `ObjectReference` The Editor Context object.
  options `Object`
    options.contentType `String` The Content Type ID of the file being edited.
    options.title `String` The path and filename of the file being edited.

Returns (or fulfills to) an `Object` giving the validation result. The returned object must have a `problems` property giving an Array of problems found in the file.

## Service methods (Orion 3.0)

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 a `problems` property whose value is an array giving the problems found in the file.

## The Problem object

A Problem object has the following properties:

  description `String` A description of the problem.
  severity `String` Optional. Gives the severity of this problem. The severity affects how the problem is displayed in the Orion UI. Allowed values are `"warning"` and `"error"`. (If omitted, `"error"` is assumed.)

A problem will have additional properties that give its location within the file. The location can be specified using line+column, or using offsets.

For a line-based problem, you provide a line number and columns:

  line `Number` The line number where the problem was found. (Line numbers begin counting from 1.)
  start `Number` The column within the line where the problem begins. (Columns begin counting from 1.)
  end `Number` Optional The column within the line where the problems ends. (If omitted, `start+1` is assumed.)

For a document-based problem, you provide character offsets:

  start `Number` The offset at which the problem begins. (0=first character in the document.)
  end `Number` Optional The offset at which the problem ends. (If omitted, `start+1` is assumed.)

A document-based problem can span several lines.

## Service attributes

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

contentType
  `Array` An array of Content Type IDs giving the types of files that this validator is capable of validating.

## Examples

     var provider = new eclipse.PluginProvider();
     var serviceProvider = provider.registerServiceProvider("orion.edit.validator",
       {
          checkSyntax: function(title, contents) {
            var problems = [];
            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) {
                problems.push({
                  description: "Mixed spaces and tabs",
                  line: i + 1,
                  start: match.index + 1,
                  end: match.index + match[0].length + 1,
                  severity: "warning" });
              }
            }
            var result = { problems: problems };
            return result;
          }
       },
       {
          contentType: ["application/javascript"]
       });
     provider.connect();

This example will validate JavaScript files. It finds lines containing a sequence of space-tab-space or tab-space-tab and produces a warning on every such line. Note that `+1` is necessary because column and line indices in the Orion UI are numbered from 1, not 0.