'use strict'; var ast = require('ts-graphviz/ast'); var prettier = require('prettier'); /* eslint-disable @typescript-eslint/no-non-null-assertion */ const DotParser = { parse(text) { return ast.parse(text); }, hasPragma(text) { try { ast.parse(text); return true; } catch { return false; } }, locStart(node) { return node.location.start.offset; }, locEnd(node) { return node.location.end.offset; }, astFormat: 'dot-ast', }; function printAttribute({ path, print }) { return [path.call(print, 'key'), '=', path.call(print, 'value'), ';']; } const { builders: { join: join$5, hardline: hardline$4, literalline }, } = prettier.doc; function printOriginal(option) { return getOriginal(option) .split('\n') .flatMap((o, i) => (i == 0 ? o : [literalline, o])); } function printBody({ path, print, options }) { const parts = []; path.each((pathChild, i, nodes) => { const prevNode = nodes[i - 1]; if (prevNode && prevNode.type === 'Comment' && prevNode.value.trim() === 'prettier-ignore') { const childNode = pathChild.getValue(); parts.push(printOriginal({ node: childNode, path: pathChild, print, options })); } else { parts.push(print(pathChild)); } }, 'children'); return join$5(hardline$4, parts); } function getOriginal({ node, options }) { return options.originalText.slice(options.locStart(node), options.locEnd(node)); } const { builders: { group: group$5, indent: indent$5, hardline: hardline$3, line: line$3 }, } = prettier.doc; function printAttributeList(option) { const { node } = option; return [group$5([node.kind, ' [']), indent$5([line$3, printBody(option)]), hardline$3, '];']; } const { builders: { join: join$4, hardline: hardline$2 }, } = prettier.doc; function printComment({ node }) { switch (node.kind) { case 'Slash': return join$4( hardline$2, node.value.split('\n').map((l) => join$4(' ', ['//', l])), ); case 'Macro': return join$4( hardline$2, node.value.split('\n').map((l) => join$4(' ', ['#', l])), ); case 'Block': return join$4(hardline$2, ['/**', ...node.value.split('\n').map((l) => join$4(' ', [' *', l])), ' */']); } } const { builders: { hardline: hardline$1 }, } = prettier.doc; function printDot(option) { return [printBody(option), hardline$1]; } const { builders: { join: join$3, indent: indent$4, group: group$4, softline: softline$4, line: line$2 }, } = prettier.doc; function getGraph(path) { let count = 0; while (true) { const node = path.getParentNode(count++); if (node === null) { return null; } else if (node.type === 'Graph') { return node; } } } function printEdge(option) { const { node, path, print } = option; return node.children.length === 0 ? [group$4([join$3(getGraph(path)?.directed ? ' -> ' : ' -- ', path.map(print, 'targets')), ';'])] : [ group$4([join$3(getGraph(path)?.directed ? ' -> ' : ' -- ', path.map(print, 'targets')), ' [']), indent$4([line$2, printBody(option)]), softline$4, '];', ]; } const { builders: { group: group$3, indent: indent$3, softline: softline$3, hardline }, } = prettier.doc; function embed(path, textToDoc, options) { const node = path.getValue(); if (node.type == 'Literal' && node.quoted === 'html') { const htmlLike = textToDoc(node.value, { ...options, parser: 'html' }); if (Array.isArray(htmlLike)) { const index = htmlLike.lastIndexOf(hardline); htmlLike.splice(index, 1); } return group$3(['<', indent$3([softline$3, htmlLike]), softline$3, '>']); } } const { builders: { softline: softline$2, group: group$2, indent: indent$2, line: line$1 }, } = prettier.doc; function printGraph(option) { const { node, path, print } = option; const parts = [ ...(node.strict ? ['strict '] : []), node.directed ? 'digraph ' : 'graph ', ...(node.id ? [path.call(print, 'id'), ' '] : []), softline$2, ]; return node.children.length === 0 ? [group$2([...parts, '{}'])] : [group$2([...parts, '{']), indent$2([line$1, printBody(option)]), softline$2, '}']; } const { makeString } = prettier.util; function printLiteral({ node }) { switch (node.quoted) { case true: return makeString(node.value, '"'); case false: return node.value; case 'html': return ['<', node.value, '>']; } } const { builders: { group: group$1, indent: indent$1, line, softline: softline$1 }, } = prettier.doc; function printNode(option) { const { node, path, print } = option; return node.children.length === 0 ? [path.call(print, 'id'), ';'] : [group$1([path.call(print, 'id'), ' [']), indent$1([line, printBody(option)]), softline$1, '];']; } const { builders: { join: join$2 }, } = prettier.doc; function printNodeRef({ node, path, print }) { return join$2(':', [ path.call(print, 'id'), ...(node.port ? [path.call(print, 'port')] : []), ...(node.compass ? [path.call(print, 'compass')] : []), ]); } const { builders: { join: join$1 }, } = prettier.doc; function printNodeRefGroup({ path, print }) { return ['{ ', join$1(' ', path.map(print, 'children')), ' }']; } const { builders: { group, ifBreak, softline, indent, join }, } = prettier.doc; const keyword = 'subgraph'; function printSubgraph(option) { const { node, path, print } = option; return getOriginal(option).slice(0, keyword.length).toLowerCase() === keyword ? node.children.length === 0 ? [node.id ? group(['subgraph ', path.call(print, 'id'), ifBreak(softline, ' '), '{}']) : 'subgraph {}'] : [ node.id ? group(['subgraph ', path.call(print, 'id'), ifBreak(softline, ' '), '{']) : 'subgraph {', indent([path.map((p) => [softline, print(p)], 'children')]), softline, '}', ] : // Short Hand ['{ ', join(' ', path.map(print, 'children')), ' }']; } const DotASTPrinter = { print(path, options, print) { const node = path.getValue(); switch (node.type) { case 'Dot': return printDot({ node, path, options, print }); case 'Graph': return printGraph({ node, path, options, print }); case 'Attribute': return printAttribute({ node, path, options, print }); case 'AttributeList': return printAttributeList({ node, path, options, print }); case 'Comment': return printComment({ node, path, options, print }); case 'Edge': return printEdge({ node, path, options, print }); case 'Node': return printNode({ node, path, options, print }); case 'Literal': return printLiteral({ node, path, options, print }); case 'NodeRef': return printNodeRef({ node, path, options, print }); case 'NodeRefGroup': return printNodeRefGroup({ node, path, options, print }); case 'Subgraph': return printSubgraph({ node, path, options, print }); } }, embed(path, print, textToDoc, options) { return embed(path, textToDoc, options) ?? null; }, }; const languages = [ { name: 'dot', parsers: ['dot-parser'], extensions: ['.dot', '.gv', '.DOT'], vscodeLanguageIds: ['dot'], }, ]; const parsers = { 'dot-parser': DotParser, }; const printers = { 'dot-ast': DotASTPrinter, }; exports.languages = languages; exports.parsers = parsers; exports.printers = printers;