Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use ReadonlyArray for DocumentSelector #10070

Merged
merged 8 commits into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)!
}));
({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not the formatting the typescript formatter creates. Let's stick to that unless there is a compelling reason.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have directions on how to set this up? VS Code formats like this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@paul-marechal it doesn't for me 🤷

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