首页 > 其他分享 >vscode-code-runner去除末尾自动添加的文件名

vscode-code-runner去除末尾自动添加的文件名

时间:2022-12-11 19:45:06浏览次数:52  
标签:regex code vscode runner replaced go placeholder replaceValue

起因

对于一个go项目,如果main package包含多个文件,则需要运行go run .这样的命令。在vscode中,code-runner默认的go文件运行命令在defaultSettings.json中如下:

"code-runner.executorMap": {
  "go": "go run",
}

实际运行时,会变成`go run "$fullFileName",此时main package无法识别多个.go文件的内容。

操作

好,那就在settings.json里,把他改成:"go": "go run .",运行!

确实可以正常运行了,但是末尾怎么会自动带上$fullFileName呢,太丑了,而且还会影响到程序的argv。
对比其他的命令,发现如果command中不自己写上$fileName$dir之类的变量,code-runner就会自动给末尾加上$fullFileName。再看了一遍defaultSettings,似乎没有什么配置可以指定不加上这段文件名。
那就看插件源码。
发现在src\codeManager.ts::getFinalCommandToRunCodeFile中,确实有这种逻辑。如果没有变量需要替换,就自动加上文件名了。

// code from https://github.com/formulahendry/vscode-code-runner
private async getFinalCommandToRunCodeFile(executor: string, appendFile: boolean = true): Promise<string> {
    let cmd = executor;

    if (this._codeFile) {
        const codeFileDir = this.getCodeFileDir();
        const pythonPath = cmd.includes("$pythonPath") ? await Utility.getPythonPath(this._document) : Constants.python;
        const placeholders: Array<{ regex: RegExp, replaceValue: string }> = [
            // A placeholder that has to be replaced by the path of the folder opened in VS Code
            // If no folder is opened, replace with the directory of the code file
            { regex: /\$workspaceRoot/g, replaceValue: this.getWorkspaceRoot(codeFileDir) },
            // A placeholder that has to be replaced by the code file name without its extension
            { regex: /\$fileNameWithoutExt/g, replaceValue: this.getCodeFileWithoutDirAndExt() },
            // A placeholder that has to be replaced by the full code file name
            { regex: /\$fullFileName/g, replaceValue: this.quoteFileName(this._codeFile) },
            // A placeholder that has to be replaced by the code file name without the directory
            { regex: /\$fileName/g, replaceValue: this.getCodeBaseFile() },
            // A placeholder that has to be replaced by the drive letter of the code file (Windows only)
            { regex: /\$driveLetter/g, replaceValue: this.getDriveLetter() },
            // A placeholder that has to be replaced by the directory of the code file without a trailing slash
            { regex: /\$dirWithoutTrailingSlash/g, replaceValue: this.quoteFileName(this.getCodeFileDirWithoutTrailingSlash()) },
            // A placeholder that has to be replaced by the directory of the code file
            { regex: /\$dir/g, replaceValue: this.quoteFileName(codeFileDir) },
            // A placeholder that has to be replaced by the path of Python interpreter
            { regex: /\$pythonPath/g, replaceValue: pythonPath },
        ];

        placeholders.forEach((placeholder) => {
            cmd = cmd.replace(placeholder.regex, placeholder.replaceValue);
        });
    }

    return (cmd !== executor ? cmd : executor + (appendFile ? " " + this.quoteFileName(this._codeFile) : ""));
}

对于go项目,运行go run .需要在根目录运行,有时候编辑完其他go文件,需要先切换回根目录的文件再用code-runner,不太方便。所以把executorMap修改为:"go": "cd $workspaceRoot && go run ."即可。

标签:regex,code,vscode,runner,replaced,go,placeholder,replaceValue
From: https://www.cnblogs.com/crazyfz/p/16974265.html

相关文章

  • AtCoder Beginner Contest 281
    \(\mathtt{rank:1119th}\)\(\mathtt{problem}\)\(\mathtt{A}\)\(\mathtt{B}\)\(\mathtt{C}\)\(\mathtt{D}\)\(\mathtt{E}\)\(\mathtt{F}\)\(\mathtt{G}\)\(\ma......
  • 力扣 leetcode 213. 打家劫舍 II
    问题描述你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金。这个地方所有的房屋都围成一圈,这意味着第一个房屋和最后一个房屋是紧挨着的。同时,相邻的房屋......
  • 力扣 leetcode 62. 不同路径
    问题描述一个机器人位于一个mxn网格的左上角(起始点在下图中标记为“Start”)。机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为......
  • AtCoder Beginner Contest 281
    https://atcoder.jp/contests/abc281A-CountDown原题链接题意给出一个数\(n\),按降序输出所有小于或等于\(n\)的非负整数。分析签到题,循环并输出从\(n\)到\(......
  • 【atcoder abc281_d】动态规划
    importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;/***@authorfishcanfly*/publicclassMain{/***......
  • 【Java】【LeetCode】字符串操作
    将空格替换成"%20"Stringa=s.replace("","%20");//注意replace不是直接在s上做修改,而是返回了一个字符串StringBuilderStringBuildersb=newStringBuilder("1234a......
  • AtCoder Beginner Contest 281
    比赛链接A-CountDownA题题面直接输出即可B-SandwichNumberB题题面这道题首先判断开头结尾是否为大写字母,然后判断总长度是否为8,然后对中间一段转数字即可。C......
  • #yyds干货盘点# LeetCode程序员面试金典:链表求和
    题目:给定两个用链表表示的整数,每个节点包含一个数位。这些数位是反向存放的,也就是个位排在链表首部。编写函数对这两个整数求和,并用链表形式返回结果。 示例:输入:(7->1->......
  • 算法12:LeetCode_二叉树的最大深度
    给定一个二叉树,找出其最大深度。二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。说明: 叶子节点是指没有子节点的节点。示例:给定二叉树[3,9,20,null,null,......
  • Atcoder-ABC281-DEF题解
    AtcoderBeginnerContest281D.MaxMultiple(DP)题意在长度为\(N\)的序列\(A\)中,找到\(K\)个元素其和为\(D\)的倍数,找出满足要求最大的和,没有则返回-1。数......