|
| 1 | +package org.eclipse.jdt.internal.ui.javaeditor; |
| 2 | + |
| 3 | +import java.util.LinkedList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import org.eclipse.jface.text.BadLocationException; |
| 7 | +import org.eclipse.jface.text.source.ISourceViewer; |
| 8 | + |
| 9 | +import org.eclipse.ui.texteditor.stickyscroll.IStickyLine; |
| 10 | +import org.eclipse.ui.texteditor.stickyscroll.IStickyLinesProvider; |
| 11 | +import org.eclipse.ui.texteditor.stickyscroll.StickyLine; |
| 12 | + |
| 13 | +import org.eclipse.jdt.core.IJavaElement; |
| 14 | +import org.eclipse.jdt.core.ISourceRange; |
| 15 | +import org.eclipse.jdt.core.ISourceReference; |
| 16 | +import org.eclipse.jdt.core.JavaModelException; |
| 17 | + |
| 18 | +public class StickyLinesProviderJava implements IStickyLinesProvider { |
| 19 | + |
| 20 | + @Override |
| 21 | + public List<IStickyLine> getStickyLines(ISourceViewer sourceViewer, int lineNumber, StickyLinesProperties properties) { |
| 22 | + LinkedList<IStickyLine> stickyLines= new LinkedList<>(); |
| 23 | + JavaEditor javaEditor= (JavaEditor) properties.editor(); |
| 24 | + |
| 25 | + IJavaElement element= null; |
| 26 | + try { |
| 27 | + element= javaEditor.getElementAt(sourceViewer.getDocument().getLineOffset(lineNumber)); |
| 28 | + } catch (BadLocationException e) { |
| 29 | + // TODO Auto-generated catch block |
| 30 | + e.printStackTrace(); |
| 31 | + } |
| 32 | + |
| 33 | + while (element != null) { |
| 34 | + if (element.getElementType() == IJavaElement.METHOD || element.getElementType() == IJavaElement.TYPE) { |
| 35 | + try { |
| 36 | + ISourceRange sourceRange= ((ISourceReference) element).getNameRange(); |
| 37 | + int offset= sourceRange.getOffset(); |
| 38 | + int stickyLineNumber= sourceViewer.getDocument().getLineOfOffset(offset); |
| 39 | + stickyLines.addFirst(new StickyLine(stickyLineNumber, sourceViewer)); |
| 40 | + } catch (JavaModelException | BadLocationException e) { |
| 41 | + // TODO Auto-generated catch block |
| 42 | + e.printStackTrace(); |
| 43 | + } |
| 44 | + } |
| 45 | + element= element.getParent(); |
| 46 | + } |
| 47 | + |
| 48 | + return stickyLines; |
| 49 | + } |
| 50 | + |
| 51 | +} |
0 commit comments