Skip to content

Commit d9678dd

Browse files
committed
feat: work with cursor b4 when alone on line
1 parent 35c6558 commit d9678dd

File tree

2 files changed

+41
-28
lines changed

2 files changed

+41
-28
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jsdoc-comment-toggler",
3-
"version": "1.9.0",
3+
"version": "1.10.0",
44
"preview": true,
55
"displayName": "jsdoc comment toggler",
66
"description": "toggle JSDoc comment on line or selection",

src/extension.ts

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/no-floating-promises */
12
// useful API pages
23
// https://code.visualstudio.com/api/references/vscode-api#TextEditor
34
// https://code.visualstudio.com/api/references/vscode-api#TextDocument
@@ -200,6 +201,7 @@ export const getIndentation = (line: vscode.TextLine | number): string => {
200201
* _after_, the cursor movement is noticeable and somewhat slow
201202
* @param isSingleLineComment - precalculated
202203
*/
204+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
203205
const adjustCursorPos = async (isSingleLineComment: boolean) => {
204206
const editor = getEditor()
205207
const cursorPos = editor.selection.active
@@ -224,7 +226,7 @@ const adjustCursorPos = async (isSingleLineComment: boolean) => {
224226
// at end of line
225227
if (cursorPos.isEqual(getContentEndPos(cursorPos.line))) {
226228
// https://code.visualstudio.com/api/references/commands
227-
vscode.commands.executeCommand("cursorMove", {
229+
await vscode.commands.executeCommand("cursorMove", {
228230
to: "left",
229231
by: "character",
230232
value: 3,
@@ -381,16 +383,23 @@ export const toggleJSDocComment = async (): Promise<boolean> => {
381383
// construct and trigger single batch of changes
382384
return editor.edit((editBuilder) => {
383385
// #region - remove single line jsdoc, selection or no selection
386+
const isJSDocCommentFullLine =
387+
lineFirst.firstNonWhitespaceCharacterIndex === jsdocStart?.index &&
388+
jsdocEnd &&
389+
getContentEndPos(lineFirst).character - jsdocEnd[0].length ===
390+
jsdocEnd.index
391+
384392
if (
385393
isSingleLineSelection &&
386394
jsdocStart?.index !== undefined &&
387395
jsdocEnd?.index !== undefined &&
388-
new vscode.Range(
396+
(new vscode.Range(
389397
lineFirst.lineNumber,
390398
jsdocStart.index,
391399
lineLast.lineNumber,
392400
jsdocEnd.index + jsdocEnd[0].length
393-
).contains(editor.selection.active)
401+
).contains(editor.selection.active) ||
402+
isJSDocCommentFullLine)
394403
) {
395404
log("removing single line jsdoc")
396405

@@ -467,12 +476,17 @@ export const toggleJSDocComment = async (): Promise<boolean> => {
467476
// #region - no jsdoc exists but possibly block or line comment
468477
if (isSingleLineSelection) {
469478
const lineCommentIndex = lineFirst.text.indexOf(LINE_COMMENT_TAG)
479+
const isLineCommentFullLine =
480+
lineFirst.firstNonWhitespaceCharacterIndex === lineCommentIndex
481+
470482
const blockCommentStartIndex = lineFirst.text.indexOf(
471483
BLOCK_COMMENT_START_TAG
472484
)
473485
const blockCommentEndIndex = lineFirst.text.indexOf(BLOCK_COMMENT_END_TAG)
474-
const isLineCommentFullLine =
475-
lineFirst.firstNonWhitespaceCharacterIndex === lineCommentIndex
486+
const isBlockCommentFullLine =
487+
lineFirst.firstNonWhitespaceCharacterIndex === blockCommentStartIndex &&
488+
getContentEndPos(lineFirst).character - BLOCK_COMMENT_END_TAG.length ===
489+
blockCommentEndIndex
476490
const isBlockCommentTrailing =
477491
lineFirst.firstNonWhitespaceCharacterIndex !== blockCommentStartIndex &&
478492
(lineFirst.text.length - BLOCK_COMMENT_END_TAG.length ===
@@ -504,12 +518,14 @@ export const toggleJSDocComment = async (): Promise<boolean> => {
504518
} else if (
505519
blockCommentStartIndex > -1 &&
506520
blockCommentEndIndex > -1 &&
507-
editor.selection.active.character > blockCommentStartIndex &&
508-
editor.selection.active.character <
509-
blockCommentEndIndex + BLOCK_COMMENT_END_TAG.length
521+
// active cursor within block comment or full line is a block comment
522+
((editor.selection.active.character >= blockCommentStartIndex &&
523+
editor.selection.active.character <
524+
blockCommentEndIndex + BLOCK_COMMENT_END_TAG.length + 1) ||
525+
isBlockCommentFullLine)
510526
) {
511527
log("converting block comment to jsdoc")
512-
// block comment already exists and active cursor within it
528+
513529
const firstChar = editor.document.getText(
514530
new vscode.Range(
515531
lineFirst.lineNumber,
@@ -583,7 +599,6 @@ export const toggleJSDocComment = async (): Promise<boolean> => {
583599
`${nextCommentChars} */\n${indent}${prevContent}${nextContent}`
584600
)
585601
} else {
586-
// block comment not alone
587602
editBuilder.replace(
588603
new vscode.Range(
589604
lineFirst.lineNumber,
@@ -601,20 +616,10 @@ export const toggleJSDocComment = async (): Promise<boolean> => {
601616
}
602617
} else if (
603618
lineCommentIndex > -1 &&
604-
editor.selection.active.character > lineCommentIndex
619+
(editor.selection.active.character > lineCommentIndex ||
620+
isLineCommentFullLine)
605621
) {
606622
log("converting line comment to jsdoc")
607-
const firstChar = editor.document.getText(
608-
new vscode.Range(
609-
lineFirst.lineNumber,
610-
lineFirst.firstNonWhitespaceCharacterIndex +
611-
LINE_COMMENT_TAG.length,
612-
lineFirst.lineNumber,
613-
lineFirst.firstNonWhitespaceCharacterIndex +
614-
LINE_COMMENT_TAG.length +
615-
1
616-
)
617-
)
618623

619624
const indent = getIndentation(lineFirst)
620625
const prevLineText = getPrevLine(lineFirst)?.text.trim()
@@ -647,20 +652,28 @@ export const toggleJSDocComment = async (): Promise<boolean> => {
647652
"*"
648653
)
649654
} else if (isLineCommentFullLine) {
655+
const firstChar = editor.document.getText(
656+
new vscode.Range(
657+
lineFirst.lineNumber,
658+
lineCommentIndex + LINE_COMMENT_TAG.length,
659+
lineFirst.lineNumber,
660+
lineCommentIndex + LINE_COMMENT_TAG.length + 1
661+
)
662+
)
663+
650664
// REVIEW: could try to standardize by matching before and after cursor with regex here
651665
editBuilder.replace(
652666
new vscode.Range(
653667
lineFirst.lineNumber,
654-
0,
668+
lineCommentIndex,
655669
lineFirst.lineNumber,
656-
lineFirst.firstNonWhitespaceCharacterIndex +
657-
LINE_COMMENT_TAG.length
670+
lineCommentIndex + LINE_COMMENT_TAG.length
658671
),
659672
""
660673
)
661674
editBuilder.insert(
662-
new vscode.Position(lineFirst.lineNumber, 0),
663-
`${indent}/**${firstChar !== " " ? " " : ""}`
675+
new vscode.Position(lineFirst.lineNumber, lineCommentIndex),
676+
`/**${firstChar !== " " ? " " : ""}`
664677
)
665678

666679
const lastChar = editor.document.getText(

0 commit comments

Comments
 (0)