vscode容器调试使用-1.调试使用深入

1.vscode-python代码调试

创建launch.json文件

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python Debugger: My Current File",
            "type": "debugpy",
            "request": "launch",
            "program": "${file}",
            "cwd":"${workspaceFolder}/Path_to_FlashOcc/",
            "console": "integratedTerminal"
            //"args": [
            //   "${command:pickArgs}"
            //]
        }
    ]
}

1.1 vscode中的${workspaceFolder}等变量

${workspaceFolder} :表示当前workspace文件夹路径,.vscode的绝对路经
${workspaceRootFolderName}:表示workspace的文件夹名,也即Test
${file}:文件自身的绝对路径,也即/home/Coding/Test/.vscode/tasks.json
${relativeFile}:文件在workspace中的路径,也即.vscode/tasks.json
${fileBasenameNoExtension}:当前文件的文件名,不带后缀,也即task
${fileBasename}:当前文件的文件名,tasks.json
${fileDirname}:文件所在的文件夹路径,也即/home/Coding/Test/.vscode
${fileExtname}:当前文件的后缀,也即.json
${lineNumber}:当前文件光标所在的行号
${env:PATH}:系统中的环境变量

1.2 vscode配置文件内容分析

1.2.1 launch.json

launch.json中存放运行或者调试可执行文件时的配置:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++ - Build and debug active file",  // 名字随便取
            "type": "cppdbg",
            "request": "launch",
            // program 指定调试那个可执行文件,需要绝对路径
            // ${fileDirname} 当前打开的文件所在的绝对路径,不包括文件名
            // ${fileBasenameNoExtension} 当前打开的文件的文件名,不包括路径和后缀名
            "program": "${fileDirname}/${fileBasenameNoExtension}.out", 
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}", //  cwd 字段来指定应用程序的当前工作目录,访问资源的时候从cwd指定的目录下访问。
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "Set Disassembly Flavor to Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++ build active file",  // 在执行lanuch.json之前要做的任务
            "miDebuggerPath": "/bin/gdb"        // 指定调试工具
        }
    ]
}

其他参数解释:

${fileBasename}  当前打开的文件名+后缀名,不包括路径
${fileExtname} 当前打开的文件的后缀名
${cwd} the task runner's current working directory on startup
${workspaceFolder} .vscode所在目录的绝对路径

【注】文件名launch.json的前后不能有空格。如果你发现launch.json中明明正确的地方竟然都有红色的波浪线,很可能就是你的文件名launch.json有问题。

1.2.2 tasks.json

tasks.json中存放生成可执行文件的命令:

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ build active file",  // 任务名,在lanuch.json使用此任务名,从而执行此任务
            "command": "/bin/g++",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                // "${file}",
                "${fileDirname}/*.cpp",   // 编译当前打开的文件所在目录下的所有.cpp文件
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.out"  // 目标文件名
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ],
    "version": "2.0.0"
}

1.2.3 c_cpp_properties.json

本小节使用cmake生成compile_commands.json,然后在c_cpp_properties.json中配置c_cpp_properties.json文件实现代码的跳转。具体见vscode使用compile_commands.json配置includePath环境.
这里的代码跳转指的是:光标移动到相应函数,然后按“ctrl+点击”就可以进行跳转。如果需要在debug的时候实现代码跳转,直接在launch.json中指定cmake生成的可执行文件就可以。

vscode中配置C/C++调试环境(launch.json和tasks.json)

windows下json文件:
launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++.exe - build active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "D:\\softwares\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "build"
        }
    ]
}

tasks.json

