Skip to content

Commit d79a331

Browse files
committed
DuplicateLine: respect selections
Similarly to CutLine, DeleteLine and CopyLine actions, if there is a selection, duplicate not just the current line but all the lines covered (fully or partially) by the selection.
1 parent 5a72351 commit d79a331

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

internal/action/actions.go

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,11 +1279,44 @@ func (h *BufPane) Duplicate() bool {
12791279
return true
12801280
}
12811281

1282-
// DuplicateLine duplicates the current line
1282+
// DuplicateLine duplicates the current line. If there is a selection, DuplicateLine
1283+
// duplicates all the lines that are (fully or partially) in the selection.
12831284
func (h *BufPane) DuplicateLine() bool {
1284-
h.Cursor.End()
1285-
h.Buf.Insert(h.Cursor.Loc, "\n"+string(h.Buf.LineBytes(h.Cursor.Y)))
1286-
InfoBar.Message("Duplicated line")
1285+
if h.Cursor.HasSelection() {
1286+
origLoc := h.Cursor.Loc
1287+
origLastVisualX := h.Cursor.LastVisualX
1288+
origSelection := h.Cursor.CurSelection
1289+
1290+
start := h.Cursor.CurSelection[0]
1291+
end := h.Cursor.CurSelection[1]
1292+
if start.GreaterThan(end) {
1293+
start, end = end, start
1294+
}
1295+
if end.X == 0 {
1296+
end = end.Move(-1, h.Buf)
1297+
}
1298+
1299+
h.Cursor.Deselect(true)
1300+
h.Cursor.Loc = end
1301+
h.Cursor.End()
1302+
for y := start.Y; y <= end.Y; y++ {
1303+
h.Buf.Insert(h.Cursor.Loc, "\n"+string(h.Buf.LineBytes(y)))
1304+
}
1305+
1306+
h.Cursor.Loc = origLoc
1307+
h.Cursor.LastVisualX = origLastVisualX
1308+
h.Cursor.CurSelection = origSelection
1309+
1310+
if start.Y < end.Y {
1311+
InfoBar.Message(fmt.Sprintf("Duplicated %d lines", end.Y-start.Y+1))
1312+
} else {
1313+
InfoBar.Message("Duplicated line")
1314+
}
1315+
} else {
1316+
h.Cursor.End()
1317+
h.Buf.Insert(h.Cursor.Loc, "\n"+string(h.Buf.LineBytes(h.Cursor.Y)))
1318+
InfoBar.Message("Duplicated line")
1319+
}
12871320
h.Relocate()
12881321
return true
12891322
}

0 commit comments

Comments
 (0)