Skip to content

Commit 21619c7

Browse files
committed
fix: display resolved codelens when file is opened.
Signed-off-by: azerr <azerr@redhat.com>
1 parent 97edb8f commit 21619c7

3 files changed

Lines changed: 50 additions & 8 deletions

File tree

src/main/java/com/redhat/devtools/lsp4ij/features/codeLens/LSPCodeLensEditorFactoryListener.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ public static LSPCodeLensSupport getCodeLensSupport(@NotNull Editor editor) {
7676
}
7777

7878
private static void updateViewportLinesAndRefreshCodeVision(@NotNull CodeLensDataResult result, LSPCodeLensSupport codeLensSupport, UnresolvedCodeLensViewportContext context) {
79+
if (!context.isViewportInitialized()) {
80+
// Viewport not initialized yet, cannot determine what to resolve
81+
return;
82+
}
7983
if (!result.hasToResolve(context.getFirstViewportLine(), context.getLastViewportLine())) {
8084
// No codelens to resolve to the current viewport
8185
return;
@@ -124,9 +128,23 @@ private static boolean updateViewportLinesAndRefreshCodeVision(@NotNull CodeLens
124128
private void attachScrollListener(@NotNull Editor editor) {
125129
// Initialize context
126130
final var context = getCodeLensResolveContext(editor);
131+
127132
// Adding a listener for visible area changes
128133
editor.getScrollingModel().addVisibleAreaListener((e) -> {
129134
Rectangle newRect = e.getNewRectangle();
135+
136+
// Initialize viewport on first event (when editor is fully rendered)
137+
if (!context.isViewportInitialized() && newRect.width > 0 && newRect.height > 0) {
138+
// First time - initialize viewport even if it hasn't "changed"
139+
// We're on EDT here, and we have a valid rectangle
140+
if (ApplicationManager.getApplication().isReadAccessAllowed()) {
141+
context.updateViewportLines(newRect);
142+
} else {
143+
ReadAction.run(() -> context.updateViewportLines(newRect));
144+
}
145+
// Don't return - continue to process the event normally
146+
}
147+
130148
if (newRect.equals(e.getOldRectangle())) {
131149
// View port range has not changed
132150
return;

src/main/java/com/redhat/devtools/lsp4ij/features/codeLens/LSPCodeLensProvider.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,21 @@ public CodeVisionState computeCodeVision(@NotNull Editor editor, Void uiData) {
134134
// There is some codelens to resolve
135135
applicableCodeLens = new ArrayList<>();
136136
var resolveContext = LSPCodeLensEditorFactoryListener.getCodeLensResolveContext(editor);
137+
// Get viewport lines - if not initialized yet (viewport is updated by scroll listener on EDT),
138+
// use a default range to resolve code lenses in the first visible area
139+
int firstLine = resolveContext.getFirstViewportLine();
140+
int lastLine = resolveContext.getLastViewportLine();
141+
if (firstLine == -1) {
142+
// Viewport not yet initialized, use a reasonable default (first ~100 lines)
143+
firstLine = 0;
144+
lastLine = 100;
145+
}
137146
// Get the codelens to resolve which are inside the view port range (visible lines)
138147
@Nullable CompletableFuture<Void> visibleCodeLensToResolve =
139148
collectApplicableCodeLens(codelensData,
140149
applicableCodeLens,
141-
resolveContext.getFirstViewportLine(),
142-
resolveContext.getLastViewportLine());
150+
firstLine,
151+
lastLine);
143152
if (visibleCodeLensToResolve != null) {
144153
// Resolve all visible code lens
145154
try {

src/main/java/com/redhat/devtools/lsp4ij/features/codeLens/UnresolvedCodeLensViewportContext.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public class UnresolvedCodeLensViewportContext implements Disposable {
3232
private static final long VIEWPORT_CHANGE_DELAY_MS = 500L; // Debounce delay before processing viewport changes
3333

3434
private final @NotNull Editor editor;
35-
private int firstViewportLine;
36-
private int lastViewportLine;
35+
private int firstViewportLine = -1;
36+
private int lastViewportLine = -1;
3737
private long modificationStamp;
3838
private volatile Alarm scrollStopAlarm = null;
3939

@@ -61,7 +61,7 @@ public void updateViewportLines(@NotNull Rectangle visibleArea) {
6161
/**
6262
* Gets the first visible line in the editor's viewport.
6363
*
64-
* @return The first visible line number.
64+
* @return The first visible line number, or -1 if not yet initialized.
6565
*/
6666
public int getFirstViewportLine() {
6767
return firstViewportLine;
@@ -70,12 +70,21 @@ public int getFirstViewportLine() {
7070
/**
7171
* Gets the last visible line in the editor's viewport.
7272
*
73-
* @return The last visible line number.
73+
* @return The last visible line number, or -1 if not yet initialized.
7474
*/
7575
public int getLastViewportLine() {
7676
return lastViewportLine;
7777
}
7878

79+
/**
80+
* Checks if the viewport has been initialized.
81+
*
82+
* @return true if viewport lines have been set, false otherwise.
83+
*/
84+
public boolean isViewportInitialized() {
85+
return firstViewportLine != -1;
86+
}
87+
7988
/**
8089
* Resolves unresolved code lens elements in the current viewport and refreshes the editor accordingly.
8190
* If a previous refresh task is pending, it will be cancelled before scheduling a new one.
@@ -136,8 +145,14 @@ public void dispose() {
136145
* @return true if the file content has changed and false otherwise.
137146
*/
138147
public boolean hasFileChanged(@NotNull PsiFile file) {
139-
if (modificationStamp != file.getModificationStamp()) {
140-
modificationStamp = file.getModificationStamp();
148+
long currentStamp = file.getModificationStamp();
149+
if (modificationStamp == 0) {
150+
// First time initialization - don't consider this as a change
151+
modificationStamp = currentStamp;
152+
return false;
153+
}
154+
if (modificationStamp != currentStamp) {
155+
modificationStamp = currentStamp;
141156
return true;
142157
}
143158
return false;

0 commit comments

Comments
 (0)