File tree Expand file tree Collapse file tree 1 file changed +62
-0
lines changed
Expand file tree Collapse file tree 1 file changed +62
-0
lines changed Original file line number Diff line number Diff line change @@ -1568,4 +1568,66 @@ function execPromise(command: string): Promise<{ stdout: string; stderr: string
15681568 } ) ;
15691569}
15701570
1571+ // 获取VSCode工作区路径工具
1572+ export const getVsCodeWorkspacePath : Tool = {
1573+ name : 'get_vscode_workspace_path' ,
1574+ description : `获取当前VSCode工作区根目录的绝对路径。支持单工作区和多工作区模式。
1575+ 当工作区包含多个根目录时,返回所有路径的列表。适用于需要定位项目根目录或验证工作区配置的场景。
1576+
1577+ 使用场景:
1578+ - 需要确定当前项目的绝对路径
1579+ - 处理多工作区项目时需要所有根路径
1580+ - 自动化脚本需要基于工作区路径进行文件操作
1581+ - 调试时验证环境路径是否正确
1582+
1583+ 示例:
1584+ 1. 单工作区 -> "/User/projects/web-app"
1585+ 2. 多工作区 -> "/User/projects/frontend; /User/projects/backend"
1586+ 3. 未打开工作区 -> "当前未打开任何工作区目录"` ,
1587+
1588+ // 不需要输入参数
1589+ parameters : {
1590+ type : 'object' ,
1591+ properties : { } ,
1592+ required : [ ]
1593+ } ,
1594+
1595+ function : async ( ) => {
1596+ try {
1597+ // 获取工作区文件夹配置
1598+ const workspaceFolders = vscode . workspace . workspaceFolders ;
1599+
1600+ // 处理未打开工作区的情况
1601+ if ( ! workspaceFolders || workspaceFolders . length === 0 ) {
1602+ return "当前未打开任何工作区目录" ;
1603+ }
1604+
1605+ // 处理多工作区路径格式化
1606+ const pathList = workspaceFolders . map ( folder => {
1607+ // 确保返回绝对路径
1608+ const rawPath = folder . uri . fsPath ;
1609+
1610+ // 处理Windows路径的反斜杠问题
1611+ return process . platform === 'win32'
1612+ ? rawPath . replace ( / \\ / g, '/' ) // 统一转换为正斜杠
1613+ : path . resolve ( rawPath ) ; // Linux/macOS直接解析
1614+ } ) ;
1615+
1616+ // 去重处理(防止异常配置)
1617+ const uniquePaths = Array . from ( new Set ( pathList ) ) ;
1618+
1619+ return uniquePaths . join ( '; ' ) ;
1620+ } catch ( error ) {
1621+ // 错误处理流程
1622+ const errorMessage = ( error as Error ) . message ;
1623+ vscode . window . showErrorMessage ( `路径获取失败: ${ errorMessage } ` ) ;
1624+
1625+ // 返回结构化的错误信息
1626+ return `ERROR: 无法获取工作区路径 (${ errorMessage } )` ;
1627+ }
1628+ }
1629+ } ;
1630+
1631+ registerTool ( getVsCodeWorkspacePath ) ;
1632+
15711633
You can’t perform that action at this time.
0 commit comments