modify dedent functionality to support dedenting multiple lines, acting on count parameter
This commit is contained in:
@@ -59,7 +59,37 @@ struct
|
|||||||
loop (app, #cursorIdx app, #buffer app, count, time)
|
loop (app, #cursorIdx app, #buffer app, count, time)
|
||||||
end
|
end
|
||||||
|
|
||||||
(* todo: use count to find number of lines to dedent *)
|
local
|
||||||
|
fun loop (cursorIdx, buffer, count) =
|
||||||
|
if count = 0 then
|
||||||
|
buffer
|
||||||
|
else
|
||||||
|
let
|
||||||
|
val buffer = LineGap.goToIdx (cursorIdx, buffer)
|
||||||
|
val lineStart = Cursor.vi0 (buffer, cursorIdx)
|
||||||
|
val firstNonSpaceChr = Cursor.firstNonSpaceChr (buffer, lineStart)
|
||||||
|
|
||||||
|
(* delete from buffer *)
|
||||||
|
val difference = firstNonSpaceChr - lineStart
|
||||||
|
val deleteLength = Int.min (difference, 2)
|
||||||
|
val buffer =
|
||||||
|
if difference = 0 then
|
||||||
|
(* can't dedent as there is no leading space *)
|
||||||
|
buffer
|
||||||
|
else
|
||||||
|
LineGap.delete (lineStart, deleteLength, buffer)
|
||||||
|
|
||||||
|
(* get next line to dedent *)
|
||||||
|
val buffer = LineGap.goToIdx (lineStart, buffer)
|
||||||
|
val lineEnd = Cursor.viDlr (buffer, lineStart, 1)
|
||||||
|
val buffer = LineGap.goToIdx (lineEnd, buffer)
|
||||||
|
val nextLine = Cursor.viL (buffer, lineEnd)
|
||||||
|
|
||||||
|
val count = if lineEnd = nextLine then 0 else count - 1
|
||||||
|
in
|
||||||
|
loop (nextLine, buffer, count)
|
||||||
|
end
|
||||||
|
in
|
||||||
fun dedentLine (app: app_type, count, time) =
|
fun dedentLine (app: app_type, count, time) =
|
||||||
let
|
let
|
||||||
open MailboxType
|
open MailboxType
|
||||||
@@ -69,28 +99,38 @@ struct
|
|||||||
|
|
||||||
val lineStart = Cursor.vi0 (buffer, cursorIdx)
|
val lineStart = Cursor.vi0 (buffer, cursorIdx)
|
||||||
val firstNonSpaceChr = Cursor.firstNonSpaceChr (buffer, lineStart)
|
val firstNonSpaceChr = Cursor.firstNonSpaceChr (buffer, lineStart)
|
||||||
in
|
|
||||||
if lineStart = firstNonSpaceChr then
|
|
||||||
(* can't dedent; no leading spaces *)
|
|
||||||
app
|
|
||||||
else
|
|
||||||
let
|
|
||||||
(* calculate length to delete *)
|
(* calculate length to delete *)
|
||||||
val difference = firstNonSpaceChr - lineStart
|
val difference = firstNonSpaceChr - lineStart
|
||||||
val deleteLength = if difference < 2 then difference else 2
|
val deleteLength = Int.min (difference, 2)
|
||||||
|
|
||||||
val buffer = LineGap.delete (lineStart, deleteLength, buffer)
|
(* delete once *)
|
||||||
val buffer = LineGap.goToStart buffer
|
val buffer =
|
||||||
val initialMsg = [SEARCH (buffer, searchString, time)]
|
if deleteLength = 0 then buffer
|
||||||
|
else LineGap.delete (lineStart, deleteLength, buffer)
|
||||||
|
|
||||||
(* The cursorIdx might be past the current line after we dedent.
|
(* Calculate nextLine and newCursorIdx.
|
||||||
|
* The cursorIdx might be past the current line after we dedent.
|
||||||
* If it is, we put the cursorIdx at the last char of the line. *)
|
* If it is, we put the cursorIdx at the last char of the line. *)
|
||||||
val buffer = LineGap.goToIdx (lineStart, buffer)
|
val buffer = LineGap.goToIdx (lineStart, buffer)
|
||||||
val lineEnd = Cursor.viDlr (buffer, lineStart, 1)
|
val lineEnd = Cursor.viDlr (buffer, lineStart, 1)
|
||||||
val cursorIdx = if lineEnd < cursorIdx then lineEnd else cursorIdx
|
val buffer = LineGap.goToIdx (lineEnd, buffer)
|
||||||
|
val nextLine = Cursor.viL (buffer, lineEnd)
|
||||||
|
val newCursorIdx = Int.min (lineEnd, cursorIdx)
|
||||||
|
|
||||||
|
val buffer =
|
||||||
|
if lineEnd = nextLine then
|
||||||
|
(* at end of file, so we cannot dedent anymore *)
|
||||||
|
buffer
|
||||||
|
else
|
||||||
|
(* dedent remaining lines specified by count *)
|
||||||
|
loop (nextLine, buffer, count - 1)
|
||||||
|
|
||||||
|
val buffer = LineGap.goToStart buffer
|
||||||
|
val initialMsg = [SEARCH (buffer, searchString, time)]
|
||||||
in
|
in
|
||||||
NormalDelete.finishAfterDeletingBuffer
|
NormalDelete.finishAfterDeletingBuffer
|
||||||
(app, cursorIdx, buffer, time, initialMsg)
|
(app, newCursorIdx, buffer, time, initialMsg)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user