Skip to content

Commit

Permalink
Fix issue where clicking on JSON/XML cell values opened a blank edito…
Browse files Browse the repository at this point in the history
…r window (microsoft#841)

- Fix issue where clicking on a JSON or XML cell value link would open a blank document instead of a document containing the JSON or XML text

- Also update the open document functionality to open them as untitled documents instead of generating a temporary filename
  • Loading branch information
MattIrv authored Apr 27, 2017
1 parent 1fdf1ee commit 0ad7a4d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 32 deletions.
25 changes: 1 addition & 24 deletions src/models/SqlOutputContentProvider.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';
import vscode = require('vscode');
import path = require('path');
import os = require('os');
import Constants = require('../constants/constants');
import LocalizedConstants = require('../constants/localizedConstants');
import LocalWebService from '../controllers/localWebService';
Expand Down Expand Up @@ -35,7 +34,6 @@ export class SqlOutputContentProvider implements vscode.TextDocumentContentProvi
// CONSTANTS ///////////////////////////////////////////////////////////
public static providerName = 'tsqloutput';
public static providerUri = vscode.Uri.parse('tsqloutput://');
public static tempFileCount: number = 1;

// MEMBER VARIABLES ////////////////////////////////////////////////////
private _queryResultsMap: Map<string, QueryRunnerState> = new Map<string, QueryRunnerState>();
Expand Down Expand Up @@ -454,8 +452,6 @@ export class SqlOutputContentProvider implements vscode.TextDocumentContentProvi
*/
public openLink(content: string, columnName: string, linkType: string): void {
const self = this;
let tempFileName = self.getXmlTempFileName(columnName, linkType);
let uri = vscode.Uri.parse('untitled:' + tempFileName);
if (linkType === 'xml') {
try {
content = pd.xml(content);
Expand All @@ -475,7 +471,7 @@ export class SqlOutputContentProvider implements vscode.TextDocumentContentProvi
}
}

vscode.workspace.openTextDocument(uri).then((doc: vscode.TextDocument) => {
vscode.workspace.openTextDocument({ language: linkType }).then((doc: vscode.TextDocument) => {
vscode.window.showTextDocument(doc, 1, false).then(editor => {
editor.edit(edit => {
edit.insert(new vscode.Position(0, 0), content);
Expand Down Expand Up @@ -517,25 +513,6 @@ export class SqlOutputContentProvider implements vscode.TextDocumentContentProvi
return vscode.Uri.parse(SqlOutputContentProvider.providerUri + srcUri).toString();
}

/**
* Return temp file name for opening a link
*/
private getXmlTempFileName(columnName: string, linkType: string): string {
if (columnName === 'XML Showplan') {
columnName = 'Showplan';
}
let baseFileName = columnName + '-';
let retryCount: number = 200;
for (let i = 0; i < retryCount; i++) {
let tempFileName = path.join(os.tmpdir(), baseFileName + SqlOutputContentProvider.tempFileCount + '.' + linkType);
SqlOutputContentProvider.tempFileCount++;
if (!Utils.isFileExisting(tempFileName)) {
return tempFileName;
}
}
return path.join(os.tmpdir(), columnName + '_' + String(Math.floor( Date.now() / 1000)) + String(process.pid) + '.' + linkType);
}

/**
* Returns whether or not a result pane with the same URI exists
* @param The string value of a Uri.
Expand Down
30 changes: 22 additions & 8 deletions src/views/htmlcontent/src/js/components/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,24 +537,39 @@ export class AppComponent implements OnInit, AfterViewChecked {
* Add handler for clicking on xml link
*/
xmlLinkHandler = (cellRef: string, row: number, dataContext: JSON, colDef: any) => {
const self = this;
let value = dataContext[colDef.field];
$(cellRef).children('.xmlLink').click(function(): void {
self.dataService.openLink(value, colDef.name, 'xml');
});
this.handleLink(cellRef, row, dataContext, colDef, 'xml');
}

/**
* Add handler for clicking on json link
*/
jsonLinkHandler = (cellRef: string, row: number, dataContext: JSON, colDef: any) => {
this.handleLink(cellRef, row, dataContext, colDef, 'json');
}

private handleLink(cellRef: string, row: number, dataContext: JSON, colDef: any, linkType: string): void {
const self = this;
let value = dataContext[colDef.field];
let value = self.getCellValueString(dataContext, colDef);
$(cellRef).children('.xmlLink').click(function(): void {
self.dataService.openLink(value, colDef.name, 'json');
self.dataService.openLink(value, colDef.name, linkType);
});
}

private getCellValueString(dataContext: JSON, colDef: any): string {
let returnVal = '';
if (!dataContext) {
return returnVal;
}

let value = dataContext[colDef.field];
if (Utils.isDbCellValue(value)) {
returnVal = value.displayValue;
} else if (typeof value === 'string') {
returnVal = value;
}
return returnVal;
}

/**
* Return asyncPostRender handler based on type
*/
Expand All @@ -564,7 +579,6 @@ export class AppComponent implements OnInit, AfterViewChecked {
} else if (type === 'json') {
return this.jsonLinkHandler;
}

}

/**
Expand Down

0 comments on commit 0ad7a4d

Please sign in to comment.