├── LICENSE.md ├── README.md └── denops └── goplayground ├── dem.json ├── gp.ts ├── mod.ts └── vendor └── https └── deno.land └── x ├── denops_std └── mod.ts └── goplayground └── index.ts /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright 2021-present [syumai](https://github.com/syumai/) 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # goplayground-vim 2 | 3 | * A Vim plugin to add commands to call [The Go Playground](https://play.golang.org/) from Vim. 4 | 5 | ## Installation 6 | 7 | This plugin needs [denops.vim](https://github.com/vim-denops/denops.vim) and [Deno](https://deno.land). 8 | 9 | ### with vim-plug 10 | 11 | ``` 12 | Plug 'vim-denops/denops.vim' 13 | Plug 'syumai/goplayground-vim' 14 | ``` 15 | 16 | ## Usage 17 | 18 | ``` 19 | :GoPlayRun " Run code in current buffer and show result. 20 | :GoPlayFmt " Format code in current buffer. 21 | :GoPlayShare " Share code in current buffer and show shared URL. 22 | :GoPlayGet " Get code by given The Go Playground's key and open in new buffer. 23 | " Example: GoPlayGet lVnPEvROxzH 24 | 25 | "Commands for [go2go](https://go2goplay.golang.org/) 26 | :Go2GoPlayRun 27 | :Go2GoPlayFmt 28 | :Go2GoPlayShare 29 | :Go2GoPlayGet 30 | ``` 31 | 32 | ## Development 33 | 34 | * This plugin is using [dem](https://github.com/syumai/dem) for module version management. 35 | 36 | ``` 37 | cd denops/goplayground 38 | dem update https://...@x.x.x # update module 39 | ``` 40 | 41 | ## Author 42 | 43 | syumai 44 | 45 | ## LICENSE 46 | 47 | MIT 48 | 49 | -------------------------------------------------------------------------------- /denops/goplayground/dem.json: -------------------------------------------------------------------------------- 1 | { 2 | "modules": [ 3 | { 4 | "protocol": "https", 5 | "path": "deno.land/x/denops_std", 6 | "version": "v0.2", 7 | "files": [ 8 | "/mod.ts" 9 | ] 10 | }, 11 | { 12 | "protocol": "https", 13 | "path": "deno.land/x/goplayground", 14 | "version": "0.1.6", 15 | "files": [ 16 | "/index.ts" 17 | ] 18 | } 19 | ], 20 | "aliases": {} 21 | } -------------------------------------------------------------------------------- /denops/goplayground/gp.ts: -------------------------------------------------------------------------------- 1 | import { GoPlayground, defaultGoPlaygroundHostName } from "./vendor/https/deno.land/x/goplayground/index.ts"; 2 | const go2GoPlaygroundHostName = "https://go2goplay.golang.org"; 3 | 4 | const gpOriginal = new GoPlayground(); 5 | const gpGo2Go = new GoPlayground(go2GoPlaygroundHostName); 6 | 7 | type GPType = 'original' | 'go2go'; 8 | 9 | function gp(t: GPType): GoPlayground { 10 | switch(t) { 11 | case 'original': 12 | return gpOriginal; 13 | case 'go2go': 14 | return gpGo2Go; 15 | } 16 | return gpOriginal; 17 | } 18 | 19 | function gpHostName(t: GPType): string { 20 | switch(t) { 21 | case 'original': 22 | return defaultGoPlaygroundHostName; 23 | case 'go2go': 24 | return go2GoPlaygroundHostName; 25 | } 26 | return defaultGoPlaygroundHostName; 27 | } 28 | 29 | export async function run(t: GPType, src: string): Promise { 30 | const result = await gp(t).compile(src); 31 | if (result.Errors !== "") { 32 | throw new Error(result.Errors) 33 | } 34 | return result.Events 35 | .map(({ Message }) => Message) 36 | .join("\n"); 37 | } 38 | 39 | export async function fmt(t: GPType, src: string): Promise { 40 | const result = await gp(t).format(src, false); 41 | if (result.Error !== "") { 42 | throw new Error(result.Error) 43 | } 44 | return result.Body; 45 | } 46 | 47 | export async function share(t: GPType, src: string): Promise { 48 | const result = await gp(t).share(src); 49 | return `${gpHostName(t)}/p/${result}`; 50 | } 51 | 52 | export async function get(t: GPType, key: string): Promise { 53 | return await gp(t).download(key); 54 | } 55 | -------------------------------------------------------------------------------- /denops/goplayground/mod.ts: -------------------------------------------------------------------------------- 1 | import { start } from "./vendor/https/deno.land/x/denops_std/mod.ts"; 2 | import { run, fmt, share, get } from "./gp.ts"; 3 | 4 | start(async (vim) => { 5 | const getLines = async (): Promise => { 6 | const lines = await vim.call("getline", 0, "$") as string[]; 7 | return lines.join("\n"); 8 | } 9 | 10 | const replaceBuffer = async (text: string): Promise => { 11 | await vim.call("execute", "%d"); 12 | const lines = text.split("\n"); 13 | await vim.call("setline", "1", lines); 14 | } 15 | 16 | const createBuffer = async (text: string): Promise => { 17 | await vim.call("execute", "new"); 18 | const lines = text.split("\n"); 19 | await vim.call("setline", "1", lines); 20 | } 21 | 22 | vim.register({ 23 | // Original playground 24 | async goPlayRun(_: unknown): Promise { 25 | const src = await getLines(); 26 | return run('original', src); 27 | }, 28 | async goPlayFmt(_: unknown): Promise { 29 | const src = await getLines(); 30 | const result = await fmt('original', src); 31 | await replaceBuffer(result); 32 | }, 33 | async goPlayShare(_: unknown): Promise { 34 | const src = await getLines(); 35 | return share('original', src); 36 | }, 37 | async goPlayGet(key: unknown): Promise { 38 | if (typeof key !== "string") { 39 | throw new Error("attribute must be string"); 40 | } 41 | const result = await get('original', key); 42 | await createBuffer(result); 43 | }, 44 | 45 | // Go2Go playground 46 | async go2GoPlayRun(_: unknown): Promise { 47 | const src = await getLines(); 48 | return run('go2go', src); 49 | }, 50 | async go2GoPlayFmt(_: unknown): Promise { 51 | const src = await getLines(); 52 | const result = await fmt('go2go', src); 53 | await replaceBuffer(result); 54 | }, 55 | async go2GoPlayShare(_: unknown): Promise { 56 | const src = await getLines(); 57 | return share('go2go', src); 58 | }, 59 | async go2GoPlayGet(key: unknown): Promise { 60 | if (typeof key !== "string") { 61 | throw new Error("attribute must be string"); 62 | } 63 | const result = await get('go2go', key); 64 | await createBuffer(result); 65 | }, 66 | }); 67 | 68 | await vim.execute(` 69 | function! s:goplayground_show_all_lines(text) " {{{ 70 | let lines = split(a:text, '\\n') 71 | for line in lines 72 | echomsg line 73 | endfor 74 | endfunction 75 | 76 | command! GoPlayRun call s:goplayground_show_all_lines(denops#request('${vim.name}', 'goPlayRun', [])) 77 | command! GoPlayFmt call denops#request('${vim.name}', 'goPlayFmt', []) 78 | command! GoPlayShare echomsg denops#request('${vim.name}', 'goPlayShare', []) 79 | command! -nargs=1 GoPlayGet call denops#request('${vim.name}', 'goPlayGet', []) 80 | 81 | command! Go2GoPlayRun call s:goplayground_show_all_lines(denops#request('${vim.name}', 'go2GoPlayRun', [])) 82 | command! Go2GoPlayFmt call denops#request('${vim.name}', 'go2GoPlayFmt', []) 83 | command! Go2GoPlayShare echomsg denops#request('${vim.name}', 'go2GoPlayShare', []) 84 | command! -nargs=1 Go2GoPlayGet call denops#request('${vim.name}', 'go2GoPlayGet', []) 85 | `); 86 | }); 87 | -------------------------------------------------------------------------------- /denops/goplayground/vendor/https/deno.land/x/denops_std/mod.ts: -------------------------------------------------------------------------------- 1 | export * from "https://deno.land/x/denops_std@v0.2/mod.ts"; 2 | -------------------------------------------------------------------------------- /denops/goplayground/vendor/https/deno.land/x/goplayground/index.ts: -------------------------------------------------------------------------------- 1 | export * from "https://deno.land/x/goplayground@0.1.6/index.ts"; 2 | --------------------------------------------------------------------------------