From 82082b1f369b97111168fe9a613f6958621dc001 Mon Sep 17 00:00:00 2001 From: kimlimjustin Date: Wed, 18 Aug 2021 21:57:58 +0700 Subject: [PATCH] feat: update jsdoc insteaed of creating new if it already exists (#4) --- src/main.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/main.ts b/src/main.ts index e243e30..f844dc5 100644 --- a/src/main.ts +++ b/src/main.ts @@ -19,6 +19,19 @@ const generateJSDoc = () => { } const paramList = m[1].replace(/[\t\s\r]/g, '').split(',').filter(s => s !== ''); + let jsdocExist = editor.document.lineAt(editor.selection.active.line - 1).text.indexOf(' */') !== -1; + let _line = editor.selection.active.line - 2; + if(jsdocExist){ + while(editor.document.lineAt(_line).text.indexOf("/**") === -1){ + if(editor.document.lineAt(_line).text.indexOf("*") === -1){ + jsdocExist = false; + break; + } + _line -= 1; + } + } + + editor.edit(editBuilder => { // Insert text above current line const selectionLine = editor.document.lineAt(selection.start.line); @@ -49,6 +62,9 @@ const generateJSDoc = () => { text = `${padSpaceStr}${text}`; text = text.slice(0, text.length - whitespace - 1); + if(jsdocExist){ + editBuilder.delete(new vscode.Range(new vscode.Position(_line, 0), new vscode.Position(editor.selection.active.line, 0))); + } // Insert the text :) editBuilder.insert(insertPosition, text); });