use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project n4js by eclipse.
the class FixedContentFormatter method format.
/**
* This method makes sure that no changes are applied (no dirty state), if there are no changes. This fixes bug
* GHOLD-272
*/
@Override
public void format(IDocument document, IRegion region) {
IXtextDocument doc = (IXtextDocument) document;
TextEdit e = doc.priorityReadOnly(new FormattingUnitOfWork(doc, region));
if (e == null)
return;
if (e instanceof ReplaceEdit) {
ReplaceEdit r = (ReplaceEdit) e;
if ((r.getOffset() == 0) && (r.getLength() == 0) && (r.getText().isEmpty())) {
return;
}
}
try {
e.apply(document);
} catch (BadLocationException ex) {
throw new RuntimeException(ex);
}
}
use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project statecharts by Yakindu.
the class AsyncXtextContentAssistProcessor method computeCompletionProposals.
@Override
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
if (getContentProposalProvider() == null)
return null;
IXtextDocument document = (IXtextDocument) viewer.getDocument();
final CancelableCompletionProposalComputer computer = createCompletionProposalComputer(viewer, offset);
ICompletionProposal[] result = document.readOnly(new CancelableUnitOfWork<ICompletionProposal[], XtextResource>() {
@Override
public ICompletionProposal[] exec(XtextResource state, CancelIndicator cancelIndicator) throws Exception {
computer.setCancelIndicator(cancelIndicator);
try {
return computer.exec(state);
} catch (Throwable t) {
return new ICompletionProposal[] {};
}
}
});
Arrays.sort(result, getCompletionProposalComparator());
result = getCompletionProposalPostProcessor().postProcess(result);
return result;
}
use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project dsl-devkit by dsldevkit.
the class CheckCfgQuickfixProvider method fixSeverityToMaxSeverity.
/**
* Fix severity by setting it to a legal value as is defined by severity range of referenced check. Legal
* severities are passed as issue data (org.eclipse.xtext.validation.Issue#getData()).
*
* @param issue
* the issue
* @param acceptor
* the acceptor
*/
@Fix(IssueCodes.SEVERITY_NOT_ALLOWED)
public void fixSeverityToMaxSeverity(final Issue issue, final IssueResolutionAcceptor acceptor) {
if (issue.getData() != null) {
for (final String severityProposal : issue.getData()) {
final String label = NLS.bind(Messages.CheckCfgQuickfixProvider_CORRECT_SEVERITY_LABEL, severityProposal);
final String descn = NLS.bind(Messages.CheckCfgQuickfixProvider_CORRECT_SEVERITY_DESCN, severityProposal);
acceptor.accept(issue, label, descn, NO_IMAGE, new IModification() {
public void apply(final IModificationContext context) throws BadLocationException {
IXtextDocument xtextDocument = context.getXtextDocument();
xtextDocument.replace(issue.getOffset(), issue.getLength(), severityProposal);
}
});
}
}
}
use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project dsl-devkit by dsldevkit.
the class EObjectContentProviderTest method mockSelectedElement.
/**
* Mocks XtextEditor and XtextElementSelectionListener to return an element selection that has:
* - given EAttribute with a specific value (AttributeValuePair)
* - given EStructuralFeature
* - given EOperation.
*
* @param attributeValuePair
* EAttribute with a specific value
* @param feature
* the EStructuralFeature
* @param operation
* the EOperation
* @return the EClass of the "selected" element
*/
@SuppressWarnings("unchecked")
private EClass mockSelectedElement(final AttributeValuePair attributeValuePair, final EStructuralFeature feature, final EOperation operation) {
EClass mockSelectionEClass = mock(EClass.class);
when(mockSelectionListener.getSelectedElementType()).thenReturn(mockSelectionEClass);
// Mockups for returning AttributeValuePair
URI elementUri = URI.createURI("");
when(mockSelectionListener.getSelectedElementUri()).thenReturn(elementUri);
XtextEditor mockEditor = mock(XtextEditor.class);
when(mockSelectionListener.getEditor()).thenReturn(mockEditor);
IXtextDocument mockDocument = mock(IXtextDocument.class);
when(mockEditor.getDocument()).thenReturn(mockDocument);
when(mockDocument.<ArrayList<AttributeValuePair>>readOnly(any(IUnitOfWork.class))).thenReturn(newArrayList(attributeValuePair));
// Mockups for returning EOperation
BasicEList<EOperation> mockEOperationsList = new BasicEList<EOperation>();
mockEOperationsList.add(operation);
when(mockSelectionEClass.getEAllOperations()).thenReturn(mockEOperationsList);
// Mockups for returning EStructuralFeature
BasicEList<EStructuralFeature> mockEStructuralFeatureList = new BasicEList<EStructuralFeature>();
mockEStructuralFeatureList.add(feature);
mockEStructuralFeatureList.add(attributeValuePair.getAttribute());
when(mockSelectionEClass.getEAllStructuralFeatures()).thenReturn(mockEStructuralFeatureList);
return mockSelectionEClass;
}
use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project dsl-devkit by dsldevkit.
the class XtextElementSelectionListenerTest method mockNodeAtSelection.
/**
* Mocks a text selection and causes the (mocked) XtextEditor to return an ILeafNode on any read(IUnitOfWork).
*
* @return the mocked ILeafNode
*/
@SuppressWarnings("unchecked")
private ILeafNode mockNodeAtSelection(final XtextEditor mockEditor) {
IXtextDocument mockDocument = mock(IXtextDocument.class);
ILeafNode mockNode = mock(ILeafNode.class);
when(mockEditor.getDocument()).thenReturn(mockDocument);
when(mockDocument.<ILeafNode>priorityReadOnly(any(IUnitOfWork.class))).thenReturn(mockNode);
when(mockDocument.<ILeafNode>readOnly(any(IUnitOfWork.class))).thenReturn(mockNode);
// when(mockDocument.readOnly(any(IUnitOfWork<ILeafNode, XtextResource>.class))).thenReturn(mockLeafNode);
selectionListener.selectionChanged(null, mock(ITextSelection.class));
return mockNode;
}
Aggregations