Skip to content

Commit

Permalink
fix(add-remove-comment): fixed for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
YuryShkoda committed May 19, 2023
1 parent f3a2db1 commit e79a6c1
Showing 1 changed file with 46 additions and 8 deletions.
54 changes: 46 additions & 8 deletions src/commands/addRemoveComment/addRemoveComment.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import { window, ExtensionContext, commands, Range } from 'vscode'
import {
window,
ExtensionContext,
commands,
Range,
Selection,
Position
} from 'vscode'
import { MockedActiveEditor } from '../../types/spec/activeEditor'
import { isWindows } from '@sasjs/utils'

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

constructor(private context: ExtensionContext) {}

Expand Down Expand Up @@ -50,7 +59,8 @@ export class AddRemoveCommentCommand {
const editedLines = lines
.reduce((acc: string[], line: string, i: number) => {
if (i >= start.line && i <= end.line) {
acc.push(this.addRemoveCommentToLine(line))
// INFO: \r has to be removed due to line ending differences between Windows and Unix systems
acc.push(this.addRemoveCommentToLine(line.replace(/\r/, '')))
}

return acc
Expand All @@ -66,15 +76,43 @@ export class AddRemoveCommentCommand {
const range = new Range(start.line, 0, end.line, lines[end.line].length)

editBuilder.replace(range, editedLines)

// INFO: select all updated lines
const lastLine = lines[end.line]
const lastUpdatedLineLength =
lastLine.length +
(this.isWrappedWithComments(lastLine) ? 0 : this.comment.length)

const selectionStart = new Position(start.line, 0)
const selectionEnd = new Position(end.line, lastUpdatedLineLength)

activeEditor.selection = new Selection(selectionStart, selectionEnd)
})

// 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
})
if (editedLines === this.comment) {
if (isWindows()) {
// INFO: on Windows the courser is moved only when selection is true.
commands
.executeCommand('cursorMove', {
to: 'left',
by: 'halfLine',
value: 1,
select: true
})
.then(() => {
// INFO: clear selection after cursor movement
const position = activeEditor.selection.end as Position

activeEditor.selection = new Selection(position, position)
})
} else {
commands.executeCommand('cursorMove', {
to: 'left',
by: 'character',
value: 3
})
}
}
}
}
Expand Down

0 comments on commit e79a6c1

Please sign in to comment.