Skip to content

Commit

Permalink
Support relative path in inline schema comment (redhat-developer#499)
Browse files Browse the repository at this point in the history
* redhat-developer#371 Support relative path in inline schema comment

Signed-off-by: Yevhen Vydolob <[email protected]>

* Fix test on windows
  • Loading branch information
evidolob authored Jun 30, 2021
1 parent a34abce commit 8b50f5c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,22 @@ yaml.schemas: {

It is possible to specify a yaml schema using a modeline.

```
```yaml
# yaml-language-server: $schema=<urlToTheSchema>
```

Also it is possible to use relative path in a modeline:

```yaml
# yaml-language-server: $schema=../relative/path/to/schema
```

or absolute path:

```yaml
# yaml-language-server: $schema=/absolute/path/to/schema
```

## Containerized Language Server
An image is provided for users who would like to use the YAML language server without having to install dependencies locally.

Expand Down
20 changes: 14 additions & 6 deletions src/languageservice/services/yamlSchemaService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { convertSimple2RegExpPattern } from '../utils/strings';
import { SingleYAMLDocument } from '../parser/yamlParser07';
import { JSONDocument } from '../parser/jsonParser07';
import { load } from 'js-yaml';
import * as path from 'path';

const localize = nls.loadMessageBundle();

Expand Down Expand Up @@ -90,9 +91,8 @@ export class YAMLSchemaService extends JSONSchemaService {
private customSchemaProvider: CustomSchemaProvider | undefined;
private filePatternAssociations: JSONSchemaService.FilePatternAssociation[];
private contextService: WorkspaceContextService;
private customSchemaProvider: CustomSchemaProvider | undefined;
private requestService: SchemaRequestService;
public schemaPriorityMapping: Map<string, SchemaPriority[]>;
public schemaPriorityMapping: Map<string, Set<SchemaPriority>>;

constructor(
requestService: SchemaRequestService,
Expand Down Expand Up @@ -279,8 +279,16 @@ export class YAMLSchemaService extends JSONSchemaService {
const seen: { [schemaId: string]: boolean } = Object.create(null);
const schemas: string[] = [];

const schemaFromModeline = this.getSchemaFromModeline(doc);
let schemaFromModeline = this.getSchemaFromModeline(doc);
if (schemaFromModeline !== undefined) {
if (!schemaFromModeline.startsWith('file:')) {
if (!path.isAbsolute(schemaFromModeline)) {
const resUri = URI.parse(resource);
schemaFromModeline = URI.file(path.resolve(path.parse(resUri.fsPath).dir, schemaFromModeline)).toString();
} else {
schemaFromModeline = URI.file(schemaFromModeline).toString();
}
}
this.addSchemaPriority(schemaFromModeline, SchemaPriority.Modeline);
schemas.push(schemaFromModeline);
seen[schemaFromModeline] = true;
Expand Down Expand Up @@ -381,10 +389,10 @@ export class YAMLSchemaService extends JSONSchemaService {
public addSchemaPriority(uri: string, priority: number): void {
let currSchemaArray = this.schemaPriorityMapping.get(uri);
if (currSchemaArray) {
currSchemaArray = currSchemaArray.concat(priority);
currSchemaArray = currSchemaArray.add(priority);
this.schemaPriorityMapping.set(uri, currSchemaArray);
} else {
this.schemaPriorityMapping.set(uri, [priority]);
this.schemaPriorityMapping.set(uri, new Set<SchemaPriority>().add(priority));
}
}

Expand Down Expand Up @@ -460,7 +468,7 @@ export class YAMLSchemaService extends JSONSchemaService {
public async saveSchema(schemaId: string, schemaContent: JSONSchema): Promise<void> {
const id = this.normalizeId(schemaId);
this.getOrAddSchemaHandle(id, schemaContent);
this.schemaPriorityMapping.set(id, [SchemaPriority.Settings]);
this.schemaPriorityMapping.set(id, new Set<SchemaPriority>().add(SchemaPriority.Settings));
return Promise.resolve(undefined);
}

Expand Down
24 changes: 24 additions & 0 deletions test/autoCompletion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1567,6 +1567,30 @@ describe('Auto Completion Tests', () => {
.then(done, done);
}, done);
});

it('should handle absolute path', async () => {
const documentContent = `# yaml-language-server: $schema=${path.join(
__dirname,
'./fixtures/testArrayMaxProperties.json'
)} anothermodeline=value\n- `;
const content = `${documentContent}\n---\n- `;
const result = await parseSetup(content, documentContent.length);
assert.strictEqual(result.items.length, 3, `Expecting 3 items in completion but found ${result.items.length}`);
});

it('should handle relative path', async () => {
const documentContent = `# yaml-language-server: $schema=./fixtures/testArrayMaxProperties.json anothermodeline=value\n- `;
const content = `${documentContent}\n---\n- `;

const testTextDocument = setupSchemaIDTextDocument(content, path.join(__dirname, 'test.yaml'));
yamlSettings.documents = new TextDocumentTestManager();
(yamlSettings.documents as TextDocumentTestManager).set(testTextDocument);
const result = await languageHandler.completionHandler({
position: testTextDocument.positionAt(documentContent.length),
textDocument: testTextDocument,
});
assert.strictEqual(result.items.length, 3, `Expecting 3 items in completion but found ${result.items.length}`);
});
});

describe('Configuration based indentation', () => {
Expand Down

0 comments on commit 8b50f5c

Please sign in to comment.