Search in sources :

Example 71 with IXtextDocument

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());
    }
}
Also used : IUnitOfWork(org.eclipse.xtext.util.concurrent.IUnitOfWork) XtextResource(org.eclipse.xtext.resource.XtextResource) IDocument(org.eclipse.jface.text.IDocument) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) Display(org.eclipse.swt.widgets.Display)

Example 72 with IXtextDocument

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()));
}
Also used : IPackageFragment(org.eclipse.jdt.core.IPackageFragment) JavaModelException(org.eclipse.jdt.core.JavaModelException) IFile(org.eclipse.core.resources.IFile) IJavaProject(org.eclipse.jdt.core.IJavaProject) IDocument(org.eclipse.jface.text.IDocument) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) IFolder(org.eclipse.core.resources.IFolder)

Example 73 with IXtextDocument

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;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IAdaptable(org.eclipse.core.runtime.IAdaptable) JavaModelException(org.eclipse.jdt.core.JavaModelException) IFile(org.eclipse.core.resources.IFile) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) ContentOutline(org.eclipse.ui.views.contentoutline.ContentOutline) IJavaProject(org.eclipse.jdt.core.IJavaProject) IWorkbenchPart(org.eclipse.ui.IWorkbenchPart) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 74 with IXtextDocument

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);
                }
            });
        }
    }
}
Also used : IModificationContext(org.eclipse.xtext.ui.editor.model.edit.IModificationContext) BadLocationException(org.eclipse.jface.text.BadLocationException) IModification(org.eclipse.xtext.ui.editor.model.edit.IModification) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) Fix(org.eclipse.xtext.ui.editor.quickfix.Fix)

Example 75 with IXtextDocument

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;
}
Also used : Position(org.eclipse.jface.text.Position) XtextResource(org.eclipse.xtext.resource.XtextResource) IAnnotationModelExtension(org.eclipse.jface.text.source.IAnnotationModelExtension) EObject(org.eclipse.emf.ecore.EObject) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) CancelIndicator(org.eclipse.xtext.util.CancelIndicator) Map(java.util.Map) Annotation(org.eclipse.jface.text.source.Annotation) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Aggregations

IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)147 XtextResource (org.eclipse.xtext.resource.XtextResource)51 Test (org.junit.Test)46 XtextEditor (org.eclipse.xtext.ui.editor.XtextEditor)38 IFile (org.eclipse.core.resources.IFile)37 BadLocationException (org.eclipse.jface.text.BadLocationException)26 AbstractEditorTest (org.eclipse.xtext.ui.testing.AbstractEditorTest)26 IUnitOfWork (org.eclipse.xtext.util.concurrent.IUnitOfWork)25 IModificationContext (org.eclipse.xtext.ui.editor.model.edit.IModificationContext)23 EObject (org.eclipse.emf.ecore.EObject)22 DefaultFoldingRegionProvider (org.eclipse.xtext.ui.editor.folding.DefaultFoldingRegionProvider)18 FoldedPosition (org.eclipse.xtext.ui.editor.folding.FoldedPosition)18 Fix (org.eclipse.xtext.ui.editor.quickfix.Fix)16 List (java.util.List)12 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)12 ArrayList (java.util.ArrayList)11 Issue (org.eclipse.xtext.validation.Issue)11 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)10 URI (org.eclipse.emf.common.util.URI)9 IRegion (org.eclipse.jface.text.IRegion)9