MCPcopy
hub / github.com/VSCodeVim/Vim / exec

Method exec

src/actions/commands/insert.ts:218–292  ·  view source on GitHub ↗
(position: Position, vimState: VimState)

Source from the content-addressed store, hash-verified

216 }
217
218 public override async exec(position: Position, vimState: VimState): Promise<void> {
219 void vscode.commands.executeCommand('closeParameterHints');
220 void vscode.commands.executeCommand('editor.action.inlineSuggest.hide');
221
222 vimState.cursors = vimState.cursors.map((x) => x.withNewStop(x.stop.getLeft()));
223 if (vimState.returnToInsertAfterCommand && position.character !== 0) {
224 vimState.cursors = vimState.cursors.map((x) => x.withNewStop(x.stop.getRight()));
225 }
226
227 // only remove leading spaces inserted by vscode.
228 // vscode only inserts them when user enter a new line,
229 // ie, o/O in Normal mode or \n in Insert mode.
230 const lastActionBeforeEsc =
231 vimState.recordedState.actionsRun[vimState.recordedState.actionsRun.length - 2];
232 if (
233 vimState.document.languageId !== 'plaintext' &&
234 (lastActionBeforeEsc instanceof InsertBelow ||
235 lastActionBeforeEsc instanceof InsertAbove ||
236 (lastActionBeforeEsc instanceof DocumentContentChangeAction &&
237 lastActionBeforeEsc.keysPressed[lastActionBeforeEsc.keysPressed.length - 1] === '\n'))
238 ) {
239 for (const cursor of vimState.cursors) {
240 const line = vimState.document.lineAt(cursor.stop);
241 if (line.text.length > 0 && line.isEmptyOrWhitespace) {
242 vimState.recordedState.transformer.delete(line.range);
243 }
244 }
245 }
246 await vimState.setCurrentMode(Mode.Normal);
247
248 // If we wanted to repeat this insert (only for i and a), now is the time to do it. Insert
249 // count amount of these strings before returning back to normal mode
250 const shouldRepeatInsert =
251 vimState.recordedState.count > 1 &&
252 vimState.recordedState.actionsRun.find(
253 (a) =>
254 a instanceof Insert ||
255 a instanceof Append ||
256 a instanceof InsertAtLineBegin ||
257 a instanceof InsertAtLineEnd ||
258 a instanceof InsertAfterFirstWhitespaceOnLine ||
259 a instanceof InsertAtLastChange,
260 ) !== undefined;
261
262 // If this is the type to repeat insert, do this now
263 if (shouldRepeatInsert) {
264 const changeAction = vimState.recordedState.actionsRun
265 .slice()
266 .reverse()
267 .find((a) => a instanceof DocumentContentChangeAction);
268 if (changeAction instanceof DocumentContentChangeAction) {
269 // Add count amount of inserts in the case of 4i=<esc>
270 // TODO: A few actions such as <C-t> should be repeated, but are not
271 for (let i = 0; i < vimState.recordedState.count - 1; i++) {
272 // If this is the last transform, move cursor back one character
273 const positionDiff =
274 i === vimState.recordedState.count - 2
275 ? PositionDiff.offset({ character: -1 })

Callers

nothing calls this directly

Calls 10

withNewStopMethod · 0.80
getLeftMethod · 0.80
getRightMethod · 0.80
findMethod · 0.80
offsetMethod · 0.80
identityMethod · 0.80
addTransformationMethod · 0.80
getTransformationMethod · 0.80
deleteMethod · 0.45
setCurrentModeMethod · 0.45

Tested by

no test coverage detected