Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | /* * This file is released under the MIT license. * Copyright (c) 2017, 2022, Mike Lischke * * See LICENSE file for more info. */ import { basename, extname } from "path"; import { TextEditor, Uri, Webview } from "vscode"; import { SymbolKind } from "../../backend/types"; import { WebviewProvider, IWebviewShowOptions } from "./WebviewProvider"; import { FrontendUtils } from "../FrontendUtils"; export class RailroadDiagramProvider extends WebviewProvider { public generateContent(webview: Webview, uri: Uri, options: IWebviewShowOptions): string { const fileName = uri.fsPath; const baseName = basename(fileName, extname(fileName)); const nonce = this.generateNonce(); const scripts = [ FrontendUtils.getMiscPath("railroad-diagrams.js", this.context, webview), ]; const exportScriptPath = FrontendUtils.getOutPath("src/webview-scripts/GraphExport.js", this.context, webview); Iif (!this.currentRule || this.currentRuleIndex === undefined) { return `<!DOCTYPE html> <html> <head> ${this.generateContentSecurityPolicy(webview, nonce)} </head> <body><span style="color: #808080; font-size: 16pt;">No rule selected</span></body> </html>`; } let diagram = `<!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=UTF-8"/> ${this.generateContentSecurityPolicy(webview, nonce)} ${this.getStyles(webview)} <base href="${uri.toString(true)}"> <script nonce="${nonce}"> let graphExport; </script> </head> <body> ${this.getScripts(nonce, scripts)}`; if (options.fullList) { diagram += ` <div class="header"> <span class="rrd-color"><span class="graph-initial">Ⓡ</span>rd </span>All rules <span class="action-box"> Save to HTML<a onClick="graphExport.exportToHTML('rrd', '${baseName}');"> <span class="rrd-save-image" /> </a> </span> </div> <div id="container">`; const symbols = this.backend.listTopLevelSymbols(fileName, false); for (const symbol of symbols) { Iif (symbol.kind === SymbolKind.LexerRule || symbol.kind === SymbolKind.ParserRule || symbol.kind === SymbolKind.FragmentLexerToken) { const script = this.backend.getRRDScript(fileName, symbol.name); diagram += `<h3 class="${symbol.name}-class">${symbol.name}</h3> <script nonce="${nonce}">${script}</script>`; } } diagram += "</div>"; } else { diagram += ` <div class="header"> <span class="rrd-color"> <span class="graph-initial">Ⓡ</span>ule </span> ${this.currentRule} <span class="rule-index">(rule index: ${this.currentRuleIndex}) </span> <span class="action-box"> Save to SVG <a onClick="graphExport.exportToSVG('rrd', '${this.currentRule}');"> <span class="rrd-save-image" /> </a> </span> </div> <div id="container"> <script nonce="${nonce}" >${this.backend.getRRDScript(fileName, this.currentRule)}</script> </div>`; } diagram += ` <script nonce="${nonce}" type="module"> import { GraphExport } from "${exportScriptPath}"; graphExport = new GraphExport(); </script> </body></html>`; return diagram; } /** * Called when the webview must be updated. This can happen when: * - The user switched to another editor, which holds an ANTLR4 grammar. * - The user moved the caret in an editor holding a grammar. * - New data was generated for the grammar in that editor. * * @param editor The editor that holds a grammar. * @param forced If true update regardless of the selected rule (e.g. when new ATN data was generated). */ public update(editor: TextEditor, forced = false): void { // Keep track of the currently selected rule in the given editor and trigger a visual update // if the ATN graph is currently visible. const caret = editor.selection.active; const [rule, index] = this.backend.ruleFromPosition(editor.document.fileName, caret.character, caret.line + 1); Iif (!rule || index === undefined) { super.update(editor); return; } Iif (this.currentRule !== rule || this.currentRuleIndex !== index || forced) { this.currentRule = rule; this.currentRuleIndex = index; super.update(editor); } } } |