Skip to content

Commit

Permalink
Remove arrays.findIndex
Browse files Browse the repository at this point in the history
For #103454

This should be a direct map to the `.findIndex` mathod
  • Loading branch information
mjbvz committed Sep 10, 2020
1 parent a9a7cf1 commit eba1cc8
Show file tree
Hide file tree
Showing 13 changed files with 31 additions and 61 deletions.
4 changes: 2 additions & 2 deletions extensions/git/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { workspace, WorkspaceFoldersChangeEvent, Uri, window, Event, EventEmitter, QuickPickItem, Disposable, SourceControl, SourceControlResourceGroup, TextEditor, Memento, OutputChannel, commands } from 'vscode';
import { Repository, RepositoryState } from './repository';
import { memoize, sequentialize, debounce } from './decorators';
import { dispose, anyEvent, filterEvent, isDescendant, firstIndex, pathEquals, toDisposable, eventToPromise } from './util';
import { dispose, anyEvent, filterEvent, isDescendant, pathEquals, toDisposable, eventToPromise } from './util';
import { Git } from './git';
import * as path from 'path';
import * as fs from 'fs';
Expand Down Expand Up @@ -372,7 +372,7 @@ export class Model implements IRemoteSourceProviderRegistry, IPushErrorHandlerRe
const picks = this.openRepositories.map((e, index) => new RepositoryPick(e.repository, index));
const active = window.activeTextEditor;
const repository = active && this.getRepository(active.document.fileName);
const index = firstIndex(picks, pick => pick.repository === repository);
const index = picks.findIndex(pick => pick.repository === repository);

// Move repository pick containing the active text editor to appear first
if (index > -1) {
Expand Down
10 changes: 0 additions & 10 deletions extensions/git/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,16 +182,6 @@ export function uniqueFilter<T>(keyFn: (t: T) => string): (t: T) => boolean {
};
}

export function firstIndex<T>(array: T[], fn: (t: T) => boolean): number {
for (let i = 0; i < array.length; i++) {
if (fn(array[i])) {
return i;
}
}

return -1;
}

export function find<T>(array: T[], fn: (t: T) => boolean): T | undefined {
let result: T | undefined = undefined;

Expand Down
6 changes: 3 additions & 3 deletions src/vs/base/browser/ui/list/listWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import 'vs/css!./list';
import { IDisposable, dispose, DisposableStore } from 'vs/base/common/lifecycle';
import { isNumber } from 'vs/base/common/types';
import { range, firstIndex, binarySearch } from 'vs/base/common/arrays';
import { range, binarySearch } from 'vs/base/common/arrays';
import { memoize } from 'vs/base/common/decorators';
import * as DOM from 'vs/base/browser/dom';
import * as platform from 'vs/base/common/platform';
Expand Down Expand Up @@ -55,7 +55,7 @@ class TraitRenderer<T> implements IListRenderer<T, ITraitTemplateData>
}

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

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

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

