Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@

package software.aws.toolkits.eclipse.amazonq.lsp.model;

import java.util.List;

import org.eclipse.lsp4j.TextDocumentPositionAndWorkDoneProgressParams;

public class InlineCompletionParams extends TextDocumentPositionAndWorkDoneProgressParams {

private InlineCompletionContext context;
private List<String> openTabFilepaths;

public final InlineCompletionContext getContext() {
return context;
Expand All @@ -17,4 +20,12 @@ public final void setContext(final InlineCompletionContext context) {
this.context = context;
}

public final List<String> getOpenTabFilepaths() {
return openTabFilepaths;
}

public final void setOpenTabFilepaths(final List<String> openTabFilepaths) {
this.openTabFilepaths = openTabFilepaths;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

package software.aws.toolkits.eclipse.amazonq.util;

import java.util.List;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.lsp4j.Position;
Expand Down Expand Up @@ -41,6 +43,12 @@ public static InlineCompletionParams cwParamsFromContext(final ITextEditor edito
invocationPosition.setLine(startLine);
invocationPosition.setCharacter(lineOffset);
params.setPosition(invocationPosition);

List<String> openTabFilepaths = QEclipseEditorUtils.getOpenEditorFilePaths();
if (!openTabFilepaths.isEmpty()) {
params.setOpenTabFilepaths(openTabFilepaths);
}

return params;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static software.aws.toolkits.eclipse.amazonq.util.QConstants.Q_INLINE_HINT_TEXT_STYLE;

import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
Expand Down Expand Up @@ -37,6 +38,7 @@
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
Expand Down Expand Up @@ -156,6 +158,37 @@ private static String getOpenFilePath(final IEditorInput editorInput) {
}
}

/**
* Returns file paths for all currently open editor tabs, excluding in-memory
* editors. Used to provide supplemental context for inline completions.
*/
public static List<String> getOpenEditorFilePaths() {
List<String> filePaths = new ArrayList<>();
try {
IWorkbenchPage page = getActivePage();
if (page == null) {
return filePaths;
}
for (IEditorReference editorRef : page.getEditorReferences()) {
try {
IEditorInput input = editorRef.getEditorInput();
if (input instanceof InMemoryInput) {
continue;
}
String path = getOpenFilePath(input);
if (path != null && !path.isEmpty()) {
filePaths.add(path);
}
} catch (Exception e) {
Activator.getLogger().warn("Skipping editor tab: unable to resolve file path", e);
}
}
} catch (Exception e) {
Activator.getLogger().error("Error collecting open editor file paths", e);
}
return filePaths;
}

public static Optional<Range> getSelectionRange(final ITextEditor editor) {
if (editor == null) {
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package software.aws.toolkits.eclipse.amazonq.util;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.texteditor.ITextEditor;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;

import software.aws.toolkits.eclipse.amazonq.lsp.model.InlineCompletionParams;
import software.aws.toolkits.eclipse.amazonq.lsp.model.InlineCompletionTriggerKind;

public class InlineCompletionUtilsTest {

private static MockedStatic<QEclipseEditorUtils> editorUtilsMock;

@BeforeAll
public static void setUp() {
editorUtilsMock = mockStatic(QEclipseEditorUtils.class);
}

@AfterAll
public static void tearDown() {
if (editorUtilsMock != null) {
editorUtilsMock.close();
}
}

@Test
void testCwParamsIncludesOpenTabFilepaths() throws Exception {
ITextEditor editor = mock(ITextEditor.class);
IEditorInput editorInput = mock(IEditorInput.class);
when(editor.getEditorInput()).thenReturn(editorInput);

ITextViewer viewer = mock(ITextViewer.class);
IDocument document = mock(IDocument.class);
when(viewer.getDocument()).thenReturn(document);
when(document.getLineOfOffset(0)).thenReturn(0);
when(document.getLineOffset(0)).thenReturn(0);

List<String> mockPaths = Arrays.asList("/project/src/Foo.java", "/project/src/Bar.java");
editorUtilsMock.when(() -> QEclipseEditorUtils.getOpenFileUri(any(IEditorInput.class)))
.thenReturn(Optional.of("file:///project/src/Active.java"));
editorUtilsMock.when(QEclipseEditorUtils::getOpenEditorFilePaths)
.thenReturn(mockPaths);

InlineCompletionParams params = InlineCompletionUtils.cwParamsFromContext(
editor, viewer, 0, InlineCompletionTriggerKind.Invoke);

assertNotNull(params.getOpenTabFilepaths());
assertEquals(2, params.getOpenTabFilepaths().size());
assertTrue(params.getOpenTabFilepaths().contains("/project/src/Foo.java"));
assertTrue(params.getOpenTabFilepaths().contains("/project/src/Bar.java"));
}

@Test
void testCwParamsOmitsOpenTabFilepathsWhenEmpty() throws Exception {
ITextEditor editor = mock(ITextEditor.class);
IEditorInput editorInput = mock(IEditorInput.class);
when(editor.getEditorInput()).thenReturn(editorInput);

ITextViewer viewer = mock(ITextViewer.class);
IDocument document = mock(IDocument.class);
when(viewer.getDocument()).thenReturn(document);
when(document.getLineOfOffset(0)).thenReturn(0);
when(document.getLineOffset(0)).thenReturn(0);

editorUtilsMock.when(() -> QEclipseEditorUtils.getOpenFileUri(any(IEditorInput.class)))
.thenReturn(Optional.of("file:///project/src/Active.java"));
editorUtilsMock.when(QEclipseEditorUtils::getOpenEditorFilePaths)
.thenReturn(Collections.emptyList());

InlineCompletionParams params = InlineCompletionUtils.cwParamsFromContext(
editor, viewer, 0, InlineCompletionTriggerKind.Invoke);

assertNull(params.getOpenTabFilepaths());
}

@Test
void testCwParamsSetsCorrectPositionAndContext() throws Exception {
ITextEditor editor = mock(ITextEditor.class);
IEditorInput editorInput = mock(IEditorInput.class);
when(editor.getEditorInput()).thenReturn(editorInput);

ITextViewer viewer = mock(ITextViewer.class);
IDocument document = mock(IDocument.class);
when(viewer.getDocument()).thenReturn(document);
when(document.getLineOfOffset(25)).thenReturn(2);
when(document.getLineOffset(2)).thenReturn(20);

editorUtilsMock.when(() -> QEclipseEditorUtils.getOpenFileUri(any(IEditorInput.class)))
.thenReturn(Optional.of("file:///project/src/Test.java"));
editorUtilsMock.when(QEclipseEditorUtils::getOpenEditorFilePaths)
.thenReturn(Arrays.asList("/project/src/Foo.java"));

InlineCompletionParams params = InlineCompletionUtils.cwParamsFromContext(
editor, viewer, 25, InlineCompletionTriggerKind.Automatic);

assertEquals(2, params.getPosition().getLine());
assertEquals(5, params.getPosition().getCharacter());
assertEquals(InlineCompletionTriggerKind.Automatic, params.getContext().getTriggerKind());
assertNotNull(params.getTextDocument());
assertEquals("file:///project/src/Test.java", params.getTextDocument().getUri());
}
}
Loading
Loading