mirror of
https://github.com/microsoft/vscode.git
synced 2025-12-25 04:36:23 +00:00
Add asArray helper
This commit is contained in:
@@ -16,6 +16,7 @@ import { StandardKeyboardEvent } from 'vs/base/browser/keyboardEvent';
|
||||
import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
|
||||
import { IContextViewProvider } from 'vs/base/browser/ui/contextview/contextview';
|
||||
import { Event, Emitter } from 'vs/base/common/event';
|
||||
import { asArray } from 'vs/base/common/arrays';
|
||||
|
||||
export interface IActionItem {
|
||||
actionRunner: IActionRunner;
|
||||
@@ -593,7 +594,7 @@ export class ActionBar extends Disposable implements IActionRunner {
|
||||
}
|
||||
|
||||
push(arg: IAction | IAction[], options: IActionOptions = {}): void {
|
||||
const actions: IAction[] = !Array.isArray(arg) ? [arg] : arg;
|
||||
const actions: IAction[] = asArray(arg);
|
||||
|
||||
let index = types.isNumber(options.index) ? options.index : null;
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ import { Event, Emitter } from 'vs/base/common/event';
|
||||
import { KeyCode, ResolvedKeybinding } from 'vs/base/common/keyCodes';
|
||||
import { Disposable, dispose, IDisposable } from 'vs/base/common/lifecycle';
|
||||
import { withNullAsUndefined } from 'vs/base/common/types';
|
||||
import { asArray } from 'vs/base/common/arrays';
|
||||
|
||||
const $ = DOM.$;
|
||||
|
||||
@@ -184,7 +185,7 @@ export class MenuBar extends Disposable {
|
||||
}
|
||||
|
||||
push(arg: MenuBarMenu | MenuBarMenu[]): void {
|
||||
const menus: MenuBarMenu[] = !Array.isArray(arg) ? [arg] : arg;
|
||||
const menus: MenuBarMenu[] = asArray(arg);
|
||||
|
||||
menus.forEach((menuBarMenu) => {
|
||||
const menuIndex = this.menuCache.length;
|
||||
|
||||
@@ -549,3 +549,7 @@ export function mapArrayOrNot<T, U>(items: T | T[], fn: (_: T) => U): U | U[] {
|
||||
items.map(fn) :
|
||||
fn(items);
|
||||
}
|
||||
|
||||
export function asArray<T>(x: T | T[]): T[] {
|
||||
return Array.isArray(x) ? x : [x];
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ import { HoverOperation, HoverStartMode, IHoverComputer } from 'vs/editor/contri
|
||||
import { ContentHoverWidget } from 'vs/editor/contrib/hover/hoverWidgets';
|
||||
import { MarkdownRenderer } from 'vs/editor/contrib/markdown/markdownRenderer';
|
||||
import { IThemeService } from 'vs/platform/theme/common/themeService';
|
||||
import { coalesce, isNonEmptyArray } from 'vs/base/common/arrays';
|
||||
import { coalesce, isNonEmptyArray, asArray } from 'vs/base/common/arrays';
|
||||
import { IMarker, IMarkerData, MarkerSeverity } from 'vs/platform/markers/common/markers';
|
||||
import { basename } from 'vs/base/common/resources';
|
||||
import { IMarkerDecorationsService } from 'vs/editor/common/services/markersDecorationService';
|
||||
@@ -148,16 +148,7 @@ class ModesContentComputer implements IHoverComputer<HoverPart[]> {
|
||||
return null;
|
||||
}
|
||||
|
||||
let contents: IMarkdownString[] = [];
|
||||
|
||||
if (d.options.hoverMessage) {
|
||||
if (Array.isArray(d.options.hoverMessage)) {
|
||||
contents = [...d.options.hoverMessage];
|
||||
} else {
|
||||
contents = [d.options.hoverMessage];
|
||||
}
|
||||
}
|
||||
|
||||
const contents: IMarkdownString[] = d.options.hoverMessage ? asArray(d.options.hoverMessage) : [];
|
||||
return { contents, range };
|
||||
}
|
||||
});
|
||||
|
||||
@@ -12,6 +12,7 @@ import { GlyphHoverWidget } from 'vs/editor/contrib/hover/hoverWidgets';
|
||||
import { MarkdownRenderer } from 'vs/editor/contrib/markdown/markdownRenderer';
|
||||
import { IModeService } from 'vs/editor/common/services/modeService';
|
||||
import { IOpenerService, NullOpenerService } from 'vs/platform/opener/common/opener';
|
||||
import { asArray } from 'vs/base/common/arrays';
|
||||
|
||||
export interface IHoverMessage {
|
||||
value: IMarkdownString;
|
||||
@@ -62,11 +63,7 @@ class MarginComputer implements IHoverComputer<IHoverMessage[]> {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (Array.isArray(hoverMessage)) {
|
||||
result.push(...hoverMessage.map(toHoverMessage));
|
||||
} else {
|
||||
result.push(toHoverMessage(hoverMessage));
|
||||
}
|
||||
result.push(...asArray(hoverMessage).map(toHoverMessage));
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
@@ -9,6 +9,7 @@ import { MainContext, IMainContext, ExtHostDecorationsShape, MainThreadDecoratio
|
||||
import { Disposable } from 'vs/workbench/api/node/extHostTypes';
|
||||
import { CancellationToken } from 'vs/base/common/cancellation';
|
||||
import { ExtensionIdentifier } from 'vs/platform/extensions/common/extensions';
|
||||
import { asArray } from 'vs/base/common/arrays';
|
||||
|
||||
interface ProviderData {
|
||||
provider: vscode.DecorationProvider;
|
||||
@@ -32,7 +33,7 @@ export class ExtHostDecorations implements ExtHostDecorationsShape {
|
||||
this._proxy.$registerDecorationProvider(handle, extensionId.value);
|
||||
|
||||
const listener = provider.onDidChangeDecorations(e => {
|
||||
this._proxy.$onDidChange(handle, !e ? null : Array.isArray(e) ? e : [e]);
|
||||
this._proxy.$onDidChange(handle, !e ? null : asArray(e));
|
||||
});
|
||||
|
||||
return new Disposable(() => {
|
||||
|
||||
@@ -19,7 +19,7 @@ import { MainContext, MainThreadLanguageFeaturesShape, ExtHostLanguageFeaturesSh
|
||||
import { regExpLeadsToEndlessLoop, regExpFlags } from 'vs/base/common/strings';
|
||||
import { IPosition } from 'vs/editor/common/core/position';
|
||||
import { IRange, Range as EditorRange } from 'vs/editor/common/core/range';
|
||||
import { isFalsyOrEmpty, isNonEmptyArray, coalesce } from 'vs/base/common/arrays';
|
||||
import { isFalsyOrEmpty, isNonEmptyArray, coalesce, asArray } from 'vs/base/common/arrays';
|
||||
import { isObject } from 'vs/base/common/types';
|
||||
import { ISelection, Selection } from 'vs/editor/common/core/selection';
|
||||
import { ILogService } from 'vs/platform/log/common/log';
|
||||
@@ -193,12 +193,7 @@ class CodeInsetAdapter {
|
||||
}
|
||||
|
||||
function convertToLocationLinks(value: vscode.Definition): modes.LocationLink[] {
|
||||
if (Array.isArray(value)) {
|
||||
return (value as (vscode.DefinitionLink | vscode.Location)[]).map(typeConvert.DefinitionLink.from);
|
||||
} else if (value) {
|
||||
return [typeConvert.DefinitionLink.from(value)];
|
||||
}
|
||||
return [];
|
||||
return value ? asArray(value).map(typeConvert.DefinitionLink.from) : [];
|
||||
}
|
||||
|
||||
class DefinitionAdapter {
|
||||
@@ -1071,11 +1066,7 @@ export class ExtHostLanguageFeatures implements ExtHostLanguageFeaturesShape {
|
||||
}
|
||||
|
||||
private _transformDocumentSelector(selector: vscode.DocumentSelector): Array<ISerializedDocumentFilter> {
|
||||
if (Array.isArray(selector)) {
|
||||
return coalesce(selector.map(sel => this._doTransformDocumentSelector(sel)));
|
||||
}
|
||||
|
||||
return coalesce([this._doTransformDocumentSelector(selector)]);
|
||||
return coalesce(asArray(selector).map(sel => this._doTransformDocumentSelector(sel)));
|
||||
}
|
||||
|
||||
private _doTransformDocumentSelector(selector: string | vscode.DocumentFilter): ISerializedDocumentFilter | undefined {
|
||||
|
||||
Reference in New Issue
Block a user