Skip to content

Commit

Permalink
UI: Initial Cursor Implementation (onivim#31)
Browse files Browse the repository at this point in the history
* Rename Position -> Index

* Complete refactoring of Position -> Index

* Hook up cursor move action

* Add an initial, basic cursor
  • Loading branch information
bryphe authored Feb 6, 2019
1 parent 61fea4d commit 83b5731
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 42 deletions.
1 change: 1 addition & 0 deletions src/editor/Core/Actions.re
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ open Types;
type t =
| BufferUpdate(BufferUpdate.t)
| ChangeMode(Mode.t)
| CursorMove(BufferPosition.t)
| SetEditorFont(EditorFont.t)
| Noop;
1 change: 1 addition & 0 deletions src/editor/Core/Reducer.re
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ let reduce: (State.t, Actions.t) => State.t =
| ChangeMode(m) =>
let ret: State.t = {...s, mode: m};
ret;
| CursorMove(b) => {...s, cursorPosition: b}
| BufferUpdate(bu) => {...s, buffer: Buffer.update(s.buffer, bu)}
| SetEditorFont(font) => {...s, editorFont: font}
| Noop => s
Expand Down
2 changes: 2 additions & 0 deletions src/editor/Core/State.re
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ type t = {
tabs: list(Tab.t),
buffer: Buffer.t,
editorFont: EditorFont.t,
cursorPosition: BufferPosition.t,
};

let create: unit => t =
() => {
mode: Insert,
buffer: Buffer.ofLines([|"testing"|]),
cursorPosition: BufferPosition.createFromZeroBasedIndices(0, 0),
editorFont:
EditorFont.create(
~fontFile="FiraCode-Regular.ttf",
Expand Down
12 changes: 6 additions & 6 deletions src/editor/Core/TokenizedBufferView.re
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ module BufferViewLine = {
* The original line number for this line. May be less than the
* line index, if any lines were wrapped
*/
lineNumber: Position.t,
lineNumber: Index.t,
/*
* The 'virtual' line number - this is the screen-space line number
* that accounts for wrapping. If there is no wrapping, this will be
* equal to the lineNumber - if there is wrapping, this may be greater
* than the original lineNumber */
virtualLineNumber: Position.t,
virtualLineNumber: Index.t,
/*
* lineOffset is the value that position 0 of the virtual line
* maps to in the original buffer line. If `lineOffset` is 0,
* that means this is not a wrapped line. If `lineOffset` <> 0,
* it is a wrapped line.
*/
lineOffset: Position.t,
lineOffset: Index.t,
tokens: list(Tokenizer.t),
};
};
Expand All @@ -38,9 +38,9 @@ let _toViewWithoutWrapping = (tokenizedBuffer: TokenizedBuffer.t) => {
let f: (int, list(Tokenizer.t)) => BufferViewLine.t =
(i, tokens) => {
let ret: BufferViewLine.t = {
lineNumber: ZeroBasedPosition(i),
virtualLineNumber: ZeroBasedPosition(i),
lineOffset: ZeroBasedPosition(0),
lineNumber: ZeroBasedIndex(i),
virtualLineNumber: ZeroBasedIndex(i),
lineOffset: ZeroBasedIndex(0),
tokens,
};
ret;
Expand Down
8 changes: 4 additions & 4 deletions src/editor/Core/Tokenizer.re
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ open Types;

type t = {
text: string,
startPosition: Position.t,
endPosition: Position.t,
startPosition: Index.t,
endPosition: Index.t,
};

let _isWhitespace = c => {
Expand Down Expand Up @@ -52,8 +52,8 @@ let tokenize: string => list(t) =

let token: t = {
text: tokenText,
startPosition: ZeroBasedPosition(startToken),
endPosition: ZeroBasedPosition(endToken),
startPosition: ZeroBasedIndex(startToken),
endPosition: ZeroBasedIndex(endToken),
};

tokens := List.append([token], tokens^);
Expand Down
28 changes: 22 additions & 6 deletions src/editor/Core/Types.re
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module Position = {
module Index = {
type t =
| ZeroBasedPosition(int)
| OneBasedPosition(int);
| ZeroBasedIndex(int)
| OneBasedIndex(int);

let toZeroBasedIndex = (pos: t) =>
let toZeroBasedInt = (pos: t) =>
switch (pos) {
| ZeroBasedPosition(n) => n
| OneBasedPosition(n) => n - 1
| ZeroBasedIndex(n) => n
| OneBasedIndex(n) => n - 1
};
};

Expand All @@ -24,6 +24,22 @@ module Mode = {
};
};

module BufferPosition = {
type t = {
line: Index.t,
character: Index.t,
};

let create = (line, character) => {
line, character
};

let createFromZeroBasedIndices = (line: int, character: int) => {
line: ZeroBasedIndex(line),
character: ZeroBasedIndex(character),
};
}

module BufferUpdate = {
type t = {
startLine: int,
Expand Down
4 changes: 2 additions & 2 deletions src/editor/Neovim/Notification.re
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ let parseAutoCommand = (autocmd: string, args: list(Msgpck.t)) => {
| [M.Int(activeBufferId), M.Int(cursorLine), M.Int(cursorColumn)] =>
Types.AutoCommandContext.create(
~activeBufferId,
~cursorLine,
~cursorColumn,
~cursorLine=OneBasedIndex(cursorLine),
~cursorColumn=OneBasedIndex(cursorColumn),
(),
)
| _ => raise(InvalidAutoCommandContext)
Expand Down
12 changes: 6 additions & 6 deletions src/editor/Neovim/Types.re
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ open Oni_Core.Types;

module CursorPosition = {
type t = {
line: Position.t,
character: Position.t,
line: Index.t,
character: Index.t,
};
};

Expand All @@ -18,8 +18,8 @@ module EditorState = {
module AutoCommandContext = {
type t = {
activeBufferId: int,
cursorLine: int,
cursorColumn: int,
cursorLine: Index.t,
cursorColumn: Index.t,
};

let create = (~activeBufferId, ~cursorLine, ~cursorColumn, ()) => {
Expand All @@ -32,8 +32,8 @@ module AutoCommandContext = {
"activeBufferId: "
++ string_of_int(v.activeBufferId)
++ " line: "
++ string_of_int(v.cursorLine)
++ string_of_int(Index.toZeroBasedInt(v.cursorLine))
++ " column: "
++ string_of_int(v.cursorColumn);
++ string_of_int(Index.toZeroBasedInt(v.cursorColumn));
};
};
29 changes: 25 additions & 4 deletions src/editor/UI/EditorSurface.re
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* the view of the buffer in the window.
*/

/* open Revery.Core; */
open Revery.Core;
open Revery.UI;

open CamomileLibraryDefault.Camomile;
Expand Down Expand Up @@ -37,7 +37,7 @@ let tokensToElement =
Style.[
position(`Absolute),
top(fontHeight * virtualLineNumber),
left(fontWidth * Position.toZeroBasedIndex(token.startPosition)),
left(fontWidth * Index.toZeroBasedInt(token.startPosition)),
fontFamily("FiraCode-Regular.ttf"),
fontSize(14),
height(fontHeight),
Expand All @@ -55,7 +55,7 @@ let viewLinesToElements =
tokensToElement(
fontWidth,
fontHeight,
Position.toZeroBasedIndex(b.virtualLineNumber),
Index.toZeroBasedInt(b.virtualLineNumber),
b.tokens,
);
};
Expand All @@ -80,14 +80,35 @@ let make = (state: State.t) =>
bufferView,
);

let fontHeight = state.editorFont.measuredHeight;
let fontWidth = state.editorFont.measuredWidth;

let cursorWidth = switch(state.mode) {
| Insert => 2
| _ => fontWidth
};

let cursorStyle =
Style.[
position(`Absolute),
top(fontHeight * Index.toZeroBasedInt(state.cursorPosition.line)),
left(fontWidth * Index.toZeroBasedInt(state.cursorPosition.character)),
height(fontHeight),
width(cursorWidth),
opacity(0.8),
backgroundColor(Colors.white),
];

let elements = [ <View style=cursorStyle />, ...textElements ];

let style =
Style.[
backgroundColor(theme.background),
color(theme.foreground),
flexGrow(1),
];

<View style> ...textElements </View>;
<View style> ...elements </View>;
});

let createElement = (~state, ~children as _, ()) =>
Expand Down
4 changes: 4 additions & 0 deletions src/editor/bin/Oni2.re
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ let init = app => {
| ModeChanged("normal") => Core.Actions.ChangeMode(Normal)
| ModeChanged("insert") => Core.Actions.ChangeMode(Insert)
| ModeChanged(_) => Core.Actions.ChangeMode(Other)
| CursorMoved(c) => Core.Actions.CursorMove(Core.Types.BufferPosition.create(
c.cursorLine,
c.cursorColumn,
))
| BufferLines(bc) =>
Core.Actions.BufferUpdate(
Core.Types.BufferUpdate.create(
Expand Down
8 changes: 4 additions & 4 deletions test/editor/Core/Helpers.re
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ let validateToken =
expectedToken: Tokenizer.t,
) => {
expect.string(actualToken.text).toEqual(expectedToken.text);
expect.int(Position.toZeroBasedIndex(actualToken.startPosition)).toBe(
Position.toZeroBasedIndex(expectedToken.startPosition),
expect.int(Index.toZeroBasedInt(actualToken.startPosition)).toBe(
Index.toZeroBasedInt(expectedToken.startPosition),
);
expect.int(Position.toZeroBasedIndex(actualToken.endPosition)).toBe(
Position.toZeroBasedIndex(expectedToken.endPosition),
expect.int(Index.toZeroBasedInt(actualToken.endPosition)).toBe(
Index.toZeroBasedInt(expectedToken.endPosition),
);
};

Expand Down
20 changes: 10 additions & 10 deletions test/editor/Core/TokenizerTests.re
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ describe("tokenize", ({test, _}) => {
let expectedTokens: list(Tokenizer.t) = [
{
text: "testWord",
startPosition: ZeroBasedPosition(0),
endPosition: ZeroBasedPosition(8),
startPosition: ZeroBasedIndex(0),
endPosition: ZeroBasedIndex(8),
},
];

Expand All @@ -34,8 +34,8 @@ describe("tokenize", ({test, _}) => {
let expectedTokens: list(Tokenizer.t) = [
{
text: "testWord",
startPosition: ZeroBasedPosition(2),
endPosition: ZeroBasedPosition(10),
startPosition: ZeroBasedIndex(2),
endPosition: ZeroBasedIndex(10),
},
];

Expand All @@ -48,8 +48,8 @@ describe("tokenize", ({test, _}) => {
let expectedTokens: list(Tokenizer.t) = [
{
text: "a",
startPosition: ZeroBasedPosition(0),
endPosition: ZeroBasedPosition(1),
startPosition: ZeroBasedIndex(0),
endPosition: ZeroBasedIndex(1),
},
];

Expand All @@ -62,13 +62,13 @@ describe("tokenize", ({test, _}) => {
let expectedTokens: list(Tokenizer.t) = [
{
text: "a",
startPosition: ZeroBasedPosition(1),
endPosition: ZeroBasedPosition(2),
startPosition: ZeroBasedIndex(1),
endPosition: ZeroBasedIndex(2),
},
{
text: "btest",
startPosition: ZeroBasedPosition(3),
endPosition: ZeroBasedPosition(8),
startPosition: ZeroBasedIndex(3),
endPosition: ZeroBasedIndex(8),
},
];

Expand Down

0 comments on commit 83b5731

Please sign in to comment.