Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6c2c839e747edec2bf5652e5b1dba5416bb41904 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*******************************************************************************
 * Copyright (c) 2008-2010 Sonatype, Inc.
 * 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:
 *      Sonatype, Inc. - initial API and implementation
 *******************************************************************************/

package org.eclipse.m2e.editor.xml;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.maven.model.InputLocation;
import org.apache.maven.model.InputSource;
import org.apache.maven.model.Model;
import org.apache.maven.project.MavenProject;
import org.w3c.dom.Node;

import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IInformationControl;
import org.eclipse.jface.text.IInformationControlCreator;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextHover;
import org.eclipse.jface.text.ITextHoverExtension;
import org.eclipse.jface.text.ITextHoverExtension2;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.text.source.IAnnotationModel;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.viewers.StyledString;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.texteditor.MarkerAnnotation;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument;

import org.eclipse.m2e.editor.xml.PomHyperlinkDetector.ExpressionRegion;
import org.eclipse.m2e.editor.xml.PomHyperlinkDetector.ManagedArtifactRegion;
import org.eclipse.m2e.editor.xml.internal.MarkerHoverControl;
import org.eclipse.m2e.editor.xml.internal.Messages;
import org.eclipse.m2e.editor.xml.internal.NodeOperation;
import org.eclipse.m2e.editor.xml.internal.XmlUtils;

public class PomTextHover implements ITextHover, ITextHoverExtension, ITextHoverExtension2 {

  public PomTextHover(ISourceViewer sourceViewer, String contentType, int stateMask) {
  }
  
  public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
    
    if (hoverRegion instanceof ExpressionRegion) {
      return getLabelForRegion((ExpressionRegion)hoverRegion).toString();
    } else if (hoverRegion instanceof ManagedArtifactRegion) {
      ManagedArtifactRegion region = (ManagedArtifactRegion) hoverRegion;
      return getLabelForRegion(region).toString();
    }
    
    return null;
  }

  /**
   * @param region
   */
  public static StyledString getLabelForRegion(ManagedArtifactRegion region) {
    MavenProject mavprj = region.project;
    if (mavprj != null) {
      String version = null;
      if (region.isDependency) {
        version = PomTemplateContext.searchDM(mavprj, region.groupId, region.artifactId);
      }
      if (region.isPlugin) {
        version = PomTemplateContext.searchPM(mavprj, region.groupId, region.artifactId);
      }
      StyledString ret = new StyledString();
      if (version != null) {
        ret.append(Messages.PomTextHover_managed_version);
        ret.append(version, StyledString.DECORATIONS_STYLER);//not happy with decorations but how to just do bold text
      } else {
        ret.append(Messages.PomTextHover_managed_version_missing);
      }
      InputLocation openLocation = PomHyperlinkDetector.findLocationForManagedArtifact(region, mavprj);
      if (openLocation != null) {
        //MNGECLIPSE-2539 apparently you can have an InputLocation with null input source.
        // check!
        InputSource source = openLocation.getSource();
        if (source != null) {
          ret.append(NLS.bind(Messages.PomTextHover_managed_location, source.getModelId()));
        }
      } else {
        ret.append(Messages.PomTextHover_managed_location_missing);
      }
      return ret;
    }
    return new StyledString(""); //$NON-NLS-1$
  }

  /**
   * @param hoverRegion
   */
  public static StyledString getLabelForRegion(ExpressionRegion region) {
    MavenProject mavprj = region.project;
    if (mavprj != null) {
      String value = PomTemplateContext.simpleInterpolate(region.project, "${" + region.property + "}"); //$NON-NLS-1$ //$NON-NLS-2$
      String loc = null;
      Model mdl = mavprj.getModel();
      if (mdl.getProperties() != null && mdl.getProperties().containsKey(region.property)) {
        if (mdl.getLocation("properties") != null) { //$NON-NLS-1$
          InputLocation location = mdl.getLocation("properties").getLocation(region.property); //$NON-NLS-1$
          if (location != null) {
            //MNGECLIPSE-2539 apparently you can have an InputLocation with null input source.
            // check!
            InputSource source = location.getSource();
            if (source != null) {
              loc = source.getModelId();
            }
          }
        }
      }
      StyledString ret = new StyledString();
      ret.append(Messages.PomTextHover_eval1);
      ret.append(value, StyledString.DECORATIONS_STYLER); //not happy with decorations but how to just do bold text
      if (loc != null) {
        ret.append(" "); //$NON-NLS-1$
        ret.append(NLS.bind(Messages.PomTextHover_eval2, loc));
      }
      return ret;
    }
    return new StyledString(""); //$NON-NLS-1$
  }

  public IRegion getHoverRegion(final ITextViewer textViewer, final int offset) {
    IDocument document = textViewer.getDocument();
    if(document == null) {
      return null;
    }
    final IRegion[] regs = new IRegion[2];
    XmlUtils.performOnCurrentElement(document, offset, new NodeOperation<Node>() {
      public void process(Node node, IStructuredDocument structured) {
        ExpressionRegion region = PomHyperlinkDetector.findExpressionRegion(node, textViewer, offset);
        if (region != null) {
          regs[0] = region;
          return;
        }
        ManagedArtifactRegion manReg = PomHyperlinkDetector.findManagedArtifactRegion(node, textViewer, offset);
        if (manReg != null) {
          regs[1] = manReg;
          return;
        }
      }
    });
    CompoundRegion toRet = new CompoundRegion(textViewer, offset);
    if (regs[0] != null) {
      toRet.addRegion(regs[0]);
    }
    if (regs[1] != null) {
      toRet.addRegion(regs[1]);
    }
    if (textViewer instanceof ISourceViewer) {
      ISourceViewer sourceViewer = (ISourceViewer) textViewer;
      IAnnotationModel model = sourceViewer.getAnnotationModel();
      if (model != null) { //eg. in tests
        Iterator<Annotation> it = model.getAnnotationIterator();
        while (it.hasNext()) {
          Annotation ann = it.next();
          if (ann instanceof MarkerAnnotation) {
            Position pos = sourceViewer.getAnnotationModel().getPosition(ann);
            if (pos.includes(offset)) {
              toRet.addRegion( new PomHyperlinkDetector.MarkerRegion(pos.getOffset(), pos.getLength(), (MarkerAnnotation)ann));
            }
          }
        }
      }
    }
    
    return toRet.getRegions().size() > 0 ? toRet : null;
  }
  
  public Object getHoverInfo2(ITextViewer textViewer, IRegion hoverRegion) {
    return hoverRegion;
  }

  public IInformationControlCreator getHoverControlCreator() {
    return new IInformationControlCreator() {
      public IInformationControl createInformationControl(Shell parent) {
        return new MarkerHoverControl(parent);
      }
    };
  }
  
  public static class CompoundRegion implements IRegion {

    private int length = Integer.MIN_VALUE;
    private int offset = Integer.MAX_VALUE;
    private List<IRegion> regions = new ArrayList<IRegion>();
    public final ITextViewer textViewer;
    public final int textOffset;
    
    public CompoundRegion(ITextViewer textViewer, int textOffset) {
      this.textViewer = textViewer;
      this.textOffset = textOffset;
    }
    
    public int getLength() {
      return length;
    }

    public int getOffset() {
      return offset;
    }
    
    public void addRegion(IRegion region) {
      regions.add(region);
      int start = Math.min(region.getOffset(), offset);
      int end = Math.max(region.getOffset() + region.getLength(), offset + length);
      offset = start;
      length = end - start; 
    }
    
    public List<IRegion> getRegions() {
      return regions;
    }
    
  }

}

Back to the top