|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2024 SAP SE. |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + * |
| 11 | + * Contributors: |
| 12 | + * SAP SE - initial API and implementation |
| 13 | + *******************************************************************************/ |
| 14 | +package org.eclipse.jdt.internal.ui.javaeditor; |
| 15 | + |
| 16 | +import java.util.LinkedList; |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +import org.eclipse.jface.text.BadLocationException; |
| 20 | +import org.eclipse.jface.text.source.ISourceViewer; |
| 21 | + |
| 22 | +import org.eclipse.ui.texteditor.stickyscroll.IStickyLine; |
| 23 | +import org.eclipse.ui.texteditor.stickyscroll.IStickyLinesProvider; |
| 24 | +import org.eclipse.ui.texteditor.stickyscroll.StickyLine; |
| 25 | + |
| 26 | +import org.eclipse.jdt.core.IJavaElement; |
| 27 | +import org.eclipse.jdt.core.ISourceRange; |
| 28 | +import org.eclipse.jdt.core.ISourceReference; |
| 29 | +import org.eclipse.jdt.core.JavaModelException; |
| 30 | + |
| 31 | +public class StickyLinesProviderJava implements IStickyLinesProvider { |
| 32 | + |
| 33 | + @Override |
| 34 | + public List<IStickyLine> getStickyLines(ISourceViewer sourceViewer, int lineNumber, StickyLinesProperties properties) { |
| 35 | + LinkedList<IStickyLine> stickyLines= new LinkedList<>(); |
| 36 | + JavaEditor javaEditor= (JavaEditor) properties.editor(); |
| 37 | + |
| 38 | + IJavaElement element= null; |
| 39 | + try { |
| 40 | + element= javaEditor.getElementAt(sourceViewer.getDocument().getLineOffset(lineNumber)); |
| 41 | + } catch (BadLocationException e) { |
| 42 | + // TODO Auto-generated catch block |
| 43 | + e.printStackTrace(); |
| 44 | + } |
| 45 | + |
| 46 | + while (element != null) { |
| 47 | + if (element.getElementType() == IJavaElement.METHOD || element.getElementType() == IJavaElement.TYPE) { |
| 48 | + try { |
| 49 | + ISourceRange sourceRange= ((ISourceReference) element).getNameRange(); |
| 50 | + int offset= sourceRange.getOffset(); |
| 51 | + int stickyLineNumber= sourceViewer.getDocument().getLineOfOffset(offset); |
| 52 | + stickyLines.addFirst(new StickyLine(stickyLineNumber, sourceViewer)); |
| 53 | + } catch (JavaModelException | BadLocationException e) { |
| 54 | + // TODO Auto-generated catch block |
| 55 | + e.printStackTrace(); |
| 56 | + } |
| 57 | + } |
| 58 | + element= element.getParent(); |
| 59 | + } |
| 60 | + |
| 61 | + return stickyLines; |
| 62 | + } |
| 63 | + |
| 64 | +} |
0 commit comments