Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: edb218ceb6bef2375da3180da320502d7e2a46c5 (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
/*
 * Copyright (c) 2014, 2015, 2018 Eike Stepper (Loehne, 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.oomph.version.ui.quickfixes;

import org.eclipse.oomph.version.Markers;
import org.eclipse.oomph.version.ui.Activator;

import org.eclipse.core.resources.IMarker;
import org.eclipse.jface.text.IDocument;

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

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

  private String replacement;

  public ReplaceResolution(IMarker marker, String problemType, String replacement)
  {
    super(marker, getLabel(marker, problemType, replacement), replacement == null ? Activator.CORRECTION_DELETE_GIF : Activator.CORRECTION_CHANGE_GIF);
    this.problemType = problemType;
    this.replacement = replacement == null ? "" : replacement;
  }

  private static String getLabel(IMarker marker, String problemType, String replacement)
  {
    if (Markers.SCHEMA_BUILDER_PROBLEM.equals(problemType))
    {
      return replacement == null ? "Remove the schema builder" : "Add the schema builder";
    }

    if (Markers.FEATURE_NATURE_PROBLEM.equals(problemType))
    {
      return "Add the feature builder";
    }

    if (Markers.FEATURE_CLOSURE_PROBLEM.equals(problemType))
    {
      return "Add the plug-in reference";
    }

    if (Markers.DEBUG_OPTION_PROBLEM.equals(problemType))
    {
      return "Change the debug option";
    }

    if (Markers.AUTOMATIC_MODULE_NAME_PROBLEM.equals(problemType))
    {
      return "Change the automatic module name";
    }

    if (Markers.MAVEN_POM_PROBLEM.equals(problemType))
    {
      String message = Markers.getAttribute(marker, IMarker.MESSAGE);
      if (message != null)
      {
        if (message.startsWith("Maven artifactId"))
        {
          return "Change the Maven artifactId";
        }

        if (message.startsWith("Maven version"))
        {
          return "Change the Maven version";
        }
      }
    }

    return replacement == null ? "Remove the reference"
        : Markers.getQuickFixAlternativeReplacement(marker) == null ? "Change the version" : "Change to the extact version";
  }

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

    return super.getDescription();
  }

  @Override
  protected boolean isApplicable(IMarker marker)
  {
    if (!problemType.equals(Markers.getProblemType(marker)))
    {
      return false;
    }

    if (Markers.getQuickFixPattern(marker) == null)
    {
      return false;
    }

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

  protected String getQuickFixReplacement(IMarker marker)
  {
    return Markers.getQuickFixReplacement(marker);
  }

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

    String regEx = Markers.getQuickFixPattern(marker);
    String replacement = 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 != null && replacement.length() != 0)
      {
        start = matcher.start(1);
        end = matcher.end(1);

        for (int i = 1; i < 9; i++)
        {
          String insertTag = "\\" + i;
          if (replacement.contains(insertTag))
          {
            int offset = matcher.start(i);
            int length = matcher.end(i) - offset;
            String found = document.get(offset, length);
            replacement = replacement.replace(insertTag, found);
          }
        }
      }
      else
      {
        start = matcher.start();
        end = matcher.end();
        replacement = "";
      }

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

    return false;
  }
}

Back to the top