How to efficiently read a lot of code from the active editor? #2754
-
I made this API a while back that takes in some code as a string and uses an LLM to generate a list of messages reacting to that code. My idea was to turn that into an extension that could provide live feedback as you edit code (sort of like you're a streamer, lol). The only thing is that I'm not sure what the best way to read the editor content is. On one hand, I want to provide the API with enough code context to give meaningful feedback. However, I don't want to overload it with like too many lines of Python or C. Any ideas for how I could read the editor, based on the constraints I described? (I know it's a tough problem 😆 ) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi @ryyHardy , I think the best way may vary depending on the content you need to read. As more code you need, more complex it may be. Either way, if you need the content from the editor instead of the disk, you should use the I wonder if the currently visible text of the editor is a good start. If yes, you could use the function getVisibleText(): string {
const editor = window.activeTextEditor;
if (!editor) {
return '';
}
const visibleRanges = editor.visibleRanges;
return visibleRanges.map(range => editor.document.getText(range)).join('\n');
} Maybe you need the content of the function or class the user is working. If that's the case, you could use the const docSymbols = await commands.executeCommand(
'vscode.executeDocumentSymbolProvider',
window.activeTextEditor.document.uri
) as DocumentSymbol[]; I'm not sure this will provide you the amount of code and context you need, but may be a start. Otherwise you should look for incremental parsing techniques, for a more robust solution. Hope this helps |
Beta Was this translation helpful? Give feedback.
Hi @ryyHardy ,
I think the best way may vary depending on the content you need to read. As more code you need, more complex it may be. Either way, if you need the content from the editor instead of the disk, you should use the
TextEditor
API.I wonder if the currently visible text of the editor is a good start. If yes, you could use the
TextEditor.visibleRanges
property and have access to the text that is being displayed to the user. Something like this: