import { commands, window } from 'vscode'
import { extensionCommands } from './commands'

export const activate = (context) => {
  extensionCommands.map(cmd => {
    context.subscriptions.push(
      commands.registerCommand(cmd.key, () => editorInsert(cmd.callback()))
    )
  })
}

const editorInsert = (text) => {
  const editor = window.activeTextEditor

  if (!editor) {
    window.showErrorMessage('No editor found!')
    return
  }

  const cursorPosition = editor.selection.active

  editor.edit((builder) => {
    builder.insert(cursorPosition, text)
  })
}

import { commands, window, Position, Selection } from 'vscode'
import { extensionCommands } from './commands'

export const activate = (context) => {
  extensionCommands.map(cmd => {
    context.subscriptions.push(
      commands.registerCommand(cmd.key, () => editorInsert(cmd.callback()))
    )
  })
}

const editorInsert = (text) => {
  const editor = window.activeTextEditor

  if (!editor) {
    window.showErrorMessage('No active text editor found!')
    return
  }

  const newSelections = []
  editor.edit((builder) => {
    editor.selections.map(selection => {
      builder.replace(selection, text)
      newSelections.push(getEndPosition(selection, text))
    })
  }).then(() => {
    editor.selections = newSelections
  })
}

const getEndPosition = (selection, text) => {
  var endPosition = new Position(selection.start.line, selection.start.character + text.length)
  return new Selection(endPosition, endPosition)
}

Add support to multiple selections when inserting text
Add support to multiple selections when inserting text
