use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project xtext-eclipse by eclipse.
the class XtextReconciler method handleInputDocumentChanged.
protected void handleInputDocumentChanged(IDocument oldInput, IDocument newInput) {
if (Display.getCurrent() == null) {
log.error("Changes to the document must only be applied from the Display thread to keep them ordered", new Exception());
}
if (shouldInstallCompletionListener) {
ContentAssistantFacade facade = ((ISourceViewerExtension4) textViewer).getContentAssistantFacade();
if (facade != null) {
facade.addCompletionListener(documentListener);
}
shouldInstallCompletionListener = false;
}
if (oldInput != newInput) {
if (oldInput instanceof IXtextDocument) {
((IXtextDocument) oldInput).removeXtextDocumentContentObserver(documentListener);
}
if (newInput instanceof IXtextDocument) {
((IXtextDocument) newInput).addXtextDocumentContentObserver(documentListener);
final IXtextDocument document = xtextDocumentUtil.getXtextDocument(textViewer);
strategy.setDocument(document);
if (!initalProcessDone && strategy instanceof IReconcilingStrategyExtension) {
initalProcessDone = true;
IReconcilingStrategyExtension reconcilingStrategyExtension = (IReconcilingStrategyExtension) strategy;
reconcilingStrategyExtension.initialReconcile();
}
}
}
if (oldInput != null && newInput != null) {
handleDocumentChanged(new InputChangedDocumentEvent(oldInput, newInput));
}
}
use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project xtext-eclipse by eclipse.
the class ImportsAwareClipboardAction method getXtextDocument.
private IXtextDocument getXtextDocument() {
XtextEditor xtextEditor = EditorUtils.getXtextEditor(getTextEditor());
IXtextDocument document = xtextEditor.getDocument();
return document;
}
use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project xtext-eclipse by eclipse.
the class XbaseQuickfixProvider method moveUp.
protected void moveUp(ICompositeNode node, ICompositeNode previousNode, IModificationContext context) throws BadLocationException {
IXtextDocument document = context.getXtextDocument();
String text = node.getText() + previousNode.getText();
text = text.trim();
remove(document, node);
document.replace(previousNode.getOffset(), previousNode.getLength(), text);
}
use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project xtext-eclipse by eclipse.
the class XbaseQuickfixProvider method fixUnreachableIfBlock.
@Fix(IssueCodes.UNREACHABLE_IF_BLOCK)
public void fixUnreachableIfBlock(final Issue issue, IssueResolutionAcceptor acceptor) {
acceptor.accept(issue, "Remove if block", "Remove if block", null, new ISemanticModification() {
@Override
public void apply(EObject element, IModificationContext context) throws Exception {
XIfExpression ifExpression = EcoreUtil2.getContainerOfType(element, XIfExpression.class);
if (ifExpression == null) {
return;
}
ICompositeNode node = NodeModelUtils.findActualNodeFor(ifExpression);
if (node == null) {
return;
}
int[] offsetAndLength = getOffsetAndLength(ifExpression, node);
context.getXtextDocument().replace(offsetAndLength[0], offsetAndLength[1], "");
}
});
acceptor.accept(issue, "Move if block up", "Move if block up", null, new ISemanticModification() {
@Override
public void apply(EObject element, IModificationContext context) throws Exception {
XIfExpression ifExpression = EcoreUtil2.getContainerOfType(element, XIfExpression.class);
if (ifExpression == null) {
return;
}
ICompositeNode node = NodeModelUtils.findActualNodeFor(ifExpression);
if (node == null) {
return;
}
XIfExpression firstIfExpression = getFirstIfExpression(ifExpression);
if (firstIfExpression == null) {
return;
}
XInstanceOfExpression actualIfPart = (XInstanceOfExpression) ifExpression.getIf();
XAbstractFeatureCall actualFeatureCall = (XAbstractFeatureCall) actualIfPart.getExpression();
JvmIdentifiableElement actualFeature = actualFeatureCall.getFeature();
ITypeReferenceOwner owner = new StandardTypeReferenceOwner(services, firstIfExpression);
LightweightTypeReference actualTypeReference = owner.toLightweightTypeReference(actualIfPart.getType());
List<XExpression> ifParts = collectIfParts(firstIfExpression, new ArrayList<XExpression>());
for (XExpression previousIfPart : ifParts) {
if (actualIfPart == previousIfPart) {
return;
}
if (!(previousIfPart instanceof XInstanceOfExpression)) {
continue;
}
XInstanceOfExpression instanceOfExpression = (XInstanceOfExpression) previousIfPart;
if (!(instanceOfExpression.getExpression() instanceof XAbstractFeatureCall)) {
continue;
}
XAbstractFeatureCall previousFeatureCall = (XAbstractFeatureCall) instanceOfExpression.getExpression();
if (previousFeatureCall.getFeature() != actualFeature) {
continue;
}
LightweightTypeReference previousTypeReference = owner.toLightweightTypeReference(instanceOfExpression.getType());
if (typesOrderUtil.isHandled(actualTypeReference, previousTypeReference)) {
ICompositeNode previousNode = NodeModelUtils.findActualNodeFor(instanceOfExpression.eContainer());
if (previousNode == null) {
return;
}
int[] offsetAndLength = getOffsetAndLength(ifExpression, node);
int offset = offsetAndLength[0];
int length = offsetAndLength[1];
int endOffset = offset + length;
String text = node.getRootNode().getText().substring(offset, endOffset).trim();
if (text.startsWith("else")) {
text = text.substring("else".length()).trim();
}
if (!text.endsWith("else")) {
text = text + " else ";
} else {
text = text + " ";
}
IXtextDocument document = context.getXtextDocument();
document.replace(offset, length, "");
document.replace(previousNode.getOffset(), 0, text);
return;
}
}
}
private XIfExpression getFirstIfExpression(XIfExpression ifExpression) {
EObject container = ifExpression.eContainer();
if (container instanceof XIfExpression) {
XIfExpression parentIfExpression = (XIfExpression) container;
if (parentIfExpression.getElse() == ifExpression) {
return getFirstIfExpression(parentIfExpression);
}
}
return ifExpression;
}
private List<XExpression> collectIfParts(XIfExpression expression, List<XExpression> result) {
result.add(expression.getIf());
if (expression.getElse() instanceof XIfExpression) {
collectIfParts((XIfExpression) expression.getElse(), result);
}
return result;
}
});
}
use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project xtext-eclipse by eclipse.
the class XbaseQuickfixProvider method createLinkingIssueResolutions.
/**
* Filter quickfixes for types and constructors.
*/
@Override
public void createLinkingIssueResolutions(final Issue issue, final IssueResolutionAcceptor issueResolutionAcceptor) {
final IModificationContext modificationContext = getModificationContextFactory().createModificationContext(issue);
final IXtextDocument xtextDocument = modificationContext.getXtextDocument();
if (xtextDocument != null) {
xtextDocument.tryReadOnly(new CancelableUnitOfWork<Void, XtextResource>() {
@Override
public java.lang.Void exec(XtextResource state, CancelIndicator cancelIndicator) throws Exception {
try {
EObject target = state.getEObject(issue.getUriToProblem().fragment());
EReference reference = getUnresolvedEReference(issue, target);
if (reference != null && reference.getEReferenceType() != null) {
createLinkingIssueQuickfixes(issue, getCancelableAcceptor(issueResolutionAcceptor, cancelIndicator), xtextDocument, state, target, reference);
}
} catch (WrappedException e) {
// issue information seems to be out of sync, e.g. there is no
// EObject with the given fragment
}
return null;
}
});
}
}
Aggregations