Repository navigation
feat(jetbrains): add editor context and prompt attachments #13015
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
62923ad
feat(jetbrains): add automatic editor context
kirillk 74470aa
fix(jetbrains): render prompt attachments as chips
kirillk f66b7ef
fix(jetbrains): harden editor context and fix tool-view placement
kirillk 9ebe1f6
fix(jetbrains): align prompt attachment chips
kirillk 0293919
fix(jetbrains): trim prompt attachment panel chrome
kirillk aaf2820
fix(jetbrains): handle prompt bulk updates and context apply
kirillk 5c08bda
fix(jetbrains): underline prompt attachment file links
kirillk ad933a3
feat(jetbrains): align prompt background with code fragments
kirillk 9f32194
test(jetbrains): stabilize ContextSettingsStateTest against platform app
kirillk 025bbdf
fix(jetbrains): preserve session scroll follow intent
kirillk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@kilocode/kilo-jetbrains": minor | ||
| --- | ||
|
|
||
| Include the active editor file, open files, visible files, and selected text in JetBrains chat context by default, with a Context settings toggle to disable it. Files matched by `.kilocodeignore` (or `.gitignore` plus `.env` files) are excluded, and the default shell is reported to the agent. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
...ains/frontend/src/main/kotlin/ai/kilocode/client/session/context/EditorContextGatherer.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| package ai.kilocode.client.session.context | ||
|
|
||
| import ai.kilocode.client.plugin.KiloPluginSettings | ||
| import ai.kilocode.client.vfs.KiloVirtualFileSystem | ||
| import ai.kilocode.log.KiloLog | ||
| import ai.kilocode.rpc.dto.EditorContextDto | ||
| import ai.kilocode.rpc.dto.PromptPartDto | ||
| import com.intellij.codeWithMe.ClientId | ||
| import com.intellij.openapi.editor.Editor | ||
| import com.intellij.openapi.fileEditor.FileDocumentManager | ||
| import com.intellij.openapi.fileEditor.FileEditorManager | ||
| import com.intellij.openapi.fileEditor.impl.EditorHistoryManager | ||
| import com.intellij.openapi.project.Project | ||
| import com.intellij.openapi.util.SystemInfo | ||
| import com.intellij.openapi.vfs.VirtualFile | ||
| import com.intellij.util.EnvironmentUtil | ||
| import java.nio.file.Path | ||
| import kotlin.io.path.name | ||
|
|
||
| /** | ||
| * Reads the active/open editors and current selection at prompt-send time. | ||
| * | ||
| * Split mode caveat: the chat UI runs under a non-local [ClientId], so | ||
| * [FileEditorManager.getOpenFiles]/[FileEditorManager.getSelectedTextEditor] take | ||
| * the per-client branch and return nothing. The `*WithRemotes` variants read the | ||
| * local composites directly and are the ones that actually see the user's tabs. | ||
| * These APIs are `@ApiStatus.Experimental`. | ||
| */ | ||
| internal object EditorContextGatherer { | ||
| private val LOG = KiloLog.create(EditorContextGatherer::class.java) | ||
|
|
||
| // Resolved once per process; the login shell does not change during a session. | ||
| private val shell: String? by lazy { | ||
| if (SystemInfo.isWindows) EnvironmentUtil.getValue("COMSPEC") else EnvironmentUtil.getValue("SHELL") | ||
| } | ||
|
|
||
| data class Result( | ||
| val context: EditorContextDto?, | ||
| val selection: PromptPartDto?, | ||
| ) | ||
|
|
||
| fun gather(project: Project, root: String): Result { | ||
| if (!KiloPluginSettings.getAutoEditorContext()) { | ||
| LOG.debug { "kind=editor-context enabled=false" } | ||
| return Result(null, null) | ||
| } | ||
| val manager = FileEditorManager.getInstance(project) | ||
| val base = Path.of(root).toAbsolutePath().normalize() | ||
| val openFiles = manager.openFilesWithRemotes | ||
| val editor = manager.selectedTextEditorWithRemotes.firstOrNull() | ||
| val activeFile = editor?.let { file(it) } ?: lastOpen(project, openFiles) ?: openFiles.firstOrNull() | ||
| val ignore = KiloIgnore.load(rootDir(listOfNotNull(activeFile) + openFiles, base)) | ||
|
|
||
| fun keep(file: VirtualFile?): String? = rel(file, base)?.takeUnless { ignore.ignored(it) } | ||
|
|
||
| val active = keep(activeFile) | ||
| val openRel = openFiles.mapNotNull { rel(it, base) }.distinct() | ||
| val open = openRel.filterNot { ignore.ignored(it) }.take(20) | ||
| val visible = (listOfNotNull(activeFile) + manager.selectedTextEditorWithRemotes.mapNotNull { file(it) }) | ||
| .mapNotNull { keep(it) } | ||
| .distinct() | ||
| .take(200) | ||
| val ctx = EditorContextDto( | ||
| activeFile = active, | ||
| openTabs = open.takeIf { it.isNotEmpty() }, | ||
| visibleFiles = visible.takeIf { it.isNotEmpty() }, | ||
| shell = shell, | ||
| ).takeIf { active != null || open.isNotEmpty() || visible.isNotEmpty() || shell != null } | ||
| val part = editor?.let { selection(it, base, ignore) } | ||
| LOG.debug { | ||
| val first = openFiles.firstOrNull() | ||
| val filtered = openRel.count { ignore.ignored(it) } | ||
| "kind=editor-context enabled=true localId=${ClientId.isCurrentlyUnderLocalId}" + | ||
| " rawOpen=${openFiles.size} rawSel=${manager.selectedTextEditorWithRemotes.size}" + | ||
| " active=${active ?: "none"} open=${open.size} visible=${visible.size} selection=${part != null}" + | ||
| " ignored=$filtered shell=${shell ?: "none"}" + | ||
| " firstFs=${first?.fileSystem?.protocol ?: "none"} firstLocal=${first?.isInLocalFileSystem ?: false}" + | ||
| " firstPath=${first?.path ?: "none"}" | ||
| } | ||
| return Result(ctx, part) | ||
| } | ||
|
|
||
| private fun lastOpen(project: Project, open: List<VirtualFile>): VirtualFile? { | ||
| val set = open.toHashSet() | ||
| return EditorHistoryManager.getInstance(project).fileList.lastOrNull { it in set } | ||
| } | ||
|
|
||
| // Walks up from an open editor file to the workspace-root directory so ignore | ||
| // files can be read via the same (possibly remote) VFS as the editor files. | ||
| private fun rootDir(files: List<VirtualFile>, root: Path): VirtualFile? { | ||
| for (file in files) { | ||
| var cur: VirtualFile? = file | ||
| while (cur != null) { | ||
| if (runCatching { Path.of(cur.path).toAbsolutePath().normalize() }.getOrNull() == root) return cur | ||
| cur = cur.parent | ||
| } | ||
| } | ||
| return null | ||
| } | ||
|
|
||
| private fun selection(editor: Editor, root: Path, ignore: KiloIgnore): PromptPartDto? { | ||
| val model = editor.selectionModel | ||
| if (!model.hasSelection()) return null | ||
| val file = file(editor) ?: return null | ||
| val path = local(file, root) ?: return null | ||
| if (ignore.ignored(root.relativize(path).toString())) return null | ||
| val start = model.selectionStart | ||
| val end = model.selectionEnd | ||
| if (start == end) return null | ||
| val doc = editor.document | ||
| val last = (end - 1).coerceAtLeast(start) | ||
| val first = doc.getLineNumber(start) + 1 | ||
| val line = doc.getLineNumber(last) + 1 | ||
| val url = "${path.toUri()}?start=$first&end=$line" | ||
| return PromptPartDto( | ||
| type = "file", | ||
| mime = "text/plain", | ||
| url = url, | ||
| filename = path.name, | ||
| ) | ||
| } | ||
|
|
||
| private fun file(editor: Editor): VirtualFile? = FileDocumentManager.getInstance().getFile(editor.document) | ||
|
|
||
| private fun rel(file: VirtualFile?, root: Path): String? { | ||
| val path = file?.let { local(it, root) } ?: return null | ||
| return root.relativize(path).toString() | ||
| } | ||
|
|
||
| private fun local(file: VirtualFile, root: Path): Path? { | ||
| if (file.fileSystem.protocol == KiloVirtualFileSystem.PROTOCOL) return null | ||
| val path = Path.of(file.path).toAbsolutePath().normalize() | ||
|
kirillk marked this conversation as resolved.
Resolved
|
||
| if (!path.startsWith(root)) return null | ||
| return path | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.