Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
. * Copyright (c) 2009 itemis AG (http://www.itemis.eu) 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 /
  
  package org.eclipse.xtext.ui.editor.syntaxcoloring;
  
 import java.util.List;
 import java.util.Map;
 
 import  org.eclipse.core.runtime.IProgressMonitor;
 import  org.eclipse.core.runtime.IStatus;
 import  org.eclipse.core.runtime.OperationCanceledException;
 import  org.eclipse.core.runtime.Status;
 import  org.eclipse.core.runtime.jobs.Job;
 import  org.eclipse.jface.text.IDocument;
 import  org.eclipse.jface.text.ITextInputListener;
 import  org.eclipse.jface.text.TextAttribute;
 import  org.eclipse.jface.text.TextPresentation;
 import  org.eclipse.swt.widgets.Display;
 import  org.eclipse.swt.widgets.Shell;
 import  org.eclipse.ui.IWorkbenchPartSite;
 
Highlighting reconciler - Background thread implementation. Initially copied from org.eclipse.jdt.internal.ui.javaeditor.SemanticHighlightingReconciler

Author(s):
Sebastian Zarnekow
 
 @SuppressWarnings("deprecation")
 
 	@Inject(optional=true)
 	
 	@Inject(optional=true)
 	private org.eclipse.xtext.ide.editor.syntaxcoloring.ISemanticHighlightingCalculator newCalculator;
 	
The Xtext editor this highlighting reconciler is installed on
 
 	private XtextEditor editor;
The source viewer this highlighting reconciler is installed on
 
The highlighting presenter
 
Background job's added highlighted positions
 
Background job's removed highlighted positions
 
 	
 	private static class PositionHandle {
 		private final int offset;
 		private final int length;
 		private final TextAttribute textAttribute;
 		private PositionHandle(int offsetint length, TextAttribute textAttribute) {
 			this. = offset;
 			this. = length;
 			this. = textAttribute;
 		}
 		public int hashCode() {
 			final int prime = 31;
 			int result = 1;
 			result = prime * result + ;
 			result = prime * result + ;
 			result = prime * result + (( == null) ? 0 : .hashCode());
 			return result;
 		}

See also:
AttributedPosition.isEqual(int, int, TextAttribute)
 
 		public boolean equals(Object obj) {
 			if (obj == null)
 				return false;
 			if (obj == this)
 				return true;
 			PositionHandle other = (PositionHandleobj;
			return  == other.offset &&  == other.length &&  == other.textAttribute;
		}
	}
Number of removed positions
	private int removedPositionCount;

Reconcile operation lock.

Since:
3.2
	private final Object fReconcileLock = new Object();
true if any thread is executing reconcile, false otherwise.

Since:
3.2
	private boolean reconciling = false;

Start reconciling positions.
	private void startReconcilingPositions() {
		for(int i = 0; i < i++) {
			.put(new PositionHandle(position.getOffset(), position.getLength(), position.getHighlighting()), i);
		}
	}

Reconcile positions using org.eclipse.xtext.ide.editor.syntaxcoloring.MergingHighlightedPositionAcceptor

Parameters:
resource XtextResource
	private void reconcilePositions(XtextResource resourceCancelIndicator cancelIndicator) {
		// A default binding was registered from ISemanticHighlightingCalculator to DefaultHighlightingCalculator
		// thus clienst may have bound DefaultHighlightingCalculator to their custom impl
		// use the custom impl if available, otherwise go for the new impl
			acceptor.provideHighlightingFor(resourcethis);
else if ( != null) {
			org.eclipse.xtext.ide.editor.syntaxcoloring.MergingHighlightedPositionAcceptor acceptor =
					new org.eclipse.xtext.ide.editor.syntaxcoloring.MergingHighlightedPositionAcceptor();
			acceptor.provideHighlightingFor(resourcethiscancelIndicator);
else {
			throw new IllegalStateException("No semantic highlighting calculator bound.");
		}
		for (int i = 0, n = oldPositions.size(); i < ni++) {
			AttributedPosition current = oldPositions.get(i);
			if (current != null)
				newPositions.add(current);
		}
		 = newPositions;
	}

Add a position with the given range and highlighting if it does not exist already.

Parameters:
offset The range offset
length The range length
ids The highlighting attribute ids
	public void addPosition(int offsetint lengthString... ids) {
		TextAttribute highlighting = ids.length == 1 ? 
		if (highlighting == null)
			return;
		boolean isExistingfalse;
		PositionHandle handle = new PositionHandle(offsetlengthhighlighting);
		Integer index = .remove(handle);
		if (index != null) {
			if (position == null) {
				throw new IllegalStateException("Position may not be null if the handle is still present.");
			}
			isExisting = true;
			.set(indexnull);
		}
		if (!isExisting &&  != null) { // in case we have been uninstalled due to exceptions
			AttributedPosition position.createHighlightedPosition(offsetlengthhighlighting);
		}
	}

Update the presentation.

Parameters:
textPresentation the text presentation
addedPositions the added positions
removedPositions the removed positions
resource the resource for which the positions have been computed
	private void updatePresentation(TextPresentation textPresentationList<AttributedPositionaddedPositions,
			List<AttributedPositionremovedPositionsXtextResource resource) {
		final Runnable runnable = .createUpdateRunnable(textPresentationaddedPositionsremovedPositions);
		if (runnable == null)
			return;
		final XtextResourceSet resourceSet = (XtextResourceSetresource.getResourceSet();
		final int modificationStamp = resourceSet.getModificationStamp();
		Display display = getDisplay();
		display.asyncExec(new Runnable() {
			public void run() {
				// never apply outdated highlighting
				if( != null	&& modificationStamp == resourceSet.getModificationStamp())
					runnable.run();
			}
		});
	}
	private Display getDisplay() {
		XtextEditor editor = this.;
		if (editor == null){
			if( != null)
				return .getControl().getDisplay();
			return null;
		}
		IWorkbenchPartSite site = editor.getSite();
		if (site == null)
			return null;
		Shell shell = site.getShell();
		if (shell == null || shell.isDisposed())
			return null;
		Display display = shell.getDisplay();
		if (display == null || display.isDisposed())
			return null;
		return display;
	}

Stop reconciling positions.
	private void stopReconcilingPositions() {
	}

Install this reconciler on the given editor and presenter.

Parameters:
editor the editor
sourceViewer the source viewer
presenter the highlighting presenter
	public void install(XtextEditor editorXtextSourceViewer sourceViewerHighlightingPresenter presenter) {
		this. = presenter;
		this. = editor;
		this. = sourceViewer;
		if ( != null ||  != null) {
			if(editor == null){
				((IXtextDocumentsourceViewer.getDocument()).addModelListener(this);
else if (editor.getDocument() != null)
			sourceViewer.addTextInputListener(this);
		}
	}

Uninstall this reconciler from the editor
	public void uninstall() {
		if ( != null)
		if (.getDocument() != null) {
			if ( != null ||  != null) {
				document.removeModelListener(this);
			}
		}
		 = null;
		 = null;
		 = null;
	}
	/*
	 * @see org.eclipse.jface.text.ITextInputListener#inputDocumentAboutToBeChanged(org.eclipse.jface.text.IDocument, org.eclipse.jface.text.IDocument)
	 */
	public void inputDocumentAboutToBeChanged(IDocument oldInput, IDocument newInput) {
		if (oldInput != null)
	}
	/*
	 * @see org.eclipse.jface.text.ITextInputListener#inputDocumentChanged(org.eclipse.jface.text.IDocument, org.eclipse.jface.text.IDocument)
	 */
	public void inputDocumentChanged(IDocument oldInput, IDocument newInput) {
		if (newInput != null) {
			((IXtextDocumentnewInput).addModelListener(this);
		}
	}

Refreshes the highlighting.
	public void refresh() {
		if ( != null ||  != null) {
			new Job("calculating highlighting") {
				protected IStatus run(IProgressMonitor monitor) {
						public java.lang.Void exec(XtextResource stateCancelIndicator cancelIndicator)
								throws Exception {
							beforeRefresh(statecancelIndicator);
							modelChanged(statecancelIndicator);
							return null;
						}
					});
					return Status.OK_STATUS;
				}
else {
			Display display = getDisplay();
		}
	}

Since:
2.8
	protected void beforeRefresh(XtextResource resourceCancelIndicator cancelIndicator) {
		if (resource instanceof DerivedStateAwareResource)
			resource.getContents(); // trigger derived state computation
		if (resource instanceof IBatchLinkableResource)
			((IBatchLinkableResourceresource).linkBatched(cancelIndicator);
	}
	public void modelChanged(XtextResource resource) {
	}

Since:
2.7
	public void modelChanged(XtextResource resourceCancelIndicator cancelIndicator) {
		if (resource == null) {
			return;
		}
		// ensure at most one thread can be reconciling at any time
		synchronized () {
				return;
			 = true;
		}
		final HighlightingPresenter highlightingPresenter = ;
		try {
			if (highlightingPresenter == null)
				return;
			highlightingPresenter.setCanceled(cancelIndicator.isCanceled());
			if (highlightingPresenter.isCanceled())		
				return;
			checkCanceled(cancelIndicator);
			if (highlightingPresenter.isCanceled())		
				return;
			checkCanceled(cancelIndicator);
			reconcilePositions(resourcecancelIndicator);
			if (highlightingPresenter.isCanceled())		
				return;
			checkCanceled(cancelIndicator);
			final TextPresentation textPresentation = highlightingPresenter.createPresentation();
			if (highlightingPresenter.isCanceled())		
				return;
			checkCanceled(cancelIndicator);
			updatePresentation(textPresentationresource);
finally {
			if (highlightingPresenter != null)
				highlightingPresenter.setCanceled(false);
			synchronized () {
				 = false;
			}
		}
	}

Since:
2.8
	protected void checkCanceled(CancelIndicator cancelIndicator) {
		if (cancelIndicator.isCanceled())
			throw new OperationCanceledException();
	}
	public void setCalculator(ISemanticHighlightingCalculator calculator) {
		this. = calculator;
	}
	}
New to GrepCode? Check out our FAQ X