Skip to content

Commit 9e4565a

Browse files
Implement provider for sticky lines in JAVA
1 parent cac5317 commit 9e4565a

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

org.eclipse.jdt.ui/plugin.xml

+14
Original file line numberDiff line numberDiff line change
@@ -7830,4 +7830,18 @@
78307830
file-extensions="class without source">
78317831
</file-association>
78327832
</extension>
7833+
<extension
7834+
point="org.eclipse.ui.editors.stickyLinesProviders">
7835+
<stickyLinesProvider
7836+
class="org.eclipse.jdt.internal.ui.javaeditor.StickyLinesProviderJava"
7837+
id="org.eclipse.jdt.internal.ui.StickyLinesProviderJava">
7838+
<enabledWhen>
7839+
<and>
7840+
<with variable="editor">
7841+
<instanceof value="org.eclipse.jdt.internal.ui.javaeditor.JavaEditor"/>
7842+
</with>
7843+
</and>
7844+
</enabledWhen>
7845+
</stickyLinesProvider>
7846+
</extension>
78337847
</plugin>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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

Comments
 (0)