Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6b0c73df408026fac7bb6301982b615e5484ed9b (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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
 * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) 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:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.emf.cdo.releng.version.ui;

import org.eclipse.emf.cdo.releng.internal.version.Markers;
import org.eclipse.emf.cdo.releng.internal.version.VersionBuilderArguments;

import org.eclipse.core.filebuffers.ITextFileBuffer;
import org.eclipse.core.filebuffers.ITextFileBufferManager;
import org.eclipse.core.filebuffers.LocationKind;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.text.IDocument;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IMarkerResolution;
import org.eclipse.ui.IMarkerResolutionGenerator2;
import org.eclipse.ui.views.markers.WorkbenchMarkerResolution;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author Eike Stepper
 */
public class QuickFixer implements IMarkerResolutionGenerator2
{
  public QuickFixer()
  {
  }

  public IMarkerResolution[] getResolutions(IMarker marker)
  {
    List<IMarkerResolution> resolutions = new ArrayList<IMarkerResolution>();

    String regEx = Markers.getQuickFixPattern(marker);
    if (regEx != null)
    {
      String replacement = Markers.getQuickFixReplacement(marker);
      resolutions.add(new ReplaceResolution(marker, replacement));
    }

    String ignoreOption = Markers.getQuickFixConfigureOption(marker);
    if (ignoreOption != null)
    {
      resolutions.add(new ConfigureResolution(marker, ignoreOption));
    }

    return resolutions.toArray(new IMarkerResolution[resolutions.size()]);
  }

  public boolean hasResolutions(IMarker marker)
  {
    if (Markers.getQuickFixPattern(marker) != null)
    {
      return true;
    }

    if (Markers.getQuickFixConfigureOption(marker) != null)
    {
      return true;
    }

    return false;
  }

  /**
   * @author Eike Stepper
   */
  private abstract class AbstractResolution extends WorkbenchMarkerResolution
  {
    private IMarker marker;

    private String label;

    private String imageKey;

    public AbstractResolution(IMarker marker, String label, String imageKey)
    {
      this.marker = marker;
      this.label = label;
      this.imageKey = imageKey;
    }

    public IMarker getMarker()
    {
      return marker;
    }

    public String getLabel()
    {
      return label;
    }

    public String getDescription()
    {
      return "";
    }

    public final Image getImage()
    {
      ImageRegistry imageRegistry = Activator.getPlugin().getImageRegistry();
      return imageRegistry.get(imageKey);
    }

    @Override
    public IMarker[] findOtherMarkers(IMarker[] markers)
    {
      List<IMarker> result = new ArrayList<IMarker>();
      for (IMarker marker : markers)
      {
        try
        {
          if (marker != this.marker && isApplicable(marker))
          {
            result.add(marker);
          }
        }
        catch (Exception ex)
        {
          Activator.log(ex);
        }
      }

      return result.toArray(new IMarker[result.size()]);
    }

    protected abstract boolean isApplicable(IMarker marker) throws Exception;

    public final void run(IMarker marker)
    {
      try
      {
        apply(marker);
      }
      catch (Exception ex)
      {
        Activator.log(ex);
      }
    }

    protected abstract void apply(IMarker marker) throws Exception;
  }

  /**
   * @author Eike Stepper
   */
  private abstract class AbstractDocumentResolution extends AbstractResolution
  {
    public AbstractDocumentResolution(IMarker marker, String label, String imageKey)
    {
      super(marker, label, imageKey);
    }

    @Override
    protected final void apply(IMarker marker) throws Exception
    {
      IPath fullPath = ((IFile)marker.getResource()).getFullPath();
      ITextFileBufferManager.DEFAULT.connect(fullPath, LocationKind.IFILE, new NullProgressMonitor());

      try
      {
        ITextFileBuffer buffer = ITextFileBufferManager.DEFAULT.getTextFileBuffer(fullPath, LocationKind.IFILE);
        boolean wasDirty = buffer.isDirty();

        IDocument document = buffer.getDocument();
        if (apply(marker, document))
        {
          if (!wasDirty && !buffer.isShared())
          {
            buffer.commit(new NullProgressMonitor(), true);
          }
        }
      }
      finally
      {
        ITextFileBufferManager.DEFAULT.disconnect(fullPath, LocationKind.IFILE, new NullProgressMonitor());
      }
    }

    protected abstract boolean apply(IMarker marker, IDocument document) throws Exception;
  }

  /**
   * @author Eike Stepper
   */
  private class ReplaceResolution extends AbstractDocumentResolution
  {
    private String replacement;

    public ReplaceResolution(IMarker marker, String replacement)
    {
      super(marker, replacement == null ? "Remove the reference" : "Change the version",
          replacement == null ? Activator.CORRECTION_DELETE_GIF : Activator.CORRECTION_CHANGE_GIF);
      this.replacement = replacement == null ? "" : replacement;
    }

    @Override
    public String getDescription()
    {
      if (replacement.length() != 0)
      {
        return getLabel() + " to " + replacement;
      }

      return super.getDescription();
    }

    @Override
    protected boolean isApplicable(IMarker marker)
    {
      if (Markers.getQuickFixPattern(marker) == null)
      {
        return false;
      }

      boolean expectedReplacement = replacement.length() != 0;
      boolean actualReplacement = Markers.getQuickFixReplacement(marker) != null;
      return actualReplacement == expectedReplacement;
    }

    @Override
    protected boolean apply(IMarker marker, IDocument document) throws Exception
    {
      String content = document.get();

      String regEx = Markers.getQuickFixPattern(marker);
      String replacement = Markers.getQuickFixReplacement(marker);

      Pattern pattern = Pattern.compile(regEx, Pattern.MULTILINE | Pattern.DOTALL);
      Matcher matcher = pattern.matcher(content);
      if (matcher.find())
      {
        int start;
        int end;
        if (replacement.length() != 0)
        {
          start = matcher.start(1);
          end = matcher.end(1);
        }
        else
        {
          start = matcher.start();
          end = matcher.end();
        }

        document.replace(start, end - start, replacement);
        return true;
      }

      return false;
    }
  }

  /**
   * @author Eike Stepper
   */
  private class ConfigureResolution extends AbstractResolution
  {
    private String option;

    public ConfigureResolution(IMarker marker, String option)
    {
      super(marker, "Configure the project to ignore the problem", Activator.CORRECTION_CONFIGURE_GIF);
      this.option = option;
    }

    @Override
    public String getDescription()
    {
      IProject project = getMarker().getResource().getProject();
      return "Set " + option + " = true in '/" + project.getName() + "/.project'";
    }

    @Override
    protected boolean isApplicable(IMarker marker)
    {
      String requiredOption = Markers.getQuickFixConfigureOption(marker);
      return option.equals(requiredOption);
    }

    @Override
    protected void apply(IMarker marker) throws Exception
    {
      String option = Markers.getQuickFixConfigureOption(marker);

      IProject project = marker.getResource().getProject();
      VersionBuilderArguments arguments = new VersionBuilderArguments(project);
      arguments.put(option, "true");
      arguments.applyTo(project);
    }
  }
}

Back to the top