Skip to content

Commit

Permalink
Fix formatter TextEdit
Browse files Browse the repository at this point in the history
  • Loading branch information
5nord committed Jul 2, 2023
1 parent a42fe28 commit 741a1a0
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions internal/lsp/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/nokia/ntt/internal/log"
"github.com/nokia/ntt/internal/lsp/protocol"
"github.com/nokia/ntt/ttcn3"
"github.com/nokia/ntt/ttcn3/syntax"
"github.com/nokia/ntt/ttcn3/v2/printer"
)

Expand All @@ -24,6 +23,11 @@ func (s *Server) formatting(ctx context.Context, params *protocol.DocumentFormat
log.Debug("formatting: ", err.Error())
return nil, nil
}
if len(b) < 1 {
log.Debugln("formatting: zero length file")
return nil, nil
}

var out bytes.Buffer
p := printer.NewCanonicalPrinter(&out)
p.TabWidth = int(params.Options.TabSize)
Expand All @@ -41,11 +45,9 @@ func (s *Server) formatting(ctx context.Context, params *protocol.DocumentFormat
}

tree := ttcn3.ParseFile(uri)
begin := syntax.Begin(tree.Root)
end := syntax.End(tree.Root)
begin := tree.Position(0)
end := tree.Position(len(b)) // The end position is exclusive.

// TODO(5nord) Test this workaround with other language protocol
// clients (e.g. emacs, vim, ...)
if b[len(b)-1] == '\n' {
end.Line++
end.Column = 1
Expand Down

0 comments on commit 741a1a0

Please sign in to comment.