Update d2compiler/compile.go

Co-authored-by: gavin-ts <85081687+gavin-ts@users.noreply.github.com>
This commit is contained in:
Barry Nolte 2023-11-03 13:38:51 -07:00 committed by GitHub
parent 9b463d5c9f
commit 1c4d076812
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -35,38 +35,24 @@ type LspOutputData struct {
Err error 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) { func Compile(p string, r io.Reader, opts *CompileOptions) (*d2graph.Graph, *d2target.Config, error) {
if opts == nil { if opts == nil {
opts = &CompileOptions{} opts = &CompileOptions{}
} }
lspMode := os.Getenv("D2_LSP_MODE") == "1"
defer LspOutput(lspMode)
ast, err := d2parser.Parse(p, r, &d2parser.ParseOptions{ ast, err := d2parser.Parse(p, r, &d2parser.ParseOptions{
UTF16Pos: opts.UTF16Pos, UTF16Pos: opts.UTF16Pos,
}) })
lod.Ast = ast if os.Getenv("D2_LSP_MODE") == "1" {
if err != nil { jsonOutput, _ := json.Marshal(LspOutputData{Ast: ast, Err: err})
lod.Err = err fmt.Print(string(jsonOutput))
os.Exit(42)
return nil, nil, err return nil, nil, err
} }
if lspMode { if err != nil {
return nil, nil, nil return nil, nil, err
} }
ir, err := d2ir.Compile(ast, &d2ir.CompileOptions{ ir, err := d2ir.Compile(ast, &d2ir.CompileOptions{