领先一步
VMware 提供培训和认证,助您加速进步。
了解更多本周我们发布了专注于 JavaScript 的代码编辑器 0.4 版本。您可以在此处阅读有关 Scripted 背景的信息。
0.4 版本的完整发布说明在此处,但在这篇文章中,我将重点介绍一些更有趣的更改。
Ctrl/Cmd+Space
选择第一个模板完成时,编辑器内容变为
我们设想的关键用例之一是使您能够编写一个插件,为编辑器贡献新的注释(这些注释出现在左侧标尺中并允许对编辑器文本进行样式设置)。这是一个非常简单的插件。它只是定位代码中的水果名称并为其添加注释。这可能不是最有用的插件,但它应该展示插件的关键部分是什么,而不是纠缠于注释计算。
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');
});
这个插件的另外两个部分是样式 CSS(抱歉内联图像数据,只是为了方便重用我们从 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=");
}
以及一个 plugin.json(对于完全微不足道的插件来说,.json 文件不是必需的)。
{
"name": "annotation-adding-plugin",
"version": "0.1",
"description": "Scripted plugin to add markers in the editor",
"main": "annotation-adding-plugin",
"scripted": {
"plugin": true
}
}
所有这些部分都在这里,在 Git 存储库中。一旦激活,添加注释后的样式将如下所示
基于此模型,我们实现了一个更真实的插件,该插件基于 Ariya Hidayat 的一篇博客文章,该文章描述了如何“检测布尔陷阱”。该插件的源代码在这里。
有关插件开发的更多信息,请查阅维基。
最后但并非最不重要的是,我们正在 Orion 的主题支持基础上进行构建,现在编辑器有了一个亮色背景上的暗色主题。目前无法配置单个颜色,但我们仍然觉得默认设置的颜色看起来相当不错:
npm install -g scripted
scr foo.js
自述文件描述了其他安装选项,它作为一个独立的 zip 文件提供。许多用户乐于跟随主分支并每周/每天更新。