Skip to content

Commit

Permalink
Support reverse selections
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Jervis committed Feb 13, 2024
1 parent ce107c3 commit 55e4008
Showing 1 changed file with 23 additions and 58 deletions.
81 changes: 23 additions & 58 deletions src/Playroom/CodeEditor/keymaps/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,22 @@ import type CodeMirror from 'codemirror';
import { type Editor, Pos } from 'codemirror';
import type { Selection } from './types';

const COMMENT_SYNTAX_OFFSET = '{/* '.length;

// Todo - find word for bookend in software
interface GetSelectionRangeOffsetCountOptions {
pointType: 'anchor' | 'head';
multiLine?: boolean;
reverseSelection: boolean;
}

function getSelectionRangeOffsetCount({
pointType,
multiLine,
reverseSelection,
}: GetSelectionRangeOffsetCountOptions): number {
if (!multiLine) {
return COMMENT_SYNTAX_OFFSET;
}

if (
(pointType === 'head' && !reverseSelection) ||
(pointType === 'anchor' && reverseSelection)
) {
return 0;
}

return COMMENT_SYNTAX_OFFSET;
}
const BLOCK_COMMENT_OFFSET = '{/* '.length;
// const LINE_COMMENT_OFFSET = '// '.length;

interface IsReverseSelectionOptions {
from: CodeMirror.Position;
to: CodeMirror.Position;
anchor: CodeMirror.Position;
head: CodeMirror.Position;
}

function isReverseSelection({ from, to }: IsReverseSelectionOptions): boolean {
return from.line > to.line || (from.line === to.line && from.ch > to.ch);
function isReverseSelection({
anchor,
head,
}: IsReverseSelectionOptions): boolean {
return (
anchor.line > head.line ||
(anchor.line === head.line && anchor.ch > head.ch)
);
}

interface TagRange {
Expand Down Expand Up @@ -71,37 +52,21 @@ export const wrapInComment = (cm: Editor) => {
existingIndent,
});

// Todo - change offset "+ 4" for prop comment
const anchorOffset = getSelectionRangeOffsetCount({
pointType: 'anchor',
multiLine: isMultiLineSelection,
reverseSelection: isReverseSelection({ from, to }),
});

const headOffset = getSelectionRangeOffsetCount({
pointType: 'head',
multiLine: isMultiLineSelection,
reverseSelection: isReverseSelection({ from, to }),
});
// Todo - change offset from BLOCK_COMMENT_OFFSET to LINE_COMMENT_OFFSET for prop comment

const newSelectionRangeAnchor = new Pos(from.line, from.ch + anchorOffset);
const newSelectionRangeHead = new Pos(to.line, to.ch + headOffset);
const toOffset = isMultiLineSelection ? 0 : BLOCK_COMMENT_OFFSET;

newSelections.push({
anchor: newSelectionRangeAnchor,
head: newSelectionRangeHead,
});
const newSelectionRangeFrom = new Pos(
from.line,
from.ch + BLOCK_COMMENT_OFFSET
);
const newSelectionRangeTo = new Pos(to.line, to.ch + toOffset);

if (from.ch < to.ch) {
console.log('from.ch < to.ch');
} else {
console.log('from.ch >= to.ch');
}
const newSelection = isReverseSelection(range)
? { anchor: newSelectionRangeTo, head: newSelectionRangeFrom }
: { anchor: newSelectionRangeFrom, head: newSelectionRangeTo };

// Todo - incorporate lines added
// if (isMultiLineSelection) {
// linesAdded += 2;
// }
newSelections.push(newSelection);
}

cm.operation(() => {
Expand Down

0 comments on commit 55e4008

Please sign in to comment.