Skip to content

Commit 924e4f7

Browse files
committed
feat:改进输入框
1 parent eeb2870 commit 924e4f7

File tree

4 files changed

+129
-12
lines changed

4 files changed

+129
-12
lines changed

package.json

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@
3939
"command": "codeReDesign.applyCvb",
4040
"title": "CodeReDesign: Apply CVB to Workspace"
4141
},
42-
{
43-
"command": "codeReDesign.applyThisCvb",
44-
"title": "CodeReDesign: Apply this CVB to Workspace"
45-
},
4642
{
4743
"command": "codeReDesign.stopOperation",
4844
"title": "CodeReDesign: Stop Operation"
@@ -51,13 +47,21 @@
5147
"command": "codeReDesign.analyzeCode",
5248
"title": "CodeReDesign: Analyze Code"
5349
},
50+
51+
{
52+
"command": "codeReDesign.applyThisCvb",
53+
"title": "CodeReDesign: Apply this CVB to Workspace",
54+
"when": "false"
55+
},
5456
{
5557
"command": "codeReDesign.uploadThisCvb",
56-
"title": "CodeReDesign: Upload this CVB and Call API"
58+
"title": "CodeReDesign: Upload this CVB and Call API",
59+
"when": "false"
5760
},
5861
{
5962
"command": "codeReDesign.analyzeThisCvb",
60-
"title": "CodeReDesign: Analyze this CVB"
63+
"title": "CodeReDesign: Analyze this CVB",
64+
"when": "false"
6165
}
6266
],
6367
"views": {

src/UIComponents.ts

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import * as vscode from 'vscode';
2+
3+
interface InputMultiLineBoxOptions {
4+
prompt: string;
5+
placeHolder?: string;
6+
title?: string;
7+
}
8+
9+
let currentPanel: vscode.WebviewPanel | undefined = undefined; // 当前打开的 Webview 面板
10+
11+
export async function showInputMultiLineBox(options: InputMultiLineBoxOptions): Promise<string | undefined> {
12+
return new Promise((resolve) => {
13+
// 如果已有面板,先关闭
14+
if (currentPanel) {
15+
currentPanel.dispose();
16+
}
17+
18+
// 创建新的 Webview 面板
19+
currentPanel = vscode.window.createWebviewPanel(
20+
'multiLineInput',
21+
options.title || 'Multi-line Input',
22+
vscode.ViewColumn.One,
23+
{ enableScripts: true, localResourceRoots: [] } // 允许 JavaScript
24+
);
25+
26+
// 设置 Webview 内容
27+
currentPanel.webview.html = getWebviewContent(options.prompt, options.placeHolder || "");
28+
29+
// 监听 Webview 消息
30+
currentPanel.webview.onDidReceiveMessage(
31+
(message) => {
32+
if (message.command === 'submit') {
33+
resolve(message.text); // 返回用户输入的文本
34+
if (currentPanel) {
35+
currentPanel.dispose(); // 关闭 Webview
36+
currentPanel = undefined; // 清空当前面板引用
37+
}
38+
}
39+
},
40+
undefined
41+
);
42+
43+
// 监听 Webview 关闭,避免无限等待
44+
currentPanel.onDidDispose(() => {
45+
currentPanel = undefined; // 清空面板引用
46+
resolve(undefined);
47+
});
48+
});
49+
}
50+
51+
// Webview HTML 内容,使用 Monaco Editor
52+
function getWebviewContent(prompt: string, placeHolder: string): string {
53+
return `
54+
<!DOCTYPE html>
55+
<html lang="en">
56+
<head>
57+
<meta charset="UTF-8">
58+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
59+
<style>
60+
body {
61+
font-family: "Consolas", "Courier New", monospace;
62+
padding: 10px;
63+
background-color: #1e1e1e;
64+
color: white;
65+
height: 100%;
66+
}
67+
textarea {
68+
width: 100%;
69+
height: 50vh; /* 使输入框高度为视口的一半 */
70+
background-color: #252526;
71+
color: white;
72+
border: 1px solid #444;
73+
padding: 10px;
74+
font-size: 14px;
75+
border-radius: 4px;
76+
}
77+
button {
78+
margin-top: 10px;
79+
background-color: #007acc;
80+
color: white;
81+
border: none;
82+
padding: 10px;
83+
font-size: 14px;
84+
cursor: pointer;
85+
border-radius: 4px;
86+
}
87+
button:hover {
88+
background-color: #005a8c;
89+
}
90+
</style>
91+
</head>
92+
<body>
93+
<h3>${prompt}</h3>
94+
<textarea placeholder="${placeHolder}" id="inputField"></textarea>
95+
<button id="submitButton">Submit</button>
96+
<script>
97+
const vscode = acquireVsCodeApi();
98+
document.getElementById('submitButton').addEventListener('click', () => {
99+
const inputText = document.getElementById('inputField').value;
100+
vscode.postMessage({
101+
command: 'submit',
102+
text: inputText
103+
});
104+
});
105+
</script>
106+
</body>
107+
</html>
108+
`;
109+
}

src/extension.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { generateCvb, applyCvbToWorkspace, generateTimestamp, Cvb, TCVB, mergeCv
66
import { queryCodeReDesign, generateFilenameFromRequest, analyzeCode, callDeepSeekFixApi, GetLastMessageBody } from './deepseekApi';
77
import { setupCvbAsMarkdown } from './cvbMarkdownHandler';
88
import { registerCvbContextMenu } from './siderBar';
9+
import { showInputMultiLineBox } from './UIComponents';
910

1011
let currentOperationController: AbortController | null = null;
1112

@@ -161,7 +162,7 @@ export function activate(context: vscode.ExtensionContext) {
161162
return;
162163
}
163164

164-
const userPrompt = await vscode.window.showInputBox({
165+
const userPrompt = await showInputMultiLineBox({
165166
prompt: 'Enter your prompt for the refactoring',
166167
placeHolder: 'e.g., Refactor the code to improve readability',
167168
});
@@ -255,7 +256,7 @@ export function activate(context: vscode.ExtensionContext) {
255256
const cvbContent = fs.readFileSync(cvbFilePath, 'utf-8');
256257

257258
// 获取用户的分析需求
258-
const userRequest = await vscode.window.showInputBox({
259+
const userRequest = await showInputMultiLineBox({
259260
prompt: 'Enter your analysis request',
260261
placeHolder: 'e.g., Analyze the code for potential bugs',
261262
});

src/siderBar.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import * as vscode from 'vscode';
22
import * as path from 'path';
33
import * as fs from 'fs';
4-
import { applyCvbToWorkspace, generateTimestamp, Cvb, TCVB, mergeCvb } from './cvbManager';
5-
import { queryCodeReDesign, analyzeCode, generateFilenameFromRequest } from './deepseekApi';
4+
import { applyCvbToWorkspace} from './cvbManager';
5+
import { analyzeCode } from './deepseekApi';
66
import { getCurrentOperationController, resetCurrentOperationController, clearCurrentOperationController, doUploadCommand} from './extension';
7+
import { showInputMultiLineBox } from './UIComponents';
78

89
export function registerCvbContextMenu(context: vscode.ExtensionContext) {
910

@@ -128,6 +129,7 @@ class CvbFile extends vscode.TreeItem {
128129
arguments: [uri]
129130
};
130131
this.iconPath = new vscode.ThemeIcon('files'); // 使用代码图标
132+
this.resourceUri = uri;
131133
this.contextValue = 'cvbFile'; // 上下文值保持不变
132134
}
133135
}
@@ -144,6 +146,7 @@ class MDFile extends vscode.TreeItem {
144146
arguments: [uri]
145147
};
146148
this.iconPath = new vscode.ThemeIcon('comment-discussion'); // 使用文档图标
149+
this.resourceUri = uri;
147150
this.contextValue = 'mdFile'; // 新的上下文值
148151
}
149152
}
@@ -192,7 +195,7 @@ async function uploadThisCvb(filePath: string) {
192195
}
193196
// 测试 end
194197
*/
195-
const userPrompt = await vscode.window.showInputBox({
198+
const userPrompt = await showInputMultiLineBox({
196199
prompt: 'Enter your prompt for the refactoring',
197200
placeHolder: 'e.g., Refactor the code to improve readability',
198201
});
@@ -209,7 +212,7 @@ async function uploadThisCvb(filePath: string) {
209212
* @param filePath .cvb 文件的路径
210213
*/
211214
async function analyzeThisCvb(filePath: string) {
212-
const userRequest = await vscode.window.showInputBox({
215+
const userRequest = await showInputMultiLineBox({
213216
prompt: 'Enter your analysis request',
214217
placeHolder: 'e.g., Analyze the code for potential bugs',
215218
});

0 commit comments

Comments
 (0)