use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project xtext-eclipse by eclipse.
the class ContentFormatter method format.
@Override
public void format(IDocument document, IRegion region) {
IXtextDocument doc = xtextDocumentUtil.getXtextDocument(document);
TextEdit r = doc.priorityReadOnly(new FormattingUnitOfWork(doc, region));
try {
if (r != null)
r.apply(document);
} catch (BadLocationException e) {
throw new RuntimeException(e);
}
}
use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project xtext-eclipse by eclipse.
the class HighlightingReconciler method uninstall.
/**
* Uninstall this reconciler from the editor
*/
public void uninstall() {
if (presenter != null)
presenter.setCanceled(true);
if (sourceViewer.getDocument() != null) {
if (oldCalculator != null || newCalculator != null) {
IXtextDocument document = xtextDocumentUtil.getXtextDocument(sourceViewer);
if (document != null) {
document.removeModelListener(this);
}
sourceViewer.removeTextInputListener(this);
}
}
editor = null;
sourceViewer = null;
presenter = null;
}
use of org.eclipse.xtext.ui.editor.model.IXtextDocument 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.tryReadOnly(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.ui.editor.model.IXtextDocument in project xtext-eclipse by eclipse.
the class XtextReconciler method uninstall.
@Override
public void uninstall() {
if (isInstalled) {
textViewer.removeTextInputListener(textInputListener);
isInstalled = false;
if (documentListener != null) {
if (textViewer instanceof ISourceViewerExtension4) {
ContentAssistantFacade facade = ((ISourceViewerExtension4) textViewer).getContentAssistantFacade();
facade.removeCompletionListener(documentListener);
}
if (textViewer.getDocument() instanceof IXtextDocument) {
((IXtextDocument) textViewer.getDocument()).removeXtextDocumentContentObserver(documentListener);
}
}
cancel();
}
}
use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project xtext-eclipse by eclipse.
the class XtextReconciler method run.
@Override
protected IStatus run(final IProgressMonitor monitor) {
if (monitor.isCanceled() || paused)
return Status.CANCEL_STATUS;
if (pendingChanges.isEmpty()) {
return Status.OK_STATUS;
}
long start = System.currentTimeMillis();
final IXtextDocument document = xtextDocumentUtil.getXtextDocument(textViewer);
if (document instanceof XtextDocument) {
((XtextDocument) document).internalModify(new IUnitOfWork.Void<XtextResource>() {
@Override
public void process(XtextResource state) throws Exception {
doRun(state, monitor);
}
});
}
if (monitor.isCanceled()) {
return Status.CANCEL_STATUS;
}
if (log.isDebugEnabled())
// $NON-NLS-1$
log.debug("Reconciliation finished. Time required: " + (System.currentTimeMillis() - start));
return Status.OK_STATUS;
}
Aggregations