提交 eba1cc8d 编写于 作者: M Matt Bierner

Remove arrays.findIndex

For #103454

This should be a direct map to the `.findIndex` mathod
上级 a9a7cf18
......@@ -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';
......@@ -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) {
......
......@@ -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;
......
......@@ -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';
......@@ -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];
......@@ -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;
......
......@@ -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';
......@@ -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;
......@@ -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;
......@@ -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;
......@@ -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;
......
......@@ -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';
......@@ -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(
......@@ -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);
......
......@@ -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];
}
......
......@@ -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 {
......@@ -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 {
......
......@@ -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';
......@@ -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)];
......
......@@ -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;
......
......@@ -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 {
......@@ -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
......@@ -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) {
......
......@@ -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';
......@@ -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;
}
}
......
......@@ -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';
......@@ -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;
......
......@@ -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';
......@@ -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)");
......@@ -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;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册