Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Showing lines of code in chat context #503

Open
jnsw opened this issue Feb 7, 2025 · 1 comment
Open

[FEATURE] Showing lines of code in chat context #503

jnsw opened this issue Feb 7, 2025 · 1 comment
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@jnsw
Copy link

jnsw commented Feb 7, 2025

if i add lines with cmd + i, i would like to see which lines are currently covered, like 34:55 or similar, like supermaven does that probably

genie:

Image

supermaven:

Image

@stephanj stephanj added the enhancement New feature or request label Feb 7, 2025
@stephanj stephanj changed the title Showing lines of code in chat context [FEATURE] Showing lines of code in chat context Feb 7, 2025
@stephanj
Copy link
Contributor

stephanj commented Feb 7, 2025

DeepSeek R1 suggestino: To capture and store the actual line numbers of the selected code in your plugin, follow these steps:

  1. Define Keys for Line Numbers:
    Add new keys in AddSnippetAction to store the start and end line numbers.

  2. Modify Method Parameters:
    Update the createAndAddVirtualFile method to accept an Editor instance instead of SelectionModel for better access to the document.

  3. Calculate Line Numbers:
    Use the editor's Document to convert selection offsets to line numbers.

  4. Store Line Numbers:
    Save the calculated line numbers in the virtual file's user data.

Here's the modified code:

// In AddSnippetAction.java
public static final Key<Integer> SELECTION_START_LINE_KEY = Key.create("SELECTION_START_LINE");
public static final Key<Integer> SELECTION_END_LINE_KEY = Key.create("SELECTION_END_LINE");

// Update actionPerformed to pass the editor
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
    Editor editor = e.getData(CommonDataKeys.EDITOR);
    VirtualFile selectedFile = e.getData(CommonDataKeys.VIRTUAL_FILE);

    if (editor != null && selectedFile != null) {
        ensureToolWindowVisible(e.getProject());

        SelectionModel selectionModel = editor.getSelectionModel();
        String selectedText = selectionModel.getSelectedText();
        if (selectedText != null) {
            createAndAddVirtualFile(e.getProject(), selectedFile, editor, selectedText);
        } else {
            addSelectedFile(e.getProject(), selectedFile);
        }
    }
}

// Modify createAndAddVirtualFile to take Editor and compute lines
private void createAndAddVirtualFile(@Nullable Project project, @NotNull VirtualFile originalFile,
                                     @NotNull Editor editor, String selectedText) {
    SelectionModel selectionModel = editor.getSelectionModel();
    Document document = editor.getDocument();
    
    int startOffset = selectionModel.getSelectionStart();
    int endOffset = selectionModel.getSelectionEnd();
    int startLine = document.getLineNumber(startOffset);
    int endLine = document.getLineNumber(endOffset);

    LightVirtualFile virtualFile = new LightVirtualFile(originalFile.getName(), selectedText);
    virtualFile.setFileType(fileType);
    virtualFile.putUserData(ORIGINAL_FILE_KEY, originalFile);
    virtualFile.putUserData(SELECTED_TEXT_KEY, selectedText);
    virtualFile.putUserData(SELECTION_START_KEY, startOffset);
    virtualFile.putUserData(SELECTION_END_KEY, endOffset);
    virtualFile.putUserData(SELECTION_START_LINE_KEY, startLine);
    virtualFile.putUserData(SELECTION_END_LINE_KEY, endLine);

    FileListManager.getInstance().addFile(project, virtualFile);
}

Explanation:

  • Line Number Calculation:

    • document.getLineNumber(offset) converts a character offset to a line number (0-based).
  • Data Storage:

    • The line numbers are stored in the LightVirtualFile's user data, making them accessible later via the FileListManager.

Retrieving Line Numbers:
When accessing the stored files, retrieve the line numbers using the keys:

Integer startLine = virtualFile.getUserData(AddSnippetAction.SELECTION_START_LINE_KEY);
Integer endLine = virtualFile.getUserData(AddSnippetAction.SELECTION_END_LINE_KEY);

This approach ensures that each code snippet's original line numbers are tracked and can be used for reference or display within your plugin.

@stephanj stephanj added this to the v0.5.0 milestone Feb 10, 2025
@stephanj stephanj self-assigned this Feb 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants