Search in sources :

Example 16 with SourceRelativeURI

use of org.eclipse.xtext.generator.trace.SourceRelativeURI in project xtext-eclipse by eclipse.

the class StratumBreakpointAdapterFactory method toggleBreakpoints.

@Override
public void toggleBreakpoints(IWorkbenchPart part, ISelection selection) throws CoreException {
    if (!(part instanceof XtextEditor) || !(selection instanceof ITextSelection)) {
        return;
    }
    try {
        final XtextEditor xtextEditor = (XtextEditor) part;
        final IEditorInput editorInput = xtextEditor.getEditorInput();
        final IResource breakpointResource = breakpointUtil.getBreakpointResource(editorInput);
        final SourceRelativeURI breakpointUri = breakpointUtil.getBreakpointURI(editorInput);
        final int offset = ((ITextSelection) selection).getOffset();
        final int line = xtextEditor.getDocument().getLineOfOffset(offset) + 1;
        Data data = xtextEditor.getDocument().readOnly(new IUnitOfWork<Data, XtextResource>() {

            @Override
            public Data exec(XtextResource state) throws Exception {
                IResourceServiceProvider provider = state.getResourceServiceProvider();
                IStratumBreakpointSupport breakpointSupport = provider.get(IStratumBreakpointSupport.class);
                Data result = new Data();
                result.name = state.getURI().lastSegment();
                result.valid = breakpointSupport != null && breakpointSupport.isValidLineForBreakPoint(state, line);
                result.types = getClassNamePattern(state);
                result.lang = provider.get(LanguageInfo.class);
                result.sourceUri = state.getURI();
                if (editorInput.getAdapter(IClassFile.class) != null) {
                    result.classHandle = ((IClassFile) editorInput.getAdapter(IClassFile.class)).getHandleIdentifier();
                }
                return result;
            }
        });
        IJavaStratumLineBreakpoint existingBreakpoint = findExistingBreakpoint(breakpointResource, breakpointUri, line);
        if (existingBreakpoint != null) {
            existingBreakpoint.delete();
            return;
        }
        if (!data.valid || data.types == null)
            return;
        if (log.isDebugEnabled()) {
            log.debug("Types the breakpoint listens for : " + data.types);
        }
        final IRegion information = xtextEditor.getDocument().getLineInformation(line - 1);
        final int charStart = information.getOffset();
        final int charEnd = information.getOffset() + information.getLength();
        final String shortName = data.lang.getShortName();
        Map<String, Object> attributes = Maps.newHashMap();
        if (breakpointUri != null)
            attributes.put(JarFileMarkerAnnotationModel.MARKER_URI, breakpointUri.toString());
        attributes.put(ORG_ECLIPSE_JDT_DEBUG_CORE_SOURCE_NAME, data.name);
        attributes.put(ORG_ECLIPSE_XTEXT_XBASE_SOURCE_URI, data.sourceUri.toString());
        if (data.classHandle != null)
            attributes.put(ORG_ECLIPSE_XTEXT_XBASE_CLASS_HANDLE, data.classHandle);
        final IJavaStratumLineBreakpoint breakpoint = new XbaseLineBreakpoint(breakpointResource, shortName, data.types, line, charStart, charEnd, attributes);
        // make sure the class name pattern gets updated on change
        final IMarker marker = breakpoint.getMarker();
        final IWorkspace ws = marker.getResource().getWorkspace();
        IResourceChangeListener listener = new IResourceChangeListener() {

            @Override
            public void resourceChanged(IResourceChangeEvent event) {
                if (!marker.exists())
                    ws.removeResourceChangeListener(this);
                IResourceDelta delta = event.getDelta();
                if (delta == null)
                    return;
                final IResourceDelta findMember = event.getDelta().findMember(marker.getResource().getFullPath());
                if (findMember == null)
                    return;
                IResource res = findMember.getResource();
                if (res == null || !res.exists())
                    return;
                if (event.getType() == IResourceChangeEvent.PRE_DELETE) {
                    ws.removeResourceChangeListener(this);
                } else if (event.getType() == IResourceChangeEvent.POST_CHANGE && (findMember.getFlags() & IResourceDelta.CONTENT) != 0) {
                    String classNamePattern = getClassNamePattern(event.getResource());
                    try {
                        breakpoint.getMarker().setAttribute("org.eclipse.jdt.debug.pattern", classNamePattern);
                    } catch (CoreException e) {
                        log.info(e.getMessage(), e);
                    }
                }
            }
        };
        ws.addResourceChangeListener(listener, IResourceChangeEvent.POST_CHANGE | IResourceChangeEvent.PRE_DELETE);
    } catch (BadLocationException e) {
        log.info(e.getMessage(), e);
    }
}
Also used : IClassFile(org.eclipse.jdt.core.IClassFile) XtextResource(org.eclipse.xtext.resource.XtextResource) IRegion(org.eclipse.jface.text.IRegion) IJavaStratumLineBreakpoint(org.eclipse.jdt.debug.core.IJavaStratumLineBreakpoint) IResourceServiceProvider(org.eclipse.xtext.resource.IResourceServiceProvider) IResourceChangeEvent(org.eclipse.core.resources.IResourceChangeEvent) IStratumBreakpointSupport(org.eclipse.xtext.debug.IStratumBreakpointSupport) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) IResourceChangeListener(org.eclipse.core.resources.IResourceChangeListener) ITextSelection(org.eclipse.jface.text.ITextSelection) IJavaStratumLineBreakpoint(org.eclipse.jdt.debug.core.IJavaStratumLineBreakpoint) IBreakpoint(org.eclipse.debug.core.model.IBreakpoint) CoreException(org.eclipse.core.runtime.CoreException) BadLocationException(org.eclipse.jface.text.BadLocationException) CoreException(org.eclipse.core.runtime.CoreException) IWorkspace(org.eclipse.core.resources.IWorkspace) IMarker(org.eclipse.core.resources.IMarker) SourceRelativeURI(org.eclipse.xtext.generator.trace.SourceRelativeURI) IEditorInput(org.eclipse.ui.IEditorInput) IResource(org.eclipse.core.resources.IResource) BadLocationException(org.eclipse.jface.text.BadLocationException) IResourceDelta(org.eclipse.core.resources.IResourceDelta)

