Skip to content

Commit

Permalink
fix: LSP multi-line deletions
Browse files Browse the repository at this point in the history
  • Loading branch information
a-h committed Mar 27, 2023
1 parent 0e4834d commit 2fccbf3
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 29 deletions.
30 changes: 6 additions & 24 deletions cmd/templ/lspcmd/proxy/documentcontents.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
86 changes: 81 additions & 5 deletions cmd/templ/lspcmd/proxy/documentcontents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down

0 comments on commit 2fccbf3

Please sign in to comment.