-
Notifications
You must be signed in to change notification settings - Fork 280
/
Copy pathmodelineUtil.ts
38 lines (35 loc) · 1.53 KB
/
modelineUtil.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*---------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { SingleYAMLDocument } from '../parser/yamlParser07';
import { JSONDocument } from '../parser/jsonParser07';
/**
* Retrieve schema if declared as modeline.
* Public for testing purpose, not part of the API.
* @param doc
*/
export function getSchemaFromModeline(doc: SingleYAMLDocument | JSONDocument): string | undefined {
if (doc instanceof SingleYAMLDocument) {
const yamlLanguageServerModeline = doc.lineComments.find((lineComment) => {
return isModeline(lineComment);
});
if (yamlLanguageServerModeline != undefined) {
const schemaMatchs = yamlLanguageServerModeline.matchAll(/\$schema(?:=|:\s*)(\S+)/g);
const { value: schemaMatch, done } = schemaMatchs.next();
if (!done) {
if (!schemaMatchs.next().done) {
console.log(
'Several $schema attributes have been found on the yaml-language-server modeline. The first one will be picked.'
);
}
return schemaMatch[1];
}
}
}
return undefined;
}
export function isModeline(lineText: string): boolean {
const matchModeline = lineText.match(/^#\s+(?:yaml-language-server|\$schema)\s*:/g);
return matchModeline !== null && matchModeline.length === 1;
}