Example 17 with SourceRelativeURI

use of org.eclipse.xtext.generator.trace.SourceRelativeURI in project xtext-eclipse by eclipse.

the class EclipseResourceFileSystemAccess2 method updateTraceInformation.

/**
 * @throws CoreException
 *             if something unexpected happens during resource access
 * @throws IOException
 *             if serialization of the trace data fails
 * @since 2.3
 */
protected void updateTraceInformation(IFile traceFile, CharSequence contents, boolean derived) throws CoreException, IOException {
    if (contents instanceof ITraceRegionProvider) {
        try {
            AbstractTraceRegion traceRegion = ((ITraceRegionProvider) contents).getTraceRegion();
            if (sourceTraces == null) {
                sourceTraces = HashMultimap.create();
            }
            IPath tracePath = traceFile.getFullPath();
            Iterator<AbstractTraceRegion> iterator = traceRegion.treeIterator();
            while (iterator.hasNext()) {
                AbstractTraceRegion region = iterator.next();
                for (ILocationData location : region.getAssociatedLocations()) {
                    SourceRelativeURI path = location.getSrcRelativePath();
                    if (path != null) {
                        sourceTraces.put(path, tracePath);
                    }
                }
            }
            class AccessibleOutputStream extends ByteArrayOutputStream {

                byte[] internalBuffer() {
                    return buf;
                }

                int internalLength() {
                    return count;
                }
            }
            AccessibleOutputStream data = new AccessibleOutputStream();
            traceSerializer.writeTraceRegionTo(traceRegion, data);
            // avoid copying the byte array
            InputStream input = new ByteArrayInputStream(data.internalBuffer(), 0, data.internalLength());
            if (traceFile.exists()) {
                traceFile.setContents(input, true, false, monitor);
            } else {
                traceFile.create(input, true, monitor);
            }
            setDerived(traceFile, derived);
            return;
        } catch (TraceNotFoundException e) {
        // ok
        }
    }
    if (traceFile.exists()) {
        traceFile.delete(IResource.FORCE, monitor);
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) ByteArrayInputStream(java.io.ByteArrayInputStream) StringInputStream(org.eclipse.xtext.util.StringInputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) TraceNotFoundException(org.eclipse.xtext.generator.trace.TraceNotFoundException) AbstractTraceRegion(org.eclipse.xtext.generator.trace.AbstractTraceRegion) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ITraceRegionProvider(org.eclipse.xtext.generator.trace.ITraceRegionProvider) SourceRelativeURI(org.eclipse.xtext.generator.trace.SourceRelativeURI) ILocationData(org.eclipse.xtext.generator.trace.ILocationData)

Example 18 with SourceRelativeURI

use of org.eclipse.xtext.generator.trace.SourceRelativeURI in project xtext-xtend by eclipse.

the class CompilerTraceTest method tracesTo.

public void tracesTo(final CharSequence xtend, final CharSequence java) {
    try {
        final String xtendWithSpaces = ((" " + xtend) + " ");
        final Matcher xtendMatcher = this.p.matcher(xtendWithSpaces);
        Assert.assertTrue("xtendMatcher.matches", xtendMatcher.matches());
        final String xtendGroup1 = xtendMatcher.group(1);
        final String xtendGroup2 = xtendMatcher.group(2);
        final String xtendGroup3 = xtendMatcher.group(3);
        final String actualXtendCode = ((xtendGroup1 + xtendGroup2) + xtendGroup3);
        final XtendFile file = this.file(actualXtendCode, true);
        XtendTypeDeclaration _head = IterableExtensions.<XtendTypeDeclaration>head(file.getXtendTypes());
        final JvmGenericType inferredType = this._iXtendJvmAssociations.getInferredType(((XtendClass) _head));
        CharSequence compiledCode = this.generator.generateType(inferredType, this.generatorConfigProvider.get(inferredType));
        compiledCode = this.postProcessor.postProcess(null, compiledCode);
        final Matcher javaMatcher = this.p.matcher(java.toString());
        Assert.assertTrue("javaMatcher.matches", javaMatcher.matches());
        final String javaGroup1 = javaMatcher.group(1);
        final String javaGroup2 = javaMatcher.group(2);
        final String javaGroup3 = javaMatcher.group(3);
        final String actualJavaExpectation = ((javaGroup1 + javaGroup2) + javaGroup3);
        Assert.assertEquals(actualJavaExpectation, compiledCode.toString());
        AbstractTraceRegion _traceRegion = ((ITraceRegionProvider) compiledCode).getTraceRegion();
        URI _createURI = URI.createURI(file.eResource().getURI().path());
        SourceRelativeURI _sourceRelativeURI = new SourceRelativeURI(_createURI);
        URI _createURI_1 = URI.createURI(file.eResource().getURI().path());
        SourceRelativeURI _sourceRelativeURI_1 = new SourceRelativeURI(_createURI_1);
        AbstractTraceRegion _merge = this.merge(_traceRegion.invertFor(_sourceRelativeURI, _sourceRelativeURI_1));
        final SimpleTrace trace = new SimpleTrace(_merge);
        int _length = xtendGroup1.length();
        int _length_1 = xtendGroup2.length();
        TextRegion _textRegion = new TextRegion(_length, _length_1);
        final Iterable<? extends ILocationInResource> locations = trace.getAllAssociatedLocations(_textRegion);
        int _length_2 = javaGroup1.length();
        int _length_3 = javaGroup2.length();
        final TextRegion expectedRegion = new TextRegion(_length_2, _length_3);
        Assert.assertFalse(IterableExtensions.isEmpty(locations));
        for (final ILocationInResource location : locations) {
            ITextRegionWithLineInformation _textRegion_1 = location.getTextRegion();
            boolean _equals = Objects.equal(_textRegion_1, expectedRegion);
            if (_equals) {
                return;
            }
        }
        Assert.fail(((("locations did not match expectation: " + locations) + " / ") + expectedRegion));
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) TextRegion(org.eclipse.xtext.util.TextRegion) Matcher(java.util.regex.Matcher) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) URI(org.eclipse.emf.common.util.URI) SourceRelativeURI(org.eclipse.xtext.generator.trace.SourceRelativeURI) ILocationInResource(org.eclipse.xtext.generator.trace.ILocationInResource) ITextRegionWithLineInformation(org.eclipse.xtext.util.ITextRegionWithLineInformation) XtendClass(org.eclipse.xtend.core.xtend.XtendClass) XtendTypeDeclaration(org.eclipse.xtend.core.xtend.XtendTypeDeclaration) AbstractTraceRegion(org.eclipse.xtext.generator.trace.AbstractTraceRegion) ITraceRegionProvider(org.eclipse.xtext.generator.trace.ITraceRegionProvider) SourceRelativeURI(org.eclipse.xtext.generator.trace.SourceRelativeURI)

Example 19 with SourceRelativeURI

use of org.eclipse.xtext.generator.trace.SourceRelativeURI in project xtext-core by eclipse.

the class AbstractTrace method isAssociatedWith.

protected boolean isAssociatedWith(AbstractTraceRegion region, AbsoluteURI uri, IProjectConfig project) {
    ITraceURIConverter traceURIConverter = getService(uri, ITraceURIConverter.class);
    if (traceURIConverter == null) {
        traceURIConverter = getService(getLocalURI(), ITraceURIConverter.class);
    }
    SourceRelativeURI convertedUri = traceURIConverter.getURIForTrace(project, uri);
    return convertedUri.equals(region.getAssociatedSrcRelativePath());
}
Also used : ITraceURIConverter(org.eclipse.xtext.generator.trace.ITraceURIConverter) SourceRelativeURI(org.eclipse.xtext.generator.trace.SourceRelativeURI)

Example 20 with SourceRelativeURI

use of org.eclipse.xtext.generator.trace.SourceRelativeURI in project xtext-core by eclipse.

the class TracingSugar method location.

/**
 * @param obj
 *            the EObject containing the feature
 * @param feature
 *            the EStructuralFeature to trace
 * @param idx
 *            the index of the value to trace, in case the feature contains a list, should be <code>-1</code>
 *            otherwise.
 *
 * @return ILocationData covering the <code>fullTextRegion</code> of the given feature in the given EObject.
 */
public ILocationData location(EObject obj, EStructuralFeature feature, int idx) {
    ITextRegion region = locationProvider.getFullTextRegion(obj, feature, idx);
    SourceRelativeURI uri = traceURIConverter.getURIForTrace(obj.eResource());
    return new LocationData((ITextRegionWithLineInformation) region, uri);
}
Also used : LocationData(org.eclipse.xtext.generator.trace.LocationData) ILocationData(org.eclipse.xtext.generator.trace.ILocationData) ITextRegion(org.eclipse.xtext.util.ITextRegion) SourceRelativeURI(org.eclipse.xtext.generator.trace.SourceRelativeURI)

Aggregations

SourceRelativeURI (org.eclipse.xtext.generator.trace.SourceRelativeURI)22 AbstractTraceRegion (org.eclipse.xtext.generator.trace.AbstractTraceRegion)7 ITraceRegionProvider (org.eclipse.xtext.generator.trace.ITraceRegionProvider)5 IFile (org.eclipse.core.resources.IFile)4 IPath (org.eclipse.core.runtime.IPath)4 URI (org.eclipse.emf.common.util.URI)4 AbsoluteURI (org.eclipse.xtext.generator.trace.AbsoluteURI)4 ILocationData (org.eclipse.xtext.generator.trace.ILocationData)4 TraceNotFoundException (org.eclipse.xtext.generator.trace.TraceNotFoundException)4 InputStream (java.io.InputStream)3 IResource (org.eclipse.core.resources.IResource)3 CoreException (org.eclipse.core.runtime.CoreException)3 IResourceServiceProvider (org.eclipse.xtext.resource.IResourceServiceProvider)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IContainer (org.eclipse.core.resources.IContainer)2 IProject (org.eclipse.core.resources.IProject)2 IStorage (org.eclipse.core.resources.IStorage)2 Path (org.eclipse.core.runtime.Path)2 IClassFile (org.eclipse.jdt.core.IClassFile)2 IEditorInput (org.eclipse.ui.IEditorInput)2