Skip to content

Commit

Permalink
Change preview windows to be one per template
Browse files Browse the repository at this point in the history
Used to be one per chart.
  • Loading branch information
technosophos committed May 23, 2017
1 parent 74aefa1 commit 94f485b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 64 deletions.
65 changes: 24 additions & 41 deletions src/documentProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,51 +58,34 @@ export class HelmTemplatePreviewDocumentProvider implements vscode.TextDocumentC

public provideTextDocumentContent(uri: vscode.Uri, tok: vscode.CancellationToken): vscode.ProviderResult<string> {
return new Promise<string>((resolve, reject) => {
let editor = vscode.window.activeTextEditor
if (!editor) {
// Substitute an empty doc. This basically happens when switching back and forth between non-editor
// windows, especially when there are multiple preview windows open.
resolve(previewBody(exec.loadChartMetadata(uri.fsPath).name, "no file selected"))
return
}

let filePath = editor.document.fileName
let chartPath = uri.fsPath
let prevWin = this
let reltpl = filepath.relative(filepath.dirname(chartPath), filePath)


if (reltpl.indexOf("templates") < 0) {
// No reason to send this through the template renderer.
//resolve(editor.document.getText())
resolve(previewBody(exec.loadChartMetadata(uri.fsPath).name, "no template selected"))
return
}
// The URI is the encapsulated path to the template to render.
let tpl = uri.fsPath

console.log("templating " + reltpl)
exec.helmExec("template " + chartPath + " --execute " + reltpl, (code, out, err) => {
if (code != 0) {
resolve(previewBody("Chart Preview", "Failed template call." + err, true))
return
}
// First, we need to get the top-most chart:
exec.pickChartForFile(tpl, chartPath => {
// We need the relative path for 'helm template'
let reltpl = filepath.relative(filepath.dirname(chartPath), tpl)
exec.helmExec("template " + chartPath + " --execute " + reltpl, (code, out, err) => {
if (code != 0) {
resolve(previewBody("Chart Preview", "Failed template call." + err, true))
return
}

if (filepath.basename(reltpl) == "NOTES.txt") {
// NOTES.txt is not a YAML doc.
if (filepath.basename(reltpl) != "NOTES.txt") {
var res
try {
res = YAML.parse(out)
} catch (e) {
// TODO: Figure out the best way to display this message, but have it go away when the
// file parses correctly.
//resolve(previewBody("Chart Preview", "Invalid YAML: " + err.message, true))
vscode.window.showErrorMessage(`YAML failed to parse: ${ e.message }`)
}
}

resolve(previewBody(reltpl, out))
return
}
var res
try {
res = YAML.parse(out)
} catch (e) {
// TODO: Figure out the best way to display this message, but have it go away when the
// file parses correctly.
//resolve(previewBody("Chart Preview", "Invalid YAML: " + err.message, true))
vscode.window.showErrorMessage(`YAML failed to parse: ${ e.message }`)
}
resolve(previewBody(reltpl, out))
})
})
return
})

}
Expand Down
15 changes: 3 additions & 12 deletions src/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,10 @@ export function helmTemplatePreview() {
vscode.window.showInformationMessage("Not a template: " +filePath)
return
}
pickChartForFile(filePath, path => {
let reltpl = filepath.relative(path, filePath)
let u = vscode.Uri.parse("helm-template-preview://" + path)
let chart = loadChartMetadata(path)
vscode.commands.executeCommand("vscode.previewHtml", u, vscode.ViewColumn.Two, `${ chart.name } ${ chart.version }`)
})

let u = vscode.Uri.parse("helm-template-preview://" + filePath)
let f = filepath.basename(filePath)
vscode.commands.executeCommand("vscode.previewHtml", u, vscode.ViewColumn.Two, `Preview ${ f }`)
}

export function helmDepUp() {
Expand Down Expand Up @@ -195,12 +192,6 @@ export function pickChartForFile(file: string, fn) {
fn(filepath.join(vscode.workspace.rootPath, paths[0]))
return
}

/*
vscode.window.showQuickPick(paths).then( picked => {
fn(filepath.join(vscode.workspace.rootPath, picked))
})
*/
return
}
})
Expand Down
17 changes: 6 additions & 11 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,31 +76,26 @@ export function activate(context: vscode.ExtensionContext) {
if (e === vscode.window.activeTextEditor.document) {
let doc = vscode.window.activeTextEditor.document
if (doc.uri.scheme != "file") {
logger.log("Skipping non-file")
return
}

exec.pickChartForFile(vscode.window.activeTextEditor.document.fileName, chartPath => {
let u = vscode.Uri.parse(HELM_PREVIEW_SCHEME + "://" + chartPath);
previewProvider.update(u);
})

let u = vscode.Uri.parse(HELM_PREVIEW_SCHEME + "://" + doc.uri.fsPath);
previewProvider.update(u)
}
});
// On editor change, refresh the YAML preview
vscode.window.onDidChangeActiveTextEditor((e: vscode.TextEditor) => {
if (!editorIsActive()) {
//logger.log("No active editor")
logger.log("No active editor")
return
}
if (e.document === vscode.window.activeTextEditor.document) {
let doc = vscode.window.activeTextEditor.document
if (doc.uri.scheme != "file") {
return
}
exec.pickChartForFile(vscode.window.activeTextEditor.document.fileName, chartPath => {
let u = vscode.Uri.parse(HELM_PREVIEW_SCHEME + "://" + chartPath);
previewProvider.update(u);
})
let u = vscode.Uri.parse(HELM_PREVIEW_SCHEME + "://" + doc.uri.fsPath);
previewProvider.update(u)
}
})

Expand Down

0 comments on commit 94f485b

Please sign in to comment.