Skip to content

Commit

Permalink
feat(commands): added addRemoveComment command
Browse files Browse the repository at this point in the history
  • Loading branch information
YuryShkoda committed May 18, 2023
1 parent ebda9ef commit 58d41df
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@
"icon": "./assets/images/docs.png"
}
],
"keybindings": [
{
"command": "sasjs-for-vscode.addRemoveComment",
"key": "ctrl+alt+l",
"mac": "cmd+/",
"win": "ctr+/"
}
],
"configuration": {
"title": "SASJS",
"properties": {
Expand Down
81 changes: 81 additions & 0 deletions src/commands/addRemoveComment/addRemoveComment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { window, ExtensionContext, commands, Range } from 'vscode'
import { MockedActiveEditor } from '../../types/spec/activeEditor'

export class AddRemoveCommentCommand {
private commentStartRegExp = /^\/\*\s{0,1}/
private commentEndRegExp = /\s{0,1}\*\/$/

constructor(private context: ExtensionContext) {}

initialise = () => {
const commentOutLineCommand = commands.registerCommand(
'sasjs-for-vscode.addRemoveComment',
() => this.addRemoveComment()
)

this.context.subscriptions.push(commentOutLineCommand)
}

private isWrappedWithComments(line: string) {
return (
this.commentStartRegExp.test(line) && this.commentEndRegExp.test(line)
)
}

private addRemoveCommentToLine(line: string) {
const commentStartRegExp = /^\/\*\s{0,1}/
const commentEndRegExp = /\s{0,1}\*\/$/

const lineWithoutComments = line
.replace(commentStartRegExp, '')
.replace(commentEndRegExp, '')

if (this.isWrappedWithComments(line)) {
return lineWithoutComments
} else {
return `/* ${lineWithoutComments} */`
}
}

private addRemoveComment = async (
mockedActiveEditor?: MockedActiveEditor // INFO: used for unit tests
) => {
const activeEditor = mockedActiveEditor || window.activeTextEditor

if (activeEditor) {
const { start, end } = activeEditor.selection
const text = activeEditor.document.getText()
const lines = text.split(`\n`) || []

const editedLines = lines
.reduce((acc: string[], line: string, i: number) => {
if (i >= start.line && i <= end.line) {
acc.push(this.addRemoveCommentToLine(line))
}

return acc
}, [])
.join(`\n`)

// INFO: exit point for unit test
if (mockedActiveEditor) {
return editedLines
}

activeEditor.edit((editBuilder) => {
const range = new Range(start.line, 0, end.line, lines[end.line].length)

editBuilder.replace(range, editedLines)
})

// INFO: move cursor to the middle of the empty comment
if (editedLines.length === 6 && this.isWrappedWithComments(editedLines)) {
commands.executeCommand('cursorMove', {
to: 'left',
by: 'character',
value: 3
})
}
}
}
}
5 changes: 5 additions & 0 deletions src/types/spec/activeEditor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface MockedActiveEditor {
document: { getText: () => string }
selection: { start: { line: number }; end: { line: number } }
edit: () => void
}

0 comments on commit 58d41df

Please sign in to comment.