Skip to content

Commit

Permalink
feat: selectedTextFromLines() function
Browse files Browse the repository at this point in the history
  • Loading branch information
kuuote committed Apr 9, 2022
1 parent 0542589 commit 49d193a
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 7 deletions.
7 changes: 1 addition & 6 deletions src/selection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@
import React from "./deps/react.ts";
import { defaultPosition } from "./line.tsx";
import { Rect } from "./rect.ts";
import { Position } from "./types.ts";

export type Selection = {
start: Position;
end: Position;
};
import { Selection } from "./types.ts";

export const defaultSelection: Selection = {
start: defaultPosition,
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ export type CursorView = {
top: number;
height: number;
};

export type Selection = {
start: Position;
end: Position;
};
29 changes: 28 additions & 1 deletion src/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { assertEquals, assertStrictEquals } from "./deps/std/asserts.ts";
import { clamp, countIndent } from "./util.ts";
import { assertStrictEquals } from "./deps/std/asserts.ts";
import { Selection } from "./types.ts";
import { selectedTextFromLines } from "./util.ts";

Deno.test("clamp()", () => {
assertStrictEquals(clamp(34, 56, 43.0), 43.0);
Expand All @@ -13,3 +15,28 @@ Deno.test("countIndent()", () => {
assertStrictEquals(countIndent("  hoge"), 2);
assertStrictEquals(countIndent(" \tfuga"), 2);
});

function mkSelection(...args: number[]): Selection {
return {
start: {
line: args[0],
column: args[1],
},
end: {
line: args[2],
column: args[3],
},
};
}

Deno.test("selectedTextFromLines()", () => {
const lines = ["one", "two", "three"].map((s) => ({ text: s }));
const tests: [number[], string[]][] = [
[[2, 1, 2, 4], ["t", "hre", "e"]],
[[0, 2, 2, 2], ["on", "e\ntwo\nth", "ree"]],
];
for (const [sel, expected] of tests) {
const actual = selectedTextFromLines(lines, mkSelection(...sel));
assertEquals(actual, expected);
}
});
28 changes: 28 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,35 @@
import { Selection } from "./types.ts";

export function clamp(min: number, num: number, max: number) {
return Math.max(min, Math.min(num, max));
}

export function countIndent(text: string) {
return text.length - text.trimStart().length;
}

export type SelectedText = [start: string, selected: string, end: string];

export function selectedTextFromLines(
lines: { text: string }[],
selection: Selection,
): SelectedText {
const text = lines.slice(
selection.start.line,
selection.end.line + 1,
)
.map((l) => l.text);

const start = text[0].slice(0, selection.start.column);
const end = text[text.length - 1].slice(selection.end.column);

text[text.length - 1] = text[text.length - 1].slice(
0,
selection.end.column,
);
text[0] = text[0].slice(selection.start.column, text[0].length);

const selected = text.join("\n");

return [start, selected, end];
}

0 comments on commit 49d193a

Please sign in to comment.