lsp mode refactor

This commit is contained in:
Gavin Nishizawa 2023-11-28 14:06:48 -08:00
parent baa97fd713
commit 9c578b035f
No known key found for this signature in database
GPG key ID: AE3B177777CE55CD
3 changed files with 35 additions and 16 deletions

View file

@ -2,6 +2,7 @@ package d2cli
import ( import (
"context" "context"
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@ -21,6 +22,7 @@ import (
"oss.terrastruct.com/util-go/go2" "oss.terrastruct.com/util-go/go2"
"oss.terrastruct.com/util-go/xmain" "oss.terrastruct.com/util-go/xmain"
"oss.terrastruct.com/d2/d2ast"
"oss.terrastruct.com/d2/d2graph" "oss.terrastruct.com/d2/d2graph"
"oss.terrastruct.com/d2/d2lib" "oss.terrastruct.com/d2/d2lib"
"oss.terrastruct.com/d2/d2parser" "oss.terrastruct.com/d2/d2parser"
@ -389,6 +391,26 @@ func compile(ctx context.Context, ms *xmain.State, plugins []d2plugin.Plugin, fs
FS: fs, FS: fs,
} }
if os.Getenv("D2_LSP_MODE") == "1" {
// only the parse result is needed if running d2 for lsp
ast, err := d2lib.Parse(ctx, string(input), opts)
if err != nil {
return nil, false, err
}
type LspOutputData struct {
Ast *d2ast.Map
Err error
}
jsonOutput, err := json.Marshal(LspOutputData{Ast: ast, Err: err})
if err != nil {
return nil, false, err
}
fmt.Print(string(jsonOutput))
os.Exit(42)
return nil, false, nil
}
cancel := background.Repeat(func() { cancel := background.Repeat(func() {
ms.Log.Info.Printf("compiling & running layout algorithms...") ms.Log.Info.Printf("compiling & running layout algorithms...")
}, time.Second*5) }, time.Second*5)

View file

@ -1,13 +1,11 @@
package d2compiler package d2compiler
import ( import (
"encoding/json"
"encoding/xml" "encoding/xml"
"fmt" "fmt"
"io" "io"
"io/fs" "io/fs"
"net/url" "net/url"
"os"
"strconv" "strconv"
"strings" "strings"
@ -29,12 +27,6 @@ type CompileOptions struct {
FS fs.FS FS fs.FS
} }
// Changes for Language Server 'mode'
type LspOutputData struct {
Ast *d2ast.Map
Err error
}
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{}
@ -43,14 +35,6 @@ func Compile(p string, r io.Reader, opts *CompileOptions) (*d2graph.Graph, *d2ta
ast, err := d2parser.Parse(p, r, &d2parser.ParseOptions{ ast, err := d2parser.Parse(p, r, &d2parser.ParseOptions{
UTF16Pos: opts.UTF16Pos, UTF16Pos: opts.UTF16Pos,
}) })
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 err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }

View file

@ -7,11 +7,13 @@ import (
"os" "os"
"strings" "strings"
"oss.terrastruct.com/d2/d2ast"
"oss.terrastruct.com/d2/d2compiler" "oss.terrastruct.com/d2/d2compiler"
"oss.terrastruct.com/d2/d2exporter" "oss.terrastruct.com/d2/d2exporter"
"oss.terrastruct.com/d2/d2graph" "oss.terrastruct.com/d2/d2graph"
"oss.terrastruct.com/d2/d2layouts" "oss.terrastruct.com/d2/d2layouts"
"oss.terrastruct.com/d2/d2layouts/d2dagrelayout" "oss.terrastruct.com/d2/d2layouts/d2dagrelayout"
"oss.terrastruct.com/d2/d2parser"
"oss.terrastruct.com/d2/d2renderers/d2fonts" "oss.terrastruct.com/d2/d2renderers/d2fonts"
"oss.terrastruct.com/d2/d2renderers/d2svg" "oss.terrastruct.com/d2/d2renderers/d2svg"
"oss.terrastruct.com/d2/d2target" "oss.terrastruct.com/d2/d2target"
@ -39,6 +41,17 @@ type CompileOptions struct {
InputPath string InputPath string
} }
func Parse(ctx context.Context, input string, compileOpts *CompileOptions) (*d2ast.Map, error) {
if compileOpts == nil {
compileOpts = &CompileOptions{}
}
ast, err := d2parser.Parse(compileOpts.InputPath, strings.NewReader(input), &d2parser.ParseOptions{
UTF16Pos: compileOpts.UTF16Pos,
})
return ast, err
}
func Compile(ctx context.Context, input string, compileOpts *CompileOptions, renderOpts *d2svg.RenderOpts) (*d2target.Diagram, *d2graph.Graph, error) { func Compile(ctx context.Context, input string, compileOpts *CompileOptions, renderOpts *d2svg.RenderOpts) (*d2target.Diagram, *d2graph.Graph, error) {
if compileOpts == nil { if compileOpts == nil {
compileOpts = &CompileOptions{} compileOpts = &CompileOptions{}