blob: b5b8a18a8969784f1cc1038283be5b225d448cc5 (
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
|
/*******************************************************************************
* Copyright (c) 2011 Alena Laskavaia and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Alena Laskavaia - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.codan.internal.checkers.ui.quickfix;
import org.eclipse.cdt.codan.core.model.IProblem;
import org.eclipse.cdt.codan.core.param.RootProblemPreference;
import org.eclipse.cdt.codan.internal.checkers.CaseBreakChecker;
import org.eclipse.cdt.codan.ui.AbstractCodanCMarkerResolution;
import org.eclipse.core.resources.IMarker;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.text.edits.InsertEdit;
import org.eclipse.text.edits.MalformedTreeException;
public class CaseBreakQuickFixComment extends AbstractCodanCMarkerResolution {
@Override
public String getLabel() {
return QuickFixMessages.CaseBreakQuickFixComment_Label;
}
@Override
public void apply(IMarker marker, IDocument document) {
try {
int line = marker.getAttribute(IMarker.LINE_NUMBER, -1);
if (line < 0)
return;
int offset = document.getLineOffset(line);
String indent = getIndentationStr(document, line);
String comment = getNoBreakComment(marker);
String editStr = String.format("%s/* %s */\n", indent, comment); //$NON-NLS-1$
InsertEdit edit = new InsertEdit(offset, editStr);
edit.apply(document);
} catch (MalformedTreeException e) {
return;
} catch (BadLocationException e) {
return;
}
}
private String getIndentationStr(IDocument document, int line) throws BadLocationException {
int prevLine = line - 1;
IRegion lineInformation = document.getLineInformation(prevLine);
String prevLineStr = document.get(lineInformation.getOffset(), lineInformation.getLength());
int nonSpace = prevLineStr.indexOf(prevLineStr.trim());
String indent = prevLineStr.substring(0, nonSpace);
return indent;
}
private String getNoBreakComment(IMarker marker) {
IProblem problem = getProblem(marker);
RootProblemPreference map = (RootProblemPreference) problem.getPreference();
String comment = (String) map.getChildValue(CaseBreakChecker.PARAM_NO_BREAK_COMMENT);
if (comment == null || comment.trim().length() == 0)
comment = "no break"; //$NON-NLS-1$
return comment;
}
}
|