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);
}
}
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);
}
}
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);
}
}
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());
}
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);
}
Aggregations