Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

Commit

Permalink
Add parse command to context menu (#124)
Browse files Browse the repository at this point in the history
* Add parse command to context menu

Signed-off-by: Ayman161803 <[email protected]>

* Add webview to take in user input for parse command

Signed-off-by: Ayman161803 <[email protected]>
  • Loading branch information
Ayman161803 authored Jun 15, 2022
1 parent dda3072 commit a6b0b54
Show file tree
Hide file tree
Showing 4 changed files with 257 additions and 8 deletions.
229 changes: 226 additions & 3 deletions client/src/commandHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.';

Expand Down Expand Up @@ -353,9 +421,164 @@ ${result.mermaid}
return html;
}

function getParseWebviewContent(samplePath){

const defaultOutPath = samplePath+".json";
const html = getParseWebviewHtml(samplePath,defaultOutPath);

const styles = `<style>
* {
box-sizing: border-box;
}
input, select, textarea {
width: 80%;
padding: 12px;
border-radius: 4px;
resize: vertical;
}
label {
padding: 12px 12px 12px 0;
display: inline-block;
}
button {
background-color: #04AA6D;
color: white;
padding: 12px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
float: left;
}
button:hover {
background-color: #45a049;
}
.container {
border-radius: 5px;
padding: 20px;
}
.col-25 {
float: left;
width: 25%;
margin-top: 6px;
}
.col-75 {
float: left;
width: 75%;
margin-top: 6px;
}
/* Clear floats after the columns */
.row:after {
content: "";
display: table;
clear: both;
}
.button{
width: 30%;
}
.button-container{
text-align: center;
}
@media screen and (max-width: 600px) {
input[type=submit] {
width: 100%;
margin-top: 0;
}
}
</style>`

return `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Accord Project</title>
<style>${styles}</style>
</head>
<body>
${html}
</body>
<script>
let btn = document.getElementById("button");
const vscode = acquireVsCodeApi();
btn.addEventListener('click', event => {
vscode.postMessage({
command: 'parse',
outputPath: document.getElementById("outputPath").value,
samplePath: document.getElementById("samplePath").value,
utcOffset: document.getElementById("utcOffset").value,
currentTime: document.getElementById("currentTime").value,
})
});
</script>
</html>`;
}

function getParseWebviewHtml(defaultSamplePath,defaultOutPath){

return `
<div class="container">
<div class="row">
<h4>*Paths mentioned here are relative to the template directory</h4>
</div>
<div class="row">
<div class="col-25">
<label for="samplePath">Sample Path</label>
</div>
<div class="col-75">
<input type="text" id="samplePath" name="samplePath" placeholder="Choose Sample Path" value=${defaultSamplePath} >
</div>
</div>
<br>
<div class="row">
<div class="col-25">
<label for="outputPath">Ouput Path</label>
</div>
<div class="col-75">
<input type="text" id="outputPath" name="outputPath" placeholder="Choose Output Path" value=${defaultOutPath}>
</div>
</div>
<br>
<div class="row">
<div class="col-25">
<label for="utcOffset">UTC Offset</label>
</div>
<div class="col-75">
<input type="number" id="utcOffset" name="utcOffset" placeholder="UTC Offset" value="0">
</div>
</div>
<br>
<div class="row">
<div class="col-25">
<label for="currentTime">Current Time</label>
</div>
<div class="col-75">
<input type="text" id="currentTime" name="currentTime" placeholder="Current Time">
</div>
</div>
<br>
<r>
<br>
<div class="row">
<button type="submit" id="button" class="button">Parse</button>
</div>
</div>`

}

export async function getWebviewContent() {
const html = await getHtml();
export async function getPreviewWebviewContent() {
const html = await getPreviewHtml();

const styles = `
table, th, td {
Expand Down
11 changes: 7 additions & 4 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -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(
Expand Down
8 changes: 8 additions & 0 deletions client/src/test/suite/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 16 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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": [
Expand All @@ -181,6 +192,10 @@
{
"command": "cicero-vscode-extension.triggerClause",
"when": "false"
},
{
"command": "cicero-vscode-extension.parseClause",
"when": "false"
}
]
}
Expand Down

0 comments on commit a6b0b54

Please sign in to comment.