Skip to content

Commit 2bae915

Browse files
authored
Merge pull request #352 from redhat-developer/thenable-to-promise
Rename all applicable Thenables to Promise
2 parents 1db6287 + e02c221 commit 2bae915

16 files changed

+37
-118
lines changed

src/languageservice/services/schemaRequestHandler.ts

+3-17
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import { IConnection } from 'vscode-languageserver';
33
import { xhr, XHRResponse, getErrorStatusDescription } from 'request-light';
44
import * as fs from 'fs';
55

6-
import { VSCodeContentRequest, CustomSchemaContentRequest } from '../../requestTypes';
6+
import { CustomSchemaContentRequest } from '../../requestTypes';
77
import { isRelativePath, relativeToAbsolutePath } from '../utils/paths';
88

99
/**
1010
* Handles schema content requests given the schema URI
1111
* @param uri can be a local file, vscode request, http(s) request or a custom request
1212
*/
13-
export const schemaRequestHandler = (connection: IConnection, uri: string): Thenable<string> => {
13+
export const schemaRequestHandler = (connection: IConnection, uri: string): Promise<string> => {
1414
if (!uri) {
1515
return Promise.reject('No schema specified');
1616
}
@@ -45,20 +45,6 @@ export const schemaRequestHandler = (connection: IConnection, uri: string): Then
4545
});
4646
}
4747

48-
// vscode schema content requests are forwarded to the client through LSP
49-
// This is a non-standard LSP extension introduced by the JSON language server
50-
// See https://github.com/microsoft/vscode/blob/master/extensions/json-language-features/server/README.md
51-
if (scheme === 'vscode') {
52-
return connection.sendRequest(VSCodeContentRequest.type, uri).then(
53-
(responseText) => {
54-
return responseText;
55-
},
56-
(error) => {
57-
return error.message;
58-
}
59-
);
60-
}
61-
6248
// HTTP(S) requests are sent and the response result is either the schema content or an error
6349
if (scheme === 'http' || scheme === 'https') {
6450
// Send the HTTP(S) schema content request and return the result
@@ -74,5 +60,5 @@ export const schemaRequestHandler = (connection: IConnection, uri: string): Then
7460
}
7561

7662
// Neither local file nor vscode, nor HTTP(S) schema request, so send it off as a custom request
77-
return connection.sendRequest(CustomSchemaContentRequest.type, uri) as Thenable<string>;
63+
return connection.sendRequest(CustomSchemaContentRequest.type, uri) as Promise<string>;
7864
};

