Get ahead
VMware offers training and certification to turbo-charge your progress.
Learn moreThis week we've released version 0.4 of our JavaScript focused code editor. You can read about the background of Scripted here.
The full release notes for 0.4 are here but in this article I'll call out a few of the more interesting changes.
Ctrl/Cmd+Space
:
And on selecting the first template completion the editor contents become:
One of the key use cases we had in mind was enabling you to write a plugin that contributed new annotations to the editor (that appear in the left hand ruler and allow styling of the editor text) . Here is a very simple plugin. This simply locates the names of fruits in your code and adds annotations for them. Perhaps not the most useful plugin but it should show what the key parts of a plugin are, rather than getting bogged down in annotation computation.
define(function (require) {
// Grab the editor API
var editorApi = require('scripted/api/editor-extensions');
var pathExp = new RegExp('.*\\.js$');
// Register the new annotation type
editorApi.registerAnnotationType('example.message');
// Load the styling for our annotation (very simple bit of css)
editorApi.loadCss(require('text!./styling.css'));
// Register an 'annotation computer'.
// The return value of the function is the new set of annotations
// which replace any added on previous calls to the function.
editorApi.addAnnotationComputer(function (editor) {
// Only interested in .js files
var path = editor.getFilePath();
if (!path || !pathExp.test(path)) {
return;
}
var text = editor.getText();
var fruits = ['apple', 'banana', 'kiwi', 'orange'];
var annotations=[];
for (var f=0; f<fruits.length; f++) {
var fruit = fruits[f];
var index = text.indexOf(fruit);
while (index!=-1) {
// Create the annotation: needs type/start/end/text
annotations.push({type:'example.message', start:index,
end:index+fruit.length, text:'Found '+fruit });
index = text.indexOf(fruit,index+1);
}
}
return annotations;
});
console.log('Annotation adding sample plugin');
});
The other two pieces of this plugin are the styling css (sorry about the inline image data, it is just convenient to reuse some of the images we inherit from Eclipse Orion):
/* Styling for the text in the editor */
.annotationRange.message {
/* the icon for a 'squiggly' underline */
background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAQAAAADCAYAAAC09K7GAAAAAXNSR0IArs4c6QAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9sJFhQXEbhTg7YAAAAZdEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIEdJTVBXgQ4XAAAAMklEQVQI12NkgIIvJ3QXMjAwdDN+OaEbysDA4MPAwNDNwMCwiOHLCd1zX07o6kBVGQEAKBANtobskNMAAAAASUVORK5CYII=");
background-color: cyan;
color: blue;
}
/* Styling for the right hand overview ruler */
.annotationOverview.message {
background-color: grey;
border: 1px solid #000080;
}
/* Styling for the left hand annotation ruler */
.annotationHTML.message {
/* the icon for a 'flashlight' */
background-image: url("data:image/gif;base64,R0lGODlhEAAQANUAALClrLu1ubOpsKqdp6eapKufqMTAw7attLSrsrGnr62jq8C7v765vaebpb22vLmyuMbCxsnGycfEx8G+wcrIysTBxUltof//yf///v70jergpPvws+nWc/npqvrpqvrpq/raffffnvXVkfTVkvXUkd+9f+SiOemvV+uyXa2OX7mYZqeIXKuNX/ClO7KQYqiIXJ59Vp19VpFvTo9uTZBvTpNyUJNyUf///////wAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAADgALAAAAAAQABAAAAZ4QJxwSCwajS2aS1U6DlunzcagcuKgG4sn5HJiLZ2QiHbEbj6hEapVTKVYr3OItG5TIhVGLF0npigUEAsPAjV9Q24pEhMBCAoybEUmGRcrDgcAAzNGkxcYNzAJBQSbRJ0YqBc2DaVEHJ6pGTStRBqfGBcZILRWvThBADs=");
}
and a plugin.json (a .json file isn't necessary for totally trivial plugins).
{
"name": "annotation-adding-plugin",
"version": "0.1",
"description": "Scripted plugin to add markers in the editor",
"main": "annotation-adding-plugin",
"scripted": {
"plugin": true
}
}
All these pieces are here in the git repo. Once activated, the styling will look like this when the annotations are added:
Based on this model we have implemented a more realistic plugin based on a blog article by Ariya Hidayat describing how to 'detect boolean traps'. The source for that plugin is here.
Consult the wiki for more information on plugin development.
Last but not least we are building on the theming support in Orion and now have a light-on-dark theme for the editor. The individual colors are not currently configurable but we still feel the default colors that have been setup look pretty nice:
npm install -g scripted
scr foo.js
The readme describes the other install options, it is available as a standalone zip. Many users are happy to follow master and update weekly/daily.