-
Notifications
You must be signed in to change notification settings - Fork 15
/
EndStatementCodeMining.java
54 lines (50 loc) · 1.98 KB
/
EndStatementCodeMining.java
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
/**
* Copyright (c) 2018 Angelo ZERR.
* 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:
* Angelo Zerr <angelo.zerr@gmail.com>
*/
package org.eclipse.jdt.experimental.ui.javaeditor.codemining.endstatement;
import org.eclipse.jdt.core.dom.Statement;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.codemining.ICodeMiningProvider;
import org.eclipse.jface.text.codemining.LineContentCodeMining;
import org.eclipse.ui.texteditor.ITextEditor;
/**
* Show start statement text in the end statement as mining.
*
*/
public class EndStatementCodeMining extends LineContentCodeMining {
public EndStatementCodeMining(Statement node, ITextEditor textEditor, ITextViewer viewer, int minLineNumber,
ICodeMiningProvider provider) {
super(new Position(node.getStartPosition() + node.getLength(), 1), provider, e -> {
textEditor.selectAndReveal(node.getStartPosition(), 0);
});
String label = getLabel(node, viewer.getDocument(), minLineNumber);
super.setLabel(label);
}
private static String getLabel(Statement node, IDocument document, int minLineNumber) {
try {
int offset = node.getStartPosition();
if (minLineNumber > 0) {
int startLine = document.getLineOfOffset(offset);
int endLine = document.getLineOfOffset(offset + node.getLength());
if (endLine - startLine <= minLineNumber) {
return "";
}
}
IRegion first = document.getLineInformationOfOffset(offset);
return " // --> " + document.get(first.getOffset(), first.getLength()).trim();
} catch (BadLocationException e1) {
return "";
}
}
}