{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "cppbuild",
			"label": "build",
			"command": "D:\\softwares\\mingw64\\bin\\g++.exe",
			"args": [
				"-g",
				"${file}",
				"-o",
				"${fileDirname}\\${fileBasenameNoExtension}.exe"
			],
			"options": {
				"cwd": "${fileDirname}"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": {
				"kind": "build",
				"isDefault": true
			},
			"detail": "compiler: \"D:\\softwares\\mingw64\\bin\\g++.exe\""
		}
	]
}
<think>我们正在处理的是VSCode 1.101.2版本的源码,需要创建一个新的页面。根据之前的回答,步骤包括创建组件、样式、注册路由、添加菜单命令等。下面将给出具体的代码示例,并注意该版本的特定实现方式。 注意:由于VSCode的版本差异,1.101.2的API可能与最新版不同,因此需要参考该版本的源码结构。 ### 1. 创建页面组件 在`src/vs/workbench/contrib/myPage/browser/myPage.ts`中: ```typescript import 'vs/css!./myPage'; import { Component } from 'vs/workbench/common/component'; import { IThemeService } from 'vs/platform/theme/common/themeService'; import { attachStyler } from 'vs/platform/theme/common/styler'; import { editorBackground } from 'vs/platform/theme/common/colorRegistry'; import { Disposable } from 'vs/base/common/lifecycle'; export class MyPage extends Component { static readonly ID = 'workbench.component.myPage'; constructor( @IThemeService private readonly themeService: IThemeService ) { super('my-page'); } protected createContentArea(parent: HTMLElement): void { super.createContentArea(parent); const content = document.createElement('div'); content.style.padding = '20px'; content.textContent = '这是我的新页面'; parent.appendChild(content); // 主题适配 this._register(attachStyler(this.themeService, { backgroundColor: editorBackground }, colors => { parent.style.backgroundColor = colors.backgroundColor ? colors.backgroundColor.toString() : ''; })); } } ``` ### 2. 创建样式文件 在`src/vs/workbench/contrib/myPage/browser/myPage.css`中: ```css .my-page { display: flex; flex-direction: column; height: 100%; } ``` ### 3. 注册页面路由 在`src/vs/workbench/contrib/myPage/common/myPage.contribution.ts`中: ```typescript import { Registry } from 'vs/platform/registry/common/platform'; import { Extensions, IWorkbenchContributionsRegistry } from 'vs/workbench/common/contributions'; import { LifecyclePhase } from 'vs/platform/lifecycle/common/lifecycle'; import { IWorkbenchRouteService } from 'vs/workbench/services/route/common/routeService'; class MyPageRouteContribution implements IWorkbenchContribution { constructor( @IWorkbenchRouteService private readonly routeService: IWorkbenchRouteService ) { this.registerRoute(); } private registerRoute(): void { this.routeService.registerRoute({ path: '/my-page', componentId: MyPage.ID }); } } Registry.as<IWorkbenchContributionsRegistry>(Extensions.Workbench) .registerWorkbenchContribution(MyPageRouteContribution, LifecyclePhase.Ready); ``` ### 4. 添加打开页面的命令 在`src/vs/workbench/contrib/myPage/electron-sandbox/myPageActions.ts`中: ```typescript import { registerAction2, Action2 } from 'vs/platform/actions/common/actions'; import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation'; import { IWorkbenchRouteService } from 'vs/workbench/services/route/common/routeService'; import { KeyMod, KeyCode } from 'vs/base/common/keyCodes'; import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey'; export class OpenMyPageAction extends Action2 { constructor() { super({ id: 'workbench.action.openMyPage', title: '打开我的页面', f1: true, // 是否在命令面板中显示 keybinding: { weight: 100, when: ContextKeyExpr.true(), primary: KeyMod.CtrlCmd | KeyCode.KeyM } }); } run(accessor: ServicesAccessor): void { const routeService = accessor.get(IWorkbenchRouteService); routeService.openRoute('/my-page'); } } registerAction2(OpenMyPageAction); ``` ### 5. 注册命令到菜单(可选) 如果需要添加到菜单,可以在同一个文件中添加菜单注册: ```typescript import { MenuId, MenuRegistry } from 'vs/platform/actions/common/actions'; MenuRegistry.appendMenuItem(MenuId.MenubarViewMenu, { group: 'navigation', command: { id: 'workbench.action.openMyPage', title: '我的页面' }, order: 10 }); ``` ### 注意点 - **依赖注入**:在构造函数中通过参数注入服务,需要在注册时由IoC容器提供。 - **生命周期**:注册工作台贡献(WorkbenchContribution)时注意生命周期阶段(LifecyclePhase)。 - **主题适配**:使用`attachStyler`来确保页面背景色等随主题变化。 §§相关问题§§ 1. VSCode 1.101.2中如何传递参数到新页面(如查询字符串)? 2.VSCode 1.101.2中如何实现自定义页面的右键菜单? 3. 如何将自定义页面添加到VSCode 1.101.2的视图容器(例如资源管理器侧边栏)? 4. VSCode 1.101.2中如何监听页面激活和失活事件? 5. 在旧版VSCode中如何调试自定义页面的内存泄漏问题?</think>在 VSCode 1.101.2 源码中创建新页面的完整代码实现如下: ### 1. 页面组件实现 ```typescript // src/vs/workbench/contrib/newPage/browser/newPage.ts import 'vs/css!./newPage'; import { Component } from 'vs/workbench/common/component'; import { IThemeService } from 'vs/platform/theme/common/themeService'; import { attachStyler } from 'vs/platform/theme/common/styler'; import { editorBackground } from 'vs/platform/theme/common/colorRegistry'; import { Disposable } from 'vs/base/common/lifecycle'; export class NewPage extends Component { static readonly ID = 'workbench.part.newPage'; constructor( @IThemeService private readonly themeService: IThemeService ) { super('new-page-container'); this._register(this.themeService.onDidColorThemeChange(() => this.updateStyles())); } protected renderContents(container: HTMLElement): void { container.innerHTML = ` <div class="content"> <h1>新页面标题</h1> <p>这是 VSCode 1.101.2 中创建的新页面</p> <button id="actionButton">执行操作</button> </div> `; const button = container.querySelector('#actionButton'); if (button) { button.addEventListener('click', () => { this.handleButtonClick(); }); } this.updateStyles(); } private updateStyles(): void { attachStyler(this.themeService, { backgroundColor: editorBackground }, styles => { this.element.style.backgroundColor = styles.backgroundColor ? styles.backgroundColor.toString() : ''; }); } private handleButtonClick(): void { // 页面逻辑处理 } } ``` ### 2. 样式文件 ```css /* src/vs/workbench/contrib/newPage/browser/newPage.css */ .new-page-container { padding: 20px; height: 100%; box-sizing: border-box; overflow: auto; } .new-page-container .content { max-width: 800px; margin: 0 auto; } .new-page-container button { padding: 8px 16px; background-color: var(--button-background); color: var(--button-foreground); border: none; border-radius: 2px; cursor: pointer; } ``` ### 3. 路由注册贡献点 ```typescript // src/vs/workbench/contrib/newPage/common/newPageContribution.ts import { Registry } from 'vs/platform/registry/common/platform'; import { Extensions, IWorkbenchContributionsRegistry } from 'vs/workbench/common/contributions'; import { LifecyclePhase } from 'vs/platform/lifecycle/common/lifecycle'; import { IWorkbenchRouteService } from 'vs/workbench/services/route/common/routeService'; import { NewPage } from 'vs/workbench/contrib/newPage/browser/newPage'; class NewPageRouteContribution implements IWorkbenchContribution { constructor( @IWorkbenchRouteService private readonly routeService: IWorkbenchRouteService ) { this.registerRoutes(); } private registerRoutes(): void { this.routeService.registerRoute({ path: '/new-page', componentId: NewPage.ID, title: '新页面' }); } } Registry.as<IWorkbenchContributionsRegistry>(Extensions.Workbench) .registerWorkbenchContribution(NewPageRouteContribution, LifecyclePhase.Ready); ``` ### 4. 命令和菜单注册 ```typescript // src/vs/workbench/contrib/newPage/electron-sandbox/newPageActions.ts import { registerAction2, Action2 } from 'vs/platform/actions/common/actions'; import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation'; import { IWorkbenchRouteService } from 'vs/workbench/services/route/common/routeService'; import { KeyMod, KeyCode } from 'vs/base/common/keyCodes'; import { MenuId } from 'vs/platform/actions/common/actions'; export class OpenNewPageAction extends Action2 { static readonly ID = 'workbench.action.openNewPage'; constructor() { super({ id: OpenNewPageAction.ID, title: '打开新页面', menu: { id: MenuId.ViewTitle, when: ContextKeyExpr.equals('view', 'workbench.view.newPage') }, keybinding: { weight: 100, when: ContextKeyExpr.true(), primary: KeyMod.CtrlCmd | KeyCode.KeyN } }); } run(accessor: ServicesAccessor): void { const routeService = accessor.get(IWorkbenchRouteService); routeService.openRoute('/new-page'); } } registerAction2(OpenNewPageAction); ``` ### 5. 视图容器注册(添加到活动栏) ```typescript // src/vs/workbench/contrib/newPage/browser/newPageViews.ts import { Registry } from 'vs/workbench/common/registry'; import { ViewContainerLocation, ViewContainer, IViewsRegistry } from 'vs/workbench/common/views'; import { localize } from 'vs/nls'; const NEW_PAGE_VIEW_CONTAINER_ID = 'workbench.view.newPage'; const NEW_PAGE_VIEW_CONTAINER_TITLE = localize('newPage', "新页面"); Registry.as<IViewsRegistry>(Registry.ViewsExtensions).registerViewContainer({ id: NEW_PAGE_VIEW_CONTAINER_ID, name: NEW_PAGE_VIEW_CONTAINER_TITLE, ctorDescriptor: { ctor: NewPage }, icon: 'codicon-code', storageId: 'workbench.view.newPage', hideIfEmpty: true, }, ViewContainerLocation.Sidebar); ``` ### 6. 添加到 package.json 贡献点 ```json // src/vs/workbench/contrib/newPage/package.json { "contributes": { "commands": [ { "command": "workbench.action.openNewPage", "title": "打开新页面" } ], "menus": { "commandPalette": [ { "command": "workbench.action.openNewPage", "when": "true" } ] } } } ``` ### 构建说明 1. 将上述文件添加到对应目录 2. 在 `src/vs/workbench/workbench.web.main.ts` 中导入贡献点: ```typescript import 'vs/workbench/contrib/newPage/common/newPageContribution'; ``` 3. 重新构建 VSCode: ```bash yarn run gulp vscode-web-min ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

周陽讀書

周陽也想繼往聖之絕學呀~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值