Skip to content

Commit

Permalink
Add line comments in single line selections
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Jervis committed Feb 14, 2024
1 parent 78308b7 commit e9e0a17
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/Playroom/CodeEditor/keymaps/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import type { Selection } from './types';
const BLOCK_COMMENT_START = '{/*';
const BLOCK_COMMENT_END = '*/}';

const LINE_COMMENT = '//';

const BLOCK_COMMENT_OFFSET = BLOCK_COMMENT_START.length + 1;
// const LINE_COMMENT_OFFSET = '// '.length;
const LINE_COMMENT_OFFSET = LINE_COMMENT.length + 1;

interface IsReverseSelectionOptions {
anchor: CodeMirror.Position;
Expand Down Expand Up @@ -90,18 +92,21 @@ function getSelectionToOffset({
return BLOCK_COMMENT_OFFSET;
}

type CommentType = 'line' | 'block';

interface TagRange {
from: CodeMirror.Position;
to: CodeMirror.Position;
multiLine: boolean;
existingIndent: number;
isAlreadyCommented: boolean;
commentType: CommentType;
}

const determineCommentType = (
cm: Editor,
from: CodeMirror.Position
): 'line' | 'block' => {
): CommentType => {
if (cm.getModeAt(from).name === 'javascript') {
return 'line';
}
Expand Down Expand Up @@ -133,7 +138,7 @@ export const wrapInComment = (cm: Editor) => {
}

const commentType = determineCommentType(cm, from);
console.log('commentType: ', commentType);

const fullContent = cm.getRange(new Pos(from.line, 0), new Pos(to.line));
const existingIndent = fullContent.length - fullContent.trimStart().length;

Expand All @@ -155,6 +160,7 @@ export const wrapInComment = (cm: Editor) => {
multiLine: isMultiLineSelection,
existingIndent,
isAlreadyCommented,
commentType,
});

// Todo - change offset from BLOCK_COMMENT_OFFSET to LINE_COMMENT_OFFSET for prop comment
Expand Down Expand Up @@ -189,6 +195,7 @@ export const wrapInComment = (cm: Editor) => {
const existingContent = cm.getRange(newRangeFrom, newRangeTo);

if (range.isAlreadyCommented) {
// TODO: Handle line uncomments
const existingContentWithoutComment = existingContent.replace(
/\{\/\*\s?|\s?\*\/\}/g,
''
Expand All @@ -198,8 +205,19 @@ export const wrapInComment = (cm: Editor) => {
newRangeFrom,
newRangeTo
);
} else if (range.commentType === 'block') {
cm.replaceRange(
`${BLOCK_COMMENT_START} ${existingContent} ${BLOCK_COMMENT_END}`,
newRangeFrom,
newRangeTo
);
} else {
cm.replaceRange(`{/* ${existingContent} */}`, newRangeFrom, newRangeTo);
// TODO: Handle multiline line comments
cm.replaceRange(
`${LINE_COMMENT} ${existingContent}`,
newRangeFrom,
newRangeTo
);
}
}

Expand Down

0 comments on commit e9e0a17

Please sign in to comment.