UNPKG

yaml-language-server

Version:
97 lines 4.78 kB
/*--------------------------------------------------------------------------------------------- * Copyright (c) Red Hat, Inc. All rights reserved. * Copyright (c) Adam Voss. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ (function (factory) { if (typeof module === "object" && typeof module.exports === "object") { var v = factory(require, exports); if (v !== undefined) module.exports = v; } else if (typeof define === "function" && define.amd) { define(["require", "exports", "yaml", "./yaml-documents", "./custom-tag-provider", "../utils/textBuffer"], factory); } })(function (require, exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.defaultOptions = exports.SingleYAMLDocument = exports.YAMLDocument = void 0; exports.parse = parse; const yaml_1 = require("yaml"); const yaml_documents_1 = require("./yaml-documents"); Object.defineProperty(exports, "YAMLDocument", { enumerable: true, get: function () { return yaml_documents_1.YAMLDocument; } }); Object.defineProperty(exports, "SingleYAMLDocument", { enumerable: true, get: function () { return yaml_documents_1.SingleYAMLDocument; } }); const custom_tag_provider_1 = require("./custom-tag-provider"); const textBuffer_1 = require("../utils/textBuffer"); exports.defaultOptions = { customTags: [], yamlVersion: '1.2', }; /** * `yaml-ast-parser-custom-tags` parses the AST and * returns YAML AST nodes, which are then formatted * for consumption via the language server. */ function parse(text, parserOptions = exports.defaultOptions, document) { const options = { strict: false, customTags: (0, custom_tag_provider_1.getCustomTags)(parserOptions.customTags), version: parserOptions.yamlVersion ?? exports.defaultOptions.yamlVersion, keepSourceTokens: true, }; const composer = new yaml_1.Composer(options); const lineCounter = new yaml_1.LineCounter(); let isLastLineEmpty = false; if (document) { const textBuffer = new textBuffer_1.TextBuffer(document); const position = textBuffer.getPosition(text.length); const lineContent = textBuffer.getLineContent(position.line); isLastLineEmpty = lineContent.trim().length === 0; } const parser = isLastLineEmpty ? new yaml_1.Parser() : new yaml_1.Parser(lineCounter.addNewLine); const tokens = parser.parse(text); const tokensArr = Array.from(tokens); const docs = composer.compose(tokensArr, true, text.length); const documentHeaderComments = getDocumentHeaderComments(tokensArr); // Generate the SingleYAMLDocs from the AST nodes const yamlDocs = Array.from(docs, (doc, index) => parsedDocToSingleYAMLDocument(doc, lineCounter, documentHeaderComments[index] ?? [])); // Consolidate the SingleYAMLDocs return new yaml_documents_1.YAMLDocument(yamlDocs, tokensArr); } function parsedDocToSingleYAMLDocument(parsedDoc, lineCounter, documentHeaderComments) { const syd = new yaml_documents_1.SingleYAMLDocument(lineCounter); syd.documentHeaderComments = documentHeaderComments; syd.internalDocument = parsedDoc; return syd; } function getDocumentHeaderComments(tokens) { const documentHeaderComments = []; let pendingComments = []; for (const token of tokens) { if (token.type === 'comment') { pendingComments.push(token.source); continue; } if (token.type === 'newline' || token.type === 'space' || token.type === 'byte-order-mark' || token.type === 'directive') { continue; } if (token.type === 'doc-end') { pendingComments = []; continue; } if (token.type === 'document') { const startComments = token.start .filter((startToken) => startToken.type === 'comment') .map((startToken) => startToken.source); documentHeaderComments.push([...pendingComments, ...startComments]); pendingComments = []; continue; } pendingComments = []; } if (pendingComments.length > 0) { documentHeaderComments.push(pendingComments); } return documentHeaderComments; } }); //# sourceMappingURL=yamlParser07.js.map