Skip to content
This repository has been archived by the owner on Jan 16, 2024. It is now read-only.

Commit

Permalink
feat: get file from plan
Browse files Browse the repository at this point in the history
  • Loading branch information
b4nst committed Aug 4, 2022
1 parent c495dc2 commit 8ea6580
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions server/handler/documentFormatting.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package handler

import (
"bytes"
"os"
"fmt"

"cuelang.org/go/cue/format"
"github.com/tliron/glsp"
Expand All @@ -15,23 +14,27 @@ 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)
}

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{
Expand Down

0 comments on commit 8ea6580

Please sign in to comment.