diff --git a/cmd/templ/lspcmd/proxy/documentcontents.go b/cmd/templ/lspcmd/proxy/documentcontents.go index 54c3a70c4..f92982ddd 100644 --- a/cmd/templ/lspcmd/proxy/documentcontents.go +++ b/cmd/templ/lspcmd/proxy/documentcontents.go @@ -129,31 +129,13 @@ func (d *Document) Delete(fromLine, fromCol, toLine, toCol int) { prefix := d.Lines[fromLine][:fromCol] suffix := d.Lines[toLine][toCol:] - lens := d.LineLengths() - isWithinLine := fromLine == toLine - toLineLen := lens[toLine] - fromIsWholeLine := (fromCol == 0 && !isWithinLine) || (fromCol == 0 && isWithinLine && toCol == toLineLen) - toIsWholeLine := !isWithinLine && toCol == toLineLen - - if isWithinLine { - d.Lines[fromLine] = prefix + suffix - } else { - d.Lines[fromLine] = prefix - d.Lines[toLine] = suffix - } - - if !isWithinLine { - deleteFromLineIndex := fromLine - deleteToLineIndex := toLine + 1 - if !fromIsWholeLine { - deleteFromLineIndex++ - } - if !toIsWholeLine { - deleteToLineIndex-- - } - d.DeleteLines(deleteFromLineIndex, deleteToLineIndex) - } + // Delete intermediate lines. + deleteFrom := fromLine + deleteTo := fromLine + (toLine - fromLine) + d.DeleteLines(deleteFrom, deleteTo) + // Merge the contents of the final line. + d.Lines[fromLine] = prefix + suffix } func (d *Document) DeleteLines(i, j int) { diff --git a/cmd/templ/lspcmd/proxy/documentcontents_test.go b/cmd/templ/lspcmd/proxy/documentcontents_test.go index db36c853c..d6d974444 100644 --- a/cmd/templ/lspcmd/proxy/documentcontents_test.go +++ b/cmd/templ/lspcmd/proxy/documentcontents_test.go @@ -143,6 +143,63 @@ func TestDocument(t *testing.T) { }, expected: "0\n2", }, + { + name: "Can remove line prefix", + start: "abcdef", + operations: []func(d *Document){ + func(d *Document) { + d.Apply(&lsp.Range{ + Start: lsp.Position{ + Line: 0, + Character: 0, + }, + End: lsp.Position{ + Line: 0, + Character: 3, + }, + }, "") + }, + }, + expected: "def", + }, + { + name: "Can remove line substring", + start: "abcdef", + operations: []func(d *Document){ + func(d *Document) { + d.Apply(&lsp.Range{ + Start: lsp.Position{ + Line: 0, + Character: 2, + }, + End: lsp.Position{ + Line: 0, + Character: 3, + }, + }, "") + }, + }, + expected: "abdef", + }, + { + name: "Can remove line suffix", + start: "abcdef", + operations: []func(d *Document){ + func(d *Document) { + d.Apply(&lsp.Range{ + Start: lsp.Position{ + Line: 0, + Character: 4, + }, + End: lsp.Position{ + Line: 0, + Character: 6, + }, + }, "") + }, + }, + expected: "abcd", + }, { name: "Can remove across lines", start: "0\n1\n22", @@ -163,23 +220,42 @@ func TestDocument(t *testing.T) { expected: "0\n2", }, { - name: "Can remove prefix from text", - start: "012345", + name: "Can remove part of two lines", + start: "Line one\nLine two\nLine three", operations: []func(d *Document){ func(d *Document) { d.Apply(&lsp.Range{ Start: lsp.Position{ Line: 0, - Character: 0, + Character: 4, }, End: lsp.Position{ + Line: 2, + Character: 4, + }, + }, "") + }, + }, + expected: "Line three", + }, + { + name: "Can remove all lines", + start: "0\n1\n2", + operations: []func(d *Document){ + func(d *Document) { + d.Apply(&lsp.Range{ + Start: lsp.Position{ Line: 0, - Character: 3, + Character: 0, + }, + End: lsp.Position{ + Line: 2, + Character: 1, }, }, "") }, }, - expected: "345", + expected: "", }, { name: "Can replace line prefix",