Skip to content

Commit 5a3f0b8

Browse files
smart-boRomanNikitenko
authored andcommitted
use ReadonlyArray for DocumentSelector (#10070)
fixes #10025
1 parent d57e287 commit 5a3f0b8

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

packages/plugin-ext/src/common/arrays.ts

+8
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,11 @@ export interface Splice<T> {
4444
readonly deleteCount: number;
4545
readonly toInsert: T[];
4646
}
47+
48+
/**
49+
* @returns 'true' if the 'arg' is a 'ReadonlyArray'.
50+
*/
51+
export function isReadonlyArray(arg: unknown): arg is readonly unknown[] {
52+
// Since Typescript does not properly narrow down typings for 'ReadonlyArray' we need to help it.
53+
return Array.isArray(arg);
54+
}

packages/plugin-ext/src/plugin/languages.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ import { DeclarationAdapter } from './languages/declaration';
9090
import { CallHierarchyAdapter } from './languages/call-hierarchy';
9191
import { BinaryBuffer } from '@theia/core/lib/common/buffer';
9292
import { DocumentSemanticTokensAdapter, DocumentRangeSemanticTokensAdapter } from './languages/semantic-highlighting';
93+
import { isReadonlyArray } from '../common/arrays';
9394

9495
type Adapter = CompletionAdapter |
9596
SignatureHelpAdapter |
@@ -204,7 +205,7 @@ export class LanguagesExtImpl implements LanguagesExt {
204205
}
205206

206207
private transformDocumentSelector(selector: theia.DocumentSelector): SerializedDocumentFilter[] {
207-
if (Array.isArray(selector)) {
208+
if (isReadonlyArray(selector)) {
208209
return selector.map(sel => this.doTransformDocumentSelector(sel)!);
209210
}
210211

packages/plugin-ext/src/plugin/type-converters.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { isMarkdownString, MarkdownString } from './markdown-string';
2727
import * as types from './types-impl';
2828
import { UriComponents } from '../common/uri-components';
2929
import { TaskGroup } from './types-impl';
30+
import { isReadonlyArray } from '../common/arrays';
3031

3132
const SIDE_GROUP = -2;
3233
const ACTIVE_GROUP = -1;
@@ -169,9 +170,9 @@ export function fromRangeOrRangeWithMessage(ranges: theia.Range[] | theia.Decora
169170
});
170171
} else {
171172
return ranges.map((r): DecorationOptions =>
172-
({
173-
range: fromRange(r)!
174-
}));
173+
({
174+
range: fromRange(r)!
175+
}));
175176
}
176177
}
177178

@@ -213,7 +214,7 @@ export function toMarkdown(value: model.MarkdownString): MarkdownString {
213214
export function fromDocumentSelector(selector: theia.DocumentSelector | undefined): LanguageSelector | undefined {
214215
if (!selector) {
215216
return undefined;
216-
} else if (Array.isArray(selector)) {
217+
} else if (isReadonlyArray(selector)) {
217218
return <LanguageSelector>selector.map(fromDocumentSelector);
218219
} else if (typeof selector === 'string') {
219220
return selector;

packages/plugin/src/theia.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6154,7 +6154,7 @@ declare module '@theia/plugin' {
61546154
*
61556155
* @sample `let sel:DocumentSelector = { scheme: 'file', language: 'typescript' }`;
61566156
*/
6157-
export type DocumentSelector = DocumentFilter | string | Array<DocumentFilter | string>;
6157+
export type DocumentSelector = DocumentFilter | string | ReadonlyArray<DocumentFilter | string>;
61586158

61596159
/**
61606160
* A tuple of two characters, like a pair of

0 commit comments

Comments
 (0)