Skip to content

Commit 7139de3

Browse files
committed
Light refactoring with Array.at()
1 parent ca2a0b2 commit 7139de3

File tree

10 files changed

+36
-64
lines changed

10 files changed

+36
-64
lines changed

extensionBase.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,10 @@ export async function activate(context: vscode.ExtensionContext, handleLocal: bo
111111
Register.loadFromDisk(handleLocal);
112112
await Promise.all([ExCommandLine.loadHistory(context), SearchCommandLine.loadHistory(context)]);
113113

114-
if (vscode.window.activeTextEditor) {
115-
const filepathComponents = vscode.window.activeTextEditor.document.fileName.split(/\\|\//);
116-
Register.setReadonlyRegister('%', filepathComponents[filepathComponents.length - 1]);
114+
const document = vscode.window.activeTextEditor?.document;
115+
if (document) {
116+
const filepathComponents = document.fileName.split(/\\|\//);
117+
Register.setReadonlyRegister('%', filepathComponents.at(-1)!);
117118
}
118119

119120
// workspace events
@@ -192,10 +193,9 @@ export async function activate(context: vscode.ExtensionContext, handleLocal: bo
192193
if (modeHandler == null) {
193194
shouldDelete = true;
194195
} else {
195-
const document = modeHandler.vimState.document;
196-
if (!vscode.workspace.textDocuments.includes(document)) {
196+
if (!vscode.workspace.textDocuments.includes(modeHandler.vimState.document)) {
197197
shouldDelete = true;
198-
if (closedDocument === document) {
198+
if (closedDocument === modeHandler.vimState.document) {
199199
lastClosedModeHandler = modeHandler;
200200
}
201201
}

src/actions/commands/insert.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ export class CommandInsertInInsertMode extends BaseCommand {
235235
keys = ['<character>'];
236236

237237
public override async exec(position: Position, vimState: VimState): Promise<void> {
238-
const char = this.keysPressed[this.keysPressed.length - 1];
238+
const char = this.keysPressed.at(-1)!;
239239

240240
let text = char;
241241

@@ -271,7 +271,7 @@ export class CommandInsertInInsertMode extends BaseCommand {
271271
}
272272

273273
public override toString(): string {
274-
return this.keysPressed[this.keysPressed.length - 1];
274+
return this.keysPressed.at(-1)!;
275275
}
276276
}
277277

src/actions/operator.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export abstract class BaseOperator extends BaseAction {
5555

5656
public doesRepeatedOperatorApply(vimState: VimState, keysPressed: string[]) {
5757
const nonCountActions = vimState.recordedState.actionsRun.filter((x) => x.name !== 'cmd_num');
58-
const prevAction = nonCountActions[nonCountActions.length - 1];
58+
const prevAction = nonCountActions.at(-1);
5959
return (
6060
keysPressed.length === 1 &&
6161
prevAction &&
@@ -894,7 +894,7 @@ class ActionVisualReflowParagraph extends BaseOperator {
894894
const chunksToReflow: Chunk[] = [];
895895

896896
for (const line of s.split('\n')) {
897-
let lastChunk: Chunk | undefined = chunksToReflow[chunksToReflow.length - 1];
897+
let lastChunk: Chunk | undefined = chunksToReflow.at(-1);
898898
const trimmedLine = line.trimStart();
899899

900900
// See what comment type they are using.
@@ -958,7 +958,7 @@ class ActionVisualReflowParagraph extends BaseOperator {
958958

959959
// Parse out commenting style, gather words.
960960

961-
lastChunk = chunksToReflow[chunksToReflow.length - 1];
961+
lastChunk = chunksToReflow.at(-1)!;
962962

963963
if (lastChunk.commentType.singleLine) {
964964
// is it a continuation of a comment like "//"
@@ -1001,7 +1001,7 @@ class ActionVisualReflowParagraph extends BaseOperator {
10011001
// Preserve blank lines in output.
10021002
if (line.trim() === '') {
10031003
// Replace empty content line with blank line.
1004-
if (lines[lines.length - 1] === '') {
1004+
if (lines.at(-1) === '') {
10051005
lines.pop();
10061006
}
10071007

@@ -1015,7 +1015,7 @@ class ActionVisualReflowParagraph extends BaseOperator {
10151015

10161016
// Repeatedly partition line into pieces that fit in maximumLineLength
10171017
while (line) {
1018-
const lastLine = lines[lines.length - 1];
1018+
const lastLine = lines.at(-1)!;
10191019

10201020
// Determine the separator that we'd need to add to the last line
10211021
// in order to join onto this line.
@@ -1078,7 +1078,7 @@ class ActionVisualReflowParagraph extends BaseOperator {
10781078
}
10791079

10801080
// Drop final empty content line.
1081-
if (lines[lines.length - 1] === '') {
1081+
if (lines.at(-1) === '') {
10821082
lines.pop();
10831083
}
10841084

src/cmd_line/commandLine.ts

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -359,11 +359,7 @@ export class SearchCommandLine extends CommandLine {
359359
}
360360

361361
public static async addSearchStateToHistory(searchState: SearchState) {
362-
const prevSearchString =
363-
SearchCommandLine.previousSearchStates.length === 0
364-
? undefined
365-
: SearchCommandLine.previousSearchStates[SearchCommandLine.previousSearchStates.length - 1]
366-
.searchString;
362+
const prevSearchString = SearchCommandLine.previousSearchStates.at(-1)?.searchString;
367363
// Store this search if different than previous
368364
if (searchState.searchString !== prevSearchString) {
369365
SearchCommandLine.previousSearchStates.push(searchState);
@@ -467,11 +463,9 @@ export class SearchCommandLine extends CommandLine {
467463
public async run(vimState: VimState): Promise<void> {
468464
// Repeat the previous search if no new string is entered
469465
if (this.text === '') {
470-
if (SearchCommandLine.previousSearchStates.length > 0) {
471-
this.text =
472-
SearchCommandLine.previousSearchStates[
473-
SearchCommandLine.previousSearchStates.length - 1
474-
].searchString;
466+
const prevSearchString = SearchCommandLine.previousSearchStates.at(-1)?.searchString;
467+
if (prevSearchString !== undefined) {
468+
this.text = prevSearchString;
475469
}
476470
}
477471
Logger.info(`Searching for ${this.text}`);
@@ -509,10 +503,7 @@ export class SearchCommandLine extends CommandLine {
509503
public async escape(vimState: VimState): Promise<void> {
510504
vimState.cursorStopPosition = this.searchState.cursorStartPosition;
511505

512-
const prevSearchList = SearchCommandLine.previousSearchStates;
513-
globalState.searchState = prevSearchList
514-
? prevSearchList[prevSearchList.length - 1]
515-
: undefined;
506+
globalState.searchState = SearchCommandLine.previousSearchStates.at(-1);
516507

517508
if (vimState.modeData.mode === Mode.SearchInProgressMode) {
518509
const offset =

src/cmd_line/commands/move.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,11 @@ export class MoveCommand extends ExCommand {
6060
let cursorPosition: Position;
6161
if (dest > sourceEnd) {
6262
// make the cursor position at the beginning of the endline.
63-
cursorPosition = new Position(
64-
Math.max(dest, 0),
65-
lines[lines.length - 1].match(/\S/)?.index ?? 0,
66-
);
63+
cursorPosition = new Position(Math.max(dest, 0), lines.at(-1)!.match(/\S/)?.index ?? 0);
6764
} else {
6865
cursorPosition = new Position(
6966
Math.max(dest + lines.length, 0),
70-
lines[lines.length - 1].match(/\S/)?.index ?? 0,
67+
lines.at(-1)!.match(/\S/)?.index ?? 0,
7168
);
7269
}
7370
// delete

src/common/motion/position.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -511,10 +511,7 @@ Position.prototype.advancePositionByText = function (this: Position, text: strin
511511
if (newlines.length === 0) {
512512
return new Position(this.line, this.character + text.length);
513513
} else {
514-
return new Position(
515-
this.line + newlines.length,
516-
text.length - (newlines[newlines.length - 1] + 1),
517-
);
514+
return new Position(this.line + newlines.length, text.length - (newlines.at(-1)! + 1));
518515
}
519516
};
520517

src/configuration/remapper.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export class Remapper implements IRemapper {
111111

112112
const userDefinedRemappings = configuration[this.configKey] as Map<string, IKeyRemapping>;
113113

114-
if (keys[keys.length - 1] === SpecialKeys.TimeoutFinished) {
114+
if (keys.at(-1) === SpecialKeys.TimeoutFinished) {
115115
// Timeout finished. Don't let an ambiguous or potential remap start another timeout again
116116
keys = keys.slice(0, keys.length - 1);
117117
allowBufferingKeys = false;
@@ -422,7 +422,7 @@ export class Remapper implements IRemapper {
422422
// If there was a performing remap that finished waiting for timeout then only the remaining keys
423423
// that are not part of that remap were typed by the user.
424424
let specialKey: string | undefined = '';
425-
if (remainingKeys[remainingKeys.length - 1] === SpecialKeys.TimeoutFinished) {
425+
if (remainingKeys.at(-1) === SpecialKeys.TimeoutFinished) {
426426
specialKey = remainingKeys.pop();
427427
}
428428
const lastRemap = remapState.wasPerformingRemapThatFinishedWaitingForTimeout.after!;

src/mode/modeHandler.ts

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -513,10 +513,7 @@ export class ModeHandler implements vscode.Disposable, IModeHandler {
513513
// is disabled. This makes it possible to map zero without making it impossible
514514
// to type a count with a zero.
515515
const preventZeroRemap =
516-
key === '0' &&
517-
this.vimState.recordedState.actionsRun[
518-
this.vimState.recordedState.actionsRun.length - 1
519-
] instanceof CommandNumber;
516+
key === '0' && this.vimState.recordedState.actionsRun.at(-1) instanceof CommandNumber;
520517

521518
// Check for remapped keys if:
522519
// 1. We are not currently performing a non-recursive remapping
@@ -544,10 +541,7 @@ export class ModeHandler implements vscode.Disposable, IModeHandler {
544541
// Remove the <TimeoutFinished> key and get the key before that. If the <TimeoutFinished>
545542
// key was the last key, then 'key' will be undefined and won't be sent to handle action.
546543
this.vimState.recordedState.commandList.pop();
547-
key =
548-
this.vimState.recordedState.commandList[
549-
this.vimState.recordedState.commandList.length - 1
550-
];
544+
key = this.vimState.recordedState.commandList.at(-1)!;
551545
}
552546
if (key !== undefined) {
553547
handledAsAction = await this.handleKeyAsAnAction(key);
@@ -684,11 +678,10 @@ export class ModeHandler implements vscode.Disposable, IModeHandler {
684678
recordedState.actionsRunPressedKeys.push(...recordedState.actionKeys);
685679

686680
let actionToRecord: BaseAction | undefined = action;
687-
if (recordedState.actionsRun.length === 0) {
681+
const lastAction = recordedState.actionsRun.at(-1);
682+
if (lastAction === undefined) {
688683
recordedState.actionsRun.push(action);
689684
} else {
690-
const lastAction = recordedState.actionsRun[recordedState.actionsRun.length - 1];
691-
692685
const actionCanBeMergedWithDocumentChange =
693686
action instanceof CommandInsertInInsertMode ||
694687
action instanceof CommandBackspaceInInsertMode ||
@@ -1420,7 +1413,7 @@ export class ModeHandler implements vscode.Disposable, IModeHandler {
14201413
const combinedSelections: vscode.Selection[] = [];
14211414
sel.forEach((s, i) => {
14221415
if (i > 0) {
1423-
const previousSelection = combinedSelections[combinedSelections.length - 1];
1416+
const previousSelection = combinedSelections.at(-1)!;
14241417
const overlap = s.intersection(previousSelection);
14251418
if (overlap) {
14261419
combinedSelections[combinedSelections.length - 1] = s.anchor.isBeforeOrEqual(s.active)
@@ -1498,13 +1491,11 @@ export class ModeHandler implements vscode.Disposable, IModeHandler {
14981491
* Extend this condition if it is the desired behaviour for other actions as well.
14991492
*/
15001493
const isLastCursorTracked =
1501-
this.vimState.recordedState.actionsRun[
1502-
this.vimState.recordedState.actionsRun.length - 1
1503-
] instanceof ActionOverrideCmdD;
1494+
this.vimState.recordedState.actionsRun.at(-1) instanceof ActionOverrideCmdD;
15041495

15051496
let cursorToTrack: Cursor;
15061497
if (isLastCursorTracked) {
1507-
cursorToTrack = this.vimState.cursors[this.vimState.cursors.length - 1];
1498+
cursorToTrack = this.vimState.cursors.at(-1)!;
15081499
} else {
15091500
cursorToTrack = this.vimState.cursors[0];
15101501
}
@@ -1585,13 +1576,9 @@ export class ModeHandler implements vscode.Disposable, IModeHandler {
15851576
if (this.vimState.currentMode === Mode.Insert || this.vimState.currentMode === Mode.Replace) {
15861577
let virtualKey: string | undefined;
15871578
if (this.vimState.recordedState.bufferedKeys.length > 0) {
1588-
virtualKey =
1589-
this.vimState.recordedState.bufferedKeys[
1590-
this.vimState.recordedState.bufferedKeys.length - 1
1591-
];
1579+
virtualKey = this.vimState.recordedState.bufferedKeys.at(-1);
15921580
} else if (this.vimState.recordedState.waitingForAnotherActionKey) {
1593-
virtualKey =
1594-
this.vimState.recordedState.actionKeys[this.vimState.recordedState.actionKeys.length - 1];
1581+
virtualKey = this.vimState.recordedState.actionKeys.at(-1);
15951582
if (virtualKey === '<C-r>') {
15961583
virtualKey = '"';
15971584
} else if (virtualKey === '<C-k>') {

src/textobject/word.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ export function prevWordEnd(document: TextDocument, pos: Position, wordType: Wor
158158
if (currentLine > -1) {
159159
continue;
160160
}
161-
newCharacter = positions[positions.length - 1];
161+
newCharacter = positions.at(-1)!;
162162
} else {
163163
newCharacter = positions[index];
164164
}

src/vimscript/expression/evaluate.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ export class EvaluationContext {
268268

269269
let store: VariableStore | undefined;
270270
if (this.localScopes.length > 0 && varExpr.namespace === undefined) {
271-
store = this.localScopes[this.localScopes.length - 1];
271+
store = this.localScopes.at(-1);
272272
} else if (varExpr.namespace === 'g' || varExpr.namespace === undefined) {
273273
store = EvaluationContext.globalVariables;
274274
} else {
@@ -1174,7 +1174,7 @@ export class EvaluationContext {
11741174
if (result[0] === '') {
11751175
result.shift();
11761176
}
1177-
if (result && result[result.length - 1] === '') {
1177+
if (result.at(-1) === '') {
11781178
result.pop();
11791179
}
11801180
}

0 commit comments

Comments
 (0)