Skip to content

Commit 24678bd

Browse files
authored
Merge pull request #239 from remcohaszing/deprecate-thenable
Deprecate Thenable and alias to PromiseLike
2 parents c27b284 + 356d5dd commit 24678bd

13 files changed

+66
-73
lines changed

src/example/sample.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22

3-
import { getLanguageService, JSONSchema, SchemaRequestService, TextDocument, MatchingSchema } from '../jsonLanguageService';
3+
import { getLanguageService, TextDocument } from '../jsonLanguageService';
44

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

5959
}
6060
main();
61-

src/jsonContributions.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
* Copyright (c) Microsoft Corporation. All rights reserved.
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
5-
import { Thenable, MarkedString, CompletionItem } from './jsonLanguageService';
5+
import { MarkedString, CompletionItem } from './jsonLanguageService';
66

77
export interface JSONWorkerContribution {
8-
getInfoContribution(uri: string, location: JSONPath): Thenable<MarkedString[]>;
9-
collectPropertyCompletions(uri: string, location: JSONPath, currentWord: string, addValue: boolean, isLast: boolean, result: CompletionsCollector): Thenable<any>;
10-
collectValueCompletions(uri: string, location: JSONPath, propertyKey: string, result: CompletionsCollector): Thenable<any>;
11-
collectDefaultCompletions(uri: string, result: CompletionsCollector): Thenable<any>;
12-
resolveCompletion?(item: CompletionItem): Thenable<CompletionItem>;
8+
getInfoContribution(uri: string, location: JSONPath): PromiseLike<MarkedString[]>;
9+
collectPropertyCompletions(uri: string, location: JSONPath, currentWord: string, addValue: boolean, isLast: boolean, result: CompletionsCollector): PromiseLike<any>;
10+
collectValueCompletions(uri: string, location: JSONPath, propertyKey: string, result: CompletionsCollector): PromiseLike<any>;
11+
collectDefaultCompletions(uri: string, result: CompletionsCollector): PromiseLike<any>;
12+
resolveCompletion?(item: CompletionItem): PromiseLike<CompletionItem>;
1313
}
1414
export type Segment = string | number;
1515
export type JSONPath = Segment[];

src/jsonLanguageService.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import { sort } from './utils/sort';
1717
import { format } from './utils/format';
1818

