use of org.eclipse.jface.text.source.IAnnotationModel in project xtext-xtend by eclipse.
the class DerivedSourceView method selectAndReveal.
@Override
protected void selectAndReveal(IWorkbenchPartSelection workbenchPartSelection) {
if (selectedSource != null) {
IAnnotationModel annotationModel = getSourceViewer().getAnnotationModel();
TextRegion localRegion = mapTextRegion(workbenchPartSelection);
IEclipseTrace trace = traceInformation.getTraceToTarget(getEditorResource(workbenchPartSelection));
if (trace != null) {
Iterable<? extends ILocationInEclipseResource> allAssociatedLocations = trace.getAllAssociatedLocations(localRegion, selectedSource);
ILocationInResource firstLocationInResource = Iterables.getFirst(allAssociatedLocations, null);
if (firstLocationInResource != null) {
ITextRegion textRegion = firstLocationInResource.getTextRegion();
if (textRegion != null) {
openEditorAction.setSelectedRegion(textRegion);
getSourceViewer().revealRange(textRegion.getOffset(), textRegion.getLength());
}
}
for (ILocationInResource locationInResource : allAssociatedLocations) {
ITextRegion textRegion = locationInResource.getTextRegion();
if (textRegion != null) {
annotationModel.addAnnotation(new Annotation(SEARCH_ANNOTATION_TYPE, true, null), new Position(textRegion.getOffset(), textRegion.getLength()));
}
}
}
}
}
use of org.eclipse.jface.text.source.IAnnotationModel in project eclipse-pmd by acanda.
the class JavaQuickFix method fixMarkersInFile.
/**
* Fixes all provided markers in a file.
*
* @param markers The markers to fix. There is at least one marker in this collection and all markers can be fixed
* by this quick fix.
*/
protected void fixMarkersInFile(final IFile file, final List<IMarker> markers, final IProgressMonitor monitor) {
monitor.subTask(file.getFullPath().toOSString());
final Optional<ICompilationUnit> optionalCompilationUnit = getCompilationUnit(file);
if (!optionalCompilationUnit.isPresent()) {
return;
}
final ICompilationUnit compilationUnit = optionalCompilationUnit.get();
ITextFileBufferManager bufferManager = null;
final IPath path = compilationUnit.getPath();
try {
bufferManager = FileBuffers.getTextFileBufferManager();
bufferManager.connect(path, LocationKind.IFILE, null);
final ITextFileBuffer textFileBuffer = bufferManager.getTextFileBuffer(path, LocationKind.IFILE);
final IDocument document = textFileBuffer.getDocument();
final IAnnotationModel annotationModel = textFileBuffer.getAnnotationModel();
final ASTParser astParser = ASTParser.newParser(AST.JLS4);
astParser.setKind(ASTParser.K_COMPILATION_UNIT);
astParser.setResolveBindings(needsTypeResolution());
astParser.setSource(compilationUnit);
final SubProgressMonitor parserMonitor = new SubProgressMonitor(monitor, 100);
final CompilationUnit ast = (CompilationUnit) astParser.createAST(parserMonitor);
parserMonitor.done();
startFixingMarkers(ast);
final Map<?, ?> options = compilationUnit.getJavaProject().getOptions(true);
for (final IMarker marker : markers) {
try {
final MarkerAnnotation annotation = getMarkerAnnotation(annotationModel, marker);
// if the annotation is null it means that is was deleted by a previous quick fix
if (annotation != null) {
final Optional<T> node = getNodeFinder(annotationModel.getPosition(annotation)).findNode(ast);
if (node.isPresent()) {
final boolean isSuccessful = fixMarker(node.get(), document, options);
if (isSuccessful) {
marker.delete();
}
}
}
} finally {
monitor.worked(100);
}
}
finishFixingMarkers(ast, document, options);
// commit changes to underlying file if it is not opened in an editor
if (!isEditorOpen(file)) {
final SubProgressMonitor commitMonitor = new SubProgressMonitor(monitor, 100);
textFileBuffer.commit(commitMonitor, false);
commitMonitor.done();
} else {
monitor.worked(100);
}
} catch (CoreException | MalformedTreeException | BadLocationException e) {
// TODO: log error
// PMDPlugin.getDefault().error("Error processing quickfix", e);
} finally {
if (bufferManager != null) {
try {
bufferManager.disconnect(path, LocationKind.IFILE, null);
} catch (final CoreException e) {
// TODO: log error
// PMDPlugin.getDefault().error("Error processing quickfix", e);
}
}
}
}
use of org.eclipse.jface.text.source.IAnnotationModel in project erlide_eclipse by erlang.
the class EditorTracker method markLine.
private void markLine(final ITextEditor editor, final LineResult lr) {
final IDocument doc = editor.getDocumentProvider().getDocument(editor.getEditorInput());
final IAnnotationModel annMod = editor.getDocumentProvider().getAnnotationModel(editor.getEditorInput());
log.info("mark line " + lr.getLineNum());
try {
final IRegion reg = doc.getLineInformation(lr.getLineNum() - 1);
final int length = reg.getLength();
final int offset = reg.getOffset();
final Position pos = new Position(offset, length);
Annotation annotation;
if (lr.called()) {
annotation = CoverageAnnotationFactory.create(CoverageTypes.FULL_COVERAGE);
} else {
annotation = CoverageAnnotationFactory.create(CoverageTypes.NO_COVERAGE);
}
final Annotation lastAnn = coverage.getAnnotation(editor.getTitle(), lr);
log.info(lastAnn);
if (lastAnn == null) {
annMod.addAnnotation(annotation, pos);
coverage.addAnnotation(editor.getTitle(), lr, annotation);
} else if (annMod.getPosition(lastAnn) == null) {
annMod.addAnnotation(lastAnn, pos);
} else if (lastAnn.getType().equals(CoverageTypes.NO_COVERAGE) && annotation.getType().equals(CoverageTypes.FULL_COVERAGE)) {
annMod.removeAnnotation(lastAnn);
annMod.addAnnotation(annotation, pos);
coverage.addAnnotation(editor.getTitle(), lr, annotation);
}
} catch (final BadLocationException e) {
log.error(e);
ErlLogger.error(e);
}
}
use of org.eclipse.jface.text.source.IAnnotationModel in project erlide_eclipse by erlang.
the class EditorTracker method clearAnnotations.
public void clearAnnotations(final IWorkbenchPart part) {
if (part != null) {
log.info(part.getTitle());
}
if (part instanceof ITextEditor) {
final ITextEditor editor = (ITextEditor) part;
final IAnnotationModel annMod = editor.getDocumentProvider().getAnnotationModel(editor.getEditorInput());
@SuppressWarnings("rawtypes") final Iterator it = annMod.getAnnotationIterator();
while (it.hasNext()) {
final Annotation annotation = (Annotation) it.next();
if (annotation.getType().equals(CoverageTypes.FULL_COVERAGE) || annotation.getType().equals(CoverageTypes.NO_COVERAGE)) {
annMod.removeAnnotation(annotation);
}
}
}
}
use of org.eclipse.jface.text.source.IAnnotationModel in project erlide_eclipse by erlang.
the class EditorTracker method removeAnnotationsFragment.
/**
* Removes coverage annotations from a fragment of file
*
* @param fileName
* @param start
* @param end
*/
public void removeAnnotationsFragment(final String fileName, final int start, final int end) {
final IEditorPart currentEditor = workbench.getActiveWorkbenchWindow().getActivePage().getActiveEditor();
if (currentEditor.getTitle().equals(fileName) && currentEditor instanceof ITextEditor) {
final ITextEditor editor = (ITextEditor) currentEditor;
final IAnnotationModel annMod = editor.getDocumentProvider().getAnnotationModel(editor.getEditorInput());
final Set<LineResult> list = coverage.getLineSet(editor.getTitle());
for (final LineResult lr : list) {
if (lr.getLineNum() < start || end != -1 && lr.getLineNum() > end) {
continue;
}
log.info(lr.getLineNum());
if (coverage.containsAnnotation(editor.getTitle(), lr)) {
final Annotation ann = coverage.getAnnotation(editor.getTitle(), lr);
annMod.removeAnnotation(ann);
coverage.removeAnnotation(editor.getTitle(), lr);
}
}
}
}
Aggregations