Skip to content

Commit 704cb0e

Browse files
authored
add cache to jsonSchemaService (#59)
1 parent cee43a8 commit 704cb0e

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

language-service/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
#### 0.5.5
3-
Better handling of YAML structure errors
3+
Better handling of YAML structure errors [#PR-58](https://github.com/Microsoft/azure-pipelines-language-server/pull/58)
4+
Cache schemas to improve performance [#PR-59](https://github.com/Microsoft/azure-pipelines-language-server/pull/59)
45

56
#### 0.5.4
67
Change schema service to use a schema object instead of a JSON string [#PR-53](https://github.com/Microsoft/azure-pipelines-language-server/pull/53)

language-service/src/services/jsonSchemaService.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ export class JSONSchemaService implements IJSONSchemaService {
227227
private promiseConstructor: PromiseConstructor;
228228
private customSchemaProvider: CustomSchemaProvider;
229229

230+
private schemaCache: {[resource: string]: ResolvedSchema};
231+
230232
constructor(requestService: SchemaRequestService, contextService?: WorkspaceContextService, customSchemaProvider?: CustomSchemaProvider, promiseConstructor?: PromiseConstructor) {
231233
this.contextService = contextService;
232234
this.requestService = requestService;
@@ -239,6 +241,7 @@ export class JSONSchemaService implements IJSONSchemaService {
239241
this.filePatternAssociations = [];
240242
this.filePatternAssociationById = {};
241243
this.registeredSchemasIds = {};
244+
this.schemaCache = {};
242245
}
243246

244247
public getRegisteredSchemaIds(filter?: (scheme) => boolean): string[] {
@@ -491,6 +494,11 @@ export class JSONSchemaService implements IJSONSchemaService {
491494
}
492495

493496
public getSchemaForResource(resource: string ): Thenable<ResolvedSchema> {
497+
498+
if (this.schemaCache.hasOwnProperty(resource)) {
499+
return this.promise.resolve(this.schemaCache[resource]);
500+
}
501+
494502
console.log('getSchemaForResource');
495503
console.log('resource: ' + resource);
496504

@@ -505,7 +513,10 @@ export class JSONSchemaService implements IJSONSchemaService {
505513
for (let i = this.filePatternAssociations.length - 1; i >= 0; i--) {
506514
let entry = this.filePatternAssociations[i];
507515
if (entry.matchesPattern(resource)) {
508-
return entry.getCombinedSchema(this).getResolvedSchema();
516+
return entry.getCombinedSchema(this).getResolvedSchema().then(resolvedSchema => {
517+
this.schemaCache[resource] = resolvedSchema;
518+
return this.promise.resolve(resolvedSchema);
519+
});
509520
}
510521
}
511522
return this.promise.resolve(null);

language-service/src/services/yamlCompletion.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { nodeHolder } from "../utils/yamlServiceUtils";
1717
import { CompletionItem, CompletionItemKind, CompletionList, TextDocument, Position, Range, TextEdit, InsertTextFormat } from 'vscode-languageserver-types';
1818

1919
import * as nls from 'vscode-nls';
20-
import { YAMLDocument } from '../parser/yamlParser';
20+
import { YAMLDocument, SingleYAMLDocument } from '../parser/yamlParser';
2121

2222
const localize = nls.loadMessageBundle();
2323

@@ -63,7 +63,7 @@ export class YAMLCompletion {
6363
return Promise.resolve(result);
6464
}
6565

66-
const jsonDocument = yamlDocument.documents.length > 0 ? yamlDocument.documents[0] : null;
66+
const jsonDocument: SingleYAMLDocument = yamlDocument.documents.length > 0 ? yamlDocument.documents[0] : null;
6767
if (jsonDocument === null) {
6868
return Promise.resolve(result);
6969
}
@@ -229,7 +229,7 @@ export class YAMLCompletion {
229229
return !!stringArray.some(arrayEntry => arrayEntry === key);
230230
}
231231

232-
private getPropertyCompletions(schema: SchemaService.ResolvedSchema, doc, node: Parser.ASTNode, addValue: boolean, collector: CompletionsCollector, separatorAfter: string): void {
232+
private getPropertyCompletions(schema: SchemaService.ResolvedSchema, doc: SingleYAMLDocument, node: Parser.ASTNode, addValue: boolean, collector: CompletionsCollector, separatorAfter: string): void {
233233
const nodeProperties: Parser.PropertyASTNode[] = (<Parser.ObjectASTNode>node).properties;
234234
const hasMatchingProperty = (key: string, propSchema: JSONSchema): boolean => {
235235
return nodeProperties.some((propertyNode: Parser.PropertyASTNode): boolean => {
@@ -265,7 +265,7 @@ export class YAMLCompletion {
265265
if (schemaProperties) {
266266
Object.keys(schemaProperties).forEach((key: string) => {
267267
//check for more than one property because the placeholder will always be in the list
268-
if (s.node.properties.length > 1 || this.arrayIsEmptyOrContainsKey(s.schema.firstProperty, key)) {
268+
if (s.node['properties'].length > 1 || this.arrayIsEmptyOrContainsKey(s.schema.firstProperty, key)) {
269269
const propertySchema = schemaProperties[key];
270270
if (!propertySchema.deprecationMessage &&
271271
!propertySchema["doNotSuggest"] &&
@@ -285,7 +285,7 @@ export class YAMLCompletion {
285285
});
286286
}
287287

288-
private getValueCompletions(schema: SchemaService.ResolvedSchema, doc, node: Parser.ASTNode, offset: number, document: TextDocument, collector: CompletionsCollector, types: { [type: string]: boolean }): void {
288+
private getValueCompletions(schema: SchemaService.ResolvedSchema, doc: SingleYAMLDocument, node: Parser.ASTNode, offset: number, document: TextDocument, collector: CompletionsCollector, types: { [type: string]: boolean }): void {
289289

290290

291291
let offsetForSeparator = offset;

0 commit comments

Comments
 (0)