Files
qwen-code/packages/vscode-ide-companion/src/webview/handlers/EditorMessageHandler.ts
yiliang114 4f63d92bb1 Add unit tests for CLI modules and fix ESLint issues
- Add comprehensive unit tests for all CLI-related modules:
  - CliContextManager
  - CliVersionManager
  - cliDetector
  - CliInstaller
- Fix ESLint issues by replacing @ts-ignore with @ts-expect-error
- Fix any type issues in test files
- Add tests for diff-manager functionality
- Improve loading messages random selection stability

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-28 01:06:21 +08:00

112 lines
2.9 KiB
TypeScript

/**
* @license
* Copyright 2025 Qwen Team
* SPDX-License-Identifier: Apache-2.0
*/
import * as vscode from 'vscode';
import { BaseMessageHandler } from './BaseMessageHandler.js';
import { getFileName } from '../utils/webviewUtils.js';
/**
* Editor message handler
* Handles all editor state-related messages
*/
export class EditorMessageHandler extends BaseMessageHandler {
canHandle(messageType: string): boolean {
return ['getActiveEditor', 'focusActiveEditor'].includes(messageType);
}
async handle(message: { type: string; data?: unknown }): Promise<void> {
switch (message.type) {
case 'getActiveEditor':
await this.handleGetActiveEditor();
break;
case 'focusActiveEditor':
await this.handleFocusActiveEditor();
break;
default:
console.warn(
'[EditorMessageHandler] Unknown message type:',
message.type,
);
break;
}
}
/**
* Get current active editor info
*/
private async handleGetActiveEditor(): Promise<void> {
try {
const activeEditor = vscode.window.activeTextEditor;
if (activeEditor) {
const filePath = activeEditor.document.uri.fsPath;
const fileName = getFileName(filePath);
let selectionInfo = null;
if (!activeEditor.selection.isEmpty) {
const selection = activeEditor.selection;
selectionInfo = {
startLine: selection.start.line + 1,
endLine: selection.end.line + 1,
};
}
this.sendToWebView({
type: 'activeEditorChanged',
data: { fileName, filePath, selection: selectionInfo },
});
} else {
this.sendToWebView({
type: 'activeEditorChanged',
data: { fileName: null, filePath: null, selection: null },
});
}
} catch (error) {
console.error(
'[EditorMessageHandler] Failed to get active editor:',
error,
);
}
}
/**
* Focus on active editor
*/
private async handleFocusActiveEditor(): Promise<void> {
try {
const activeEditor = vscode.window.activeTextEditor;
if (activeEditor) {
await vscode.window.showTextDocument(activeEditor.document, {
viewColumn: activeEditor.viewColumn,
preserveFocus: false,
});
} else {
// If no active editor, show file picker
const uri = await vscode.window.showOpenDialog({
defaultUri: vscode.workspace.workspaceFolders?.[0]?.uri,
canSelectMany: false,
canSelectFiles: true,
canSelectFolders: false,
openLabel: 'Open',
});
if (uri && uri.length > 0) {
await vscode.window.showTextDocument(uri[0]);
}
}
} catch (error) {
console.error(
'[EditorMessageHandler] Failed to focus active editor:',
error,
);
vscode.window.showErrorMessage(`Failed to focus editor: ${error}`);
}
}
}