diff --git a/frontend/src/lib/components/Editor.svelte b/frontend/src/lib/components/Editor.svelte index 7eb1289a73..26b9557e19 100644 --- a/frontend/src/lib/components/Editor.svelte +++ b/frontend/src/lib/components/Editor.svelte @@ -32,7 +32,12 @@ import getMessageServiceOverride from 'vscode/service-override/messages' import { StandaloneServices } from 'vscode/services' - import { DENO_INIT_CODE_CLEAR, PYTHON_INIT_CODE_CLEAR } from '$lib/script_helpers' + import { + DENO_INIT_CODE, + DENO_INIT_CODE_CLEAR, + GO_INIT_CODE, + PYTHON_INIT_CODE_CLEAR + } from '$lib/script_helpers' import { createHash as randomHash, editorConfig, @@ -109,6 +114,14 @@ } } + export function insertAtLine(code: string, line: number): void { + if (editor) { + const range = { startLineNumber: line, startColumn: 1, endLineNumber: line, endColumn: 1 } + const op = { range: range, text: code, forceMoveMarkers: true } + editor.executeEdits('external', [op]) + } + } + export function setCode(ncode: string): void { code = ncode if (editor) { @@ -132,8 +145,8 @@ setCode(DENO_INIT_CODE_CLEAR) } else if (lang == 'python') { setCode(PYTHON_INIT_CODE_CLEAR) - } else { - setCode('') + } else if (lang == 'go') { + setCode(GO_INIT_CODE) } } } diff --git a/frontend/src/lib/components/EditorBar.svelte b/frontend/src/lib/components/EditorBar.svelte index d8823bfe0e..a9623ca628 100644 --- a/frontend/src/lib/components/EditorBar.svelte +++ b/frontend/src/lib/components/EditorBar.svelte @@ -130,7 +130,7 @@ editor.insertAtCursor(`wmill.get_variable("${path}")`) } else if (lang == 'go') { if (!editor.getCode().includes('wmill "github.com/windmill-labs/windmill-go-client"')) { - editor.insertAtBeginning('import wmill "github.com/windmill-labs/windmill-go-client"\n') + editor.insertAtLine('import wmill "github.com/windmill-labs/windmill-go-client"\n\n', 3) } editor.insertAtCursor(`v, _ := wmill.GetVariable("${path}")`) } @@ -174,7 +174,7 @@ editor.insertAtCursor(`wmill.get_resource("${path}")`) } else if (lang == 'go') { if (!editor.getCode().includes('wmill "github.com/windmill-labs/windmill-go-client"')) { - editor.insertAtBeginning('import wmill "github.com/windmill-labs/windmill-go-client"\n') + editor.insertAtLine('import wmill "github.com/windmill-labs/windmill-go-client"\n\n', 3) } editor.insertAtCursor(`r, _ := wmill.GetResource("${path}")`) } diff --git a/frontend/src/lib/script_helpers.ts b/frontend/src/lib/script_helpers.ts index af2ea32fbd..b0fc0dd587 100644 --- a/frontend/src/lib/script_helpers.ts +++ b/frontend/src/lib/script_helpers.ts @@ -64,7 +64,9 @@ import ( // wmill "github.com/windmill-labs/windmill-go-client" ) -func main(x string, nested struct{ Foo string \`json:"bar"\` }) (interface{}, error) { +// the main must return (interface{}, error) + +func main(x string, nested struct{ Foo string \`json:"foo"\` }) (interface{}, error) { fmt.Println("Hello, World") fmt.Println(nested.Foo) fmt.Println(quote.Opt())