diff --git a/client/src/commandHandlers.ts b/client/src/commandHandlers.ts index 9d612ff..84c2cb0 100755 --- a/client/src/commandHandlers.ts +++ b/client/src/commandHandlers.ts @@ -307,7 +307,75 @@ export async function triggerClause(file: vscode.Uri) { return false; } -async function getHtml() { +function getProjectRoot(pathStr) { + + let currentPath = pathStr; + + while(currentPath !== '/' && currentPath.split(":").pop() !== '\\') { + // connection.console.log( `- ${currentPath}`); + + try{ + if(fs.existsSync(currentPath + '/package.json')) + return currentPath; + } + catch(err) { + // connection.console.log( `- exception ${err}`); + } + currentPath = path.normalize(path.join(currentPath, '..')); + } + return path.basename(path.dirname(pathStr)); +} + +export async function parseClause(file: vscode.Uri) { + try { + const templateDirectory = getProjectRoot(file.fsPath); + await vscode.workspace.saveAll(); + + var panel = vscode.window.createWebviewPanel( + 'parseInput', + 'Parse Input', + vscode.ViewColumn.Beside, + { + // Enable scripts in the webview + enableScripts: true + } + ); + + panel.webview.html = await getParseWebviewContent(path.relative(templateDirectory,file.path)); + + panel.webview.onDidReceiveMessage( + async (message) => { + const {samplePath,outputPath,utcOffset,currentTime} = message; + const template = await Template.fromDirectory(templateDirectory); + const clause = new Clause(template); + const sampleText = fs.readFileSync(path.resolve(templateDirectory,samplePath), 'utf8'); + + clause.parse(sampleText, currentTime, utcOffset, path.resolve(templateDirectory,samplePath)); + + outputChannel.show(); + + outputChannel.appendLine(`${samplePath} parse result`); + outputChannel.appendLine('======================'); + outputChannel.appendLine(JSON.stringify(clause.getData(),null,2)); + outputChannel.appendLine(''); + + + fs.writeFileSync( path.resolve(templateDirectory,outputPath), JSON.stringify(clause.getData(),null,2)); + outputChannel.appendLine(`Output written to ${outputPath}`); + outputChannel.appendLine(''); + }, + null + ) + + return true; + } catch (error) { + vscode.window.showErrorMessage( `Failed to parse clause ${error}`); + } + + return false; +} + +async function getPreviewHtml() { let html = 'To display preview please open a grammar.tem.md or a *.cto file.'; @@ -353,9 +421,164 @@ ${result.mermaid} return html; } +function getParseWebviewContent(samplePath){ + + const defaultOutPath = samplePath+".json"; + const html = getParseWebviewHtml(samplePath,defaultOutPath); + + const styles = `` + + return ` + + + + + Accord Project + + + + ${html} + + + `; +} + +function getParseWebviewHtml(defaultSamplePath,defaultOutPath){ + + return ` +
+
+

*Paths mentioned here are relative to the template directory

+
+
+
+ +
+
+ +
+
+
+
+
+ +
+
+ +
+
+
+
+
+ +
+
+ +
+
+
+
+
+ +
+
+ +
+
+
+ +
+
+ +
+
` + +} -export async function getWebviewContent() { - const html = await getHtml(); +export async function getPreviewWebviewContent() { + const html = await getPreviewHtml(); const styles = ` table, th, td { diff --git a/client/src/extension.ts b/client/src/extension.ts index 902f3cd..0352039 100755 --- a/client/src/extension.ts +++ b/client/src/extension.ts @@ -28,15 +28,16 @@ import { downloadModels, exportClassDiagram, triggerClause, - getWebviewContent, - setOutputChannel + getPreviewWebviewContent, + setOutputChannel, + parseClause } from './commandHandlers'; let client: LanguageClient; async function onDocumentChange(event) { if (event.document.languageId === 'ciceroMark' || event.document.languageId === 'concerto') { - return getWebviewContent();; + return getPreviewWebviewContent();; } return null; @@ -111,6 +112,8 @@ export function activate(context: vscode.ExtensionContext) { .registerCommand('cicero-vscode-extension.exportClassDiagram', exportClassDiagram)); context.subscriptions.push(vscode.commands .registerCommand('cicero-vscode-extension.triggerClause', triggerClause)); + context.subscriptions.push(vscode.commands + .registerCommand('cicero-vscode-extension.parseClause', parseClause)); let currentPanel: vscode.WebviewPanel | undefined = undefined; @@ -131,7 +134,7 @@ export function activate(context: vscode.ExtensionContext) { } ); - currentPanel.webview.html = await getWebviewContent(); + currentPanel.webview.html = await getPreviewWebviewContent(); // Reset when the current panel is closed currentPanel.onDidDispose( diff --git a/client/src/test/suite/extension.test.ts b/client/src/test/suite/extension.test.ts index 5298f8d..16d755f 100755 --- a/client/src/test/suite/extension.test.ts +++ b/client/src/test/suite/extension.test.ts @@ -81,6 +81,14 @@ suite('Extension Tests', () => { }); }); + test('should execute parseClause command', () => { + const uri = vscode.Uri.file(path.join(rootPath, '../test/data/valid/template/acceptance-of-delivery')); + + vscode.commands.executeCommand('cicero-vscode-extension.parseClause', uri).then((result) => { + assert.ok(result); + }); + }); + test('should execute showPreview command', () => { vscode.commands.executeCommand('cicero-vscode-extension.showPreview').then((result) => { assert.ok(result); diff --git a/package.json b/package.json index 4a82ed4..6497813 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "onCommand:cicero-vscode-extension.downloadModels", "onCommand:cicero-vscode-extension.viewClassDiagram", "onCommand:cicero-vscode-extension.triggerClause", + "onCommand:cicero-vscode-extension.parseClause", "onCommand:cicero-vscode-extension.showPreview" ], "main": "./client/out/extension", @@ -120,6 +121,11 @@ "title": "Trigger", "category": "Accord Project" }, + { + "command": "cicero-vscode-extension.parseClause", + "title": "Parse", + "category": "Accord Project" + }, { "command": "cicero-vscode-extension.showPreview", "title": "Open Preview", @@ -152,10 +158,15 @@ "command": "cicero-vscode-extension.triggerClause", "group": "AccordProject@4" }, + { + "when": "resourceLangId == ciceroMark || resourceLangId == markdown", + "command": "cicero-vscode-extension.parseClause", + "group": "AccordProject@5" + }, { "when": "resourceLangId == ciceroMark || resourceLangId == concerto", "command": "cicero-vscode-extension.showPreview", - "group": "AccordProject@5" + "group": "AccordProject@6" } ], "editor/title": [ @@ -181,6 +192,10 @@ { "command": "cicero-vscode-extension.triggerClause", "when": "false" + }, + { + "command": "cicero-vscode-extension.parseClause", + "when": "false" } ] }