forked from pjanik/orion.client
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Initial work on the annotation service and mark occurrences plugin
- Loading branch information
Andrew Eisenberg
authored and
Andrew Eisenberg
committed
Mar 13, 2012
1 parent
8d428c5
commit 7ec046b
Showing
8 changed files
with
237 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
bundles/org.eclipse.orion.client.core/web/plugins/markOccurrencesPlugin/markOccurrences.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| /******************************************************************************* | ||
| * @license | ||
| * Copyright (c) 2012 Contributors | ||
| * All rights reserved. This program and the accompanying materials are made | ||
| * available under the terms of the Eclipse Public License v1.0 | ||
| * (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution | ||
| * License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html). | ||
| * | ||
| * Contributors: | ||
| * Andrew Eisenberg (vmware) - initial API and implementation | ||
| *******************************************************************************/ | ||
| /*global define console */ | ||
| define("markOccurrences", [], function() { | ||
| // private variable | ||
| var _annotationProvider = { | ||
| _serviceProvider : null, | ||
| setServiceProvider : function(serviceProvider) { this._serviceProvider = serviceProvider; }, | ||
| modelChanged : function() { | ||
| if (this._serviceProvider) { | ||
| this._serviceProvider.dispatchEvent("registerAnnotationType", { | ||
| type: "orion.annotation.markoccurrences", | ||
| style: { "font-weight": "bold" }, | ||
| rangeStyle: {styleClass: "annotationOverview currentLine"}, | ||
| html: "<div class='annotationHTML currentLine'></div>", | ||
| overviewStyle: {styleClass: "annotationOverview currentLine"} | ||
| }); | ||
| } | ||
| }, | ||
| pushAnnotations : function(ranges) { | ||
| if (ranges && ranges.length > 0) { | ||
| console.log("All locations: " + JSON.stringify(ranges)); | ||
| this._serviceProvider.dispatchEvent("addAnnotations", { | ||
| type: "orion.annotation.markoccurrences", | ||
| ranges: ranges | ||
| }); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
|
|
||
| function isIdentifierPart(char) { | ||
| return (/\d|\w|\$|_/).exec(char) !== null; | ||
| } | ||
|
|
||
| function isIdentifierStart(char) { | ||
| return (/\w|\$|_/).exec(char) !== null; | ||
| } | ||
|
|
||
| function isIdentifier(word) { | ||
| if (!word || word.length === 0) { | ||
| return false; | ||
| } | ||
| if (!isIdentifierStart(word.charAt(0))) { | ||
| return false; | ||
| } | ||
| for (var i = 1; i < word.length; i++) { | ||
| if (!isIdentifierPart(word.charAt(0))) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| function findCurrentWord(start, end, buffer) { | ||
| // ensure that if caret is after last char of word, then we still see the word | ||
| start--; | ||
| while (start > 0 && isIdentifierPart(buffer.charAt(start))) { | ||
| start--; | ||
| } | ||
| if (start > 0 || !isIdentifierPart(buffer.charAt(start))) { | ||
| start++; | ||
| } | ||
| while (end < buffer.length && isIdentifierPart(buffer.charAt(end))) { | ||
| end++; | ||
| } | ||
| var word = buffer.substring(start, end); | ||
| if (isIdentifier(word)) { | ||
| return word; | ||
| } else { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| function findLocations(buffer, toFind) { | ||
| if (!toFind) { | ||
| return []; | ||
| } | ||
| var lastIndex = -1, | ||
| result = []; | ||
| while (true) { | ||
| lastIndex = buffer.indexOf(toFind, lastIndex + 1); | ||
|
|
||
| if (lastIndex === -1) { | ||
| break; | ||
| } | ||
|
|
||
| // must check the boundary of the word before choosing it | ||
| var prevChar = buffer.charAt(lastIndex - 1); | ||
| var nextChar = buffer.charAt(lastIndex + toFind.length); | ||
| if (!isIdentifierPart(prevChar) && !isIdentifierPart(nextChar)) { | ||
| result.push({ | ||
| start: lastIndex, | ||
| end: lastIndex + toFind.length, | ||
| title: "Occurrence of '" + toFind + "'" | ||
| }); | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| return { | ||
| markOccurrences: { | ||
| onModelChanging: function(e) { | ||
| _annotationProvider.modelChanged(); | ||
| }, | ||
| onSelection: function(e) { | ||
| if (e.text) { | ||
| var current = findCurrentWord(e.newValue.start, e.newValue.end, e.text); | ||
| if (current) { | ||
| console.log("Current selection: " + current); | ||
| } else { | ||
| console.log("Identifier is not selected"); | ||
| } | ||
| var ranges = findLocations(e.text, current); | ||
| _annotationProvider.pushAnnotations(ranges); | ||
| } | ||
| } | ||
| }, | ||
|
|
||
| annotationProvider : _annotationProvider | ||
| }; | ||
| }); |
32 changes: 32 additions & 0 deletions
32
...rg.eclipse.orion.client.core/web/plugins/markOccurrencesPlugin/markOccurrencesPlugin.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <meta name="copyright" content="Copyright (c) VMware Corporation and others 2012."> | ||
| <title>Esprima Content Assist</title> | ||
| <script type="text/javascript" src="http://orion.eclipse.org/orion/plugin.js"></script> | ||
| <script type="text/javascript" src="http://orion.eclipse.org/requirejs/require.js"></script> | ||
| <script type="text/javascript" src="markOccurrences.js"></script> | ||
| <script> | ||
| /*global eclipse require console */ | ||
| require(["markOccurrences"], function(markOccurrencesPlugin) { | ||
| var provider = new eclipse.PluginProvider(); | ||
| provider.registerServiceProvider("orion.edit.model", markOccurrencesPlugin.markOccurrences, | ||
| { | ||
| types: ["Selection", "ModelChanging"], | ||
| contentType: ["text.javascript"] | ||
| }); | ||
|
|
||
| var serviceProvider = | ||
| provider.registerServiceProvider("orion.edit.annotations", | ||
| markOccurrencesPlugin.annotationProvider, { }); | ||
|
|
||
| markOccurrencesPlugin.annotationProvider.setServiceProvider(serviceProvider); | ||
|
|
||
| provider.connect(); | ||
| }); | ||
| </script> | ||
| </head> | ||
| <body> | ||
| <p>A plugin that highlights all occurrences of the selected word in the current editor.</p> | ||
| </body> | ||
| </html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
bundles/org.eclipse.orion.client.editor/web/orion/textview/annotationService.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| /******************************************************************************* | ||
| * @license | ||
| * Copyright (c) 2012 VMware and others. | ||
| * All rights reserved. This program and the accompanying materials are made | ||
| * available under the terms of the Eclipse Public License v1.0 | ||
| * (http://www.eclipse.org/legal/epl-v10.html), and the Eclipse Distribution | ||
| * License v1.0 (http://www.eclipse.org/org/documents/edl-v10.html). | ||
| * | ||
| * Contributors: | ||
| * Andrew Eisenberg (VMware) - initial API and implementation | ||
| ******************************************************************************/ | ||
|
|
||
| /*global define */ | ||
|
|
||
| define('orion/textview/annotationservice', ['orion/textview/annotations', 'dojo'], function(mAnnotations, dojo) { | ||
| return { AnnotationService : function(serviceRegistry, editor) { | ||
| this._annotationModel = editor.getAnnotationModel(); | ||
| this._annotationTypes = {}; | ||
| this.registerAnnotationType = function(event) { | ||
| this._annotationTypes[event.type] = function() { | ||
| this.type = event.type; | ||
| this.title = event.title; | ||
| this.style = event.style; | ||
| this.html = event.html; | ||
| this.overviewStyle = event.overviewStyle; | ||
| this.rangeStyle = event.rangeStyle; | ||
| }; | ||
| editor.addAnnotationType(event.type); | ||
| }; | ||
|
|
||
| this.addAnnotations = function(event) { | ||
| var Annotation = this._annotationTypes[event.type]; | ||
| if (Annotation) { | ||
| this._annotationModel.removeAnnotations(event.type); | ||
| for (var i = 0; i < event.ranges.length; i++) { | ||
| var annotation = new Annotation(); | ||
| for (var prop in event.ranges[i]) { | ||
| if (event.ranges[i].hasOwnProperty(prop)) { | ||
| annotation[prop] = event.ranges[i][prop]; | ||
| } | ||
| } | ||
| this._annotationModel.addAnnotation(annotation); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| var annotationsService = serviceRegistry.getService("orion.edit.annotations"); | ||
| if (annotationsService) { | ||
| annotationsService.addEventListener("registerAnnotationType", dojo.hitch(this, "registerAnnotationType")); | ||
| annotationsService.addEventListener("addAnnotations", dojo.hitch(this, "addAnnotations")); | ||
| } | ||
| } }; | ||
| }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters