Skip to content

Commit

Permalink
use ReadonlyArray for DocumentSelector (#10070)
Browse files Browse the repository at this point in the history
fixes #10025
  • Loading branch information
smart-bo authored and RomanNikitenko committed Sep 16, 2021
1 parent d57e287 commit 5a3f0b8
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
8 changes: 8 additions & 0 deletions packages/plugin-ext/src/common/arrays.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,11 @@ export interface Splice<T> {
readonly deleteCount: number;
readonly toInsert: T[];
}

/**
* @returns 'true' if the 'arg' is a 'ReadonlyArray'.
*/
export function isReadonlyArray(arg: unknown): arg is readonly unknown[] {
// Since Typescript does not properly narrow down typings for 'ReadonlyArray' we need to help it.
return Array.isArray(arg);
}
3 changes: 2 additions & 1 deletion packages/plugin-ext/src/plugin/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ import { DeclarationAdapter } from './languages/declaration';
import { CallHierarchyAdapter } from './languages/call-hierarchy';
import { BinaryBuffer } from '@theia/core/lib/common/buffer';
import { DocumentSemanticTokensAdapter, DocumentRangeSemanticTokensAdapter } from './languages/semantic-highlighting';
import { isReadonlyArray } from '../common/arrays';

type Adapter = CompletionAdapter |
SignatureHelpAdapter |
Expand Down Expand Up @@ -204,7 +205,7 @@ export class LanguagesExtImpl implements LanguagesExt {
}

private transformDocumentSelector(selector: theia.DocumentSelector): SerializedDocumentFilter[] {
if (Array.isArray(selector)) {
if (isReadonlyArray(selector)) {
return selector.map(sel => this.doTransformDocumentSelector(sel)!);
}

Expand Down
9 changes: 5 additions & 4 deletions packages/plugin-ext/src/plugin/type-converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { isMarkdownString, MarkdownString } from './markdown-string';
import * as types from './types-impl';
import { UriComponents } from '../common/uri-components';
import { TaskGroup } from './types-impl';
import { isReadonlyArray } from '../common/arrays';

const SIDE_GROUP = -2;
const ACTIVE_GROUP = -1;
Expand Down Expand Up @@ -169,9 +170,9 @@ export function fromRangeOrRangeWithMessage(ranges: theia.Range[] | theia.Decora
});
} else {
return ranges.map((r): DecorationOptions =>
({
range: fromRange(r)!
}));
({
range: fromRange(r)!
}));
}
}

Expand Down Expand Up @@ -213,7 +214,7 @@ export function toMarkdown(value: model.MarkdownString): MarkdownString {
export function fromDocumentSelector(selector: theia.DocumentSelector | undefined): LanguageSelector | undefined {
if (!selector) {
return undefined;
} else if (Array.isArray(selector)) {
} else if (isReadonlyArray(selector)) {
return <LanguageSelector>selector.map(fromDocumentSelector);
} else if (typeof selector === 'string') {
return selector;
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin/src/theia.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6154,7 +6154,7 @@ declare module '@theia/plugin' {
*
* @sample `let sel:DocumentSelector = { scheme: 'file', language: 'typescript' }`;
*/
export type DocumentSelector = DocumentFilter | string | Array<DocumentFilter | string>;
export type DocumentSelector = DocumentFilter | string | ReadonlyArray<DocumentFilter | string>;

/**
* A tuple of two characters, like a pair of
Expand Down

0 comments on commit 5a3f0b8

Please sign in to comment.