Skip to content

Commit

Permalink
fix: stop adding extra lines at the end after formatting operations
Browse files Browse the repository at this point in the history
  • Loading branch information
a-h committed Mar 5, 2023
1 parent b1973fd commit 120aaa2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 22 deletions.
32 changes: 12 additions & 20 deletions cmd/templ/lspcmd/proxy/documentcontents.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,15 @@ func (dc *DocumentContents) Apply(uri string, changes []lsp.TextDocumentContentC
return
}

func NewDocument(s string) *Document {
func NewDocument(log *zap.Logger, s string) *Document {
return &Document{
Log: log,
Lines: strings.Split(s, "\n"),
}
}

type Document struct {
Log *zap.Logger
Lines []string
}

Expand All @@ -90,15 +92,12 @@ func (d *Document) isEmptyRange(r lsp.Range) bool {
}

func (d *Document) isRangeOfDocument(r lsp.Range) bool {
startLine, startChar := int(r.Start.Line), int(r.Start.Character)
endLine, endChar := int(r.End.Line), int(r.End.Character)
return startLine == 0 && startChar == 0 && endLine == len(d.Lines)-1 && endChar == len(d.Lines[len(d.Lines)-1])-1
}

// As per https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#range
// If you want to specify a range that contains a line including the line ending character(s) then use an end position denoting the start of the next line.
func (d *Document) isWholeLineRange(r lsp.Range) bool {
return r.Start.Character == 0 && r.End.Character == 0 && r.End.Line != r.Start.Line
rangeStartsAtBeginningOfFile := r.Start.Line == 0 && r.Start.Character == 0
rel, rec := int(r.End.Line), int(r.End.Character)
del, dec := int(len(d.Lines)-1), len(d.Lines[len(d.Lines)-1])-1
rangeEndsPastTheEndOfFile := rel > del || rel == del && rec > dec
rangeEndsAtEndOfFile := rel == del && rec == dec
return rangeStartsAtBeginningOfFile && (rangeEndsPastTheEndOfFile || rangeEndsAtEndOfFile)
}

func (d *Document) remove(i, j int) {
Expand Down Expand Up @@ -129,23 +128,16 @@ func (d *Document) normaliseRange(r *lsp.Range) {
}

func (d *Document) Overwrite(r *lsp.Range, with string) {
withLines := strings.Split(with, "\n")
if r == nil || d.isEmptyRange(*r) || len(d.Lines) == 0 {
d.Lines = strings.Split(with, "\n")
d.Lines = withLines
return
}
d.normaliseRange(r)
if d.isRangeOfDocument(*r) {
d.Lines = strings.Split(with, "\n")
return
}
if d.isWholeLineRange(*r) {
d.remove(int(r.Start.Line), int(r.End.Line))
if with != "" {
d.insert(int(r.Start.Line), strings.Split(with, "\n"))
}
d.Lines = withLines
return
}
withLines := strings.Split(with, "\n")
if r.Start.Character > 0 {
prefix := d.Lines[r.Start.Line][:r.Start.Character]
withLines[0] = prefix + withLines[0]
Expand Down
4 changes: 3 additions & 1 deletion cmd/templ/lspcmd/proxy/documentcontents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

lsp "github.com/a-h/protocol"
"github.com/google/go-cmp/cmp"
"go.uber.org/zap"
)

func TestDocument(t *testing.T) {
Expand Down Expand Up @@ -314,7 +315,8 @@ templ personTemplate(p person) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := NewDocument(tt.start)
log := zap.NewExample()
d := NewDocument(log, tt.start)
for _, f := range tt.operations {
f(d)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/templ/lspcmd/proxy/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ func (p *Server) DidOpen(ctx context.Context, params *lsp.DidOpenTextDocumentPar
return p.Target.DidOpen(ctx, params)
}
// Cache the template doc.
p.TemplSource.Set(string(params.TextDocument.URI), NewDocument(params.TextDocument.Text))
p.TemplSource.Set(string(params.TextDocument.URI), NewDocument(p.Log, params.TextDocument.Text))
// Parse the template.
template, ok, err := p.parseTemplate(ctx, params.TextDocument.URI, params.TextDocument.Text)
if err != nil {
Expand Down

0 comments on commit 120aaa2

Please sign in to comment.