This repository has been archived by the owner on Feb 24, 2022. It is now read-only.
/
annotation-adding-plugin.js
72 lines (63 loc) · 2.47 KB
/
annotation-adding-plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*******************************************************************************
* @license
* Copyright (c) 2013 VMware, Inc. All Rights Reserved.
* THIS FILE IS PROVIDED UNDER THE TERMS OF THE ECLIPSE PUBLIC LICENSE
* ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE
* CONSTITUTES RECIPIENTS ACCEPTANCE OF THE AGREEMENT.
* You can obtain a current copy of the Eclipse Public License from
* http://www.opensource.org/licenses/eclipse-1.0.php
*
* Contributors:
* Andy Clement - Initial implementation
* Kris De Volder - Use specific 'annotationComputer' API.
******************************************************************************/
/**
* Plugin sample: marker-adding-plugin
* This plugin shows how to add new annotations to the text.
*
* Notes:
* - annotations have a type name, expressed via a dotted notation.
* - the lifecycle of annotations is managed by the annotationComputer api.
* When a annotationComputer returns new annotations for a particular editor, the
* previous annotations it created will be removed automatically.
* - the annotation computer may be invoked at different points in the editor
* lifecycle. Presently it is only called on 'save' but it probably also
* makes sense to call it on 'load' to produce initial annotations when a file is opened.
* - the styling is done via a few lines of css. See styling.css:
*/
define(function (require) {
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
editorApi.loadCss(require('text!./styling.css'));
// Register an 'annotation computer'.
// This will locate the names of fruits in the text and
// mark them by adding annotations to the editor.
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) {
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');
});