From 1c4d0768127fa53ceee68bc9a0b891df5420cf99 Mon Sep 17 00:00:00 2001 From: Barry Nolte Date: Fri, 3 Nov 2023 13:38:51 -0700 Subject: [PATCH] Update d2compiler/compile.go Co-authored-by: gavin-ts <85081687+gavin-ts@users.noreply.github.com> --- d2compiler/compile.go | 26 ++++++-------------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/d2compiler/compile.go b/d2compiler/compile.go index dc3d87e1c..6b81f85a1 100644 --- a/d2compiler/compile.go +++ b/d2compiler/compile.go @@ -35,38 +35,24 @@ type LspOutputData struct { Err error } -var lod LspOutputData - -func LspOutput(m bool) { - if !m { - return - } - - jsonOutput, _ := json.Marshal(lod) - fmt.Print(string(jsonOutput)) - os.Exit(42) -} - func Compile(p string, r io.Reader, opts *CompileOptions) (*d2graph.Graph, *d2target.Config, error) { if opts == nil { opts = &CompileOptions{} } - lspMode := os.Getenv("D2_LSP_MODE") == "1" - defer LspOutput(lspMode) - ast, err := d2parser.Parse(p, r, &d2parser.ParseOptions{ UTF16Pos: opts.UTF16Pos, }) - lod.Ast = ast - if err != nil { - lod.Err = err + if os.Getenv("D2_LSP_MODE") == "1" { + jsonOutput, _ := json.Marshal(LspOutputData{Ast: ast, Err: err}) + fmt.Print(string(jsonOutput)) + os.Exit(42) return nil, nil, err } - if lspMode { - return nil, nil, nil + if err != nil { + return nil, nil, err } ir, err := d2ir.Compile(ast, &d2ir.CompileOptions{