1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-04 14:55:41 +02:00

Cleanup indexer problem annotation handling

This commit is contained in:
Anton Leherbauer 2007-02-22 14:32:31 +00:00
parent 8d344a4f6a
commit 7149e81403
4 changed files with 69 additions and 279 deletions

View file

@ -35,26 +35,15 @@ import org.eclipse.jface.text.TextUtilities;
import org.eclipse.jface.text.source.Annotation; import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.text.source.AnnotationModel; import org.eclipse.jface.text.source.AnnotationModel;
import org.eclipse.jface.text.source.AnnotationModelEvent; import org.eclipse.jface.text.source.AnnotationModelEvent;
import org.eclipse.jface.text.source.IAnnotationAccessExtension;
import org.eclipse.jface.text.source.IAnnotationModel; import org.eclipse.jface.text.source.IAnnotationModel;
import org.eclipse.jface.text.source.IAnnotationModelListener; import org.eclipse.jface.text.source.IAnnotationModelListener;
import org.eclipse.jface.text.source.IAnnotationModelListenerExtension; import org.eclipse.jface.text.source.IAnnotationModelListenerExtension;
import org.eclipse.jface.text.source.IAnnotationPresentation;
import org.eclipse.jface.text.source.ImageUtilities;
import org.eclipse.jface.util.IPropertyChangeListener; import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent; import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.ui.IFileEditorInput; import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IStorageEditorInput; import org.eclipse.ui.IStorageEditorInput;
import org.eclipse.ui.editors.text.EditorsUI;
import org.eclipse.ui.editors.text.ForwardingDocumentProvider; import org.eclipse.ui.editors.text.ForwardingDocumentProvider;
import org.eclipse.ui.editors.text.TextFileDocumentProvider; import org.eclipse.ui.editors.text.TextFileDocumentProvider;
import org.eclipse.ui.texteditor.AnnotationPreference;
import org.eclipse.ui.texteditor.AnnotationPreferenceLookup;
import org.eclipse.ui.texteditor.IDocumentProvider; import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.MarkerAnnotation; import org.eclipse.ui.texteditor.MarkerAnnotation;
import org.eclipse.ui.texteditor.MarkerUtilities; import org.eclipse.ui.texteditor.MarkerUtilities;
@ -71,11 +60,10 @@ import org.eclipse.cdt.ui.text.ICPartitions;
import org.eclipse.cdt.internal.core.model.IBufferFactory; import org.eclipse.cdt.internal.core.model.IBufferFactory;
import org.eclipse.cdt.internal.ui.CPluginImages;
import org.eclipse.cdt.internal.ui.text.IProblemRequestorExtension; import org.eclipse.cdt.internal.ui.text.IProblemRequestorExtension;
/** /**
* CDocumentProvider2 * A document provider for C/C++ content.
*/ */
public class CDocumentProvider extends TextFileDocumentProvider { public class CDocumentProvider extends TextFileDocumentProvider {
/** /**
@ -86,118 +74,20 @@ public class CDocumentProvider extends TextFileDocumentProvider {
} }
/** /**
* Annotation representating an <code>IProblem</code>. * Annotation representing an <code>IProblem</code>.
*/ */
static protected class ProblemAnnotation extends Annotation implements ICAnnotation, IAnnotationPresentation { static protected class ProblemAnnotation extends Annotation implements ICAnnotation {
private static final String SPELLING_ANNOTATION_TYPE= "org.eclipse.ui.workbench.texteditor.spelling"; //$NON-NLS-1$ private static final String INDEXER_ANNOTATION_TYPE= "org.eclipse.cdt.ui.indexmarker"; //$NON-NLS-1$
//XXX: To be fully correct these constants should be non-static
/**
* The layer in which task problem annotations are located.
*/
//private static final int TASK_LAYER;
/**
* The layer in which info problem annotations are located.
*/
private static final int INFO_LAYER;
/**
* The layer in which warning problem annotations representing are located.
*/
private static final int WARNING_LAYER;
/**
* The layer in which error problem annotations representing are located.
*/
private static final int ERROR_LAYER;
static {
AnnotationPreferenceLookup lookup= EditorsUI.getAnnotationPreferenceLookup();
//TASK_LAYER= computeLayer("org.eclipse.ui.workbench.texteditor.task", lookup); //$NON-NLS-1$
INFO_LAYER= computeLayer("org.eclipse.cdt.ui.info", lookup); //$NON-NLS-1$
WARNING_LAYER= computeLayer("org.eclipse.cdt.ui.warning", lookup); //$NON-NLS-1$
ERROR_LAYER= computeLayer("org.eclipse.cdt.ui.error", lookup); //$NON-NLS-1$
}
private static int computeLayer(String annotationType, AnnotationPreferenceLookup lookup) {
Annotation annotation= new Annotation(annotationType, false, null);
AnnotationPreference preference= lookup.getAnnotationPreference(annotation);
if (preference != null)
return preference.getPresentationLayer() + 1;
return IAnnotationAccessExtension.DEFAULT_LAYER + 1;
}
private static Image fgQuickFixImage;
private static Image fgQuickFixErrorImage;
private static boolean fgQuickFixImagesInitialized= false;
private ITranslationUnit fTranslationUnit; private ITranslationUnit fTranslationUnit;
private List fOverlaids; private List fOverlaids;
private IProblem fProblem; private IProblem fProblem;
private Image fImage;
private boolean fQuickFixImagesInitialized= false;
private int fLayer= IAnnotationAccessExtension.DEFAULT_LAYER;
public ProblemAnnotation(IProblem problem, ITranslationUnit cu) { public ProblemAnnotation(IProblem problem, ITranslationUnit cu) {
fProblem= problem; fProblem= problem;
fTranslationUnit= cu; fTranslationUnit= cu;
setType(INDEXER_ANNOTATION_TYPE);
//if (SpellProblem.Spelling == fProblem.getID()) {
// setType(SPELLING_ANNOTATION_TYPE);
// fLayer= WARNING_LAYER;
//if (IProblem.Task == fProblem.getID()) {
// setType(CMarkerAnnotation.TASK_ANNOTATION_TYPE);
// fLayer= TASK_LAYER;
if (fProblem.isWarning()) {
setType(CMarkerAnnotation.WARNING_ANNOTATION_TYPE);
fLayer= WARNING_LAYER;
} else if (fProblem.isError()) {
setType(CMarkerAnnotation.ERROR_ANNOTATION_TYPE);
fLayer= ERROR_LAYER;
} else {
setType(CMarkerAnnotation.INFO_ANNOTATION_TYPE);
fLayer= INFO_LAYER;
}
}
/*
* @see org.eclipse.jface.text.source.IAnnotationPresentation#getLayer()
*/
public int getLayer() {
return fLayer;
}
private void initializeImages() {
// http://bugs.eclipse.org/bugs/show_bug.cgi?id=18936
if (!fQuickFixImagesInitialized) {
if (isProblem() && indicateQuixFixableProblems()) { // no light bulb for tasks
if (!fgQuickFixImagesInitialized) {
fgQuickFixImage= CPluginImages.get(CPluginImages.IMG_OBJS_FIXABLE_PROBLEM);
fgQuickFixErrorImage= CPluginImages.get(CPluginImages.IMG_OBJS_FIXABLE_ERROR);
fgQuickFixImagesInitialized= true;
}
if (CMarkerAnnotation.ERROR_ANNOTATION_TYPE.equals(getType()))
fImage= fgQuickFixErrorImage;
else
fImage= fgQuickFixImage;
}
fQuickFixImagesInitialized= true;
}
}
private boolean indicateQuixFixableProblems() {
return PreferenceConstants.getPreferenceStore().getBoolean(PreferenceConstants.EDITOR_CORRECTION_INDICATION);
}
/*
* @see org.eclipse.jface.text.source.IAnnotationPresentation#paint(org.eclipse.swt.graphics.GC, org.eclipse.swt.widgets.Canvas, org.eclipse.swt.graphics.Rectangle)
*/
public void paint(GC gc, Canvas canvas, Rectangle r) {
initializeImages();
if (fImage != null) {
ImageUtilities.drawImage(fImage, gc, canvas, r, SWT.CENTER, SWT.TOP);
}
} }
/* /*
@ -225,10 +115,7 @@ public class CDocumentProvider extends TextFileDocumentProvider {
* @see ICAnnotation#isProblem() * @see ICAnnotation#isProblem()
*/ */
public boolean isProblem() { public boolean isProblem() {
String type= getType(); return fProblem.isError() || fProblem.isWarning();
return CMarkerAnnotation.WARNING_ANNOTATION_TYPE.equals(type) ||
CMarkerAnnotation.ERROR_ANNOTATION_TYPE.equals(type) ||
SPELLING_ANNOTATION_TYPE.equals(type);
} }
/* /*
@ -239,7 +126,7 @@ public class CDocumentProvider extends TextFileDocumentProvider {
} }
/* /*
* @see org.eclipse.cdt.internal.ui.editor.IJavaAnnotation#getOverlay() * @see ICAnnotation#getOverlay()
*/ */
public ICAnnotation getOverlay() { public ICAnnotation getOverlay() {
return null; return null;

View file

@ -1,158 +0,0 @@
/*******************************************************************************
* Copyright (c) 2000, 2006 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
* QNX Software System
*******************************************************************************/
package org.eclipse.cdt.internal.ui.text;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.eclipse.core.resources.IMarker;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.source.IAnnotationHover;
import org.eclipse.jface.text.source.IAnnotationModel;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.ui.texteditor.MarkerAnnotation;
import org.eclipse.cdt.internal.ui.CUIMessages;
public class CAnnotationHover implements IAnnotationHover {
/**
* Returns the distance to the ruler line.
*/
protected int compareRulerLine(Position position, IDocument document, int line) {
if (position.getOffset() > -1 && position.getLength() > -1) {
try {
int markerLine= document.getLineOfOffset(position.getOffset());
if (line == markerLine)
return 1;
if (markerLine <= line && line <= document.getLineOfOffset(position.getOffset() + position.getLength()))
return 2;
} catch (BadLocationException x) {
}
}
return 0;
}
/**
* Selects a set of markers from the two lists. By default, it just returns
* the set of exact matches.
*/
protected List select(List exactMatch, List including) {
return exactMatch;
}
/**
* Returns one marker which includes the ruler's line of activity.
*/
protected List getMarkersForLine(ISourceViewer viewer, int line) {
IDocument document= viewer.getDocument();
IAnnotationModel model= viewer.getAnnotationModel();
if (model == null)
return null;
List exact= new ArrayList();
List including= new ArrayList();
Iterator e= model.getAnnotationIterator();
while (e.hasNext()) {
Object o= e.next();
if (o instanceof MarkerAnnotation) {
MarkerAnnotation a= (MarkerAnnotation) o;
switch (compareRulerLine(model.getPosition(a), document, line)) {
case 1:
exact.add(a.getMarker());
break;
case 2:
including.add(a.getMarker());
break;
}
}
}
return select(exact, including);
}
/*
* @see IVerticalRulerHover#getHoverInfo(ISourceViewer, int)
*/
public String getHoverInfo(ISourceViewer sourceViewer, int lineNumber) {
List markers= getMarkersForLine(sourceViewer, lineNumber);
if (markers != null && markers.size() > 0) {
if (markers.size() == 1) {
// optimization
IMarker marker= (IMarker) markers.get(0);
String message= marker.getAttribute(IMarker.MESSAGE, (String) null);
if (message != null && message.trim().length() > 0)
return formatSingleMessage(message);
} else {
List messages= new ArrayList();
Iterator e= markers.iterator();
while (e.hasNext()) {
IMarker marker= (IMarker) e.next();
String message= marker.getAttribute(IMarker.MESSAGE, (String) null);
if (message != null && message.trim().length() > 0)
messages.add(message.trim());
}
if (messages.size() == 1)
return formatSingleMessage((String) messages.get(0));
if (messages.size() > 1)
return formatMultipleMessages(messages);
}
}
return null;
}
/*
* Formats a message as HTML text.
*/
private String formatSingleMessage(String message) {
StringBuffer buffer= new StringBuffer();
HTMLPrinter.addPageProlog(buffer);
HTMLPrinter.addParagraph(buffer, HTMLPrinter.convertToHTMLContent(message));
HTMLPrinter.addPageEpilog(buffer);
return buffer.toString();
}
/*
* Formats several message as HTML text.
*/
private String formatMultipleMessages(List messages) {
StringBuffer buffer= new StringBuffer();
HTMLPrinter.addPageProlog(buffer);
HTMLPrinter.addParagraph(buffer, HTMLPrinter.convertToHTMLContent(CUIMessages.getString("CAnnotationHover.multipleMarkers"))); //$NON-NLS-1$
HTMLPrinter.startBulletList(buffer);
Iterator e= messages.iterator();
while (e.hasNext())
HTMLPrinter.addBullet(buffer, HTMLPrinter.convertToHTMLContent((String) e.next()));
HTMLPrinter.endBulletList(buffer);
HTMLPrinter.addPageEpilog(buffer);
return buffer.toString();
}
}

View file

@ -46,6 +46,7 @@ import org.eclipse.jface.text.reconciler.IReconciler;
import org.eclipse.jface.text.reconciler.MonoReconciler; import org.eclipse.jface.text.reconciler.MonoReconciler;
import org.eclipse.jface.text.rules.DefaultDamagerRepairer; import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
import org.eclipse.jface.text.rules.RuleBasedScanner; import org.eclipse.jface.text.rules.RuleBasedScanner;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.text.source.IAnnotationHover; import org.eclipse.jface.text.source.IAnnotationHover;
import org.eclipse.jface.text.source.ISourceViewer; import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.SourceViewerConfiguration; import org.eclipse.jface.text.source.SourceViewerConfiguration;
@ -523,7 +524,11 @@ public class CSourceViewerConfiguration extends TextSourceViewerConfiguration {
* @see SourceViewerConfiguration#getAnnotationHover(ISourceViewer) * @see SourceViewerConfiguration#getAnnotationHover(ISourceViewer)
*/ */
public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) { public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
return new CAnnotationHover(); return new HTMLAnnotationHover() {
protected boolean isIncluded(Annotation annotation) {
return isShowInVerticalRuler(annotation);
}
};
} }
/* /*

View file

@ -0,0 +1,56 @@
/*******************************************************************************
* Copyright (c) 2000, 2007 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.internal.ui.text;
import java.util.Iterator;
import java.util.List;
import org.eclipse.jface.text.source.DefaultAnnotationHover;
import org.eclipse.cdt.internal.ui.CUIMessages;
/**
* Determines all annotations for the given line and collects, concatenates, and formats
* their messages in HTML.
*
* @since 4.0
*/
public class HTMLAnnotationHover extends DefaultAnnotationHover {
/*
* Formats a message as HTML text.
*/
protected String formatSingleMessage(String message) {
StringBuffer buffer= new StringBuffer();
HTMLPrinter.addPageProlog(buffer);
HTMLPrinter.addParagraph(buffer, HTMLPrinter.convertToHTMLContent(message));
HTMLPrinter.addPageEpilog(buffer);
return buffer.toString();
}
/*
* Formats several message as HTML text.
*/
protected String formatMultipleMessages(List messages) {
StringBuffer buffer= new StringBuffer();
HTMLPrinter.addPageProlog(buffer);
HTMLPrinter.addParagraph(buffer, HTMLPrinter.convertToHTMLContent(CUIMessages.getString("CAnnotationHover.multipleMarkers"))); //$NON-NLS-1$
HTMLPrinter.startBulletList(buffer);
Iterator e= messages.iterator();
while (e.hasNext())
HTMLPrinter.addBullet(buffer, HTMLPrinter.convertToHTMLContent((String) e.next()));
HTMLPrinter.endBulletList(buffer);
HTMLPrinter.addPageEpilog(buffer);
return buffer.toString();
}
}