Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c5186807d2b7a49594f0adc2276af26ca56fe8cc (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
/*******************************************************************************
 * Copyright (c) 2007, 2018 Alphonse Van Assche and others.
 *
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    Alphonse Van Assche - initial API and implementation
 *    Red Hat Inc. - ongoing maintenance
 *******************************************************************************/
package org.eclipse.linuxtools.internal.rpm.rpmlint.resolutions;

import org.eclipse.core.resources.IMarker;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.linuxtools.internal.rpm.rpmlint.RpmlintLog;
import org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileParser;
import org.eclipse.ui.IEditorPart;

/**
 * Defines the common functionallity for resolution which fix is to replace text
 * in a line.
 */
public abstract class AReplaceTextResolution extends ARpmlintResolution {

	/**
	 * Returns the original string.
	 *
	 * @return The original string.
	 */
	public abstract String getOriginalString();

	/**
	 * Returns the string to replace in the <code>Document</code>.
	 *
	 * @return The string to replace.
	 *
	 */
	public abstract String getReplaceString();

	@Override
	public void run(IMarker marker) {

		IEditorPart editor = getEditor(marker);
		if (editor == null) {
			return;
		}
		IDocument doc = editor.getAdapter(IDocument.class);

		try {
			int lineNumber = marker.getAttribute(IMarker.LINE_NUMBER, 0);
			int index = doc.getLineOffset(lineNumber);
			String line = new SpecfileParser().parse(doc).getLine(lineNumber);
			int rowIndex = line.indexOf(getOriginalString());
			if (rowIndex > -1) {
				doc.replace(index + rowIndex, getOriginalString().length(), getReplaceString());
			}
		} catch (BadLocationException e) {
			RpmlintLog.logError(e);
		}
	}

}

Back to the top