1919
import {
20-
Thenable,
2120
ASTNode,
2221
Color, ColorInformation, ColorPresentation,
2322
LanguageServiceParams, LanguageSettings, DocumentLanguageSettings,
@@ -37,23 +36,23 @@ export * from './jsonLanguageTypes';
3736

3837
export interface LanguageService {
3938
configure(settings: LanguageSettings): void;
40-
doValidation(document: TextDocument, jsonDocument: JSONDocument, documentSettings?: DocumentLanguageSettings, schema?: JSONSchema): Thenable<Diagnostic[]>;
39+
doValidation(document: TextDocument, jsonDocument: JSONDocument, documentSettings?: DocumentLanguageSettings, schema?: JSONSchema): PromiseLike<Diagnostic[]>;
4140
parseJSONDocument(document: TextDocument): JSONDocument;
4241
newJSONDocument(rootNode: ASTNode, syntaxDiagnostics?: Diagnostic[]): JSONDocument;
4342
resetSchema(uri: string): boolean;
44-
getMatchingSchemas(document: TextDocument, jsonDocument: JSONDocument, schema?: JSONSchema): Thenable<MatchingSchema[]>;
43+
getMatchingSchemas(document: TextDocument, jsonDocument: JSONDocument, schema?: JSONSchema): PromiseLike<MatchingSchema[]>;
4544
getLanguageStatus(document: TextDocument, jsonDocument: JSONDocument): JSONLanguageStatus;
46-
doResolve(item: CompletionItem): Thenable<CompletionItem>;
47-
doComplete(document: TextDocument, position: Position, doc: JSONDocument): Thenable<CompletionList | null>;
45+
doResolve(item: CompletionItem): PromiseLike<CompletionItem>;
46+
doComplete(document: TextDocument, position: Position, doc: JSONDocument): PromiseLike<CompletionList | null>;
4847
findDocumentSymbols(document: TextDocument, doc: JSONDocument, context?: DocumentSymbolsContext): SymbolInformation[];
4948
findDocumentSymbols2(document: TextDocument, doc: JSONDocument, context?: DocumentSymbolsContext): DocumentSymbol[];
50-
findDocumentColors(document: TextDocument, doc: JSONDocument, context?: DocumentColorsContext): Thenable<ColorInformation[]>;
49+
findDocumentColors(document: TextDocument, doc: JSONDocument, context?: DocumentColorsContext): PromiseLike<ColorInformation[]>;
5150
getColorPresentations(document: TextDocument, doc: JSONDocument, color: Color, range: Range): ColorPresentation[];
52-
doHover(document: TextDocument, position: Position, doc: JSONDocument): Thenable<Hover | null>;
51+
doHover(document: TextDocument, position: Position, doc: JSONDocument): PromiseLike<Hover | null>;
5352
getFoldingRanges(document: TextDocument, context?: FoldingRangesContext): FoldingRange[];
5453
getSelectionRanges(document: TextDocument, positions: Position[], doc: JSONDocument): SelectionRange[];
55-
findDefinition(document: TextDocument, position: Position, doc: JSONDocument): Thenable<DefinitionLink[]>;
56-
findLinks(document: TextDocument, doc: JSONDocument): Thenable<DocumentLink[]>;
54+
findDefinition(document: TextDocument, position: Position, doc: JSONDocument): PromiseLike<DefinitionLink[]>;
55+
findLinks(document: TextDocument, doc: JSONDocument): PromiseLike<DocumentLink[]>;
5756
format(document: TextDocument, range: Range, options: FormattingOptions): TextEdit[];
5857
sort(document: TextDocument, options: SortOptions): TextEdit[];
5958
}

src/jsonLanguageTypes.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ export interface WorkspaceContextService {
206206
* In case of an error, returns a rejected promise with a displayable error string.
207207
*/
208208
export interface SchemaRequestService {
209-
(uri: string): Thenable<string>;
209+
(uri: string): PromiseLike<string>;
210210
}
211211

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

221221
/**
222222
* Creates a Promise that is resolved with an array of results when all of the provided Promises
223223
* resolve, or rejected when any Promise is rejected.
224224
* @param values An array of Promises.
225225
* @returns A new Promise.
226226
*/
227-
all<T>(values: Array<T | Thenable<T>>): Thenable<T[]>;
227+
all<T>(values: Array<T | PromiseLike<T>>): PromiseLike<T[]>;
228228
/**
229229
* Creates a new rejected promise for the provided reason.
230230
* @param reason The reason the promise was rejected.
231231
* @returns A new rejected Promise.
232232
*/
233-
reject<T>(reason: any): Thenable<T>;
233+
reject<T>(reason: any): PromiseLike<T>;
234234

235235
/**
236236
* Creates a new resolved promise for the provided value.
237237
* @param value A promise.
238238
* @returns A promise whose internal state matches the provided promise.
239239
*/
240-
resolve<T>(value: T | Thenable<T>): Thenable<T>;
240+
resolve<T>(value: T | PromiseLike<T>): PromiseLike<T>;
241241

242242
}
243243

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

255251
export interface LanguageServiceParams {
256252
/**

src/services/jsonCompletion.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { stringifyObject } from '../utils/json';
1212
import { endsWith, extendedRegExp } from '../utils/strings';
1313
import { isDefined } from '../utils/objects';
1414
import {
15-
PromiseConstructor, Thenable,
15+
PromiseConstructor,
1616
ASTNode, ObjectASTNode, ArrayASTNode, PropertyASTNode, ClientCapabilities,
1717
TextDocument,
1818
CompletionItem, CompletionItemKind, CompletionList, Position, Range, TextEdit, InsertTextFormat, MarkupContent, MarkupKind
@@ -36,7 +36,7 @@ export class JSONCompletion {
3636
private clientCapabilities: ClientCapabilities = {}) {
3737
}
3838

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

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

5454
const result: CompletionList = {
5555
items: [],
@@ -130,7 +130,7 @@ export class JSONCompletion {
130130
};
131131

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

135135
let addValue = true;
136136
let currentKey = '';
@@ -512,7 +512,7 @@ export class JSONCompletion {
512512

513513
}
514514

515-
private getContributedValueCompletions(doc: Parser.JSONDocument, node: ASTNode | undefined, offset: number, document: TextDocument, collector: CompletionsCollector, collectionPromises: Thenable<any>[]) {
515+
private getContributedValueCompletions(doc: Parser.JSONDocument, node: ASTNode | undefined, offset: number, document: TextDocument, collector: CompletionsCollector, collectionPromises: PromiseLike<any>[]) {
516516
if (!node) {
517517
this.contributions.forEach((contribution) => {
518518
const collectPromise = contribution.collectDefaultCompletions(document.uri, collector);

src/services/jsonDocumentSymbols.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { colorFromHex } from '../utils/colors';
99
import * as l10n from '@vscode/l10n';
1010

1111
import {
12-
TextDocument, Thenable, ColorInformation, ColorPresentation, Color, ASTNode, PropertyASTNode, DocumentSymbolsContext, Range, TextEdit,
12+
TextDocument, ColorInformation, ColorPresentation, Color, ASTNode, PropertyASTNode, DocumentSymbolsContext, Range, TextEdit,
1313
SymbolInformation, SymbolKind, DocumentSymbol, Location
1414
} from "../jsonLanguageTypes";
1515

@@ -236,7 +236,7 @@ export class JSONDocumentSymbols {
236236
return undefined;
237237
}
238238

239-
public findDocumentColors(document: TextDocument, doc: Parser.JSONDocument, context?: DocumentSymbolsContext): Thenable<ColorInformation[]> {
239+
public findDocumentColors(document: TextDocument, doc: Parser.JSONDocument, context?: DocumentSymbolsContext): PromiseLike<ColorInformation[]> {
240240
return this.schemaService.getSchemaForResource(document.uri, doc).then(schema => {
241241
const result: ColorInformation[] = [];
242242
if (schema) {

src/services/jsonHover.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import * as Parser from '../parser/jsonParser';
77
import * as SchemaService from './jsonSchemaService';
88
import { JSONWorkerContribution } from '../jsonContributions';
9-
import { TextDocument, PromiseConstructor, Thenable, Position, Range, Hover, MarkedString } from '../jsonLanguageTypes';
9+
import { TextDocument, PromiseConstructor, Position, Range, Hover, MarkedString } from '../jsonLanguageTypes';
1010

1111
export class JSONHover {
1212

@@ -20,7 +20,7 @@ export class JSONHover {
2020
this.promise = promiseConstructor || Promise;
2121
}
2222

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

2525
const offset = document.offsetAt(position);
2626
let node = doc.getNodeFromOffset(offset);
@@ -125,4 +125,4 @@ function toMarkdownCodeBlock(content: string) {
125125
return '`` ' + content + ' ``';
126126
}
127127
return content;
128-
}
128+
}

src/services/jsonLinks.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
*--------------------------------------------------------------------------------------------*/
55

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

10-
export function findLinks(document: TextDocument, doc: JSONDocument): Thenable<DocumentLink[]> {
10+
export function findLinks(document: TextDocument, doc: JSONDocument): PromiseLike<DocumentLink[]> {
1111
const links: DocumentLink[] = [];
1212
doc.visit(node => {
1313
if (node.type === "property" && node.keyNode.value === "$ref" && node.valueNode?.type === 'string') {

src/services/jsonSchemaService.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { JSONSchema, JSONSchemaMap, JSONSchemaRef } from '../jsonSchema';
88
import { URI } from 'vscode-uri';
99
import * as Strings from '../utils/strings';
1010
import * as Parser from '../parser/jsonParser';
11-
import { SchemaRequestService, WorkspaceContextService, PromiseConstructor, Thenable, MatchingSchema, TextDocument, SchemaConfiguration } from '../jsonLanguageTypes';
11+
import { SchemaRequestService, WorkspaceContextService, PromiseConstructor, MatchingSchema, TextDocument, SchemaConfiguration } from '../jsonLanguageTypes';
1212

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

3939
/**
4040
* Returns all registered schema ids
@@ -62,12 +62,12 @@ export interface ISchemaHandle {
6262
/**
6363
* The schema from the file, with potential $ref references
6464
*/
65-
getUnresolvedSchema(): Thenable<UnresolvedSchema>;
65+
getUnresolvedSchema(): PromiseLike<UnresolvedSchema>;
6666

6767
/**
6868
* The schema from the file, with references resolved
6969
*/
70-
getResolvedSchema(): Thenable<ResolvedSchema>;
70+
getResolvedSchema(): PromiseLike<ResolvedSchema>;
7171
}
7272

7373
const BANG = '!';
@@ -138,8 +138,8 @@ class SchemaHandle implements ISchemaHandle {
138138
public readonly uri: string;
139139
public readonly dependencies: SchemaDependencies;
140140
public anchors: Map<string, JSONSchema> | undefined;
141-
private resolvedSchema: Thenable<ResolvedSchema> | undefined;
142-
private unresolvedSchema: Thenable<UnresolvedSchema> | undefined;
141+
private resolvedSchema: PromiseLike<ResolvedSchema> | undefined;
142+
private unresolvedSchema: PromiseLike<UnresolvedSchema> | undefined;
143143
private readonly service: JSONSchemaService;
144144

145145
constructor(service: JSONSchemaService, uri: string, unresolvedSchemaContent?: JSONSchema) {
@@ -152,14 +152,14 @@ class SchemaHandle implements ISchemaHandle {
152152
}
153153
}
154154

155-
public getUnresolvedSchema(): Thenable<UnresolvedSchema> {
155+
public getUnresolvedSchema(): PromiseLike<UnresolvedSchema> {
156156
if (!this.unresolvedSchema) {
157157
this.unresolvedSchema = this.service.loadSchema(this.uri);
158158
}
159159
return this.unresolvedSchema;
160160
}
161161

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

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

261261
constructor(requestService?: SchemaRequestService, contextService?: WorkspaceContextService, promiseConstructor?: PromiseConstructor) {
262262
this.contextService = contextService;
@@ -376,7 +376,7 @@ export class JSONSchemaService implements IJSONSchemaService {
376376
}
377377
}
378378

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

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

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

433433
const resolveErrors: string[] = schemaToResolve.errors.slice(0);
434434
const schema = schemaToResolve.schema;
@@ -489,7 +489,7 @@ export class JSONSchemaService implements IJSONSchemaService {
489489
}
490490
};
491491

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

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

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

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

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

0 commit comments

Comments
 (0)