vscode-leetcode代码片段:自定义模板提高效率

vscode-leetcode代码片段:自定义模板提高效率

【免费下载链接】vscode-leetcode Solve LeetCode problems in VS Code 【免费下载链接】vscode-leetcode 项目地址: https://gitcode.com/gh_mirrors/vs/vscode-leetcode

在日常LeetCode刷题过程中,重复编写相同的代码结构会浪费大量时间。vscode-leetcode插件通过灵活的模板配置功能,让开发者可以自定义代码生成规则,显著提升解题效率。本文将详细介绍如何利用插件的模板功能实现个性化代码片段生成。

模板功能核心实现

插件的模板生成逻辑主要通过src/leetCodeExecutor.ts实现,关键代码如下:

public async showProblem(problemNode: IProblem, language: string, filePath: string, showDescriptionInComment: boolean = false, needTranslation: boolean): Promise<void> {
    const templateType: string = showDescriptionInComment ? "-cx" : "-c";
    const cmd: string[] = [await this.getLeetCodeBinaryPath(), "show", problemNode.id, templateType, "-l", language];
    
    if (!needTranslation) {
        cmd.push("-T"); // 使用-T参数强制英文版本
    }
    
    if (!await fse.pathExists(filePath)) {
        await fse.createFile(filePath);
        const codeTemplate: string = await this.executeCommandWithProgressEx("Fetching problem data...", this.nodeExecutable, cmd);
        await fse.writeFile(filePath, codeTemplate);
    }
}

上述代码通过调用leetcode-cli的show命令生成代码模板,支持两种模式:

  • -c:基础模板(不含题目描述注释)
  • -cx:扩展模板(包含题目描述注释)

自定义文件路径与命名

插件允许通过配置项自定义生成文件的路径和名称,实现在src/commands/show.ts中:

const folderPath: string = leetCodeConfig
    .get<string>(`filePath.${language}.folder`, leetCodeConfig.get<string>(`filePath.default.folder`, ""))

const fileName: string = leetCodeConfig
    .get<string>(`filePath.${language}.filename`, leetCodeConfig.get<string>(`filePath.default.filename`) || genFileName(node, language))

常用配置示例

在VS Code设置中添加以下配置(通过Ctrl+,打开设置界面):

{
    "leetcode.filePath.default.folder": "${id}-${titleSlug}",
    "leetcode.filePath.default.filename": "${id}.${ext}",
    "leetcode.filePath.java.folder": "java/src/main/java/com/leetcode/${difficulty}",
    "leetcode.filePath.java.filename": "Solution_${id}.java"
}

支持的变量占位符:

  • ${id}:题目编号
  • ${title}:题目名称
  • ${titleSlug}:URL友好的题目名称
  • ${difficulty}:难度级别(Easy/Medium/Hard)
  • ${ext}:语言对应的文件扩展名

模板使用流程

1. 基础模板生成

选择题目

  1. 在LeetCode Explorer中选择题目
  2. 右键点击选择"Show Problem"
  3. 插件自动生成基础代码模板

2. 带描述的扩展模板

通过设置启用题目描述注释:

{
    "leetcode.showCommentDescription": true
}

启用后生成的模板将包含格式化的题目描述注释,方便离线查看题目要求。

3. 多语言模板支持

插件支持为不同编程语言配置独立的模板规则,如Java和Python可以有不同的文件组织结构:

{
    "leetcode.defaultLanguage": "java",
    "leetcode.filePath.java.folder": "src/main/java/com/leetcode",
    "leetcode.filePath.python.folder": "python/solutions",
    "leetcode.filePath.python.filename": "solution_${id}.py"
}

模板定制高级技巧

1. 结合代码片段(Snippets)

将vscode-leetcode模板与VS Code的用户代码片段结合使用,例如创建Java解题模板:

// java.json 用户代码片段
{
    "LeetCode Solution Template": {
        "prefix": "leetcode",
        "body": [
            "package com.leetcode.${difficulty};",
            "",
            "/**",
            " * ${title}",
            " * ${url}",
            " */",
            "public class Solution_${id} {",
            "    public ${returnType} ${methodName}(${params}) {",
            "        // Your solution here",
            "        return ${defaultReturn};",
            "    }",
            "}"
        ],
        "description": "LeetCode Java Solution Template"
    }
}

2. 工作区组织建议

推荐的项目结构:

leetcode-solutions/
├── java/
│   └── src/main/java/com/leetcode/
│       ├── easy/
│       ├── medium/
│       └── hard/
├── python/
│   ├── easy/
│   ├── medium/
│   └── hard/
└── README.md

对应的配置:

{
    "leetcode.workspaceFolder": "/path/to/leetcode-solutions",
    "leetcode.filePath.default.folder": "${difficulty}",
    "leetcode.filePath.default.filename": "Solution_${id}.${ext}"
}

相关配置项

完整配置项列表可在docs/README_zh-CN.md中查看,关键配置包括:

配置项名称描述默认值
leetcode.defaultLanguage默认编程语言N/A
leetcode.showCommentDescription在注释中显示题目描述false
leetcode.filePath.default.folder默认文件夹路径模板""
leetcode.filePath.default.filename默认文件名模板自动生成
leetcode.workspaceFolder工作区根目录$HOME/.leetcode

通过合理配置这些选项,可以打造完全符合个人习惯的解题环境,将更多精力集中在算法设计而非重复代码编写上。

快捷键操作

编辑器快捷方式

模板生成后,可使用编辑器内置的Code Lens快捷操作:

  • Submit:提交解决方案
  • Test:运行测试用例
  • Solution:查看高票解答
  • Description:查看题目描述

这些快捷操作可以通过leetcode.editor.shortcuts配置项自定义启用或禁用。

【免费下载链接】vscode-leetcode Solve LeetCode problems in VS Code 【免费下载链接】vscode-leetcode 项目地址: https://gitcode.com/gh_mirrors/vs/vscode-leetcode

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

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

抵扣说明:

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

余额充值