Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2010 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.quickfix;
  
 import java.util.List;
 import java.util.Set;
 
 import  org.eclipse.jface.text.BadLocationException;
 

Author(s):
Heiko Behrens - Initial contribution and API
Jan Koehnlein
 
 
 	private static final Logger logger = Logger.getLogger(DefaultQuickfixProvider.class);
 	
 
 
 
 	
 	
 	
 	
Can be sub classed to supress usage of value converter for e.g. operators.

Since:
2.4
 
 	public static class CrossRefResolutionConverter {
 
 		public String convertToString(String replacementString ruleName) {
 			try {
 				return .toString(replacementruleName);
 			} catch (ValueConverterException e) {
 				return null;
 			}
		}
	}
	private CrossReference findCrossReference(EObject contextINode node) {
		if (node == null || (node.hasDirectSemanticElement() && context.equals(node.getSemanticElement())))
			return null;
		EObject grammarElement = node.getGrammarElement();
		if (grammarElement instanceof CrossReference) {
			return (CrossReferencegrammarElement;
else
			return findCrossReference(contextnode.getParent());
	}
		final IssueResolutionAcceptor issueResolutionAcceptor = .get();
		createLinkingIssueResolutions(issueissueResolutionAcceptor);
		return issueResolutionAcceptor.getIssueResolutions();
	}
	public void createLinkingIssueResolutions(final Issue issuefinal IssueResolutionAcceptor issueResolutionAcceptor) {
		final IXtextDocument xtextDocument = modificationContext.getXtextDocument();
		if (xtextDocument == null)
			return;
			public java.lang.Void exec(XtextResource stateCancelIndicator cancelIndicatorthrows Exception {
				 = getCancelableAcceptor(issueResolutionAcceptorcancelIndicator);
				EObject target = state.getEObject(issue.getUriToProblem().fragment());
				EReference reference = getUnresolvedEReference(issuetarget);
				if (reference == null)
					return null;
				fixUnresolvedReference(issuextextDocumenttargetreference);
				return null;
			}
			protected void fixUnresolvedReference(final Issue issuefinal IXtextDocument xtextDocument,
					EObject targetEReference referencethrows BadLocationException {
				boolean caseInsensitive = .isIgnoreCase(reference);
				EObject crossReferenceTerminal = getCrossReference(issuetarget);
				String ruleName = null;
				Keyword keyword = null;
				if (crossReferenceTerminal instanceof RuleCall) {
					RuleCall ruleCall = (RuleCallcrossReferenceTerminal;
					ruleName = ruleCall.getRule().getName();
else if (crossReferenceTerminal instanceof Keyword) {
					keyword = (KeywordcrossReferenceTerminal;
				}
				String issueString = xtextDocument.get(issue.getOffset(), issue.getLength());
				IScope scope = .getScope(targetreference);
				List<IEObjectDescriptiondiscardedDescriptions = Lists.newArrayList();
				Set<StringqualifiedNames = Sets.newHashSet();
				int addedDescriptions = 0;
				int checkedDescriptions = 0;
				for (IEObjectDescription referableElement : queryScope(scope)) {
					String referableElementQualifiedName = .toString(referableElement.getQualifiedName());
					if (.isSimilar(issueString.toString(referableElement.getName()))) {
						addedDescriptions++;
						createResolution(issueStringreferableElementruleNamekeywordcaseInsensitive);
						qualifiedNames.add(referableElementQualifiedName);
else {
						if (qualifiedNames.add(referableElementQualifiedName))
							discardedDescriptions.add(referableElement);
					}
					checkedDescriptions++;
					if (checkedDescriptions>100)
						break;
				}
				if (discardedDescriptions.size() + addedDescriptions <= 5) {
					for(IEObjectDescription referableElementdiscardedDescriptions) {
						createResolution(issueStringreferableElementruleNamekeywordcaseInsensitive);
					}
				}
			}
			protected AbstractElement getCrossReference(final Issue issueEObject target) {
				final ICompositeNode node = NodeModelUtils.getNode(target);
				if (node == null)
					throw new IllegalStateException("Cannot happen since we found a reference");
				ICompositeNode rootNode = node.getRootNode();
				ILeafNode leaf = NodeModelUtils.findLeafNodeAtOffset(rootNodeissue.getOffset());
				CrossReference crossReference = findCrossReference(targetleaf);
				return crossReference.getTerminal();
			}
			public void createResolution(String issueStringIEObjectDescription solutionString ruleNameKeyword keywordboolean caseInsensitive) {
				String replacement = .toString(solution.getName());
				String replaceLabel = fixCrossReferenceLabel(issueStringreplacement);
				if (keyword != null) {
					if (caseInsensitive && !replacement.equalsIgnoreCase(keyword.getValue()))
						return;
					if (!caseInsensitive && !replacement.equals(keyword.getValue()))
						return;
else if (ruleName != null) {
					replacement = .convertToString(replacementruleName);
					if (replacement == null) {
						return;
					}
else {
					.error("either keyword or ruleName have to present"new IllegalStateException());
				}
				.accept(issuereplaceLabelreplaceLabelfixCrossReferenceImage(
						issueStringreplacement), new ReplaceModification(issuereplacement));
			}
		});
	}
		return scope.getAllElements();
	}
	protected EReference getUnresolvedEReference(final Issue issueEObject target) {
		final ICompositeNode node = NodeModelUtils.getNode(target);
		if (node==null)
			return null;
		ICompositeNode rootNode = node.getRootNode();
		ILeafNode leaf = NodeModelUtils.findLeafNodeAtOffset(rootNodeissue.getOffset());
		CrossReference crossReference = findCrossReference(targetleaf);
		if (crossReference != null) {
			return  GrammarUtil.getReference(crossReferencetarget.eClass());
		}
		return null;
	}
	protected String fixCrossReferenceLabel(String issueStringString replacement) {
	}
	protected String fixCrossReferenceImage(String issueStringString replacement) {
		return ""//$NON-NLS-1$
	}
		StopWatch stopWatch = new StopWatch();
		try {
				result.addAll(super.getResolutions(issue));
				return result;
else
				return super.getResolutions(issue);
finally {
			stopWatch.resetAndLog("#getResolutions");			
		}
	}
	public boolean hasResolutionFor(String issueCode) {
		return ..equals(issueCode) || super.hasResolutionFor(issueCode);
	}

Since:
2.0
	}

Since:
2.0
	}

Since:
2.0
	}

Since:
2.0
	}

Since:
2.8
	}

Since:
2.8
			final IssueResolutionAcceptor issueResolutionAcceptorCancelIndicator cancelIndicator) {
		if (getCancelManager() != null) {
			return new CancelableResolutionAcceptor(issueResolutionAcceptorcancelIndicatorgetCancelManager());
		}
		return issueResolutionAcceptor;
	}

Since:
2.8
	protected static class CancelableResolutionAcceptor extends IssueResolutionAcceptor {
			super(null);
			this. = delegate;
			this. = monitor;
			this. = manager;
		}
		public void accept(Issue issueString labelString descriptionString imageIModification modification) {
			.accept(issuelabeldescriptionimagemodification);
		}
		public void accept(Issue issueString labelString descriptionString image,
				ISemanticModification semanticModification) {
			.accept(issuelabeldescriptionimagesemanticModification);
		}
		public void accept(Issue issueString labelString descriptionString imageIModification modification,
				int relevance) {
			.accept(issuelabeldescriptionimagemodificationrelevance);
		}
		public void accept(Issue issueString labelString descriptionString image,
				ISemanticModification semanticModificationint relevance) {
			.accept(issuelabeldescriptionimagesemanticModificationrelevance);
		}
		}
	}
New to GrepCode? Check out our FAQ X