use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project dsl-devkit by dsldevkit.
the class FixedHighlightingReconciler method refresh.
/**
* Refreshes the highlighting.
*/
@Override
public void refresh() {
if (calculator != null) {
IDocument document;
if (editor != null) {
document = editor.getDocument();
} else {
document = sourceViewer.getDocument();
}
if (document instanceof IXtextDocument) {
((IXtextDocument) document).readOnly(new IUnitOfWork.Void<XtextResource>() {
@Override
public void process(final XtextResource state) throws Exception {
modelChanged(state);
}
});
}
} else {
Display display = getDisplay();
display.asyncExec(presenter.createSimpleUpdateRunnable());
}
}
use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project dsl-devkit by dsldevkit.
the class ResourceNameTemplateVariableResolver method resolveValues.
@Override
public List<String> resolveValues(final TemplateVariable variable, final XtextTemplateContext templateContext) {
final List<String> result = Lists.newArrayList();
final IDocument document = templateContext.getDocument();
final Object obj = variable.getVariableType().getParams().iterator().next();
if (obj instanceof String) {
final String variableName = (String) obj;
final IXtextDocument xtextDocument = (IXtextDocument) document;
final IFile file = xtextDocument.getAdapter(IFile.class);
switch(variableName) {
case // $NON-NLS-1$
"package":
if (document instanceof IXtextDocument && file != null && file.getParent() instanceof IFolder) {
final IJavaProject javaProject = JavaCore.create(file.getProject());
try {
final IPackageFragment packageFragment = javaProject.findPackageFragment(file.getParent().getFullPath());
result.add(packageFragment.getElementName());
} catch (final JavaModelException e) {
// $NON-NLS-1$
LOGGER.error("Could not determine package for file of given document");
}
}
break;
case // $NON-NLS-1$
"file":
final String fileName = file.getName();
result.add(fileName.indexOf('.') > 0 ? fileName.substring(0, fileName.lastIndexOf('.')) : fileName);
}
}
return Lists.newArrayList(Iterables.filter(result, Predicates.notNull()));
}
use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project dsl-devkit by dsldevkit.
the class FieldInitializerUtil method getSelectedResource.
/**
* Gets the selected resource.
*
* @param selection
* the selection
* @return the selected resource
*/
public IJavaElement getSelectedResource(final IStructuredSelection selection) {
IJavaElement elem = null;
if (selection != null && !selection.isEmpty()) {
Object o = selection.getFirstElement();
if (o instanceof IAdaptable) {
IAdaptable adaptable = (IAdaptable) o;
elem = (IJavaElement) adaptable.getAdapter(IJavaElement.class);
if (elem == null) {
// NOPMD
elem = getSelectedPackage(adaptable);
}
}
}
if (elem == null) {
IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IWorkbenchPart part = activePage.getActivePart();
if (part instanceof ContentOutline) {
part = activePage.getActiveEditor();
}
if (part instanceof XtextEditor) {
IXtextDocument doc = ((XtextEditor) part).getDocument();
IFile file = doc.getAdapter(IFile.class);
elem = getSelectedPackage(file);
}
}
if (elem == null || elem.getElementType() == IJavaElement.JAVA_MODEL) {
try {
IJavaProject[] projects = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects();
if (projects.length == 1) {
elem = projects[0];
}
} catch (JavaModelException e) {
// NOPMD
throw new RuntimeException(e.getMessage());
}
}
return elem;
}
use of org.eclipse.xtext.ui.editor.model.IXtextDocument in project dsl-devkit by dsldevkit.
the class CheckQuickfixProvider method fixIllegalDefaultSeverity.
/**
* Fixes an illegally set default severity. The default severity must be within given severity range.
*
* @param issue
* the issue
* @param acceptor
* the acceptor
*/
@Fix(IssueCodes.DEFAULT_SEVERITY_NOT_IN_RANGE)
public void fixIllegalDefaultSeverity(final Issue issue, final IssueResolutionAcceptor acceptor) {
if (issue.getData() != null) {
for (final String severityProposal : issue.getData()) {
final String label = NLS.bind(Messages.CheckQuickfixProvider_DEFAULT_SEVERITY_FIX_LABEL, severityProposal);
final String descn = NLS.bind(Messages.CheckQuickfixProvider_DEFAULT_SEVERITY_FIX_DESCN, severityProposal);
acceptor.accept(issue, label, descn, NO_IMAGE, new IModification() {
@Override
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 xtext-xtend by eclipse.
the class OverrideIndicatorModelListener method updateAnnotationModel.
private IStatus updateAnnotationModel(IProgressMonitor monitor) {
if (xtextEditor == null || xtextEditor.getDocument() == null || xtextEditor.getInternalSourceViewer().getAnnotationModel() == null) {
return Status.OK_STATUS;
}
IXtextDocument xtextDocument = xtextEditor.getDocument();
IAnnotationModel annotationModel = xtextEditor.getInternalSourceViewer().getAnnotationModel();
Map<Annotation, Position> annotationToPosition = xtextDocument.readOnly(new CancelableUnitOfWork<Map<Annotation, Position>, XtextResource>() {
@Override
public Map<Annotation, Position> exec(XtextResource xtextResource, CancelIndicator cancelIndicator) {
if (xtextResource == null)
return Collections.emptyMap();
return createOverrideIndicatorAnnotationMap(xtextResource, cancelIndicator);
}
});
if (monitor.isCanceled())
return Status.CANCEL_STATUS;
if (annotationModel instanceof IAnnotationModelExtension) {
IAnnotationModelExtension annotationModelExtension = (IAnnotationModelExtension) annotationModel;
Object lockObject = getLockObject(annotationModel);
synchronized (lockObject) {
annotationModelExtension.replaceAnnotations(overrideIndicatorAnnotations.toArray(new Annotation[overrideIndicatorAnnotations.size()]), annotationToPosition);
}
overrideIndicatorAnnotations = annotationToPosition.keySet();
}
return Status.OK_STATUS;
}
Aggregations