diff options
author | Lucas Bullen | 2017-07-12 18:08:58 +0000 |
---|---|---|
committer | Lucas Bullen | 2017-07-20 14:41:35 +0000 |
commit | 798ba99e71d97835741ac91d068afc2130c66bcc (patch) | |
tree | d69af12998dbb3279b999d63c71c05432c0acc45 /org.eclipse.ui.genericeditor.examples | |
parent | 8773dabab911ce83b5a7a4d9a5661e932a3fea4d (diff) | |
download | eclipse.platform.text-798ba99e71d97835741ac91d068afc2130c66bcc.tar.gz eclipse.platform.text-798ba99e71d97835741ac91d068afc2130c66bcc.tar.xz eclipse.platform.text-798ba99e71d97835741ac91d068afc2130c66bcc.zip |
Bug 508829 - [Generic Editor] provide a way to customize reconcilerI20170723-2000I20170722-2000I20170722-0045I20170721-2000I20170720-2000
- Extension point for reconcilers added
- Example of use with folding
Change-Id: I966e0fa4e5711fb85aa792361184fd7ddbcf0620
Signed-off-by: Lucas Bullen <lbullen@redhat.com>
Diffstat (limited to 'org.eclipse.ui.genericeditor.examples')
4 files changed, 192 insertions, 2 deletions
diff --git a/org.eclipse.ui.genericeditor.examples/plugin.xml b/org.eclipse.ui.genericeditor.examples/plugin.xml index 60f20e05bd5..2ce9bebe1ef 100644 --- a/org.eclipse.ui.genericeditor.examples/plugin.xml +++ b/org.eclipse.ui.genericeditor.examples/plugin.xml @@ -1,7 +1,7 @@ <?xml version="1.0" encoding="UTF-8"?> <?eclipse version="3.4"?> <!-- ====================================================================== --> -<!-- Copyright (c) 2016 Red Hat Inc. and others. --> +<!-- Copyright (c) 2016, 2017 Red Hat Inc. and others. --> <!-- All rights reserved. This program and the accompanying materials --> <!-- are made available under the terms of the Eclipse Public License v1.0 --> <!-- which accompanies this distribution, and is available at --> @@ -9,6 +9,7 @@ <!-- --> <!-- Contributors: --> <!-- Sopot Cela & Mickael Istria (Red Hat Inc). -initial implementation --> +<!-- Lucas Bullen (Red Hat Inc.) - Bug 508829 custom reconciler support --> <!-- ====================================================================== --> <plugin> <extension @@ -44,6 +45,13 @@ </presentationReconciler> </extension> <extension + point="org.eclipse.ui.genericeditor.reconcilers"> + <reconciler + class="org.eclipse.ui.genericeditor.examples.dotproject.FoldingReconciler" + contentType="org.eclipse.ui.genericeditor.examples.dotproject"> + </reconciler> + </extension> + <extension point="org.eclipse.ui.editors"> <editorContentTypeBinding contentTypeId="org.eclipse.ui.genericeditor.examples.dotproject" diff --git a/org.eclipse.ui.genericeditor.examples/src/org/eclipse/ui/genericeditor/examples/dotproject/FoldingReconciler.java b/org.eclipse.ui.genericeditor.examples/src/org/eclipse/ui/genericeditor/examples/dotproject/FoldingReconciler.java new file mode 100644 index 00000000000..1db68ec8d59 --- /dev/null +++ b/org.eclipse.ui.genericeditor.examples/src/org/eclipse/ui/genericeditor/examples/dotproject/FoldingReconciler.java @@ -0,0 +1,33 @@ +/******************************************************************************* + * Copyright (c) 2017 Red Hat Inc. and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Lucas Bullen (Red Hat Inc.) - initial implementation + *******************************************************************************/ +package org.eclipse.ui.genericeditor.examples.dotproject; + +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.ITextViewer; +import org.eclipse.jface.text.reconciler.Reconciler; +import org.eclipse.jface.text.source.projection.ProjectionViewer; + +public class FoldingReconciler extends Reconciler { + + private FoldingStrategy fStrategy; + + public FoldingReconciler() { + fStrategy = new FoldingStrategy(); + this.setReconcilingStrategy(fStrategy, IDocument.DEFAULT_CONTENT_TYPE); + } + + @Override + public void install(ITextViewer textViewer) { + super.install(textViewer); + ProjectionViewer pViewer =(ProjectionViewer)textViewer; + fStrategy.setProjectionViewer(pViewer); + } +}
\ No newline at end of file diff --git a/org.eclipse.ui.genericeditor.examples/src/org/eclipse/ui/genericeditor/examples/dotproject/FoldingStrategy.java b/org.eclipse.ui.genericeditor.examples/src/org/eclipse/ui/genericeditor/examples/dotproject/FoldingStrategy.java new file mode 100644 index 00000000000..42c36be84bc --- /dev/null +++ b/org.eclipse.ui.genericeditor.examples/src/org/eclipse/ui/genericeditor/examples/dotproject/FoldingStrategy.java @@ -0,0 +1,143 @@ +/******************************************************************************* + * Copyright (c) 2017 Red Hat Inc. and others + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Lucas Bullen (Red Hat Inc.) - initial implementation + *******************************************************************************/ +package org.eclipse.ui.genericeditor.examples.dotproject; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.IRegion; +import org.eclipse.jface.text.Position; +import org.eclipse.jface.text.reconciler.DirtyRegion; +import org.eclipse.jface.text.reconciler.IReconcilingStrategy; +import org.eclipse.jface.text.reconciler.IReconcilingStrategyExtension; +import org.eclipse.jface.text.source.Annotation; +import org.eclipse.jface.text.source.projection.ProjectionAnnotation; +import org.eclipse.jface.text.source.projection.ProjectionViewer; + +public class FoldingStrategy implements IReconcilingStrategy, IReconcilingStrategyExtension { + + private IDocument document; + private ProjectionViewer projectionViewer; + private Annotation[] oldAnnotations; + + @Override + public void setDocument(IDocument document) { + this.document = document; + } + + public void setProjectionViewer(ProjectionViewer projectionViewer) { + this.projectionViewer = projectionViewer; + } + + @Override + public void reconcile(DirtyRegion dirtyRegion, IRegion subRegion) { + initialReconcile(); + } + + @Override + public void reconcile(IRegion partition) { + initialReconcile(); + } + + @Override + public void initialReconcile() { + List<Position> positions = getNewPositionsOfAnnotations(); + Annotation[] annotations = new Annotation[positions.size()]; + + HashMap<Annotation, Position> newAnnotations = new HashMap<>(); + + for(int i =0;i<positions.size();i++) + { + ProjectionAnnotation annotation = new ProjectionAnnotation(); + newAnnotations.put(annotation,positions.get(i)); + annotations[i]=annotation; + } + + projectionViewer.getProjectionAnnotationModel().modifyAnnotations(oldAnnotations,newAnnotations,null); + oldAnnotations=annotations; + } + + private static enum SearchingFor { + START_OF_TAG, START_OF_WORD, END_OF_WORD, END_OF_LINE + } + + private List<Position> getNewPositionsOfAnnotations(){ + List<Position> positions = new ArrayList<>(); + Map<String, Integer> startOfAnnotation = new HashMap<>(); + SearchingFor searchingFor = SearchingFor.START_OF_TAG; + + int characters = document.getLength(); + int currentCharIndex = 0; + + int wordStartIndex = 0; + int sectionStartIndex = 0; + String word = ""; + + try { + while (currentCharIndex < characters) { + char currentChar = document.getChar(currentCharIndex); + switch (searchingFor) { + case START_OF_TAG: + if(currentChar == '<') { + char nextChar = document.getChar(currentCharIndex+1); + if(nextChar != '?') { + sectionStartIndex = currentCharIndex; + searchingFor = SearchingFor.START_OF_WORD; + } + } + break; + case START_OF_WORD: + if(Character.isLetter(currentChar)) { + wordStartIndex = currentCharIndex; + searchingFor = SearchingFor.END_OF_WORD; + } + break; + case END_OF_WORD: + if(!Character.isLetter(currentChar)) { + word = document.get(wordStartIndex, currentCharIndex - wordStartIndex); + if(startOfAnnotation.containsKey(word)) { + searchingFor = SearchingFor.END_OF_LINE; + }else { + startOfAnnotation.put(word, sectionStartIndex); + searchingFor = SearchingFor.START_OF_TAG; + } + } + break; + case END_OF_LINE: + if(currentChar == '\n') { + int start = startOfAnnotation.get(word); + if(document.getLineOfOffset(start) != document.getLineOfOffset(currentCharIndex)) { + positions.add(new Position(start,currentCharIndex + 1 - start)); + } + startOfAnnotation.remove(word); + searchingFor = SearchingFor.START_OF_TAG; + } + break; + } + currentCharIndex++; + } + } catch (BadLocationException e) { + // skip the remainder of file due to error + } + return positions; + } + + @Override + public void setProgressMonitor(IProgressMonitor monitor) { + // no progress monitor used + } + +}
\ No newline at end of file diff --git a/org.eclipse.ui.genericeditor.examples/src/org/eclipse/ui/genericeditor/examples/dotproject/SpellCheckDocumentListener.java b/org.eclipse.ui.genericeditor.examples/src/org/eclipse/ui/genericeditor/examples/dotproject/SpellCheckDocumentListener.java index 9ea533fef7d..9b247fd9c75 100644 --- a/org.eclipse.ui.genericeditor.examples/src/org/eclipse/ui/genericeditor/examples/dotproject/SpellCheckDocumentListener.java +++ b/org.eclipse.ui.genericeditor.examples/src/org/eclipse/ui/genericeditor/examples/dotproject/SpellCheckDocumentListener.java @@ -7,6 +7,7 @@ * * Contributors: * - Mickael Istria (Red Hat Inc.) + * - Lucas Bullen (Red Hat Inc.) - avoid NPE for when TextFileBuffer does not exist *******************************************************************************/ package org.eclipse.ui.genericeditor.examples.dotproject; @@ -15,6 +16,7 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; +import org.eclipse.core.filebuffers.ITextFileBuffer; import org.eclipse.core.filebuffers.ITextFileBufferManager; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; @@ -50,7 +52,11 @@ public class SpellCheckDocumentListener implements IDocumentListener { this.lastJob = new Job("Spellcheck") { @Override protected IStatus run(IProgressMonitor monitor) { - IAnnotationModel model = ITextFileBufferManager.DEFAULT.getTextFileBuffer(event.getDocument()).getAnnotationModel(); + ITextFileBuffer iTextFileBuffer = ITextFileBufferManager.DEFAULT.getTextFileBuffer(event.getDocument()); + if(iTextFileBuffer == null) { + return Status.CANCEL_STATUS; + } + IAnnotationModel model = iTextFileBuffer.getAnnotationModel(); String text = event.getDocument().get(); int commentStart = text.indexOf("<comment>"); if (commentStart < 0) { |