feat:在没有打开工作区时,相关功能弹出错误提示
This commit is contained in:
parent
66e1b54cc3
commit
318cf64ace
@ -15,5 +15,6 @@
|
||||
"join-project": "Participate in the project",
|
||||
"comment-plugin": "Comment Plugin",
|
||||
"preset-env-sync": "Preset environment variables synchronized successfully",
|
||||
"preset-env-sync.fail": "Failed to sync preset environment variables"
|
||||
"preset-env-sync.fail": "Failed to sync preset environment variables",
|
||||
"error.notOpenWorkspace": "No workspace is currently open in VSCode. Please open a workspace (e.g., open a folder) first."
|
||||
}
|
@ -15,5 +15,7 @@
|
||||
"join-project": "プロジェクトに参加する",
|
||||
"comment-plugin": "コメントプラグイン",
|
||||
"preset-env-sync": "プリセット環境変数の同期が完了しました",
|
||||
"preset-env-sync.fail": "プリセット環境変数の同期に失敗しました"
|
||||
"preset-env-sync.fail": "プリセット環境変数の同期に失敗しました",
|
||||
"error.notOpenWorkspace": "現在、VSCode でワークスペースが開かれていません。まずワークスペース(例:フォルダーを開く)を開いてください。"
|
||||
|
||||
}
|
@ -15,5 +15,6 @@
|
||||
"join-project": "参与项目",
|
||||
"comment-plugin": "评论插件",
|
||||
"preset-env-sync": "预设环境变量同步完成",
|
||||
"preset-env-sync.fail": "预设环境变量同步失败"
|
||||
"preset-env-sync.fail": "预设环境变量同步失败",
|
||||
"error.notOpenWorkspace": "当前VScode没有打开工作区,请先打开工作区(例如打开文件夹)"
|
||||
}
|
@ -2,6 +2,7 @@ import * as vscode from 'vscode';
|
||||
import * as os from 'os';
|
||||
import * as fspath from 'path';
|
||||
import * as fs from 'fs';
|
||||
import { t } from './i18n';
|
||||
|
||||
export type FsPath = string;
|
||||
export const panels = new Map<FsPath, vscode.WebviewPanel>();
|
||||
@ -96,7 +97,7 @@ export function getConnectionConfig() {
|
||||
export function getWorkspaceConnectionConfigPath() {
|
||||
const workspace = getWorkspacePath();
|
||||
if (!workspace) {
|
||||
throw new Error('No workspace found. Please open a folder in VSCode first.');
|
||||
return null; // 如果没有工作区,则返回 null
|
||||
}
|
||||
const configDir = fspath.join(workspace, '.openmcp');
|
||||
if (!fs.existsSync(configDir)) {
|
||||
@ -110,14 +111,14 @@ export function getWorkspaceConnectionConfigPath() {
|
||||
* @description 获取工作区的连接信息,工作区的连接文件的路径都是相对路径,以 {workspace} 开头
|
||||
* @param workspace
|
||||
*/
|
||||
export function getWorkspaceConnectionConfig() {
|
||||
export function getWorkspaceConnectionConfig():IConnectionConfig| null {
|
||||
if (_workspaceConnectionConfig) {
|
||||
return _workspaceConnectionConfig;
|
||||
}
|
||||
|
||||
const workspace = getWorkspacePath();
|
||||
if (!workspace) {
|
||||
throw new Error('No workspace found. Please open a folder in VSCode first.');
|
||||
return null; // 如果没有工作区,则返回 null
|
||||
}
|
||||
const configDir = fspath.join(workspace, '.openmcp');
|
||||
const connectionConfig = fspath.join(configDir, CONNECTION_CONFIG_NAME);
|
||||
@ -228,6 +229,10 @@ export function updateWorkspaceConnectionConfig(
|
||||
) {
|
||||
const connectionItem = getWorkspaceConnectionConfigItemByName(name);
|
||||
const workspaceConnectionConfig = getWorkspaceConnectionConfig();
|
||||
if (!workspaceConnectionConfig) {
|
||||
console.error('没有工作区连接配置文件,请先创建一个工作区连接');
|
||||
return;
|
||||
}
|
||||
|
||||
data.forEach(item => {
|
||||
item.cwd = item.cwd?.replace(/\\/g, '/');
|
||||
@ -326,6 +331,10 @@ export function getWorkspacePath() {
|
||||
export function getWorkspaceConnectionConfigItemByPath(absPath: string) {
|
||||
const workspacePath = getWorkspacePath();
|
||||
const workspaceConnectionConfig = getWorkspaceConnectionConfig();
|
||||
if (!workspaceConnectionConfig) {
|
||||
return null; // 如果没有工作区连接配置文件,则返回 null
|
||||
}
|
||||
|
||||
|
||||
const normaliseAbsPath = absPath.replace(/\\/g, '/');
|
||||
for (let item of workspaceConnectionConfig.items) {
|
||||
@ -345,9 +354,10 @@ export function getWorkspaceConnectionConfigItemByPath(absPath: string) {
|
||||
* @param absPath
|
||||
*/
|
||||
export function getWorkspaceConnectionConfigItemByName(name: string) {
|
||||
const workspacePath = getWorkspacePath();
|
||||
const workspaceConnectionConfig = getWorkspaceConnectionConfig();
|
||||
|
||||
if (!workspaceConnectionConfig) {
|
||||
return null; // 如果没有工作区连接配置文件,则返回 null
|
||||
}
|
||||
for (let item of workspaceConnectionConfig.items) {
|
||||
const nItem = Array.isArray(item) ? item[0] : item;
|
||||
if (nItem.name === name) {
|
||||
|
@ -4,6 +4,7 @@ import { getWorkspaceConnectionConfig, getWorkspaceConnectionConfigPath, getWork
|
||||
import { ConnectionViewItem } from './common.js';
|
||||
import { revealOpenMcpWebviewPanel } from '../webview/webview.service.js';
|
||||
import { acquireUserCustomConnection, deleteUserConnection } from './workspace.service.js';
|
||||
import { t } from '../i18n/index.js';
|
||||
|
||||
@RegisterTreeDataProvider('openmcp.sidebar.workspace-connection')
|
||||
export class McpWorkspaceConnectProvider implements vscode.TreeDataProvider<ConnectionViewItem> {
|
||||
@ -60,14 +61,18 @@ export class McpWorkspaceConnectProvider implements vscode.TreeDataProvider<Conn
|
||||
|
||||
@RegisterCommand('addConnection')
|
||||
public async addConnection(context: vscode.ExtensionContext) {
|
||||
|
||||
const workspaceConnectionConfig = getWorkspaceConnectionConfig();
|
||||
if (!workspaceConnectionConfig) {
|
||||
vscode.window.showErrorMessage('OpenMCP: ' + t('error.notOpenWorkspace'));
|
||||
return;
|
||||
}
|
||||
const item = await acquireUserCustomConnection();
|
||||
|
||||
if (item.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const workspaceConnectionConfig = getWorkspaceConnectionConfig();
|
||||
|
||||
workspaceConnectionConfig.items.push(item);
|
||||
saveWorkspaceConnectionConfig(getWorkspacePath());
|
||||
|
||||
@ -78,6 +83,10 @@ export class McpWorkspaceConnectProvider implements vscode.TreeDataProvider<Conn
|
||||
@RegisterCommand('openConfiguration')
|
||||
public async openConfiguration(context: vscode.ExtensionContext, view: ConnectionViewItem) {
|
||||
const configPath = getWorkspaceConnectionConfigPath();
|
||||
if (!configPath) {
|
||||
vscode.window.showErrorMessage('OpenMCP: ' + t('error.notOpenWorkspace'));
|
||||
return;
|
||||
}
|
||||
const uri = vscode.Uri.file(configPath);
|
||||
vscode.commands.executeCommand('vscode.open', uri);
|
||||
}
|
||||
|
@ -22,6 +22,10 @@ export async function deleteUserConnection(item: McpOptions[] | McpOptions) {
|
||||
}
|
||||
|
||||
const workspaceConnectionConfig = getWorkspaceConnectionConfig();
|
||||
if (!workspaceConnectionConfig) {
|
||||
vscode.window.showErrorMessage(t('error.notOpenWorkspace'));
|
||||
return; // 没有打开工作区
|
||||
}
|
||||
|
||||
// 从配置中移除该连接项
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user