Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2010 Michael Clay 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.folding;
 
 
 import  org.eclipse.core.runtime.AssertionFailedException;
 import  org.eclipse.core.runtime.OperationCanceledException;
 import  org.eclipse.jface.text.BadLocationException;
 import  org.eclipse.jface.text.BadPartitioningException;
 import  org.eclipse.jface.text.IDocumentExtension3;
 import  org.eclipse.jface.text.ITypedRegion;
 

Author(s):
Michael Clay - Initial contribution and API
Sebastian Zarnekow - Introduced FoldedRegion, use ILocationInFileProvider
 
 	
 	private static final Logger log = Logger.getLogger(DefaultFoldingRegionProvider.class);
 	
 	

Since:
2.4
 
 
 	public DefaultFoldingRegionProvider(ILocationInFileProvider locationInFileProvider) {
 		this. = locationInFileProvider;
 	}
 
 	}
 	
 	public Collection<FoldedPositiongetFoldingRegions(final IXtextDocument xtextDocument) {
 			public Collection<FoldedPositionexec(XtextResource xtextResourceCancelIndicator cancelIndicator)
 					throws Exception {
 				if (xtextResource == null)
 					return Collections.emptyList();
 				try {
 					DefaultFoldingRegionProvider.this. = cancelIndicator;
 					return doGetFoldingRegions(xtextDocumentxtextResource);
 				} finally {
 				}
 			}
 		});
 	}

Compute the folding region. This method is executed in a IUnitOfWork so it's save to use the given resource. However, since this is a blocking action when opening editors in e4, implementors should be careful to not resolve too many cross references. Users of the DerivedStateAwareResource should consider to access the resource via getParseResult().getRootAstElement() rather than getContents().get(0).
 
 	protected Collection<FoldedPositiondoGetFoldingRegions(IXtextDocument xtextDocumentXtextResource xtextResource) {
 		IFoldingRegionAcceptor<ITextRegionfoldingRegionAcceptor = createAcceptor(xtextDocumentresult);
 		computeObjectFolding(xtextResourcefoldingRegionAcceptor);
		computeCommentFolding(xtextDocumentfoldingRegionAcceptor);
		return result;
	}
		return new DefaultFoldingRegionAcceptor(xtextDocumentfoldedPositions);
	}
	protected void computeObjectFolding(XtextResource xtextResourceIFoldingRegionAcceptor<ITextRegionfoldingRegionAcceptor) {
		IParseResult parseResult = xtextResource.getParseResult();
		if(parseResult != null){
			EObject rootASTElement = parseResult.getRootASTElement();
			if(rootASTElement != null){
					throw new OperationCanceledException();
				if (isHandled(rootASTElement)) {
					computeObjectFolding(rootASTElementfoldingRegionAcceptor);
				}
				if (shouldProcessContent(rootASTElement)) {
					TreeIterator<EObjectallContents = rootASTElement.eAllContents();
					while (allContents.hasNext()) {
							throw new OperationCanceledException();
						EObject eObject = allContents.next();
						if (isHandled(eObject)) {
							computeObjectFolding(eObjectfoldingRegionAcceptor);
						}
						if (!shouldProcessContent(eObject)) {
							allContents.prune();
						}
					}
				}
			}
		}
	}
	}
	protected void computeObjectFolding(EObject eObjectIFoldingRegionAcceptor<ITextRegionfoldingRegionAcceptor) {
		computeObjectFolding(eObjectfoldingRegionAcceptorfalse);
	}

Since:
2.8
	protected void computeObjectFolding(EObject eObjectIFoldingRegionAcceptor<ITextRegionfoldingRegionAcceptorboolean initiallyFolded) {
		if (region != null) {
			if (significant == null)
				throw new NullPointerException("significant region may not be null");
			int offset = region.getOffset();
			((IFoldingRegionAcceptorExtension<ITextRegion>)foldingRegionAcceptor).accept(offsetregion.getLength(), initiallyFoldedsignificant);
		}
	}
	protected static final Pattern TEXT_PATTERN_IN_COMMENT = Pattern.compile("\\w");
	protected void computeCommentFolding(IXtextDocument xtextDocumentIFoldingRegionAcceptor<ITextRegionfoldingRegionAcceptor) {
		try {
			ITypedRegion[] typedRegions = xtextDocument.computePartitioning(
					IDocumentExtension3.DEFAULT_PARTITIONING, 0, xtextDocument.getLength(), false);
			for (ITypedRegion typedRegion : typedRegions) {
					throw new OperationCanceledException();
					computeCommentFolding(xtextDocumentfoldingRegionAcceptortypedRegionfalse);
				}
			}
catch (BadLocationException e) {
			.error(ee);
catch (BadPartitioningException e) {
			.error(ee);
catch (AssertionFailedException e) {
			// partioning failed
			.error(ee);
		}
	}

Since:
2.8
	protected void computeCommentFolding(IXtextDocument xtextDocumentIFoldingRegionAcceptor<ITextRegionfoldingRegionAcceptor, ITypedRegion typedRegionboolean initiallyFolded)
			throws BadLocationException {
		int offset = typedRegion.getOffset();
		int length = typedRegion.getLength();
		Matcher matcher = getTextPatternInComment().matcher(xtextDocument.get(offsetlength));
		if (matcher.find()) {
			TextRegion significant = new TextRegion(offset + matcher.start(), 0);
			((IFoldingRegionAcceptorExtension<ITextRegion>)foldingRegionAcceptor).accept(offsetlengthinitiallyFoldedsignificant);
else {
			((IFoldingRegionAcceptorExtension<ITextRegion>)foldingRegionAcceptor).accept(offsetlengthinitiallyFolded);
		}
	}

Returns:
the regular expression that finds the first significant part of a multi line comment.
	}

Returns:
true if the object should be folded if it spans more than one line. Default is false if and only if the object is the root object of the resource.
	protected boolean isHandled(EObject eObject) {
		return eObject.eContainer() != null;
	}

Returns:
clients should false to abort the traversal of the model.
	protected boolean shouldProcessContent(EObject object) {
		return true;
	}
New to GrepCode? Check out our FAQ X