mirror of
https://github.com/QwenLM/qwen-code.git
synced 2025-12-21 01:07:46 +00:00
[ide-mode] Send the cursor and selected text from the IDE server (#4621)
This commit is contained in:
@@ -18,6 +18,7 @@ import { RecentFilesManager } from './recent-files-manager.js';
|
||||
|
||||
const MCP_SESSION_ID_HEADER = 'mcp-session-id';
|
||||
const IDE_SERVER_PORT_ENV_VAR = 'GEMINI_CLI_IDE_SERVER_PORT';
|
||||
const MAX_SELECTED_TEXT_LENGTH = 16384; // 16 KiB limit
|
||||
|
||||
function sendOpenFilesChangedNotification(
|
||||
transport: StreamableHTTPServerTransport,
|
||||
@@ -29,6 +30,19 @@ function sendOpenFilesChangedNotification(
|
||||
editor && editor.document.uri.scheme === 'file'
|
||||
? editor.document.uri.fsPath
|
||||
: '';
|
||||
const selection = editor?.selection;
|
||||
const cursor = selection
|
||||
? {
|
||||
// This value is a zero-based index, but the vscode IDE is one-based.
|
||||
line: selection.active.line + 1,
|
||||
character: selection.active.character,
|
||||
}
|
||||
: undefined;
|
||||
let selectedText = editor?.document.getText(selection) ?? undefined;
|
||||
if (selectedText && selectedText.length > MAX_SELECTED_TEXT_LENGTH) {
|
||||
selectedText =
|
||||
selectedText.substring(0, MAX_SELECTED_TEXT_LENGTH) + '... [TRUNCATED]';
|
||||
}
|
||||
const notification: JSONRPCNotification = {
|
||||
jsonrpc: '2.0',
|
||||
method: 'ide/openFilesChanged',
|
||||
@@ -37,6 +51,8 @@ function sendOpenFilesChangedNotification(
|
||||
recentOpenFiles: recentFilesManager.recentFiles.filter(
|
||||
(file) => file.filePath !== filePath,
|
||||
),
|
||||
cursor,
|
||||
selectedText,
|
||||
},
|
||||
};
|
||||
log(
|
||||
@@ -69,7 +85,7 @@ export class IDEServer {
|
||||
const mcpServer = createMcpServer();
|
||||
|
||||
const recentFilesManager = new RecentFilesManager(context);
|
||||
const disposable = recentFilesManager.onDidChange(() => {
|
||||
const onDidChangeSubscription = recentFilesManager.onDidChange(() => {
|
||||
for (const transport of Object.values(transports)) {
|
||||
sendOpenFilesChangedNotification(
|
||||
transport,
|
||||
@@ -78,7 +94,7 @@ export class IDEServer {
|
||||
);
|
||||
}
|
||||
});
|
||||
context.subscriptions.push(disposable);
|
||||
context.subscriptions.push(onDidChangeSubscription);
|
||||
|
||||
app.post('/mcp', async (req: Request, res: Response) => {
|
||||
const sessionId = req.headers[MCP_SESSION_ID_HEADER] as
|
||||
|
||||
Reference in New Issue
Block a user