diff --git a/server/handler/documentFormatting.go b/server/handler/documentFormatting.go index e9e7683..b6ac869 100644 --- a/server/handler/documentFormatting.go +++ b/server/handler/documentFormatting.go @@ -1,8 +1,7 @@ package handler import ( - "bytes" - "os" + "fmt" "cuelang.org/go/cue/format" "github.com/tliron/glsp" @@ -15,13 +14,19 @@ func (h *Handler) documentFormatting(_ *glsp.Context, params *protocol.DocumentF h.log.Debugf("params: %#v", params) _uri, err := uri.Parse(params.TextDocument.URI) - source, err := os.ReadFile(_uri.Filename()) if err != nil { return nil, h.wrapError(err) } - h.log.Debugf("Find source of %s", _uri.Filename) - _fmt, err := format.Source(source, format.UseSpaces(2), format.TabIndent(false)) // TODO: gather from params.Options? + p := h.workspace.GetPlan(_uri.Filename()) + if p == nil { + return nil, h.wrapError(fmt.Errorf("plan not found")) + } + + f := p.Files()[h.workspace.TrimRootPath(_uri.Filename())] + h.log.Debugf("File %v", f) + + _fmt, err := format.Node(f.Content(), format.UseSpaces(2), format.TabIndent(false)) // TODO: gather from params.Options? if err != nil { return nil, h.wrapError(err) } @@ -29,9 +34,7 @@ func (h *Handler) documentFormatting(_ *glsp.Context, params *protocol.DocumentF h.log.Debugf("Source formatted: %s", _uri.Filename) start := protocol.Position{Line: 0, Character: 0} - nl := []byte("\n") - ll := bytes.Count(source, nl) - end := protocol.Position{Line: uint32(ll), Character: 0} + end := protocol.Position{Line: uint32(f.Content().End().Line()), Character: 0} edit := protocol.TextEdit{ Range: protocol.Range{