更上一層樓
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
readme描述了其他安裝選項,它可作為獨立 zip 檔案使用。 許多使用者很高興關注 master 並每週/每天更新。