Skip to content

Commit

Permalink
Merge pull request #239 from remcohaszing/deprecate-thenable
Browse files Browse the repository at this point in the history
Deprecate Thenable and alias to PromiseLike
  • Loading branch information
aeschli authored Aug 26, 2024
2 parents c27b284 + 356d5dd commit 24678bd
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 73 deletions.
3 changes: 1 addition & 2 deletions src/example/sample.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@


import { getLanguageService, JSONSchema, SchemaRequestService, TextDocument, MatchingSchema } from '../jsonLanguageService';
import { getLanguageService, TextDocument } from '../jsonLanguageService';

async function main() {
const jsonContentUri = 'foo://server/example.data.json';
Expand Down Expand Up @@ -58,4 +58,3 @@ async function main() {

}
main();

12 changes: 6 additions & 6 deletions src/jsonContributions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Thenable, MarkedString, CompletionItem } from './jsonLanguageService';
import { MarkedString, CompletionItem } from './jsonLanguageService';

export interface JSONWorkerContribution {
getInfoContribution(uri: string, location: JSONPath): Thenable<MarkedString[]>;
collectPropertyCompletions(uri: string, location: JSONPath, currentWord: string, addValue: boolean, isLast: boolean, result: CompletionsCollector): Thenable<any>;
collectValueCompletions(uri: string, location: JSONPath, propertyKey: string, result: CompletionsCollector): Thenable<any>;
collectDefaultCompletions(uri: string, result: CompletionsCollector): Thenable<any>;
resolveCompletion?(item: CompletionItem): Thenable<CompletionItem>;
getInfoContribution(uri: string, location: JSONPath): PromiseLike<MarkedString[]>;
collectPropertyCompletions(uri: string, location: JSONPath, currentWord: string, addValue: boolean, isLast: boolean, result: CompletionsCollector): PromiseLike<any>;
collectValueCompletions(uri: string, location: JSONPath, propertyKey: string, result: CompletionsCollector): PromiseLike<any>;
collectDefaultCompletions(uri: string, result: CompletionsCollector): PromiseLike<any>;
resolveCompletion?(item: CompletionItem): PromiseLike<CompletionItem>;
}
export type Segment = string | number;
export type JSONPath = Segment[];
Expand Down
17 changes: 8 additions & 9 deletions src/jsonLanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import { sort } from './utils/sort';
import { format } from './utils/format';

