Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ The following settings are supported:
- `yaml.schemas`: Helps you associate schemas with files in a glob pattern
- `yaml.schemaStore.enable`: When set to true the YAML language server will pull in all available schemas from [JSON Schema Store](https://www.schemastore.org/json/)
- `yaml.schemaStore.url`: URL of a schema store catalog to use when downloading schemas.
- `yaml.shouldIgnoreModelineSchema`: Enable/disable ignore of # yaml-language-server: $schema:path
- `yaml.customTags`: Array of custom tags that the parser will validate against. It has two ways to be used. Either an item in the array is a custom tag such as "!Ref" and it will automatically map !Ref to scalar or you can specify the type of the object !Ref should be e.g. "!Ref sequence". The type of object can be either scalar (for strings and booleans), sequence (for arrays), map (for objects).
- `yaml.maxItemsComputed`: The maximum number of outline symbols and folding regions computed (limited for performance reasons).
- `[yaml].editor.tabSize`: the number of spaces to use when autocompleting. Takes priority over editor.tabSize.
Expand Down
2 changes: 2 additions & 0 deletions src/languageserver/handlers/settingsHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ export class SettingsHandler {
}
this.yamlSettings.yamlVersion = settings.yaml.yamlVersion ?? '1.2';

this.yamlSettings.shouldIgnoreModelineSchema = settings.yaml.shouldIgnoreModelineSchema ?? false;

if (settings.yaml.format) {
this.yamlSettings.yamlFormatterSettings = {
proseWrap: settings.yaml.format.proseWrap || 'preserve',
Expand Down
7 changes: 5 additions & 2 deletions src/languageservice/services/yamlSchemaService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,19 +108,22 @@ export class YAMLSchemaService extends JSONSchemaService {
private filePatternAssociations: JSONSchemaService.FilePatternAssociation[];
private contextService: WorkspaceContextService;
private requestService: SchemaRequestService;
private shouldIgnoreModelineSchema: boolean | undefined;
public schemaPriorityMapping: Map<string, Set<SchemaPriority>>;

private schemaUriToNameAndDescription = new Map<string, SchemaStoreSchema>();

constructor(
requestService: SchemaRequestService,
contextService?: WorkspaceContextService,
promiseConstructor?: PromiseConstructor
promiseConstructor?: PromiseConstructor,
shouldIgnoreModelineSchema?: boolean
) {
super(requestService, contextService, promiseConstructor);
this.customSchemaProvider = undefined;
this.requestService = requestService;
this.schemaPriorityMapping = new Map();
this.shouldIgnoreModelineSchema = shouldIgnoreModelineSchema;
}

registerCustomSchemaProvider(customSchemaProvider: CustomSchemaProvider): void {
Expand Down Expand Up @@ -412,7 +415,7 @@ export class YAMLSchemaService extends JSONSchemaService {

return Promise.resolve(null);
};
const modelineSchema = resolveModelineSchema();
const modelineSchema = !this.shouldIgnoreModelineSchema && resolveModelineSchema();
if (modelineSchema) {
return resolveSchemaForResource([modelineSchema]);
}
Expand Down
7 changes: 6 additions & 1 deletion src/languageservice/yamlLanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,12 @@ export function getLanguageService(params: {
yamlSettings?: SettingsState;
clientCapabilities?: ClientCapabilities;
}): LanguageService {
const schemaService = new YAMLSchemaService(params.schemaRequestService, params.workspaceContext);
const schemaService = new YAMLSchemaService(
params.schemaRequestService,
params.workspaceContext,
undefined,
params.yamlSettings?.shouldIgnoreModelineSchema
);
const completer = new YamlCompletion(schemaService, params.clientCapabilities, yamlDocumentsCache, params.telemetry);
const hover = new YAMLHover(schemaService, params.telemetry);
const yamlDocumentSymbols = new YAMLDocumentSymbols(schemaService, params.telemetry);
Expand Down
2 changes: 2 additions & 0 deletions src/yamlSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface Settings {
keyOrdering: boolean;
maxItemsComputed: number;
yamlVersion: YamlVersion;
shouldIgnoreModelineSchema: boolean;
};
http: {
proxy: string;
Expand Down Expand Up @@ -111,6 +112,7 @@ export class SettingsState {
useSchemaSelectionRequests = false;
hasWsChangeWatchedFileDynamicRegistration = false;
fileExtensions: string[] = ['.yml', '.yaml'];
shouldIgnoreModelineSchema = false;
}

export class TextDocumentTestManager extends TextDocuments<TextDocument> {
Expand Down
11 changes: 11 additions & 0 deletions test/yamlSchemaService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,16 @@ describe('YAML Schema Service', () => {

expect(requestServiceMock).calledOnceWith('https://json-schema.org/draft-07/schema#');
});

it('should ignore modeline schema', () => {
const documentContent = `foo:\n bar\n#first comment\n# yaml-language-server: $schema=https://json-schema.org/draft-07/schema#\naa:bbb\n`;
const content = `${documentContent}`;
const yamlDock = parse(content);

const service = new SchemaService.YAMLSchemaService(requestServiceMock, undefined, undefined, true);
service.getSchemaForResource('', yamlDock.documents[0]);

expect(requestServiceMock).callCount(0)
});
});
});