if (index < 0) {
return;
Expand Down
11 changes: 5 additions & 6 deletions src/vs/base/browser/ui/splitview/paneview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { domEvent } from 'vs/base/browser/event';
import { StandardKeyboardEvent } from 'vs/base/browser/keyboardEvent';
import { KeyCode } from 'vs/base/common/keyCodes';
import { $, append, addClass, removeClass, toggleClass, trackFocus, EventHelper, clearNode } from 'vs/base/browser/dom';
import { firstIndex } from 'vs/base/common/arrays';
import { Color, RGBA } from 'vs/base/common/color';
import { SplitView, IView } from './splitview';
import { isFirefox } from 'vs/base/browser/browser';
Expand Down Expand Up @@ -474,7 +473,7 @@ export class PaneView extends Disposable {
}

removePane(pane: Pane): void {
const index = firstIndex(this.paneItems, item => item.pane === pane);
const index = this.paneItems.findIndex(item => item.pane === pane);

if (index === -1) {
return;
Expand All @@ -486,8 +485,8 @@ export class PaneView extends Disposable {
}

movePane(from: Pane, to: Pane): void {
const fromIndex = firstIndex(this.paneItems, item => item.pane === from);
const toIndex = firstIndex(this.paneItems, item => item.pane === to);
const fromIndex = this.paneItems.findIndex(item => item.pane === from);
const toIndex = this.paneItems.findIndex(item => item.pane === to);

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

resizePane(pane: Pane, size: number): void {
const index = firstIndex(this.paneItems, item => item.pane === pane);
const index = this.paneItems.findIndex(item => item.pane === pane);

if (index === -1) {
return;
Expand All @@ -510,7 +509,7 @@ export class PaneView extends Disposable {
}

getPaneSize(pane: Pane): number {
const index = firstIndex(this.paneItems, item => item.pane === pane);
const index = this.paneItems.findIndex(item => item.pane === pane);

if (index === -1) {
return -1;
Expand Down
8 changes: 4 additions & 4 deletions src/vs/base/browser/ui/splitview/splitview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Event, Emitter } from 'vs/base/common/event';
import * as types from 'vs/base/common/types';
import * as dom from 'vs/base/browser/dom';
import { clamp } from 'vs/base/common/numbers';
import { range, firstIndex, pushToStart, pushToEnd } from 'vs/base/common/arrays';
import { range, pushToStart, pushToEnd } from 'vs/base/common/arrays';
import { Sash, Orientation, ISashEvent as IBaseSashEvent, SashState } from 'vs/base/browser/ui/sash/sash';
import { Color } from 'vs/base/common/color';
import { domEvent } from 'vs/base/browser/event';
Expand Down Expand Up @@ -460,7 +460,7 @@ export class SplitView<TLayoutContext = undefined> extends Disposable {
item.enabled = false;
}

const index = firstIndex(this.sashItems, item => item.sash === sash);
const index = this.sashItems.findIndex(item => item.sash === sash);

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

const onDidResetDisposable = sash.onDidReset(() => {
const index = firstIndex(this.sashItems, item => item.sash === sash);
const index = this.sashItems.findIndex(item => item.sash === sash);
const upIndexes = range(index, -1);
const downIndexes = range(index + 1, this.viewItems.length);
const snapBeforeIndex = this.findFirstSnapIndex(upIndexes);
Expand Down
18 changes: 1 addition & 17 deletions src/vs/base/common/arrays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,29 +399,13 @@ export function lastIndex<T>(array: ReadonlyArray<T>, fn: (item: T) => boolean):
return -1;
}

/**
* @deprecated ES6: use `Array.findIndex`
*/
export function firstIndex<T>(array: ReadonlyArray<T>, fn: (item: T) => boolean): number {
for (let i = 0; i < array.length; i++) {
const element = array[i];

if (fn(element)) {
return i;
}
}

return -1;
}


/**
* @deprecated ES6: use `Array.find`
*/
export function first<T>(array: ReadonlyArray<T>, fn: (item: T) => boolean, notFoundValue: T): T;
export function first<T>(array: ReadonlyArray<T>, fn: (item: T) => boolean): T | undefined;
export function first<T>(array: ReadonlyArray<T>, fn: (item: T) => boolean, notFoundValue: T | undefined = undefined): T | undefined {
const index = firstIndex(array, fn);
const index = array.findIndex(fn);
return index < 0 ? notFoundValue : array[index];
}

Expand Down
4 changes: 2 additions & 2 deletions src/vs/editor/contrib/wordHighlighter/wordHighlighter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ class WordHighlighter {

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

public moveBack() {
let highlights = this._getSortedHighlights();
let index = arrays.firstIndex(highlights, (range) => range.containsPosition(this.editor.getPosition()));
let index = highlights.findIndex((range) => range.containsPosition(this.editor.getPosition()));
let newIndex = ((index - 1 + highlights.length) % highlights.length);
let dest = highlights[newIndex];
try {
Expand Down
3 changes: 1 addition & 2 deletions src/vs/platform/environment/node/argvHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*--------------------------------------------------------------------------------------------*/

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

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

if (index > -1) {
return [...argv.slice(0, index), ...argv.slice(index + 1)];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ export class ReviewZoneWidget extends ZoneWidget implements ICommentThreadWidget

this._disposables.add(newCommentNode);
this._disposables.add(newCommentNode.onDidClick(clickedNode =>
this.setFocusedComment(arrays.firstIndex(this._commentElements, commentNode => commentNode.comment.uniqueIdInThread === clickedNode.comment.uniqueIdInThread))
this.setFocusedComment(this._commentElements.findIndex(commentNode => commentNode.comment.uniqueIdInThread === clickedNode.comment.uniqueIdInThread))
));

return newCommentNode;
Expand Down
10 changes: 5 additions & 5 deletions src/vs/workbench/contrib/comments/common/commentModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { URI } from 'vs/base/common/uri';
import { IRange } from 'vs/editor/common/core/range';
import { Comment, CommentThread, CommentThreadChangedEvent } from 'vs/editor/common/modes';
import { groupBy, firstIndex, flatten } from 'vs/base/common/arrays';
import { groupBy, flatten } from 'vs/base/common/arrays';
import { localize } from 'vs/nls';

export interface ICommentThreadChangedEvent extends CommentThreadChangedEvent {
Expand Down Expand Up @@ -83,11 +83,11 @@ export class CommentsModel {

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

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

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

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

// Find comment node on resource that is that thread and replace it
const index = firstIndex(matchingResourceData.commentThreads, (commentThread) => commentThread.threadId === thread.threadId);
const index = matchingResourceData.commentThreads.findIndex((commentThread) => commentThread.threadId === thread.threadId);
if (index >= 0) {
matchingResourceData.commentThreads[index] = ResourceWithCommentThreads.createCommentNode(owner, URI.parse(matchingResourceData.id), thread);
} else if (thread.comments && thread.comments.length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { ActionBar } from 'vs/base/browser/ui/actionbar/actionbar';
import { Button } from 'vs/base/browser/ui/button/button';
import { ITreeElement } from 'vs/base/browser/ui/tree/tree';
import { Action } from 'vs/base/common/actions';
import * as arrays from 'vs/base/common/arrays';
import { Delayer, IntervalTimer, ThrottledDelayer, timeout } from 'vs/base/common/async';
import { CancellationToken, CancellationTokenSource } from 'vs/base/common/cancellation';
import * as collections from 'vs/base/common/collections';
Expand Down Expand Up @@ -868,19 +867,19 @@ export class SettingsEditor2 extends EditorPane {
const remoteResult = props.searchResults[SearchResultIdx.Remote];
const localResult = props.searchResults[SearchResultIdx.Local];

const localIndex = arrays.firstIndex(localResult!.filterMatches, m => m.setting.key === props.key);
const localIndex = localResult!.filterMatches.findIndex(m => m.setting.key === props.key);
groupId = localIndex >= 0 ?
'local' :
'remote';

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

if (this.searchResultModel) {
const rawResults = this.searchResultModel.getRawResults();
if (rawResults[SearchResultIdx.Remote]) {
const _nlpIndex = arrays.firstIndex(rawResults[SearchResultIdx.Remote].filterMatches, m => m.setting.key === props.key);
const _nlpIndex = rawResults[SearchResultIdx.Remote].filterMatches.findIndex(m => m.setting.key === props.key);
nlpIndex = _nlpIndex >= 0 ? _nlpIndex : undefined;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/vs/workbench/contrib/scm/browser/dirtydiffDecorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import { MenuId, IMenuService, IMenu, MenuItemAction, MenuRegistry } from 'vs/pl
import { createAndFillInActionBarActions } from 'vs/platform/actions/browser/menuEntryActionViewItem';
import { IChange, IEditorModel, ScrollType, IEditorContribution, IDiffEditorModel } from 'vs/editor/common/editorCommon';
import { OverviewRulerLane, ITextModel, IModelDecorationOptions, MinimapPosition } from 'vs/editor/common/model';
import { sortedDiff, firstIndex } from 'vs/base/common/arrays';
import { sortedDiff } from 'vs/base/common/arrays';
import { IMarginData } from 'vs/editor/browser/controller/mouseTarget';
import { ICodeEditorService } from 'vs/editor/browser/services/codeEditorService';
import { ISplice } from 'vs/base/common/sequence';
Expand Down Expand Up @@ -769,7 +769,7 @@ export class DirtyDiffController extends Disposable implements IEditorContributi
return;
}

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

if (index < 0) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

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

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

const autoFocusIndex = firstIndex(picks, p => isItem(p) && p.id === currentTheme.id);
const autoFocusIndex = picks.findIndex(p => isItem(p) && p.id === currentTheme.id);
const quickpick = this.quickInputService.createQuickPick<ThemeItem>();
quickpick.items = picks;
quickpick.placeholder = this.placeholderMessage;
Expand Down

0 comments on commit eba1cc8

Please sign in to comment.