import {
Thenable,
ASTNode,
Color, ColorInformation, ColorPresentation,
LanguageServiceParams, LanguageSettings, DocumentLanguageSettings,
Expand All @@ -37,23 +36,23 @@ export * from './jsonLanguageTypes';

export interface LanguageService {
configure(settings: LanguageSettings): void;
doValidation(document: TextDocument, jsonDocument: JSONDocument, documentSettings?: DocumentLanguageSettings, schema?: JSONSchema): Thenable<Diagnostic[]>;
doValidation(document: TextDocument, jsonDocument: JSONDocument, documentSettings?: DocumentLanguageSettings, schema?: JSONSchema): PromiseLike<Diagnostic[]>;
parseJSONDocument(document: TextDocument): JSONDocument;
newJSONDocument(rootNode: ASTNode, syntaxDiagnostics?: Diagnostic[]): JSONDocument;
resetSchema(uri: string): boolean;
getMatchingSchemas(document: TextDocument, jsonDocument: JSONDocument, schema?: JSONSchema): Thenable<MatchingSchema[]>;
getMatchingSchemas(document: TextDocument, jsonDocument: JSONDocument, schema?: JSONSchema): PromiseLike<MatchingSchema[]>;
getLanguageStatus(document: TextDocument, jsonDocument: JSONDocument): JSONLanguageStatus;
doResolve(item: CompletionItem): Thenable<CompletionItem>;
doComplete(document: TextDocument, position: Position, doc: JSONDocument): Thenable<CompletionList | null>;
doResolve(item: CompletionItem): PromiseLike<CompletionItem>;
doComplete(document: TextDocument, position: Position, doc: JSONDocument): PromiseLike<CompletionList | null>;
findDocumentSymbols(document: TextDocument, doc: JSONDocument, context?: DocumentSymbolsContext): SymbolInformation[];
findDocumentSymbols2(document: TextDocument, doc: JSONDocument, context?: DocumentSymbolsContext): DocumentSymbol[];
findDocumentColors(document: TextDocument, doc: JSONDocument, context?: DocumentColorsContext): Thenable<ColorInformation[]>;
findDocumentColors(document: TextDocument, doc: JSONDocument, context?: DocumentColorsContext): PromiseLike<ColorInformation[]>;
getColorPresentations(document: TextDocument, doc: JSONDocument, color: Color, range: Range): ColorPresentation[];
doHover(document: TextDocument, position: Position, doc: JSONDocument): Thenable<Hover | null>;
doHover(document: TextDocument, position: Position, doc: JSONDocument): PromiseLike<Hover | null>;
getFoldingRanges(document: TextDocument, context?: FoldingRangesContext): FoldingRange[];
getSelectionRanges(document: TextDocument, positions: Position[], doc: JSONDocument): SelectionRange[];
findDefinition(document: TextDocument, position: Position, doc: JSONDocument): Thenable<DefinitionLink[]>;
findLinks(document: TextDocument, doc: JSONDocument): Thenable<DocumentLink[]>;
findDefinition(document: TextDocument, position: Position, doc: JSONDocument): PromiseLike<DefinitionLink[]>;
findLinks(document: TextDocument, doc: JSONDocument): PromiseLike<DocumentLink[]>;
format(document: TextDocument, range: Range, options: FormattingOptions): TextEdit[];
sort(document: TextDocument, options: SortOptions): TextEdit[];
}
Expand Down
26 changes: 11 additions & 15 deletions src/jsonLanguageTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export interface WorkspaceContextService {
* In case of an error, returns a rejected promise with a displayable error string.
*/
export interface SchemaRequestService {
(uri: string): Thenable<string>;
(uri: string): PromiseLike<string>;
}

export interface PromiseConstructor {
Expand All @@ -216,41 +216,37 @@ export interface PromiseConstructor {
* a resolve callback used resolve the promise with a value or the result of another promise,
* and a reject callback used to reject the promise with a provided reason or error.
*/
new <T>(executor: (resolve: (value?: T | Thenable<T | undefined>) => void, reject: (reason?: any) => void) => void): Thenable<T | undefined>;
new <T>(executor: (resolve: (value?: T | PromiseLike<T | undefined>) => void, reject: (reason?: any) => void) => void): PromiseLike<T | undefined>;

/**
* Creates a Promise that is resolved with an array of results when all of the provided Promises
* resolve, or rejected when any Promise is rejected.
* @param values An array of Promises.
* @returns A new Promise.
*/
all<T>(values: Array<T | Thenable<T>>): Thenable<T[]>;
all<T>(values: Array<T | PromiseLike<T>>): PromiseLike<T[]>;
/**
* Creates a new rejected promise for the provided reason.
* @param reason The reason the promise was rejected.
* @returns A new rejected Promise.
*/
reject<T>(reason: any): Thenable<T>;
reject<T>(reason: any): PromiseLike<T>;

/**
* Creates a new resolved promise for the provided value.
* @param value A promise.
* @returns A promise whose internal state matches the provided promise.
*/
resolve<T>(value: T | Thenable<T>): Thenable<T>;
resolve<T>(value: T | PromiseLike<T>): PromiseLike<T>;

}

export interface Thenable<R> {
/**
* Attaches callbacks for the resolution and/or rejection of the Promise.
* @param onfulfilled The callback to execute when the Promise is resolved.
* @param onrejected The callback to execute when the Promise is rejected.
* @returns A Promise for the completion of which ever callback is executed.
*/
then<TResult>(onfulfilled?: (value: R) => TResult | Thenable<TResult>, onrejected?: (reason: any) => TResult | Thenable<TResult>): Thenable<TResult>;
then<TResult>(onfulfilled?: (value: R) => TResult | Thenable<TResult>, onrejected?: (reason: any) => void): Thenable<TResult>;
}
/**
* A deprecated alias of {@link PromiseLike}
*
* @deprecated
*/
export interface Thenable<R> extends PromiseLike<R> {}

export interface LanguageServiceParams {
/**
Expand Down
10 changes: 5 additions & 5 deletions src/services/jsonCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { stringifyObject } from '../utils/json';
import { endsWith, extendedRegExp } from '../utils/strings';
import { isDefined } from '../utils/objects';
import {
PromiseConstructor, Thenable,
PromiseConstructor,
ASTNode, ObjectASTNode, ArrayASTNode, PropertyASTNode, ClientCapabilities,
TextDocument,
CompletionItem, CompletionItemKind, CompletionList, Position, Range, TextEdit, InsertTextFormat, MarkupContent, MarkupKind
Expand All @@ -36,7 +36,7 @@ export class JSONCompletion {
private clientCapabilities: ClientCapabilities = {}) {
}

public doResolve(item: CompletionItem): Thenable<CompletionItem> {
public doResolve(item: CompletionItem): PromiseLike<CompletionItem> {
for (let i = this.contributions.length - 1; i >= 0; i--) {
const resolveCompletion = this.contributions[i].resolveCompletion;
if (resolveCompletion) {
Expand All @@ -49,7 +49,7 @@ export class JSONCompletion {
return this.promiseConstructor.resolve(item);
}

public doComplete(document: TextDocument, position: Position, doc: Parser.JSONDocument): Thenable<CompletionList> {
public doComplete(document: TextDocument, position: Position, doc: Parser.JSONDocument): PromiseLike<CompletionList> {

const result: CompletionList = {
items: [],
Expand Down Expand Up @@ -130,7 +130,7 @@ export class JSONCompletion {
};

return this.schemaService.getSchemaForResource(document.uri, doc).then((schema) => {
const collectionPromises: Thenable<any>[] = [];
const collectionPromises: PromiseLike<any>[] = [];

let addValue = true;
let currentKey = '';
Expand Down Expand Up @@ -512,7 +512,7 @@ export class JSONCompletion {

}

private getContributedValueCompletions(doc: Parser.JSONDocument, node: ASTNode | undefined, offset: number, document: TextDocument, collector: CompletionsCollector, collectionPromises: Thenable<any>[]) {
private getContributedValueCompletions(doc: Parser.JSONDocument, node: ASTNode | undefined, offset: number, document: TextDocument, collector: CompletionsCollector, collectionPromises: PromiseLike<any>[]) {
if (!node) {
this.contributions.forEach((contribution) => {
const collectPromise = contribution.collectDefaultCompletions(document.uri, collector);
Expand Down
4 changes: 2 additions & 2 deletions src/services/jsonDocumentSymbols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { colorFromHex } from '../utils/colors';
import * as l10n from '@vscode/l10n';

import {
TextDocument, Thenable, ColorInformation, ColorPresentation, Color, ASTNode, PropertyASTNode, DocumentSymbolsContext, Range, TextEdit,
TextDocument, ColorInformation, ColorPresentation, Color, ASTNode, PropertyASTNode, DocumentSymbolsContext, Range, TextEdit,
SymbolInformation, SymbolKind, DocumentSymbol, Location
} from "../jsonLanguageTypes";

Expand Down Expand Up @@ -236,7 +236,7 @@ export class JSONDocumentSymbols {
return undefined;
}

public findDocumentColors(document: TextDocument, doc: Parser.JSONDocument, context?: DocumentSymbolsContext): Thenable<ColorInformation[]> {
public findDocumentColors(document: TextDocument, doc: Parser.JSONDocument, context?: DocumentSymbolsContext): PromiseLike<ColorInformation[]> {
return this.schemaService.getSchemaForResource(document.uri, doc).then(schema => {
const result: ColorInformation[] = [];
if (schema) {
Expand Down
6 changes: 3 additions & 3 deletions src/services/jsonHover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import * as Parser from '../parser/jsonParser';
import * as SchemaService from './jsonSchemaService';
import { JSONWorkerContribution } from '../jsonContributions';
import { TextDocument, PromiseConstructor, Thenable, Position, Range, Hover, MarkedString } from '../jsonLanguageTypes';
import { TextDocument, PromiseConstructor, Position, Range, Hover, MarkedString } from '../jsonLanguageTypes';

export class JSONHover {

Expand All @@ -20,7 +20,7 @@ export class JSONHover {
this.promise = promiseConstructor || Promise;
}

public doHover(document: TextDocument, position: Position, doc: Parser.JSONDocument): Thenable<Hover | null> {
public doHover(document: TextDocument, position: Position, doc: Parser.JSONDocument): PromiseLike<Hover | null> {

const offset = document.offsetAt(position);
let node = doc.getNodeFromOffset(offset);
Expand Down Expand Up @@ -125,4 +125,4 @@ function toMarkdownCodeBlock(content: string) {
return '`` ' + content + ' ``';
}
return content;
}
}
4 changes: 2 additions & 2 deletions src/services/jsonLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
*--------------------------------------------------------------------------------------------*/

import { DocumentLink } from 'vscode-languageserver-types';
import { TextDocument, ASTNode, PropertyASTNode, Range, Thenable } from '../jsonLanguageTypes';
import { TextDocument, ASTNode, PropertyASTNode, Range } from '../jsonLanguageTypes';
import { JSONDocument } from '../parser/jsonParser';

export function findLinks(document: TextDocument, doc: JSONDocument): Thenable<DocumentLink[]> {
export function findLinks(document: TextDocument, doc: JSONDocument): PromiseLike<DocumentLink[]> {
const links: DocumentLink[] = [];
doc.visit(node => {
if (node.type === "property" && node.keyNode.value === "$ref" && node.valueNode?.type === 'string') {
Expand Down
34 changes: 17 additions & 17 deletions src/services/jsonSchemaService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { JSONSchema, JSONSchemaMap, JSONSchemaRef } from '../jsonSchema';
import { URI } from 'vscode-uri';
import * as Strings from '../utils/strings';
import * as Parser from '../parser/jsonParser';
import { SchemaRequestService, WorkspaceContextService, PromiseConstructor, Thenable, MatchingSchema, TextDocument, SchemaConfiguration } from '../jsonLanguageTypes';
import { SchemaRequestService, WorkspaceContextService, PromiseConstructor, MatchingSchema, TextDocument, SchemaConfiguration } from '../jsonLanguageTypes';

import * as l10n from '@vscode/l10n';
import { createRegex } from '../utils/glob';
Expand All @@ -34,7 +34,7 @@ export interface IJSONSchemaService {
/**
* Looks up the appropriate schema for the given URI
*/
getSchemaForResource(resource: string, document?: Parser.JSONDocument): Thenable<ResolvedSchema | undefined>;
getSchemaForResource(resource: string, document?: Parser.JSONDocument): PromiseLike<ResolvedSchema | undefined>;

/**
* Returns all registered schema ids
Expand Down Expand Up @@ -62,12 +62,12 @@ export interface ISchemaHandle {
/**
* The schema from the file, with potential $ref references
*/
getUnresolvedSchema(): Thenable<UnresolvedSchema>;
getUnresolvedSchema(): PromiseLike<UnresolvedSchema>;

/**
* The schema from the file, with references resolved
*/
getResolvedSchema(): Thenable<ResolvedSchema>;
getResolvedSchema(): PromiseLike<ResolvedSchema>;
}

const BANG = '!';
Expand Down Expand Up @@ -138,8 +138,8 @@ class SchemaHandle implements ISchemaHandle {
public readonly uri: string;
public readonly dependencies: SchemaDependencies;
public anchors: Map<string, JSONSchema> | undefined;
private resolvedSchema: Thenable<ResolvedSchema> | undefined;
private unresolvedSchema: Thenable<UnresolvedSchema> | undefined;
private resolvedSchema: PromiseLike<ResolvedSchema> | undefined;
private unresolvedSchema: PromiseLike<UnresolvedSchema> | undefined;
private readonly service: JSONSchemaService;

constructor(service: JSONSchemaService, uri: string, unresolvedSchemaContent?: JSONSchema) {
Expand All @@ -152,14 +152,14 @@ class SchemaHandle implements ISchemaHandle {
}
}

public getUnresolvedSchema(): Thenable<UnresolvedSchema> {
public getUnresolvedSchema(): PromiseLike<UnresolvedSchema> {
if (!this.unresolvedSchema) {
this.unresolvedSchema = this.service.loadSchema(this.uri);
}
return this.unresolvedSchema;
}

public getResolvedSchema(): Thenable<ResolvedSchema> {
public getResolvedSchema(): PromiseLike<ResolvedSchema> {
if (!this.resolvedSchema) {
this.resolvedSchema = this.getUnresolvedSchema().then(unresolved => {
return this.service.resolveSchemaContent(unresolved, this);
Expand Down Expand Up @@ -256,7 +256,7 @@ export class JSONSchemaService implements IJSONSchemaService {
private requestService: SchemaRequestService | undefined;
private promiseConstructor: PromiseConstructor;

private cachedSchemaForResource: { resource: string; resolvedSchema: Thenable<ResolvedSchema | undefined> } | undefined;
private cachedSchemaForResource: { resource: string; resolvedSchema: PromiseLike<ResolvedSchema | undefined> } | undefined;

constructor(requestService?: SchemaRequestService, contextService?: WorkspaceContextService, promiseConstructor?: PromiseConstructor) {
this.contextService = contextService;
Expand Down Expand Up @@ -376,7 +376,7 @@ export class JSONSchemaService implements IJSONSchemaService {
}
}

public getResolvedSchema(schemaId: string): Thenable<ResolvedSchema | undefined> {
public getResolvedSchema(schemaId: string): PromiseLike<ResolvedSchema | undefined> {
const id = normalizeId(schemaId);
const schemaHandle = this.schemasById[id];
if (schemaHandle) {
Expand All @@ -385,7 +385,7 @@ export class JSONSchemaService implements IJSONSchemaService {
return this.promise.resolve(undefined);
}

public loadSchema(url: string): Thenable<UnresolvedSchema> {
public loadSchema(url: string): PromiseLike<UnresolvedSchema> {
if (!this.requestService) {
const errorMessage = l10n.t('Unable to load schema from \'{0}\'. No schema request service available', toDisplayString(url));
return this.promise.resolve(new UnresolvedSchema(<JSONSchema>{}, [errorMessage]));
Expand Down Expand Up @@ -428,7 +428,7 @@ export class JSONSchemaService implements IJSONSchemaService {
);
}

public resolveSchemaContent(schemaToResolve: UnresolvedSchema, handle: SchemaHandle): Thenable<ResolvedSchema> {
public resolveSchemaContent(schemaToResolve: UnresolvedSchema, handle: SchemaHandle): PromiseLike<ResolvedSchema> {

const resolveErrors: string[] = schemaToResolve.errors.slice(0);
const schema = schemaToResolve.schema;
Expand Down Expand Up @@ -489,7 +489,7 @@ export class JSONSchemaService implements IJSONSchemaService {
}
};

const resolveExternalLink = (node: JSONSchema, uri: string, refSegment: string | undefined, parentHandle: SchemaHandle): Thenable<any> => {
const resolveExternalLink = (node: JSONSchema, uri: string, refSegment: string | undefined, parentHandle: SchemaHandle): PromiseLike<any> => {
if (contextService && !/^[A-Za-z][A-Za-z0-9+\-.+]*:\/\/.*/.test(uri)) {
uri = contextService.resolveRelativePath(uri, parentHandle.uri);
}
Expand All @@ -506,8 +506,8 @@ export class JSONSchemaService implements IJSONSchemaService {
});
};

const resolveRefs = (node: JSONSchema, parentSchema: JSONSchema, parentHandle: SchemaHandle): Thenable<any> => {
const openPromises: Thenable<any>[] = [];
const resolveRefs = (node: JSONSchema, parentSchema: JSONSchema, parentHandle: SchemaHandle): PromiseLike<any> => {
const openPromises: PromiseLike<any>[] = [];

this.traverseNodes(node, next => {
const seenRefs = new Set<string>();
Expand Down Expand Up @@ -674,7 +674,7 @@ export class JSONSchemaService implements IJSONSchemaService {
return this.getAssociatedSchemas(resource);
}

public getSchemaForResource(resource: string, document?: Parser.JSONDocument): Thenable<ResolvedSchema | undefined> {
public getSchemaForResource(resource: string, document?: Parser.JSONDocument): PromiseLike<ResolvedSchema | undefined> {
if (document) {
// first use $schema if present
let schemeId = this.getSchemaFromProperty(resource, document);
Expand Down Expand Up @@ -704,7 +704,7 @@ export class JSONSchemaService implements IJSONSchemaService {
}
}

public getMatchingSchemas(document: TextDocument, jsonDocument: Parser.JSONDocument, schema?: JSONSchema): Thenable<MatchingSchema[]> {
public getMatchingSchemas(document: TextDocument, jsonDocument: Parser.JSONDocument, schema?: JSONSchema): PromiseLike<MatchingSchema[]> {
if (schema) {
const id = schema.id || ('schemaservice://untitled/matchingSchemas/' + idCounter++);
const handle = this.addSchemaHandle(id, schema);
Expand Down
Loading

0 comments on commit 24678bd

Please sign in to comment.