src/languageservice/services/yamlCompletion.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { ASTNode, ObjectASTNode, PropertyASTNode } from '../jsonASTTypes';
1010
import { parse as parseYAML } from '../parser/yamlParser07';
1111
import { YAMLSchemaService } from './yamlSchemaService';
1212
import { JSONSchema, JSONSchemaRef } from '../jsonSchema';
13-
import { Thenable, CompletionsCollector } from 'vscode-json-languageservice';
13+
import { CompletionsCollector } from 'vscode-json-languageservice';
1414
import {
1515
CompletionItem,
1616
CompletionItemKind,
@@ -54,7 +54,7 @@ export class YAMLCompletion extends JSONCompletion {
5454
this.configuredIndentation = languageSettings.indentation;
5555
}
5656

57-
public doComplete(document: TextDocument, position: Position, isKubernetes = false): Thenable<CompletionList> {
57+
public doComplete(document: TextDocument, position: Position, isKubernetes = false): Promise<CompletionList> {
5858
const result: CompletionList = {
5959
items: [],
6060
isIncomplete: false,
@@ -157,7 +157,7 @@ export class YAMLCompletion extends JSONCompletion {
157157
const newSchema = schema;
158158

159159
// eslint-disable-next-line @typescript-eslint/no-explicit-any
160-
const collectionPromises: Thenable<any>[] = [];
160+
const collectionPromises: Promise<any>[] = [];
161161

162162
let addValue = true;
163163

src/languageservice/services/yamlHover.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*--------------------------------------------------------------------------------------------*/
66
'use strict';
77

8-
import { Thenable } from 'vscode-json-languageservice';
98
import { Hover, TextDocument, Position } from 'vscode-languageserver-types';
109
import { matchOffsetToDocument } from '../utils/arrUtils';
1110
import { LanguageSettings } from '../yamlLanguageService';
@@ -29,7 +28,7 @@ export class YAMLHover {
2928
}
3029
}
3130

32-
public doHover(document: TextDocument, position: Position, isKubernetes = false): Thenable<Hover> {
31+
public doHover(document: TextDocument, position: Position, isKubernetes = false): Promise<Hover> {
3332
if (!this.shouldHover || !document) {
3433
return Promise.resolve(undefined);
3534
}

src/languageservice/services/yamlLinks.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { parse as parseYAML } from '../parser/yamlParser07';
77
import { findLinks as JSONFindLinks } from 'vscode-json-languageservice/lib/umd/services/jsonLinks';
88
import { DocumentLink } from 'vscode-languageserver';
99

10-
export function findLinks(document: TextDocument): Thenable<DocumentLink[]> {
10+
export function findLinks(document: TextDocument): Promise<DocumentLink[]> {
1111
const doc = parseYAML(document.getText());
1212
// Find links across all YAML Documents then report them back once finished
1313
const linkPromises = [];

src/languageservice/services/yamlSchemaService.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
'use strict';
77

88
import { JSONSchema, JSONSchemaMap, JSONSchemaRef } from '../jsonSchema';
9-
import { SchemaRequestService, WorkspaceContextService, PromiseConstructor, Thenable } from '../yamlLanguageService';
9+
import { SchemaRequestService, WorkspaceContextService } from '../yamlLanguageService';
1010
import {
1111
UnresolvedSchema,
1212
ResolvedSchema,
@@ -26,7 +26,7 @@ import * as yaml from 'js-yaml';
2626

2727
const localize = nls.loadMessageBundle();
2828

29-
export declare type CustomSchemaProvider = (uri: string) => Thenable<string | string[]>;
29+
export declare type CustomSchemaProvider = (uri: string) => Promise<string | string[]>;
3030

3131
export enum MODIFICATION_ACTIONS {
3232
'delete',
@@ -105,7 +105,7 @@ export class YAMLSchemaService extends JSONSchemaService {
105105
schemaToResolve: UnresolvedSchema,
106106
schemaURL: string,
107107
dependencies: SchemaDependencies
108-
): Thenable<ResolvedSchema> {
108+
): Promise<ResolvedSchema> {
109109
const resolveErrors: string[] = schemaToResolve.errors.slice(0);
110110
const schema = schemaToResolve.schema;
111111
const contextService = this.contextService;
@@ -147,7 +147,7 @@ export class YAMLSchemaService extends JSONSchemaService {
147147
parentSchemaURL: string,
148148
parentSchemaDependencies: SchemaDependencies
149149
// eslint-disable-next-line @typescript-eslint/no-explicit-any
150-
): Thenable<any> => {
150+
): Promise<any> => {
151151
if (contextService && !/^\w+:\/\/.*/.test(uri)) {
152152
uri = contextService.resolveRelativePath(uri, parentSchemaURL);
153153
}
@@ -174,7 +174,7 @@ export class YAMLSchemaService extends JSONSchemaService {
174174
parentSchemaURL: string,
175175
parentSchemaDependencies: SchemaDependencies
176176
// eslint-disable-next-line @typescript-eslint/no-explicit-any
177-
): Thenable<any> => {
177+
): Promise<any> => {
178178
if (!node || typeof node !== 'object') {
179179
return Promise.resolve(null);
180180
}
@@ -183,7 +183,7 @@ export class YAMLSchemaService extends JSONSchemaService {
183183
const seen: JSONSchema[] = [];
184184

185185
// eslint-disable-next-line @typescript-eslint/no-explicit-any
186-
const openPromises: Thenable<any>[] = [];
186+
const openPromises: Promise<any>[] = [];
187187

188188
const collectEntries = (...entries: JSONSchemaRef[]): void => {
189189
for (const entry of entries) {
@@ -262,7 +262,7 @@ export class YAMLSchemaService extends JSONSchemaService {
262262
});
263263
}
264264

265-
public getSchemaForResource(resource: string, doc: JSONDocument): Thenable<ResolvedSchema> {
265+
public getSchemaForResource(resource: string, doc: JSONDocument): Promise<ResolvedSchema> {
266266
// eslint-disable-next-line @typescript-eslint/no-explicit-any
267267
const resolveSchema = (): any => {
268268
const seen: { [schemaId: string]: boolean } = Object.create(null);
@@ -507,7 +507,7 @@ export class YAMLSchemaService extends JSONSchemaService {
507507
return super.getOrAddSchemaHandle(id, unresolvedSchemaContent);
508508
}
509509

510-
loadSchema(schemaUri: string): Thenable<UnresolvedSchema> {
510+
loadSchema(schemaUri: string): Promise<UnresolvedSchema> {
511511
const requestService = this.requestService;
512512
return super.loadSchema(schemaUri).then((unresolvedJsonSchema: UnresolvedSchema) => {
513513
// If json-language-server failed to parse the schema, attempt to parse it as YAML instead.
@@ -570,7 +570,7 @@ export class YAMLSchemaService extends JSONSchemaService {
570570
return super.getRegisteredSchemaIds(filter);
571571
}
572572

573-
getResolvedSchema(schemaId: string): Thenable<ResolvedSchema> {
573+
getResolvedSchema(schemaId: string): Promise<ResolvedSchema> {
574574
return super.getResolvedSchema(schemaId);
575575
}
576576

src/languageservice/yamlLanguageService.ts

+7-57
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { YAMLHover } from './services/yamlHover';
2323
import { YAMLValidation } from './services/yamlValidation';
2424
import { YAMLFormatter } from './services/yamlFormatter';
2525
// eslint-disable-next-line @typescript-eslint/no-unused-vars
26-
import { JSONWorkerContribution, JSONDocument, DefinitionLink } from 'vscode-json-languageservice';
26+
import { JSONDocument, DefinitionLink } from 'vscode-json-languageservice';
2727
import { findLinks } from './services/yamlLinks';
2828

2929
export interface LanguageSettings {
@@ -41,56 +41,6 @@ export interface LanguageSettings {
4141
indentation?: string;
4242
}
4343

44-
export interface PromiseConstructor {
45-
/**
46-
* Creates a new Promise.
47-
* @param executor A callback used to initialize the promise. This callback is passed two arguments:
48-
* a resolve callback used resolve the promise with a value or the result of another promise,
49-
* and a reject callback used to reject the promise with a provided reason or error.
50-
*/
51-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
52-
new <T>(executor: (resolve: (value?: T | Thenable<T>) => void, reject: (reason?: any) => void) => void): Thenable<T>;
53-
54-
/**
55-
* Creates a Promise that is resolved with an array of results when all of the provided Promises
56-
* resolve, or rejected when any Promise is rejected.
57-
* @param values An array of Promises.
58-
* @returns A new Promise.
59-
*/
60-
all<T>(values: Array<T | Thenable<T>>): Thenable<T[]>;
61-
/**
62-
* Creates a new rejected promise for the provided reason.
63-
* @param reason The reason the promise was rejected.
64-
* @returns A new rejected Promise.
65-
*/
66-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
67-
reject<T>(reason: any): Thenable<T>;
68-
69-
/**
70-
* Creates a new resolved promise for the provided value.
71-
* @param value A promise.
72-
* @returns A promise whose internal state matches the provided promise.
73-
*/
74-
resolve<T>(value: T | Thenable<T>): Thenable<T>;
75-
}
76-
77-
export interface Thenable<R> {
78-
/**
79-
* Attaches callbacks for the resolution and/or rejection of the Promise.
80-
* @param onfulfilled The callback to execute when the Promise is resolved.
81-
* @param onrejected The callback to execute when the Promise is rejected.
82-
* @returns A Promise for the completion of which ever callback is executed.
83-
*/
84-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
85-
then<TResult>(
86-
onfulfilled?: (value: R) => TResult | Thenable<TResult>,
87-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
88-
onrejected?: (reason: any) => TResult | Thenable<TResult>
89-
): Thenable<TResult>;
90-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
91-
then<TResult>(onfulfilled?: (value: R) => TResult | Thenable<TResult>, onrejected?: (reason: any) => void): Thenable<TResult>;
92-
}
93-
9444
export interface WorkspaceContextService {
9545
resolveRelativePath(relativePath: string, resource: string): string;
9646
}
@@ -99,7 +49,7 @@ export interface WorkspaceContextService {
9949
* in case of an error, a displayable error string
10050
*/
10151
export interface SchemaRequestService {
102-
(uri: string): Thenable<string>;
52+
(uri: string): Promise<string>;
10353
}
10454

10555
export interface SchemaConfiguration {
@@ -129,13 +79,13 @@ export interface CustomFormatterOptions {
12979
export interface LanguageService {
13080
configure(settings: LanguageSettings): void;
13181
registerCustomSchemaProvider(schemaProvider: CustomSchemaProvider): void;
132-
doComplete(document: TextDocument, position: Position, isKubernetes: boolean): Thenable<CompletionList>;
133-
doValidation(document: TextDocument, isKubernetes: boolean): Thenable<Diagnostic[]>;
134-
doHover(document: TextDocument, position: Position): Thenable<Hover | null>;
82+
doComplete(document: TextDocument, position: Position, isKubernetes: boolean): Promise<CompletionList>;
83+
doValidation(document: TextDocument, isKubernetes: boolean): Promise<Diagnostic[]>;
84+
doHover(document: TextDocument, position: Position): Promise<Hover | null>;
13585
findDocumentSymbols(document: TextDocument): SymbolInformation[];
13686
findDocumentSymbols2(document: TextDocument): DocumentSymbol[];
137-
findDefinition(document: TextDocument, position: Position, doc: JSONDocument): Thenable<DefinitionLink[]>;
138-
findLinks(document: TextDocument): Thenable<DocumentLink[]>;
87+
findDefinition(document: TextDocument, position: Position, doc: JSONDocument): Promise<DefinitionLink[]>;
88+
findLinks(document: TextDocument): Promise<DocumentLink[]>;
13989
resetSchema(uri: string): boolean;
14090
doFormat(document: TextDocument, options: CustomFormatterOptions): TextEdit[];
14191
addSchema(schemaID: string, schema: JSONSchema): void;

test/autoCompletion.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const languageSettingsSetup = new ServiceSetup().withCompletion();
1616
const languageService = configureLanguageService(languageSettingsSetup.languageSettings);
1717

1818
suite('Auto Completion Tests', () => {
19-
function parseSetup(content: string, position): Thenable<CompletionList> {
19+
function parseSetup(content: string, position): Promise<CompletionList> {
2020
const testTextDocument = setupSchemaIDTextDocument(content);
2121
return languageService.doComplete(testTextDocument, testTextDocument.positionAt(position), false);
2222
}

test/customTags.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ let languageService = configureLanguageService(languageSettingsSetup.languageSet
1313

1414
// Defines a Mocha test suite to group tests of similar kind together
1515
suite('Custom Tag tests Tests', () => {
16-
function parseSetup(content: string, customTags: string[]): Thenable<Diagnostic[]> {
16+
function parseSetup(content: string, customTags: string[]): Promise<Diagnostic[]> {
1717
const testTextDocument = setupTextDocument(content);
1818
languageSettingsSetup.languageSettings.customTags = customTags;
1919
languageService = configureLanguageService(languageSettingsSetup.languageSettings);

test/defaultSnippets.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const languageService = configureLanguageService(languageSettingsSetup.languageS
1818

1919
suite('Default Snippet Tests', () => {
2020
describe('Snippet Tests', function () {
21-
function parseSetup(content: string, position: number): Thenable<CompletionList> {
21+
function parseSetup(content: string, position: number): Promise<CompletionList> {
2222
const testTextDocument = setupTextDocument(content);
2323
return languageService.doComplete(testTextDocument, testTextDocument.positionAt(position), false);
2424
}

test/findLinks.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const languageService = configureLanguageService(new ServiceSetup().languageSett
1111

1212
suite('FindDefintion Tests', () => {
1313
describe('Jump to defintion', function () {
14-
function findLinks(content: string): Thenable<DocumentLink[]> {
14+
function findLinks(content: string): Promise<DocumentLink[]> {
1515
const testTextDocument = setupTextDocument(content);
1616
return languageService.findLinks(testTextDocument);
1717
}

test/hover.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ suite('Hover Tests', () => {
1717
});
1818

1919
describe('Hover', function () {
20-
function parseSetup(content: string, position): Thenable<Hover> {
20+
function parseSetup(content: string, position): Promise<Hover> {
2121
const testTextDocument = setupSchemaIDTextDocument(content);
2222
return languageService.doHover(testTextDocument, testTextDocument.positionAt(position));
2323
}

test/integration.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ languageService.configure(languageSettings);
2525
suite('Kubernetes Integration Tests', () => {
2626
// Tests for validator
2727
describe('Yaml Validation with kubernetes', function () {
28-
function parseSetup(content: string): Thenable<Diagnostic[]> {
28+
function parseSetup(content: string): Promise<Diagnostic[]> {
2929
const testTextDocument = setupTextDocument(content);
3030
return languageService.doValidation(testTextDocument, true);
3131
}
@@ -193,7 +193,7 @@ suite('Kubernetes Integration Tests', () => {
193193

194194
describe('yamlCompletion with kubernetes', function () {
195195
describe('doComplete', function () {
196-
function parseSetup(content: string, position): Thenable<CompletionList> {
196+
function parseSetup(content: string, position): Promise<CompletionList> {
197197
const testTextDocument = setupTextDocument(content);
198198
return languageService.doComplete(testTextDocument, testTextDocument.positionAt(position), true);
199199
}
@@ -283,7 +283,7 @@ suite('Kubernetes Integration Tests', () => {
283283
});
284284

285285
describe('yamlHover with kubernetes', function () {
286-
function parseSetup(content: string, offset: number): Thenable<Hover> {
286+
function parseSetup(content: string, offset: number): Promise<Hover> {
287287
const testTextDocument = setupTextDocument(content);
288288
return languageService.doHover(testTextDocument, testTextDocument.positionAt(offset));
289289
}

test/mulipleDocuments.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ const languageSettingsSetup = new ServiceSetup()
2323
suite('Multiple Documents Validation Tests', () => {
2424
// Tests for validator
2525
describe('Multiple Documents Validation', function () {
26-
function validatorSetup(content: string): Thenable<Diagnostic[]> {
26+
function validatorSetup(content: string): Promise<Diagnostic[]> {
2727
const testTextDocument = setupTextDocument(content);
2828
const languageService = configureLanguageService(languageSettingsSetup.languageSettings);
2929
return languageService.doValidation(testTextDocument, false);
3030
}
3131

32-
function hoverSetup(content: string, position): Thenable<Hover> {
32+
function hoverSetup(content: string, position): Promise<Hover> {
3333
const testTextDocument = setupTextDocument(content);
3434
const languageService = configureLanguageService(languageSettingsSetup.languageSettings);
3535
return languageService.doHover(testTextDocument, testTextDocument.positionAt(position));

test/schema.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const workspaceContext = {
2020
},
2121
};
2222

23-
const schemaRequestServiceForURL = (uri: string): Thenable<string> => {
23+
const schemaRequestServiceForURL = (uri: string): Promise<string> => {
2424
const headers = { 'Accept-Encoding': 'gzip, deflate' };
2525
return xhr({ url: uri, followRedirects: 5, headers }).then(
2626
(response) => {

test/schemaValidation.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const languageService = configureLanguageService(languageSettingsSetup.languageS
2626

2727
// Defines a Mocha test suite to group tests of similar kind together
2828
suite('Validation Tests', () => {
29-
function parseSetup(content: string, isKubernetes = false): Thenable<Diagnostic[]> {
29+
function parseSetup(content: string, isKubernetes = false): Promise<Diagnostic[]> {
3030
const testTextDocument = setupSchemaIDTextDocument(content);
3131
return languageService.doValidation(testTextDocument, isKubernetes);
3232
}

0 commit comments

Comments
 (0)