rename cursor.sml's 'clipIdxAfterDelete' function to 'clipIdx', simplify it, and use it in other functions too
This commit is contained in:
@@ -101,12 +101,13 @@ struct
|
|||||||
val {line = bufferLine, idx = bufferIdx, ...} = buffer
|
val {line = bufferLine, idx = bufferIdx, ...} = buffer
|
||||||
|
|
||||||
val bufferIdx = bufferIdx - 1
|
val bufferIdx = bufferIdx - 1
|
||||||
|
val bufferIdx = Cursor.clipIdx (buffer, bufferIdx)
|
||||||
val bufferLine = bufferLine - 1
|
val bufferLine = bufferLine - 1
|
||||||
|
|
||||||
val buffer = LineGap.goToIdx (bufferIdx, buffer)
|
val buffer = LineGap.goToIdx (bufferIdx, buffer)
|
||||||
val bufferLine =
|
val bufferLine =
|
||||||
let
|
let
|
||||||
val maxHeight = windowHeight - (TextConstants.ySpace * 2)
|
val maxHeight = windowHeight - TextConstants.ySpace
|
||||||
in
|
in
|
||||||
TextWindow.getStartLineWithCursorCentered
|
TextWindow.getStartLineWithCursorCentered
|
||||||
(buffer, bufferIdx, bufferLine, windowWidth, maxHeight)
|
(buffer, bufferIdx, bufferLine, windowWidth, maxHeight)
|
||||||
@@ -159,6 +160,7 @@ struct
|
|||||||
let
|
let
|
||||||
val buffer = LineGap.goToIdx (cursorIdx, buffer)
|
val buffer = LineGap.goToIdx (cursorIdx, buffer)
|
||||||
val cursorIdx = fMove (buffer, cursorIdx)
|
val cursorIdx = fMove (buffer, cursorIdx)
|
||||||
|
val cursorIdx = Cursor.clipIdx (buffer, cursorIdx)
|
||||||
in
|
in
|
||||||
helpMove (app, buffer, cursorIdx, count - 1, fMove)
|
helpMove (app, buffer, cursorIdx, count - 1, fMove)
|
||||||
end
|
end
|
||||||
@@ -258,6 +260,7 @@ struct
|
|||||||
(* move LineGap to cursorIdx, which is necessary for finding newCursorIdx *)
|
(* move LineGap to cursorIdx, which is necessary for finding newCursorIdx *)
|
||||||
val buffer = LineGap.goToIdx (cursorIdx, buffer)
|
val buffer = LineGap.goToIdx (cursorIdx, buffer)
|
||||||
val cursorIdx = fMove (buffer, cursorIdx, chr)
|
val cursorIdx = fMove (buffer, cursorIdx, chr)
|
||||||
|
val cursorIdx = Cursor.clipIdx (buffer, cursorIdx)
|
||||||
in
|
in
|
||||||
helpMoveToChr (app, buffer, cursorIdx, count - 1, fMove, chr)
|
helpMoveToChr (app, buffer, cursorIdx, count - 1, fMove, chr)
|
||||||
end
|
end
|
||||||
@@ -345,7 +348,7 @@ struct
|
|||||||
* is no longer a valid idx,
|
* is no longer a valid idx,
|
||||||
* clip cursorIdx to the end. *)
|
* clip cursorIdx to the end. *)
|
||||||
val buffer = LineGap.goToIdx (cursorIdx, buffer)
|
val buffer = LineGap.goToIdx (cursorIdx, buffer)
|
||||||
val cursorIdx = Cursor.clipIdxAfterDelete (buffer, cursorIdx)
|
val cursorIdx = Cursor.clipIdx (buffer, cursorIdx)
|
||||||
in
|
in
|
||||||
buildTextAndClear (app, buffer, cursorIdx)
|
buildTextAndClear (app, buffer, cursorIdx)
|
||||||
end
|
end
|
||||||
@@ -364,13 +367,6 @@ struct
|
|||||||
val length = high - low
|
val length = high - low
|
||||||
|
|
||||||
val buffer = LineGap.delete (low, length, buffer)
|
val buffer = LineGap.delete (low, length, buffer)
|
||||||
|
|
||||||
(* todo: possibly decrement cursorIdx by 1
|
|
||||||
* if deleting put cursorIdx past end of file.
|
|
||||||
* else, if deleting put cursorIdx before 0,
|
|
||||||
* ensure that it is clipped to 0.
|
|
||||||
* else, leave cursorIdx alone.
|
|
||||||
* *)
|
|
||||||
in
|
in
|
||||||
helpDelete (app, buffer, low, count - 1, fMove)
|
helpDelete (app, buffer, low, count - 1, fMove)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1595,7 +1595,7 @@ struct
|
|||||||
val lineIdx = Vector.sub (lhd, relativeLine)
|
val lineIdx = Vector.sub (lhd, relativeLine)
|
||||||
in
|
in
|
||||||
if lineIdx = String.size shd - 1 andalso List.null stl then
|
if lineIdx = String.size shd - 1 andalso List.null stl then
|
||||||
(* if is end of buffer, return last idx in buffer; * else,
|
(* if is end of buffer, return last idx in buffer; else,
|
||||||
* increment by 1 as we want to go to first char after line break *)
|
* increment by 1 as we want to go to first char after line break *)
|
||||||
bufferIdx + lineIdx
|
bufferIdx + lineIdx
|
||||||
else
|
else
|
||||||
@@ -1715,33 +1715,36 @@ struct
|
|||||||
end
|
end
|
||||||
|
|
||||||
(* Prerequisite: lineGap is moved to cursorIdx *)
|
(* Prerequisite: lineGap is moved to cursorIdx *)
|
||||||
fun clipIdxAfterDelete (lineGap: LineGap.t, cursorIdx) =
|
fun clipIdx (lineGap: LineGap.t, cursorIdx) =
|
||||||
let
|
let
|
||||||
val {rightStrings, idx = bufferIdx, ...} = lineGap
|
val {rightStrings, idx = bufferIdx, ...} = lineGap
|
||||||
in
|
in
|
||||||
|
(* We are trying to check if cursorIdx is within the buffer. *)
|
||||||
case rightStrings of
|
case rightStrings of
|
||||||
hd :: tl =>
|
_ :: _ :: _ =>
|
||||||
|
(* if there is a string after the hd,
|
||||||
|
* we are definitely in a valid idx and should return it *)
|
||||||
|
cursorIdx
|
||||||
|
| [hd] =>
|
||||||
let
|
let
|
||||||
val strIdx = cursorIdx - bufferIdx
|
val strIdx = cursorIdx - bufferIdx
|
||||||
in
|
in
|
||||||
if strIdx < String.size hd then
|
if strIdx < String.size hd - 1 then
|
||||||
|
(* if we are before the last char in the string.
|
||||||
|
* Unix file endings always have \n at the end
|
||||||
|
* but we do not want cursor to ever go to end
|
||||||
|
* as vi also does not go to the very end.
|
||||||
|
* This is why we check strIdx is before the last char.
|
||||||
|
* *)
|
||||||
cursorIdx
|
cursorIdx
|
||||||
else
|
else
|
||||||
(* strIdx is in tl *)
|
(* if end of buffer - 2 is greater than 0, then that;
|
||||||
(case tl of
|
* else, 0 *)
|
||||||
tlhd :: tltl =>
|
Int.max (bufferIdx + String.size hd - 2, 0)
|
||||||
let
|
|
||||||
val strIdx = strIdx - String.size hd
|
|
||||||
in
|
|
||||||
if strIdx < String.size tlhd then
|
|
||||||
cursorIdx
|
|
||||||
else
|
|
||||||
bufferIdx + String.size hd + String.size tlhd - 1
|
|
||||||
end
|
|
||||||
| [] =>
|
|
||||||
bufferIdx + String.size hd - 1)
|
|
||||||
end
|
end
|
||||||
| [] =>
|
| [] =>
|
||||||
Int.max (bufferIdx - 1, 0)
|
(* if end of buffer - 2 is greater than 0, then that;
|
||||||
|
* else, 0 *)
|
||||||
|
Int.max (bufferIdx - 2, 0)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user