1
/*******************************************************************************
2
* Copyright (c) 2000, 2008 IBM Corporation and others.
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
9
* IBM Corporation - initial API and implementation
10
* Sergey Prigogin (Google)
11
*******************************************************************************/
12
package org.eclipse.cdt.internal.ui.text.c.hover;
14
import java.util.ArrayList;
15
import java.util.Collections;
16
import java.util.List;
18
import org.eclipse.core.resources.IMarker;
19
import org.eclipse.jface.text.ITextViewer;
20
import org.eclipse.jface.text.Position;
21
import org.eclipse.jface.text.contentassist.ICompletionProposal;
22
import org.eclipse.jface.text.source.Annotation;
23
import org.eclipse.jface.text.source.IAnnotationModel;
24
import org.eclipse.jface.text.source.ISourceViewer;
25
import org.eclipse.ui.IEditorInput;
26
import org.eclipse.ui.texteditor.MarkerAnnotation;
27
import org.eclipse.ui.texteditor.spelling.SpellingAnnotation;
29
import org.eclipse.cdt.core.model.CModelException;
30
import org.eclipse.cdt.core.model.ITranslationUnit;
31
import org.eclipse.cdt.ui.CUIPlugin;
32
import org.eclipse.cdt.ui.text.ICCompletionProposal;
33
import org.eclipse.cdt.ui.text.IProblemLocation;
35
import org.eclipse.cdt.internal.ui.editor.ICAnnotation;
36
import org.eclipse.cdt.internal.ui.text.contentassist.CCompletionProposalComparator;
37
import org.eclipse.cdt.internal.ui.text.correction.CCorrectionProcessor;
38
import org.eclipse.cdt.internal.ui.text.correction.CorrectionContext;
39
import org.eclipse.cdt.internal.ui.text.correction.ProblemLocation;
40
import org.eclipse.cdt.internal.ui.util.EditorUtility;
43
* This annotation hover shows the description of the
44
* selected java annotation.
46
* XXX: Currently this problem hover only works for spelling problems.
47
* see: https://bugs.eclipse.org/bugs/show_bug.cgi?id=62081
51
public class ProblemHover extends AbstractAnnotationHover {
53
protected static class ProblemInfo extends AnnotationInfo {
54
private static final ICompletionProposal[] NO_PROPOSALS= new ICompletionProposal[0];
56
public ProblemInfo(Annotation annotation, Position position, ITextViewer textViewer) {
57
super(annotation, position, textViewer);
61
* @see org.eclipse.cdt.internal.ui.text.java.hover.AbstractAnnotationHover.AnnotationInfo#getCompletionProposals()
64
public ICompletionProposal[] getCompletionProposals() {
65
if (annotation instanceof ICAnnotation) {
66
return getCAnnotationFixes((ICAnnotation) annotation);
67
} else if (annotation instanceof MarkerAnnotation) {
68
return getMarkerAnnotationFixes((MarkerAnnotation) annotation);
74
private ICompletionProposal[] getCAnnotationFixes(ICAnnotation cAnnotation) {
75
ProblemLocation location= new ProblemLocation(position.getOffset(), position.getLength(), cAnnotation);
76
ITranslationUnit tu= cAnnotation.getTranslationUnit();
78
ISourceViewer sourceViewer= null;
79
if (viewer instanceof ISourceViewer)
80
sourceViewer= (ISourceViewer) viewer;
82
CorrectionContext context= new CorrectionContext(tu, sourceViewer, location.getOffset(), location.getLength());
83
if (!SpellingAnnotation.TYPE.equals(cAnnotation.getType()))
86
List<ICCompletionProposal> proposals= new ArrayList<ICCompletionProposal>();
87
CCorrectionProcessor.collectCorrections(context, new IProblemLocation[] { location }, proposals);
88
Collections.sort(proposals, new CCompletionProposalComparator());
90
return proposals.toArray(new ICompletionProposal[proposals.size()]);
93
private ICompletionProposal[] getMarkerAnnotationFixes(MarkerAnnotation markerAnnotation) {
94
if (markerAnnotation.isQuickFixableStateSet() && !markerAnnotation.isQuickFixable())
97
IMarker marker= markerAnnotation.getMarker();
99
IEditorInput input = null;
101
input = EditorUtility.getEditorInput(marker.getResource());
102
} catch (CModelException e) {
107
ITranslationUnit tu= getTranslationUnit(input);
111
IAnnotationModel model= CUIPlugin.getDefault().getDocumentProvider().getAnnotationModel(input);
115
ISourceViewer sourceViewer= null;
116
if (viewer instanceof ISourceViewer)
117
sourceViewer= (ISourceViewer) viewer;
119
CorrectionContext context= new CorrectionContext(tu, sourceViewer, position.getOffset(), position.getLength());
121
List<ICCompletionProposal> proposals= new ArrayList<ICCompletionProposal>();
122
CCorrectionProcessor.collectProposals(context, model, new Annotation[] { markerAnnotation }, true, false, proposals);
124
return proposals.toArray(new ICompletionProposal[proposals.size()]);
127
private static ITranslationUnit getTranslationUnit(IEditorInput input) {
128
return CUIPlugin.getDefault().getWorkingCopyManager().getWorkingCopy(input);
132
public ProblemHover() {
137
protected AnnotationInfo createAnnotationInfo(Annotation annotation, Position position, ITextViewer textViewer) {
138
return new ProblemInfo(annotation, position, textViewer);