use of org.eclipse.xtext.nodemodel.ICompositeNode in project xtext-eclipse by eclipse.
the class XtextReconcilerDebugger method assertModelInSyncWithDocument.
public void assertModelInSyncWithDocument(IDocument document, XtextResource resource, final ReconcilerReplaceRegion region) {
if (document instanceof IDocumentExtension4 && resource != null) {
long beforeGet = ((IDocumentExtension4) document).getModificationStamp();
final String documentContent = document.get();
long afterGet = ((IDocumentExtension4) document).getModificationStamp();
if (beforeGet == afterGet && beforeGet == resource.getModificationStamp()) {
IParseResult parseResult = resource.getParseResult();
if (parseResult != null) {
ICompositeNode rootNode = parseResult.getRootNode();
final String resourceContent = rootNode.getText();
if (!resourceContent.equals(documentContent)) {
new DisplayRunnable() {
@Override
protected void run() throws Exception {
LOG.error("XtextDocument and XtextResource have run out of sync:\n" + DiffUtil.diff(documentContent, resourceContent));
LOG.error("Events: \n\t" + Joiner.on("\n\t").join(region.getDocumentEvents()));
LOG.error("ReplaceRegion: \n\t'" + region + "'");
MessageDialog.openError(Display.getCurrent().getActiveShell(), "XtextReconcilerDebugger", "XtextDocument and XtextResource have run out of sync." + "\n\nSee log for details.");
}
}.syncExec();
} else {
if (LOG.isDebugEnabled())
LOG.debug("Model and document are in sync");
}
}
}
}
}
use of org.eclipse.xtext.nodemodel.ICompositeNode in project xtext-eclipse by eclipse.
the class OutlineNodeFactory method createEObjectNode.
public EObjectNode createEObjectNode(IOutlineNode parentNode, EObject modelElement, ImageDescriptor imageDescriptor, Object text, boolean isLeaf) {
EObjectNode eObjectNode = new EObjectNode(modelElement, parentNode, imageDescriptor, text, isLeaf);
ICompositeNode parserNode = NodeModelUtils.getNode(modelElement);
if (parserNode != null)
eObjectNode.setTextRegion(parserNode.getTextRegion());
if (isLocalElement(parentNode, modelElement))
eObjectNode.setShortTextRegion(locationInFileProvider.getSignificantTextRegion(modelElement));
return eObjectNode;
}
use of org.eclipse.xtext.nodemodel.ICompositeNode in project xtext-eclipse by eclipse.
the class DefaultQuickfixProvider method getUnresolvedEReference.
protected EReference getUnresolvedEReference(final Issue issue, EObject target) {
final ICompositeNode node = NodeModelUtils.getNode(target);
if (node == null)
return null;
ICompositeNode rootNode = node.getRootNode();
ILeafNode leaf = NodeModelUtils.findLeafNodeAtOffset(rootNode, issue.getOffset());
CrossReference crossReference = findCrossReference(target, leaf);
if (crossReference != null) {
return GrammarUtil.getReference(crossReference, target.eClass());
}
return null;
}
use of org.eclipse.xtext.nodemodel.ICompositeNode in project xtext-eclipse by eclipse.
the class DefaultQuickfixProvider method createLinkingIssueResolutions.
public void createLinkingIssueResolutions(final Issue issue, final IssueResolutionAcceptor issueResolutionAcceptor) {
final IModificationContext modificationContext = modificationContextFactory.createModificationContext(issue);
final IXtextDocument xtextDocument = modificationContext.getXtextDocument();
if (xtextDocument == null)
return;
xtextDocument.readOnly(new CancelableUnitOfWork<Void, XtextResource>() {
IssueResolutionAcceptor myAcceptor = null;
@Override
public java.lang.Void exec(XtextResource state, CancelIndicator cancelIndicator) throws Exception {
myAcceptor = getCancelableAcceptor(issueResolutionAcceptor, cancelIndicator);
EObject target = state.getEObject(issue.getUriToProblem().fragment());
EReference reference = getUnresolvedEReference(issue, target);
if (reference == null)
return null;
fixUnresolvedReference(issue, xtextDocument, target, reference);
return null;
}
protected void fixUnresolvedReference(final Issue issue, final IXtextDocument xtextDocument, EObject target, EReference reference) throws BadLocationException {
boolean caseInsensitive = caseInsensitivityHelper.isIgnoreCase(reference);
EObject crossReferenceTerminal = getCrossReference(issue, target);
String ruleName = null;
Keyword keyword = null;
if (crossReferenceTerminal instanceof RuleCall) {
RuleCall ruleCall = (RuleCall) crossReferenceTerminal;
ruleName = ruleCall.getRule().getName();
} else if (crossReferenceTerminal instanceof Keyword) {
keyword = (Keyword) crossReferenceTerminal;
}
String issueString = xtextDocument.get(issue.getOffset(), issue.getLength());
IScope scope = scopeProvider.getScope(target, reference);
List<IEObjectDescription> discardedDescriptions = Lists.newArrayList();
Set<String> qualifiedNames = Sets.newHashSet();
int addedDescriptions = 0;
int checkedDescriptions = 0;
for (IEObjectDescription referableElement : queryScope(scope)) {
String referableElementQualifiedName = qualifiedNameConverter.toString(referableElement.getQualifiedName());
if (similarityMatcher.isSimilar(issueString, qualifiedNameConverter.toString(referableElement.getName()))) {
addedDescriptions++;
createResolution(issueString, referableElement, ruleName, keyword, caseInsensitive);
qualifiedNames.add(referableElementQualifiedName);
} else {
if (qualifiedNames.add(referableElementQualifiedName))
discardedDescriptions.add(referableElement);
}
checkedDescriptions++;
if (checkedDescriptions > 100)
break;
}
if (discardedDescriptions.size() + addedDescriptions <= 5) {
for (IEObjectDescription referableElement : discardedDescriptions) {
createResolution(issueString, referableElement, ruleName, keyword, caseInsensitive);
}
}
}
protected AbstractElement getCrossReference(final Issue issue, EObject 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(rootNode, issue.getOffset());
CrossReference crossReference = findCrossReference(target, leaf);
return crossReference.getTerminal();
}
public void createResolution(String issueString, IEObjectDescription solution, String ruleName, Keyword keyword, boolean caseInsensitive) {
String replacement = qualifiedNameConverter.toString(solution.getName());
String replaceLabel = fixCrossReferenceLabel(issueString, replacement);
if (keyword != null) {
if (caseInsensitive && !replacement.equalsIgnoreCase(keyword.getValue()))
return;
if (!caseInsensitive && !replacement.equals(keyword.getValue()))
return;
} else if (ruleName != null) {
replacement = converter.convertToString(replacement, ruleName);
if (replacement == null) {
return;
}
} else {
logger.error("either keyword or ruleName have to present", new IllegalStateException());
}
myAcceptor.accept(issue, replaceLabel, replaceLabel, fixCrossReferenceImage(issueString, replacement), new ReplaceModification(issue, replacement));
}
});
}
use of org.eclipse.xtext.nodemodel.ICompositeNode in project xtext-eclipse by eclipse.
the class DefaultOutlineTreeProvider method createEObjectNode.
/**
* @since 2.1
*/
protected EObjectNode createEObjectNode(IOutlineNode parentNode, EObject modelElement, Image image, Object text, boolean isLeaf) {
EObjectNode eObjectNode = new EObjectNode(modelElement, parentNode, image, text, isLeaf);
ICompositeNode parserNode = NodeModelUtils.getNode(modelElement);
if (parserNode != null)
eObjectNode.setTextRegion(parserNode.getTextRegion());
if (isLocalElement(parentNode, modelElement))
eObjectNode.setShortTextRegion(locationInFileProvider.getSignificantTextRegion(modelElement));
return eObjectNode;
}
Aggregations