Skip to content

Commit

Permalink
Fix #1213 reopening uri after 3min
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen Weatherford committed Feb 27, 2021
1 parent a32ab86 commit 11f65d0
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 30 deletions.
17 changes: 12 additions & 5 deletions src/AzureRMTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { getNormalizedDocumentKey } from './documents/templates/getNormalizedDoc
import { gotoResources } from './documents/templates/gotoResources';
import { getItemTypeQuickPicks, InsertItem } from "./documents/templates/insertItem";
import { LinkedFileLoadState } from './documents/templates/linkedTemplates/LinkedFileLoadState';
import { assignTemplateGraphToDeploymentTemplate, INotifyTemplateGraphArgs, openLinkedTemplateFile } from './documents/templates/linkedTemplates/linkedTemplates';
import { assignTemplateGraphToDeploymentTemplate, INotifyTemplateGraphArgs, openLinkedTemplateFile, tryOpenNonLocalLinkedFile } from './documents/templates/linkedTemplates/linkedTemplates';
import { allSchemas, getPreferredSchema } from './documents/templates/schemas';
import { getQuickPickItems, sortTemplate } from "./documents/templates/sortTemplate";
import { mightBeDeploymentParameters, mightBeDeploymentTemplate, setLangIdToArm, templateDocumentSelector, templateOrParameterDocumentSelector } from "./documents/templates/supported";
Expand Down Expand Up @@ -1017,7 +1017,7 @@ export class AzureRMTools implements IProvideOpenedDocuments {

const linkedTemplateDocumentProvider: vscode.TextDocumentContentProvider = {
provideTextDocumentContent: async (uri: vscode.Uri, _token: vscode.CancellationToken): Promise<string | undefined> => {
return this.provideContentForNonlocalUri(uri);
return await this.provideContentForNonlocalUri(uri);
}
};
ext.context.subscriptions.push(
Expand All @@ -1040,8 +1040,15 @@ export class AzureRMTools implements IProvideOpenedDocuments {
* linked-template:https%3A//raw.githubusercontent.com/StephenWeatherford/template-examples/master/linkedTemplates/uri/child.json
*/
private async provideContentForNonlocalUri(uri: vscode.Uri): Promise<string | undefined> {
return callWithTelemetryAndErrorHandlingSync('provideContentForNonlocalUris', () => {
const dt = this.getOpenedDeploymentDocument(uri);
return callWithTelemetryAndErrorHandling('provideContentForNonlocalUris', async (context: IActionContext) => {
context.errorHandling.rethrow = true;

let dt = this.getOpenedDeploymentDocument(uri);
if (!dt) {
await tryOpenNonLocalLinkedFile(uri, context, false); //asdf handle linked-template:
dt = this.getOpenedDeploymentDocument(uri);
}

return dt?.documentText;
});
}
Expand Down Expand Up @@ -1077,7 +1084,7 @@ export class AzureRMTools implements IProvideOpenedDocuments {
const fullValidationOn = deploymentTemplate.templateGraph?.fullValidationStatus.fullValidationEnabled ?? templateFileHasParamFile;
isWarning = !fullValidationOn;
statusBarText = isWarning ?
`$(warning) WARNING: Full template validation off. Add parameter file or top-level default values to enable.` :
`$(warning) WARNING: Full template validation off. Add parameter file or top-level default values to parameters to enable.` :
statusBarText;

this._paramsStatusBarItem.command = "azurerm-vscode-tools.selectParameterFile";
Expand Down
2 changes: 1 addition & 1 deletion src/documents/templates/getNormalizedDocumentKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ import { removeLinkedTemplateScheme } from "../../util/prependLinkedTemplateSche
export function getNormalizedDocumentKey(documentUri: vscode.Uri): string {
// We want a normalized file path to use as key, but also need to differentiate documents with different URI schemes
const uri = removeLinkedTemplateScheme(documentUri);
return `${uri.scheme}|${normalizePath(uri)}`;
return normalizePath(uri);
}
34 changes: 21 additions & 13 deletions src/documents/templates/linkedTemplates/linkedTemplates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { httpGet } from '../../../util/httpGet';
import { normalizePath } from '../../../util/normalizePath';
import { ofType } from '../../../util/ofType';
import { pathExists } from '../../../util/pathExists';
import { prependLinkedTemplateScheme } from '../../../util/prependLinkedTemplateScheme';
import { prependLinkedTemplateScheme, removeLinkedTemplateScheme } from '../../../util/prependLinkedTemplateScheme';
import { DeploymentTemplateDoc } from '../../templates/DeploymentTemplateDoc';
import { LinkedTemplateScope } from '../../templates/scopes/templateScopes';
import { setLangIdToArm } from '../../templates/supported';
Expand Down Expand Up @@ -111,18 +111,7 @@ export async function onRequestOpenLinkedFile(
} else {
// Something else (http etc). Try to retrieve the content and return it directly
try {
const content = await httpGet(requestedLinkUri.toString());
assert(ext.provideOpenedDocuments, "ext.provideOpenedDocuments");
const newUri = prependLinkedTemplateScheme(requestedLinkUri);

// We need to place it into our docs immediately because our text document content provider will be queried
// for content before we get the document open event
const dt = new DeploymentTemplateDoc(content, newUri, 0);
ext.provideOpenedDocuments.setOpenedDeploymentDocument(newUri, dt);

const doc = await workspace.openTextDocument(newUri);
setLangIdToArm(doc, context);

const content = await tryOpenNonLocalLinkedFile(requestedLinkUri, context, true);
return { content };
} catch (error) {
return { loadErrorMessage: parseError(error).message };
Expand All @@ -131,6 +120,25 @@ export async function onRequestOpenLinkedFile(
});
}

export async function tryOpenNonLocalLinkedFile(uri: Uri, context: IActionContext, open: boolean): Promise<string> {
uri = removeLinkedTemplateScheme(uri);
const content = await httpGet(uri.toString());
assert(ext.provideOpenedDocuments, "ext.provideOpenedDocuments");
const newUri = prependLinkedTemplateScheme(uri);

// We need to place it into our docs immediately because our text document content provider will be queried
// for content before we get the document open event
const dt = new DeploymentTemplateDoc(content, newUri, 0);
ext.provideOpenedDocuments.setOpenedDeploymentDocument(newUri, dt);

if (open) {
const doc = await workspace.openTextDocument(newUri);
setLangIdToArm(doc, context);
}

return content;
}

/**
* Attempts to load the given file into a text document in VS Code so that
* it will get sent to the language server.
Expand Down
22 changes: 12 additions & 10 deletions src/util/normalizePath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@

import * as path from 'path';
import { Uri } from "vscode";
import { isWin32 } from '../constants';
import { documentSchemes, isWin32 } from '../constants';

export function normalizePath(filePath: Uri | string): string {
const suffix: string = (typeof filePath === 'string' || !filePath.query)
? ''
: `?${filePath.query}`;
export function normalizePath(pathOrUri: Uri | string): string {
if (pathOrUri instanceof Uri && pathOrUri.scheme === documentSchemes.file) {
pathOrUri = pathOrUri.fsPath;
}

const fsPath: string = typeof filePath === 'string' ? filePath : filePath.fsPath;
let normalizedPath = path.normalize(fsPath);
if (isWin32) {
normalizedPath = normalizedPath.toLowerCase();
if (typeof pathOrUri === 'string') {
let normalizedPath = path.normalize(pathOrUri);
if (isWin32) {
normalizedPath = normalizedPath.toLowerCase();
}
pathOrUri = Uri.parse(normalizedPath);
}

return normalizedPath + suffix;
return pathOrUri.toString();
}
2 changes: 1 addition & 1 deletion src/vscodeIntegration/Treeview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ export class JsonOutlineProvider implements vscode.TreeDataProvider<IElementInfo
const activeTextEditor = vscode.window.activeTextEditor;
// tslint:disable-next-line: no-suspicious-comment
// TODO: Shouldn't be using activeTextEditor (can be null) - store in IElementInfo?
assert(activeTextEditor);
assert(activeTextEditor, "getTreeItem: no activeTextEditor");
// tslint:disable-next-line: no-non-null-assertion // Asserted
const document = activeTextEditor?.document;
const start = document.positionAt(elementInfo.current.key.start);
Expand Down

0 comments on commit 11f65d0

Please sign in to comment.