Home / Function/ create() — astro Function Reference

create() — astro Function Reference

Architecture documentation for the create() function in yaml.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  1420ef9d_e0ca_f05c_ef82_425d7b4326a0["create()"]
  5e9b95b7_b879_26fe_0542_7582ae13ba27["yaml.ts"]
  1420ef9d_e0ca_f05c_ef82_425d7b4326a0 -->|defined in| 5e9b95b7_b879_26fe_0542_7582ae13ba27
  8cf0491b_0fe2_bfaa_a91f_3ea661d10308["getSettings()"]
  1420ef9d_e0ca_f05c_ef82_425d7b4326a0 -->|calls| 8cf0491b_0fe2_bfaa_a91f_3ea661d10308
  style 1420ef9d_e0ca_f05c_ef82_425d7b4326a0 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/language-tools/language-server/src/plugins/yaml.ts lines 44–148

export const create = (collectionConfig: CollectionConfig): LanguageServicePlugin => {
	const yamlPlugin = createYAMLService({
		getLanguageSettings: () => getSettings(collectionConfig),
	}) as LanguageServicePlugin<Provide>;

	return {
		...yamlPlugin,
		capabilities: {
			...yamlPlugin.capabilities,
			codeLensProvider: undefined,
		},
		create(context) {
			const yamlPluginInstance = yamlPlugin.create(context);

			const languageService = yamlPluginInstance.provide?.['yaml/languageService']();
			if (languageService && context.env.onDidChangeWatchedFiles) {
				context.env.onDidChangeWatchedFiles(async (events) => {
					const changedSchemas = events.changes.filter((change) =>
						change.uri.endsWith('.schema.json'),
					);
					const changedConfig = events.changes.some((change) =>
						change.uri.endsWith('collections.json'),
					);

					if (changedConfig) {
						collectionConfig.reload(
							// For some reason, context.env.workspaceFolders is not an array of WorkspaceFolders nor the older format, strange
							context.env.workspaceFolders.map((folder) => ({ uri: folder.toString() })),
						);
						languageService.configure(getSettings(collectionConfig));
					}

					for (const change of changedSchemas) {
						languageService.resetSchema(change.uri);
					}
				});
			}

			return {
				...yamlPluginInstance,
				// Disable codelenses, we'll provide our own
				provideCodeLenses: undefined,
				resolveCodeLens: undefined,
				async provideDiagnostics(document, token) {
					const originalDiagnostics = await yamlPluginInstance.provideDiagnostics!(document, token);
					if (!originalDiagnostics) {
						return null;
					}

					const decoded = context.decodeEmbeddedDocumentUri(URI.parse(document.uri));
					const sourceScript = decoded && context.language.scripts.get(decoded[0]);
					const root = sourceScript?.generated?.root;
					if (!(root instanceof FrontmatterHolder)) return undefined;

					// If we don't have a frontmatter, but there are errors, it probably means a frontmatter was required
					if (!root.hasFrontmatter && originalDiagnostics.length > 0) {
						return [
							Diagnostic.create(
								Range.create(0, 0, 0, 0),
								'Frontmatter is required for this file.',
								DiagnosticSeverity.Error,
							),
						];
					}

					return originalDiagnostics.map((diagnostic) => {
						// The YAML schema source is not useful to users, since it's generated. Also, it's quite long.
						if (diagnostic.source?.startsWith('yaml-schema:')) {
							diagnostic.source = 'astro';

							// In Astro, schema errors are always fatal
							diagnostic.severity = DiagnosticSeverity.Error;

							// Map missing properties to the entire frontmatte
							if (diagnostic.message.startsWith('Missing property')) {
								diagnostic.range = Range.create(
									{ line: 0, character: 0 },
									document.positionAt(document.getText().length),
								);
							}
						}

Domain

Subdomains

Frequently Asked Questions

What does create() do?
create() is a function in the astro codebase, defined in packages/language-tools/language-server/src/plugins/yaml.ts.
Where is create() defined?
create() is defined in packages/language-tools/language-server/src/plugins/yaml.ts at line 44.
What does create() call?
create() calls 1 function(s): getSettings.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free