V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
formulahendry
V2EX  ›  程序员

Code Runner for VS Code, 支持运行 27 种语言 (C, C++, Java , JS, PHP , Python , Perl, Ruby, Go, Lua, Groovy, PowerShell...)

  •  8
     
  •   formulahendry ·
    formulahendry · 2017-01-14 12:36:32 +08:00 · 17004 次点击
    这是一个创建于 2651 天前的主题,其中的信息可能已经有所发展或是发生改变。

    经过了半年的打磨,Code Runner已经支持了 27 种语言: C, C++, Java, JS, PHP, Python, Perl, Ruby, Go, Lua, Groovy, PowerShell, BAT/CMD, BASH/SH, F#, C#, VBScript, TypeScript, CoffeeScript, Scala, Swift, Julia, Crystal, OCaml, R, AppleScript, Elixir 。并且支持以下几个主要功能:

    • Run code file of current active Text Editor
    • Run selected code snippet in Text Editor
    • Run custom command with customized parameters
    • Stop code running
    • View output in Output Window
    • Set default language to run
    • Select language to run
    • Support REPL by running code in Integrated Terminal

    run code

    如果大家发现了任何 bug 或者有什么建议,欢迎来GitHub给我提 issue 或者 PR 。

    第 1 条附言  ·  2019-01-03 12:38:18 +08:00

    最近还开通了"玩转VS Code"的公众号。希望能和大家分享一下VS Code使用和开发的一些经验,主要希望提供的内容包括但不限于:

    • VS Code 快速入门
    • VS Code 使用技巧
    • VS Code 插件推荐
    • VS Code 插件开发

    有兴趣的童鞋,可以来关注"玩转VS Code"公众号。也希望听听大家的意见,更希望看到什么样的内容。 QR

    59 条回复    2018-03-11 17:21:12 +08:00
    langmoe
        1
    langmoe  
       2017-01-14 12:39:19 +08:00
    我还以为买的 Code Runner 又有更新了。。
    chairchan
        2
    chairchan  
       2017-01-14 13:55:30 +08:00 via Android
    兹词 正在用😀
    KiriGiri
        3
    KiriGiri  
       2017-01-14 13:57:52 +08:00 via Android
    可以添加一个功能,把运行结果直接替换选中的代码,就像 vim 的:'<,'>!command 。
    asxaqz
        4
    asxaqz  
       2017-01-14 14:10:40 +08:00
    一直在用。支持。
    0915240
        5
    0915240  
       2017-01-14 15:27:31 +08:00
    我曹 !
    兹词~~
    40huo
        6
    40huo  
       2017-01-14 16:03:30 +08:00
    兹词,好用!
    cdwyd
        7
    cdwyd  
       2017-01-14 16:39:50 +08:00
    python 的输出不是实时的吗?测试了下是全部执行完才统一输出
    quericy
        8
    quericy  
       2017-01-14 16:42:11 +08:00
    厉害了,已安装
    exoticknight
        9
    exoticknight  
       2017-01-14 16:45:05 +08:00
    好厉害,是不是应该换了……
    kchum
        10
    kchum  
       2017-01-14 17:12:10 +08:00
    我也以为是 Code Runner 2 。。
    loading
        11
    loading  
       2017-01-14 17:44:25 +08:00 via Android
    比 vs go 是不是强一些呢,看看。
    pipecat
        12
    pipecat  
       2017-01-14 19:46:38 +08:00 via iPhone
    在用,支持
    formulahendry
        13
    formulahendry  
    OP
       2017-01-14 21:12:01 +08:00
    @langmoe @kchum 我们这个 Code Runner 可是免费跨平台的哦😁
    formulahendry
        14
    formulahendry  
    OP
       2017-01-14 21:19:08 +08:00
    @KiriGiri VIM 没怎么用过,能具体说下吗?比如说,运行 var a = 1 + 2 ,然后把代码替换为 var a = 3?
    blanu
        15
    blanu  
       2017-01-14 21:44:47 +08:00 via iPhone
    Great! VSCode 自带的调试运行时太重,这个看上去就非常方便
    formulahendry
        16
    formulahendry  
    OP
       2017-01-14 21:56:14 +08:00
    @blanu 对滴!而且能选择运行 code snippet ,快捷轻巧😎
    XYxe
        17
    XYxe  
       2017-01-14 22:08:06 +08:00
    Python 输出的中文会乱码
    另外,不能区分 Python2 和 Python3 吗?
    formulahendry
        18
    formulahendry  
    OP
       2017-01-14 22:42:22 +08:00   ❤️ 1
    @XYxe
    关于乱码,有两个解决方法:
    1. 设置 PYTHONIOENCODING ,参考 https://github.com/formulahendry/vscode-code-runner/issues/33#issuecomment-263131627
    "code-runner.executorMap": {
    "python": "set PYTHONIOENCODING=utf8 && python"
    }
    2. 设置使用 built-in terminal ,参考 https://github.com/formulahendry/vscode-code-runner/issues/25#issuecomment-257123682
    "code-runner.runInTerminal": true

    关于区分 Python2 和 Python3 ,可以在 user settings 自定义 code-runner.executorMap ,参考 https://github.com/formulahendry/vscode-code-runner#configuration
    XYxe
        19
    XYxe  
       2017-01-14 22:55:45 +08:00
    @formulahendry 啊,谢谢 。不能把 Python2 和 3 设置为不同语言吗?因为不同的文件可能需要不同版本, 每次修改设置会比较麻烦。
    formulahendry
        20
    formulahendry  
    OP
       2017-01-14 23:00:41 +08:00
    @cdwyd 唉, python 真的有这个问题唉,其他语言倒是能实时输出的😅不过有办法可以 workaround ,在 user settings 设置使用 ntegrated Terminal :
    "code-runner.runInTerminal": true
    cdwyd
        21
    cdwyd  
       2017-01-14 23:07:12 +08:00 via Android
    @formulahendry
    明天试试这个,开启那个 repl 也能解决
    viko16
        22
    viko16  
       2017-01-14 23:18:17 +08:00 via Android
    好棒,赞赞赞
    formulahendry
        23
    formulahendry  
    OP
       2017-01-14 23:21:25 +08:00
    @XYxe Python2 和 3 的文件都是.py 吧?似乎并不能自动区分出来。如果只是不同的 Workspace 会用不同 python 版本,可以在 File-> Preferences->Workspace Settings 设置不同的 python 解释器,这样不知道能满足需求吗?如果在同一个 Workspace 会用不同 python 版本,可以考虑使用 custom command :"code-runner.customCommand": "python3"
    cdwyd
        24
    cdwyd  
       2017-01-14 23:28:36 +08:00
    @formulahendry
    忽略上一条吧 专门起来看了下就是用的"code-runner.runInTerminal": true 😅
    Lattez
        25
    Lattez  
       2017-01-14 23:31:01 +08:00
    赞赞赞!!自己配的话太麻烦,每次开个新的都要重新弄
    XYxe
        26
    XYxe  
       2017-01-14 23:33:37 +08:00
    @formulahendry 可以通过 shebang 来判断吗?
    formulahendry
        27
    formulahendry  
    OP
       2017-01-15 00:05:49 +08:00
    @XYxe 目前对文件类型的判断是直接调用 VS Code 的 API , VS Code 并没有对 python 2 和 python 3 有区分。而且 shebang 在 Windows 上并没有吧。不过可以考虑当作个 feature 来做。欢迎到 github 给我提这个 feature ,如果你想自己实现的话,也非常欢迎 PR !
    formulahendry
        28
    formulahendry  
    OP
       2017-01-15 00:06:33 +08:00
    @cdwyd 哈哈哈😆
    Lxxyx
        29
    Lxxyx  
       2017-01-15 00:09:14 +08:00 via Android
    刚试了试,感觉很棒,谢谢楼主。
    kuaner
        30
    kuaner  
       2017-01-15 02:46:02 +08:00 via iPhone
    哈哈,原来作者在这呀,非常棒的插件
    anuxs
        31
    anuxs  
       2017-01-15 07:58:53 +08:00 via iPhone
    支持。试试。
    DearMark
        32
    DearMark  
       2017-01-15 09:22:54 +08:00 via Android
    32 个赞\(≧▽≦)/
    KiriGiri
        33
    KiriGiri  
       2017-01-15 10:06:33 +08:00
    visitantzj
        34
    visitantzj  
       2017-01-15 12:26:25 +08:00
    赞一个, 小文件直接用着很方便, 文件多再设 proj
    formulahendry
        35
    formulahendry  
    OP
       2017-01-15 12:29:52 +08:00
    @KiriGiri 很有意思的功能!可以考虑实现下!
    formulahendry
        36
    formulahendry  
    OP
       2017-01-15 12:31:41 +08:00
    @visitantzj 对!运行单个小文件非常方便!
    xuboying
        37
    xuboying  
       2017-01-15 12:55:30 +08:00 via iPhone
    这个要具体语言的运行库么,还是楼主的云上运行?
    hxtheone
        38
    hxtheone  
       2017-01-15 13:09:40 +08:00
    这个必须赞
    renyiqiu
        39
    renyiqiu  
       2017-01-15 13:12:25 +08:00
    mark 一下
    formulahendry
        40
    formulahendry  
    OP
       2017-01-15 13:34:13 +08:00
    @xuboying 对的,需要相应的解释器或编译器。比如 JS 需要 Node , C 需要 gcc 。当然也可以根据需要自己设置,比如可以用 clang 替换 gcc 。
    icreeper
        41
    icreeper  
       2017-01-15 14:07:01 +08:00
    感觉比 CodeRunner2 还好用!赞一个
    formulahendry
        42
    formulahendry  
    OP
       2017-01-15 22:42:22 +08:00 via Android
    @icreeper 😎😎
    manongvpn
        43
    manongvpn  
       2017-01-16 07:53:22 +08:00 via Android
    大牛
    araraloren
        44
    araraloren  
       2017-01-16 09:30:09 +08:00
    看起来不错的样子,最近也有点使用 VSCODE 的兴趣~~ mark 一下
    coa
        45
    coa  
       2017-01-16 22:14:08 +08:00
    这个强, Code Runner 2 胜在支持 Objective C ,可惜收费。。求问 Atom 下有没有类似的东西。。。
    hambut
        46
    hambut  
       2017-02-07 11:05:59 +08:00
    @formulahendry vs2015 没找到 CTRL+P ,怎么安装。。。
    formulahendry
        47
    formulahendry  
    OP
       2017-02-07 11:38:15 +08:00
    @hambut 这个插件是 Visual Studio Code 的: https://code.visualstudio.com/ ,不是 VS IDE 😅
    hambut
        48
    hambut  
       2017-02-07 14:47:00 +08:00
    @formulahendry #47 好尴尬啊。。刚才下了 VS CODE 试了下,别的都挺满意, CTRL SHIFT O 有些认不出来,没有 minimap 暂时性流失。。
    formulahendry
        49
    formulahendry  
    OP
       2017-02-07 15:58:45 +08:00
    @hambut 看什么语言了吧, Node.js 有 built-in 支持,其它语言的'Go to Symbol'还是要另外装插件
    vito0719
        50
    vito0719  
       2017-05-11 16:02:12 +08:00
    能在 windows 下运行 swift 吗?
    formulahendry
        51
    formulahendry  
    OP
       2017-05-12 00:02:43 +08:00
    @vito0719 Bash on Windows :)
    vito0719
        52
    vito0719  
       2017-05-12 18:13:52 +08:00
    @formulahendry 我在 bash 里已经可以用 swift 了,请问在 vs code 里还需要改什么?
    formulahendry
        53
    formulahendry  
    OP
       2017-05-13 20:00:13 +08:00
    @vito0719
    1. 设置 Integrated Terminal 用 Bash on Ubuntu (on Windows): https://code.visualstudio.com/docs/editor/integrated-terminal#_windows:"terminal.integrated.shell.windows": "C:\\Windows\\sysnative\\bash.exe"
    2. 设置 Code Runner 用 Integrated Terminal:"code-runner.runInTerminal": true
    vito0719
        55
    vito0719  
       2017-05-15 15:15:03 +08:00
    @formulahendry 好像问题是路径?我运行得到的回复是这样的:
    Playground.swift"-CZZ:/mnt/c/Users/vito0719$ swift "c:\Users\vito0719\Documents\
    <unknown>:0: error: no such file or directory: 'c:\Users\vito0719\Documents\Playground.swift'

    好像要改成 /mnt/c/Users/这样的路径才行,这个有方法吗?
    formulahendry
        56
    formulahendry  
    OP
       2017-05-15 22:35:58 +08:00
    @vito0719 的确目前有 limitation,现在有 issue 在 track: https://github.com/formulahendry/vscode-code-runner/issues/106
    jin7
        57
    jin7  
       2017-07-22 23:53:52 +08:00
    @formulahendry 嗨~. 我发现 powershell 中文乱码了.
    ps1 文件, 编码是 ucs2-le, 代码页是 1200
    设置
    ```
    "code-runner.runInTerminal": true,
    "code-runner.executorMap": {
    "powershell": "chcp 1200>nul && powershell -ExecutionPolicy ByPass -File"
    }
    ```
    输出是一行乱码: ��Ч����ҳ
    设置成 chcp 65001 可以输出, 只是中文当作 ansi 编码解析了.

    为什么我要把 ps1 文件保存为 ucs2-le 编码, 因为, powershell 好像不认 utf8 编码, 中文会乱码. 用 ucs2-le 就没事.
    jin7
        58
    jin7  
       2017-07-23 11:13:21 +08:00
    我发现 chcp 1200 为无效代码页.
    但是我把文件保存为 utf8, 并且设置如下, 也没有用
    ```
    "code-runner.runInTerminal": true,
    "code-runner.executorMap": {
    "powershell": "chcp 65001>nul && powershell -ExecutionPolicy ByPass -File"
    }
    ```
    bestswifter
        59
    bestswifter  
       2018-03-11 17:21:12 +08:00
    可否把 runCustomCommand 命令和 run 命令的快捷键设置为同一个呢?

    我有一个需求是,如果没有指定 customCommand,就按照 executorMap 里面的配置来执行,如果有 customCommand,则优先执行 customCommand。

    我知道 keybindings.json 里面有一个 when 字段,但是不知道该填什么值
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3645 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 04:28 · PVG 12:28 · LAX 21:28 · JFK 00:28
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.