-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(commands): added addRemoveComment command
- Loading branch information
1 parent
ebda9ef
commit 58d41df
Showing
3 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |