Skip to content

Commit eba1cc8

Browse files
committed
Remove arrays.findIndex
For #103454 This should be a direct map to the `.findIndex` mathod
1 parent a9a7cf1 commit eba1cc8

File tree

13 files changed

+31
-61
lines changed

13 files changed

+31
-61
lines changed

extensions/git/src/model.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import { workspace, WorkspaceFoldersChangeEvent, Uri, window, Event, EventEmitter, QuickPickItem, Disposable, SourceControl, SourceControlResourceGroup, TextEditor, Memento, OutputChannel, commands } from 'vscode';
77
import { Repository, RepositoryState } from './repository';
88
import { memoize, sequentialize, debounce } from './decorators';
9-
import { dispose, anyEvent, filterEvent, isDescendant, firstIndex, pathEquals, toDisposable, eventToPromise } from './util';
9+
import { dispose, anyEvent, filterEvent, isDescendant, pathEquals, toDisposable, eventToPromise } from './util';
1010
import { Git } from './git';
1111
import * as path from 'path';
1212
import * as fs from 'fs';
@@ -372,7 +372,7 @@ export class Model implements IRemoteSourceProviderRegistry, IPushErrorHandlerRe
372372
const picks = this.openRepositories.map((e, index) => new RepositoryPick(e.repository, index));
373373
const active = window.activeTextEditor;
374374
const repository = active && this.getRepository(active.document.fileName);
375-
const index = firstIndex(picks, pick => pick.repository === repository);
375+
const index = picks.findIndex(pick => pick.repository === repository);
376376

377377
// Move repository pick containing the active text editor to appear first
378378
if (index > -1) {

extensions/git/src/util.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,16 +182,6 @@ export function uniqueFilter<T>(keyFn: (t: T) => string): (t: T) => boolean {
182182
};
183183
}
184184

185-
export function firstIndex<T>(array: T[], fn: (t: T) => boolean): number {
186-
for (let i = 0; i < array.length; i++) {
187-
if (fn(array[i])) {
188-
return i;
189-
}
190-
}
191-
192-
return -1;
193-
}
194-
195185
export function find<T>(array: T[], fn: (t: T) => boolean): T | undefined {
196186
let result: T | undefined = undefined;
197187

src/vs/base/browser/ui/list/listWidget.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import 'vs/css!./list';
77
import { IDisposable, dispose, DisposableStore } from 'vs/base/common/lifecycle';
88
import { isNumber } from 'vs/base/common/types';
9-
import { range, firstIndex, binarySearch } from 'vs/base/common/arrays';
9+
import { range, binarySearch } from 'vs/base/common/arrays';
1010
import { memoize } from 'vs/base/common/decorators';
1111
import * as DOM from 'vs/base/browser/dom';
1212
import * as platform from 'vs/base/common/platform';
@@ -55,7 +55,7 @@ class TraitRenderer<T> implements IListRenderer<T, ITraitTemplateData>
5555
}
5656

5757
renderElement(element: T, index: number, templateData: ITraitTemplateData): void {
58-
const renderedElementIndex = firstIndex(this.renderedElements, el => el.templateData === templateData);
58+
const renderedElementIndex = this.renderedElements.findIndex(el => el.templateData === templateData);
5959

6060
if (renderedElementIndex >= 0) {
6161
const rendered = this.renderedElements[renderedElementIndex];
@@ -96,7 +96,7 @@ class TraitRenderer<T> implements IListRenderer<T, ITraitTemplateData>
9696
}
9797

9898
disposeTemplate(templateData: ITraitTemplateData): void {
99-
const index = firstIndex(this.renderedElements, el => el.templateData === templateData);
99+
const index = this.renderedElements.findIndex(el => el.templateData === templateData);
100100

101101
if (index < 0) {
102102
return;

src/vs/base/browser/ui/splitview/paneview.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { domEvent } from 'vs/base/browser/event';
1010
import { StandardKeyboardEvent } from 'vs/base/browser/keyboardEvent';
1111
import { KeyCode } from 'vs/base/common/keyCodes';
1212
import { $, append, addClass, removeClass, toggleClass, trackFocus, EventHelper, clearNode } from 'vs/base/browser/dom';
13-
import { firstIndex } from 'vs/base/common/arrays';
1413
import { Color, RGBA } from 'vs/base/common/color';
1514
import { SplitView, IView } from './splitview';
1615
import { isFirefox } from 'vs/base/browser/browser';
@@ -474,7 +473,7 @@ export class PaneView extends Disposable {
474473
}
475474

476475
removePane(pane: Pane): void {
477-
const index = firstIndex(this.paneItems, item => item.pane === pane);
476+
const index = this.paneItems.findIndex(item => item.pane === pane);
478477

479478
if (index === -1) {
480479
return;
@@ -486,8 +485,8 @@ export class PaneView extends Disposable {
486485
}
487486

488487
movePane(from: Pane, to: Pane): void {
489-
const fromIndex = firstIndex(this.paneItems, item => item.pane === from);
490-
const toIndex = firstIndex(this.paneItems, item => item.pane === to);
488+
const fromIndex = this.paneItems.findIndex(item => item.pane === from);
489+
const toIndex = this.paneItems.findIndex(item => item.pane === to);
491490

492491
if (fromIndex === -1 || toIndex === -1) {
493492
return;
@@ -500,7 +499,7 @@ export class PaneView extends Disposable {
500499
}
501500

502501
resizePane(pane: Pane, size: number): void {
503-
const index = firstIndex(this.paneItems, item => item.pane === pane);
502+
const index = this.paneItems.findIndex(item => item.pane === pane);
504503

505504
if (index === -1) {
506505
return;
@@ -510,7 +509,7 @@ export class PaneView extends Disposable {
510509
}
511510

512511
getPaneSize(pane: Pane): number {
513-
const index = firstIndex(this.paneItems, item => item.pane === pane);
512+
const index = this.paneItems.findIndex(item => item.pane === pane);
514513

515514
if (index === -1) {
516515
return -1;

src/vs/base/browser/ui/splitview/splitview.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Event, Emitter } from 'vs/base/common/event';
99
import * as types from 'vs/base/common/types';
1010
import * as dom from 'vs/base/browser/dom';
1111
import { clamp } from 'vs/base/common/numbers';
12-
import { range, firstIndex, pushToStart, pushToEnd } from 'vs/base/common/arrays';
12+
import { range, pushToStart, pushToEnd } from 'vs/base/common/arrays';
1313
import { Sash, Orientation, ISashEvent as IBaseSashEvent, SashState } from 'vs/base/browser/ui/sash/sash';
1414
import { Color } from 'vs/base/common/color';
1515
import { domEvent } from 'vs/base/browser/event';
@@ -460,7 +460,7 @@ export class SplitView<TLayoutContext = undefined> extends Disposable {
460460
item.enabled = false;
461461
}
462462

463-
const index = firstIndex(this.sashItems, item => item.sash === sash);
463+
const index = this.sashItems.findIndex(item => item.sash === sash);
464464

465465
// This way, we can press Alt while we resize a sash, macOS style!
466466
const disposable = combinedDisposable(
@@ -709,11 +709,11 @@ export class SplitView<TLayoutContext = undefined> extends Disposable {
709709
const onStartDisposable = onStart(this.onSashStart, this);
710710
const onChange = Event.map(sash.onDidChange, sashEventMapper);
711711
const onChangeDisposable = onChange(this.onSashChange, this);
712-
const onEnd = Event.map(sash.onDidEnd, () => firstIndex(this.sashItems, item => item.sash === sash));
712+
const onEnd = Event.map(sash.onDidEnd, () => this.sashItems.findIndex(item => item.sash === sash));
713713
const onEndDisposable = onEnd(this.onSashEnd, this);
714714

715715
const onDidResetDisposable = sash.onDidReset(() => {
716-
const index = firstIndex(this.sashItems, item => item.sash === sash);
716+
const index = this.sashItems.findIndex(item => item.sash === sash);
717717
const upIndexes = range(index, -1);
718718
const downIndexes = range(index + 1, this.viewItems.length);
719719
const snapBeforeIndex = this.findFirstSnapIndex(upIndexes);

src/vs/base/common/arrays.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -399,29 +399,13 @@ export function lastIndex<T>(array: ReadonlyArray<T>, fn: (item: T) => boolean):
399399
return -1;
400400
}
401401

402-
/**
403-
* @deprecated ES6: use `Array.findIndex`
404-
*/
405-
export function firstIndex<T>(array: ReadonlyArray<T>, fn: (item: T) => boolean): number {
406-
for (let i = 0; i < array.length; i++) {
407-
const element = array[i];
408-
409-
if (fn(element)) {
410-
return i;
411-
}
412-
}
413-
414-
return -1;
415-
}
416-
417-
418402
/**
419403
* @deprecated ES6: use `Array.find`
420404
*/
421405
export function first<T>(array: ReadonlyArray<T>, fn: (item: T) => boolean, notFoundValue: T): T;
422406
export function first<T>(array: ReadonlyArray<T>, fn: (item: T) => boolean): T | undefined;
423407
export function first<T>(array: ReadonlyArray<T>, fn: (item: T) => boolean, notFoundValue: T | undefined = undefined): T | undefined {
424-
const index = firstIndex(array, fn);
408+
const index = array.findIndex(fn);
425409
return index < 0 ? notFoundValue : array[index];
426410
}
427411

src/vs/editor/contrib/wordHighlighter/wordHighlighter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ class WordHighlighter {
239239

240240
public moveNext() {
241241
let highlights = this._getSortedHighlights();
242-
let index = arrays.firstIndex(highlights, (range) => range.containsPosition(this.editor.getPosition()));
242+
let index = highlights.findIndex((range) => range.containsPosition(this.editor.getPosition()));
243243
let newIndex = ((index + 1) % highlights.length);
244244
let dest = highlights[newIndex];
245245
try {
@@ -258,7 +258,7 @@ class WordHighlighter {
258258

259259
public moveBack() {
260260
let highlights = this._getSortedHighlights();
261-
let index = arrays.firstIndex(highlights, (range) => range.containsPosition(this.editor.getPosition()));
261+
let index = highlights.findIndex((range) => range.containsPosition(this.editor.getPosition()));
262262
let newIndex = ((index - 1 + highlights.length) % highlights.length);
263263
let dest = highlights[newIndex];
264264
try {

src/vs/platform/environment/node/argvHelper.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import * as assert from 'assert';
7-
import { firstIndex } from 'vs/base/common/arrays';
87
import { localize } from 'vs/nls';
98
import { MIN_MAX_MEMORY_SIZE_MB } from 'vs/platform/files/common/files';
109
import { parseArgs, ErrorReporter, OPTIONS } from 'vs/platform/environment/node/argv';
@@ -33,7 +32,7 @@ function parseAndValidate(cmdLineArgs: string[], reportWarnings: boolean): Nativ
3332
}
3433

3534
function stripAppPath(argv: string[]): string[] | undefined {
36-
const index = firstIndex(argv, a => !/^-/.test(a));
35+
const index = argv.findIndex(a => !/^-/.test(a));
3736

3837
if (index > -1) {
3938
return [...argv.slice(0, index), ...argv.slice(index + 1)];

src/vs/workbench/contrib/comments/browser/commentThreadWidget.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ export class ReviewZoneWidget extends ZoneWidget implements ICommentThreadWidget
663663

664664
this._disposables.add(newCommentNode);
665665
this._disposables.add(newCommentNode.onDidClick(clickedNode =>
666-
this.setFocusedComment(arrays.firstIndex(this._commentElements, commentNode => commentNode.comment.uniqueIdInThread === clickedNode.comment.uniqueIdInThread))
666+
this.setFocusedComment(this._commentElements.findIndex(commentNode => commentNode.comment.uniqueIdInThread === clickedNode.comment.uniqueIdInThread))
667667
));
668668

669669
return newCommentNode;

src/vs/workbench/contrib/comments/common/commentModel.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import { URI } from 'vs/base/common/uri';
77
import { IRange } from 'vs/editor/common/core/range';
88
import { Comment, CommentThread, CommentThreadChangedEvent } from 'vs/editor/common/modes';
9-
import { groupBy, firstIndex, flatten } from 'vs/base/common/arrays';
9+
import { groupBy, flatten } from 'vs/base/common/arrays';
1010
import { localize } from 'vs/nls';
1111

1212
export interface ICommentThreadChangedEvent extends CommentThreadChangedEvent {
@@ -83,11 +83,11 @@ export class CommentsModel {
8383

8484
removed.forEach(thread => {
8585
// Find resource that has the comment thread
86-
const matchingResourceIndex = firstIndex(threadsForOwner, (resourceData) => resourceData.id === thread.resource);
86+
const matchingResourceIndex = threadsForOwner.findIndex((resourceData) => resourceData.id === thread.resource);
8787
const matchingResourceData = threadsForOwner[matchingResourceIndex];
8888

8989
// Find comment node on resource that is that thread and remove it
90-
const index = firstIndex(matchingResourceData.commentThreads, (commentThread) => commentThread.threadId === thread.threadId);
90+
const index = matchingResourceData.commentThreads.findIndex((commentThread) => commentThread.threadId === thread.threadId);
9191
matchingResourceData.commentThreads.splice(index, 1);
9292

9393
// If the comment thread was the last thread for a resource, remove that resource from the list
@@ -98,11 +98,11 @@ export class CommentsModel {
9898

9999
changed.forEach(thread => {
100100
// Find resource that has the comment thread
101-
const matchingResourceIndex = firstIndex(threadsForOwner, (resourceData) => resourceData.id === thread.resource);
101+
const matchingResourceIndex = threadsForOwner.findIndex((resourceData) => resourceData.id === thread.resource);
102102
const matchingResourceData = threadsForOwner[matchingResourceIndex];
103103

104104
// Find comment node on resource that is that thread and replace it
105-
const index = firstIndex(matchingResourceData.commentThreads, (commentThread) => commentThread.threadId === thread.threadId);
105+
const index = matchingResourceData.commentThreads.findIndex((commentThread) => commentThread.threadId === thread.threadId);
106106
if (index >= 0) {
107107
matchingResourceData.commentThreads[index] = ResourceWithCommentThreads.createCommentNode(owner, URI.parse(matchingResourceData.id), thread);
108108
} else if (thread.comments && thread.comments.length) {

src/vs/workbench/contrib/preferences/browser/settingsEditor2.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { ActionBar } from 'vs/base/browser/ui/actionbar/actionbar';
99
import { Button } from 'vs/base/browser/ui/button/button';
1010
import { ITreeElement } from 'vs/base/browser/ui/tree/tree';
1111
import { Action } from 'vs/base/common/actions';
12-
import * as arrays from 'vs/base/common/arrays';
1312
import { Delayer, IntervalTimer, ThrottledDelayer, timeout } from 'vs/base/common/async';
1413
import { CancellationToken, CancellationTokenSource } from 'vs/base/common/cancellation';
1514
import * as collections from 'vs/base/common/collections';
@@ -868,19 +867,19 @@ export class SettingsEditor2 extends EditorPane {
868867
const remoteResult = props.searchResults[SearchResultIdx.Remote];
869868
const localResult = props.searchResults[SearchResultIdx.Local];
870869

871-
const localIndex = arrays.firstIndex(localResult!.filterMatches, m => m.setting.key === props.key);
870+
const localIndex = localResult!.filterMatches.findIndex(m => m.setting.key === props.key);
872871
groupId = localIndex >= 0 ?
873872
'local' :
874873
'remote';
875874

876875
displayIndex = localIndex >= 0 ?
877876
localIndex :
878-
remoteResult && (arrays.firstIndex(remoteResult.filterMatches, m => m.setting.key === props.key) + localResult.filterMatches.length);
877+
remoteResult && (remoteResult.filterMatches.findIndex(m => m.setting.key === props.key) + localResult.filterMatches.length);
879878

880879
if (this.searchResultModel) {
881880
const rawResults = this.searchResultModel.getRawResults();
882881
if (rawResults[SearchResultIdx.Remote]) {
883-
const _nlpIndex = arrays.firstIndex(rawResults[SearchResultIdx.Remote].filterMatches, m => m.setting.key === props.key);
882+
const _nlpIndex = rawResults[SearchResultIdx.Remote].filterMatches.findIndex(m => m.setting.key === props.key);
884883
nlpIndex = _nlpIndex >= 0 ? _nlpIndex : undefined;
885884
}
886885
}

src/vs/workbench/contrib/scm/browser/dirtydiffDecorator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import { MenuId, IMenuService, IMenu, MenuItemAction, MenuRegistry } from 'vs/pl
4141
import { createAndFillInActionBarActions } from 'vs/platform/actions/browser/menuEntryActionViewItem';
4242
import { IChange, IEditorModel, ScrollType, IEditorContribution, IDiffEditorModel } from 'vs/editor/common/editorCommon';
4343
import { OverviewRulerLane, ITextModel, IModelDecorationOptions, MinimapPosition } from 'vs/editor/common/model';
44-
import { sortedDiff, firstIndex } from 'vs/base/common/arrays';
44+
import { sortedDiff } from 'vs/base/common/arrays';
4545
import { IMarginData } from 'vs/editor/browser/controller/mouseTarget';
4646
import { ICodeEditorService } from 'vs/editor/browser/services/codeEditorService';
4747
import { ISplice } from 'vs/base/common/sequence';
@@ -769,7 +769,7 @@ export class DirtyDiffController extends Disposable implements IEditorContributi
769769
return;
770770
}
771771

772-
const index = firstIndex(model.changes, change => lineIntersectsChange(lineNumber, change));
772+
const index = model.changes.findIndex(change => lineIntersectsChange(lineNumber, change));
773773

774774
if (index < 0) {
775775
return;

src/vs/workbench/contrib/themes/browser/themes.contribution.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import { localize } from 'vs/nls';
77
import { Action } from 'vs/base/common/actions';
8-
import { firstIndex } from 'vs/base/common/arrays';
98
import { KeyMod, KeyChord, KeyCode } from 'vs/base/common/keyCodes';
109
import { SyncActionDescriptor, MenuRegistry, MenuId } from 'vs/platform/actions/common/actions';
1110
import { Registry } from 'vs/platform/registry/common/platform';
@@ -73,7 +72,7 @@ export class SelectColorThemeAction extends Action {
7372
return new Promise((s, _) => {
7473
let isCompleted = false;
7574

76-
const autoFocusIndex = firstIndex(picks, p => isItem(p) && p.id === currentTheme.id);
75+
const autoFocusIndex = picks.findIndex(p => isItem(p) && p.id === currentTheme.id);
7776
const quickpick = this.quickInputService.createQuickPick<ThemeItem>();
7877
quickpick.items = picks;
7978
quickpick.placeholder = localize('themes.selectTheme', "Select Color Theme (Up/Down Keys to Preview)");
@@ -150,7 +149,7 @@ abstract class AbstractIconThemeAction extends Action {
150149
return new Promise<void>((s, _) => {
151150
let isCompleted = false;
152151

153-
const autoFocusIndex = firstIndex(picks, p => isItem(p) && p.id === currentTheme.id);
152+
const autoFocusIndex = picks.findIndex(p => isItem(p) && p.id === currentTheme.id);
154153
const quickpick = this.quickInputService.createQuickPick<ThemeItem>();
155154
quickpick.items = picks;
156155
quickpick.placeholder = this.placeholderMessage;

0 commit comments

Comments
 (0)