From 09f7ce63348a07d324fc90e3bf31ae17b0883330 Mon Sep 17 00:00:00 2001 From: Eoin Murphy Date: Thu, 16 Jul 2020 16:58:39 +1000 Subject: [PATCH 1/3] document links should now work in windows --- src/extension.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/extension.js b/src/extension.js index d165c02..0eb65d1 100644 --- a/src/extension.js +++ b/src/extension.js @@ -1,7 +1,7 @@ -/** - * Copyright (C) Animal Logic Pty Ltd. All rights reserved. - */ - +/** + * Copyright (C) Animal Logic Pty Ltd. All rights reserved. + */ + 'use strict'; const vscode = require("vscode"); const util = require('util'); @@ -36,10 +36,12 @@ async function resolveAsset(inputPath, anchorPath) { const { stdout, stderr } = await exec(command); let result = stdout.trim(); // Trim new line characters. if (result.length > 0) { + var uri = vscode.Uri.file(result); + uri.scheme = "file" ; if (await isUsdcFile(result)) { - result = "usdc:" + result; + uri.scheme = "usdc" ; } - return vscode.Uri.parse(result); + return uri } } catch(err) { @@ -212,7 +214,7 @@ class DocumentLinkProvider { // vs code doesn't currently provide a way to open binary files as text so we pretend that usdc files are virtual documents. class TextDocumentContentProvider { async provideTextDocumentContent(uri) { - return catUsdFile(uri.path); + return catUsdFile(uri.fsPath); } }; From 9409c7b08a0dd41bcde71a6652c6bf915f52d53a Mon Sep 17 00:00:00 2001 From: Eoin Murphy Date: Wed, 22 Jul 2020 16:36:38 +1000 Subject: [PATCH 2/3] remove spurious schema assign --- src/extension.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/extension.js b/src/extension.js index 0eb65d1..938131b 100644 --- a/src/extension.js +++ b/src/extension.js @@ -37,7 +37,6 @@ async function resolveAsset(inputPath, anchorPath) { let result = stdout.trim(); // Trim new line characters. if (result.length > 0) { var uri = vscode.Uri.file(result); - uri.scheme = "file" ; if (await isUsdcFile(result)) { uri.scheme = "usdc" ; } From 01d6487bb92035a06280853f1741962ea11ec2ba Mon Sep 17 00:00:00 2001 From: Eoin Murphy Date: Wed, 29 Jul 2020 16:09:33 +1000 Subject: [PATCH 3/3] line endings to linux --- src/extension.js | 460 +++++++++++++++++++++++------------------------ 1 file changed, 230 insertions(+), 230 deletions(-) diff --git a/src/extension.js b/src/extension.js index 938131b..d7d8fcb 100644 --- a/src/extension.js +++ b/src/extension.js @@ -1,231 +1,231 @@ -/** - * Copyright (C) Animal Logic Pty Ltd. All rights reserved. - */ - -'use strict'; -const vscode = require("vscode"); -const util = require('util'); -const exec = util.promisify(require('child_process').exec); -const fs = require('fs'); -const open = util.promisify(fs.open); -const read = util.promisify(fs.read); -const close = util.promisify(fs.close); - -const maxUsdFileSize = 1024 * 1024 * 500; // 500 MB - -async function isUsdcFile(path) { - // Try to read the file header. - try { - const fd = await open(path, 'r'); - var buffer = Buffer.alloc(8); - await read(fd, buffer, 0, 8, 0); - const isUsdc = buffer.toString() == "PXR-USDC"; - await close(fd); - return isUsdc; - } - catch (err) { - } - // Fallback to file extension. - return path.endsWith(".usdc"); -} - -async function resolveAsset(inputPath, anchorPath) { - const template = vscode.workspace.getConfiguration().get('usd.resolve'); - const command = template.replace("{inputPath}", inputPath).replace("{anchorPath}", anchorPath); - try { - const { stdout, stderr } = await exec(command); - let result = stdout.trim(); // Trim new line characters. - if (result.length > 0) { - var uri = vscode.Uri.file(result); - if (await isUsdcFile(result)) { - uri.scheme = "usdc" ; - } - return uri - } - } - catch(err) { - return null; - } - return null; -} - -async function catUsdFile(inputPath) { - const template = vscode.workspace.getConfiguration().get('usd.cat'); - const command = template.replace("{inputPath}", inputPath); - const { stdout, stderr } = await exec(command, { maxBuffer: maxUsdFileSize }); - const result = stdout.trim(); // Trim new line characters. - if (result.length > 0) { - return result; - } - return null; -} - -function getAssets(document, lineNum) { - const results = []; - const line = document.lineAt(lineNum); - var assetType = 0; - var start = 0; - var end = 0; - for (var i = line.firstNonWhitespaceCharacterIndex; i < line.text.length; i++) { - if (line.text[i] == '@') { - if (assetType == 0) { - if(i + 2 < line.text.length && line.text[i + 1] == '@' && line.text[i + 2] == '@') { - assetType = 3; - } - else { - assetType = 1; - } - start = i; - } - else if (assetType == 1) { - assetType = 0; - end = i; - const text = line.text.substring(start + 1, end); - const range = new vscode.Range(new vscode.Position(lineNum, start + 1), new vscode.Position(lineNum, end)); - results.push({ text: text, range: range }); - } - else if (assetType == 3) { - if (i + 2 < line.text.length && line.text[i + 1] == '@' && line.text[i + 2] == '@') { - assetType = 0; - end = i; - const text = line.text.substring(start + 3, end); - const range = new vscode.Range(new vscode.Position(lineNum, start + 1), new vscode.Position(lineNum, end)); - results.push({ text: text, range: range }); - } - } - } - } - return results; -} - -function findDefinition(document, definition, token) { - let path = definition.split('/').filter(Boolean); - if (path.length == 0) return null; - let current = 0; - for (let line = 0; line < document.lineCount; line++) { - if (token.isCancellationRequested) { - return null; - } - let matches = document.lineAt(line).text.match('(?<=(over|def|class) [^"]*\")[^"]+'); - if (matches) { - for (let m = 0; m < matches.length; m++) { - if (token.isCancellationRequested) { - return null; - } - const required = path[current]; - const actual = matches[0]; - if (required == actual) { - current++; - if (current == path.length) { - return new vscode.Range(line, matches.index, line, matches.index + actual.length); - } - } - } - } - } -} - -function getPaths(document, lineNum) { - const results = []; - const line = document.lineAt(lineNum); - var start = 0; - var end = 0; - for (var i = line.firstNonWhitespaceCharacterIndex; i < line.text.length; i++) { - if (line.text[i] == '<') { - start = i; - } - else if (line.text[i] == '>') { - end = i; - const text = line.text.substring(start + 1, end); - const range = new vscode.Range(new vscode.Position(lineNum, start + 1), new vscode.Position(lineNum, end)); - results.push({ text: text, range: range }); - } - } - return results; -} - -class HoverProvider { - async provideHover(document, position, token) { - const assets = getAssets(document, position.line); - for(var i = 0; i < assets.length; i++) { - const asset = assets[i]; - if (asset.range.start.isBeforeOrEqual(position) && asset.range.end.isAfterOrEqual(position)) { - const resolved = await resolveAsset(asset.text, document.fileName); - if (resolved != null) { - return new vscode.Hover([resolved.toString()], asset.range); - } - return new vscode.Hover(["Failed to resolve: " + asset.text], asset.range); - } - } - const paths = getPaths(document, position.line); - for(var i = 0; i < paths.length; i++) { - const path = paths[i]; - if (path.range.start.isBeforeOrEqual(position) && path.range.end.isAfterOrEqual(position)) { - return new vscode.Hover([path.text], path.range); - } - } - return null; - } -} - -class DefinitionProvider { - provideDefinition(document, position, token) { - const paths = getPaths(document, position.line); - for(var i = 0; i < paths.length; i++) { - const path = paths[i]; - if (path.range.start.isBeforeOrEqual(position) && path.range.end.isAfterOrEqual(position)) { - const definition = findDefinition(document, path.text, token); - if (definition != null) { - return new vscode.Location(document.uri, definition); - } - } - } - return null; - } -} - -class DocumentLinkProvider { - provideDocumentLinks(document, token) { - const results = []; - for (var line = 0; line < document.lineCount; line++) { - const assets = getAssets(document, line); - for (var i = 0; i < assets.length; i++) { - if (token.isCancellationRequested) { - break; - } - const asset = assets[i]; - const link = new vscode.DocumentLink(asset.range, null); - link.path = asset.text; - link.fileName = document.fileName; - results.push(link); - } - } - return results; - } - async resolveDocumentLink(link, token) { - if (link.target == null) { - link.target = await resolveAsset(link.path, link.fileName); - } - return link; - } -} - -// vs code doesn't currently provide a way to open binary files as text so we pretend that usdc files are virtual documents. -class TextDocumentContentProvider { - async provideTextDocumentContent(uri) { - return catUsdFile(uri.fsPath); - } -}; - -async function activate(context) { - context.subscriptions.push(vscode.languages.registerHoverProvider('usd', new HoverProvider())); - context.subscriptions.push(vscode.languages.registerDefinitionProvider('usd', new DefinitionProvider())); - context.subscriptions.push(vscode.languages.registerDocumentLinkProvider('usd', new DocumentLinkProvider())); - context.subscriptions.push(vscode.workspace.registerTextDocumentContentProvider("usdc", new TextDocumentContentProvider())); -} -exports.activate = activate; - -async function deactivate() { - -} +/** + * Copyright (C) Animal Logic Pty Ltd. All rights reserved. + */ + +'use strict'; +const vscode = require("vscode"); +const util = require('util'); +const exec = util.promisify(require('child_process').exec); +const fs = require('fs'); +const open = util.promisify(fs.open); +const read = util.promisify(fs.read); +const close = util.promisify(fs.close); + +const maxUsdFileSize = 1024 * 1024 * 500; // 500 MB + +async function isUsdcFile(path) { + // Try to read the file header. + try { + const fd = await open(path, 'r'); + var buffer = Buffer.alloc(8); + await read(fd, buffer, 0, 8, 0); + const isUsdc = buffer.toString() == "PXR-USDC"; + await close(fd); + return isUsdc; + } + catch (err) { + } + // Fallback to file extension. + return path.endsWith(".usdc"); +} + +async function resolveAsset(inputPath, anchorPath) { + const template = vscode.workspace.getConfiguration().get('usd.resolve'); + const command = template.replace("{inputPath}", inputPath).replace("{anchorPath}", anchorPath); + try { + const { stdout, stderr } = await exec(command); + let result = stdout.trim(); // Trim new line characters. + if (result.length > 0) { + var uri = vscode.Uri.file(result); + if (await isUsdcFile(result)) { + uri.scheme = "usdc" ; + } + return uri + } + } + catch(err) { + return null; + } + return null; +} + +async function catUsdFile(inputPath) { + const template = vscode.workspace.getConfiguration().get('usd.cat'); + const command = template.replace("{inputPath}", inputPath); + const { stdout, stderr } = await exec(command, { maxBuffer: maxUsdFileSize }); + const result = stdout.trim(); // Trim new line characters. + if (result.length > 0) { + return result; + } + return null; +} + +function getAssets(document, lineNum) { + const results = []; + const line = document.lineAt(lineNum); + var assetType = 0; + var start = 0; + var end = 0; + for (var i = line.firstNonWhitespaceCharacterIndex; i < line.text.length; i++) { + if (line.text[i] == '@') { + if (assetType == 0) { + if(i + 2 < line.text.length && line.text[i + 1] == '@' && line.text[i + 2] == '@') { + assetType = 3; + } + else { + assetType = 1; + } + start = i; + } + else if (assetType == 1) { + assetType = 0; + end = i; + const text = line.text.substring(start + 1, end); + const range = new vscode.Range(new vscode.Position(lineNum, start + 1), new vscode.Position(lineNum, end)); + results.push({ text: text, range: range }); + } + else if (assetType == 3) { + if (i + 2 < line.text.length && line.text[i + 1] == '@' && line.text[i + 2] == '@') { + assetType = 0; + end = i; + const text = line.text.substring(start + 3, end); + const range = new vscode.Range(new vscode.Position(lineNum, start + 1), new vscode.Position(lineNum, end)); + results.push({ text: text, range: range }); + } + } + } + } + return results; +} + +function findDefinition(document, definition, token) { + let path = definition.split('/').filter(Boolean); + if (path.length == 0) return null; + let current = 0; + for (let line = 0; line < document.lineCount; line++) { + if (token.isCancellationRequested) { + return null; + } + let matches = document.lineAt(line).text.match('(?<=(over|def|class) [^"]*\")[^"]+'); + if (matches) { + for (let m = 0; m < matches.length; m++) { + if (token.isCancellationRequested) { + return null; + } + const required = path[current]; + const actual = matches[0]; + if (required == actual) { + current++; + if (current == path.length) { + return new vscode.Range(line, matches.index, line, matches.index + actual.length); + } + } + } + } + } +} + +function getPaths(document, lineNum) { + const results = []; + const line = document.lineAt(lineNum); + var start = 0; + var end = 0; + for (var i = line.firstNonWhitespaceCharacterIndex; i < line.text.length; i++) { + if (line.text[i] == '<') { + start = i; + } + else if (line.text[i] == '>') { + end = i; + const text = line.text.substring(start + 1, end); + const range = new vscode.Range(new vscode.Position(lineNum, start + 1), new vscode.Position(lineNum, end)); + results.push({ text: text, range: range }); + } + } + return results; +} + +class HoverProvider { + async provideHover(document, position, token) { + const assets = getAssets(document, position.line); + for(var i = 0; i < assets.length; i++) { + const asset = assets[i]; + if (asset.range.start.isBeforeOrEqual(position) && asset.range.end.isAfterOrEqual(position)) { + const resolved = await resolveAsset(asset.text, document.fileName); + if (resolved != null) { + return new vscode.Hover([resolved.toString()], asset.range); + } + return new vscode.Hover(["Failed to resolve: " + asset.text], asset.range); + } + } + const paths = getPaths(document, position.line); + for(var i = 0; i < paths.length; i++) { + const path = paths[i]; + if (path.range.start.isBeforeOrEqual(position) && path.range.end.isAfterOrEqual(position)) { + return new vscode.Hover([path.text], path.range); + } + } + return null; + } +} + +class DefinitionProvider { + provideDefinition(document, position, token) { + const paths = getPaths(document, position.line); + for(var i = 0; i < paths.length; i++) { + const path = paths[i]; + if (path.range.start.isBeforeOrEqual(position) && path.range.end.isAfterOrEqual(position)) { + const definition = findDefinition(document, path.text, token); + if (definition != null) { + return new vscode.Location(document.uri, definition); + } + } + } + return null; + } +} + +class DocumentLinkProvider { + provideDocumentLinks(document, token) { + const results = []; + for (var line = 0; line < document.lineCount; line++) { + const assets = getAssets(document, line); + for (var i = 0; i < assets.length; i++) { + if (token.isCancellationRequested) { + break; + } + const asset = assets[i]; + const link = new vscode.DocumentLink(asset.range, null); + link.path = asset.text; + link.fileName = document.fileName; + results.push(link); + } + } + return results; + } + async resolveDocumentLink(link, token) { + if (link.target == null) { + link.target = await resolveAsset(link.path, link.fileName); + } + return link; + } +} + +// vs code doesn't currently provide a way to open binary files as text so we pretend that usdc files are virtual documents. +class TextDocumentContentProvider { + async provideTextDocumentContent(uri) { + return catUsdFile(uri.fsPath); + } +}; + +async function activate(context) { + context.subscriptions.push(vscode.languages.registerHoverProvider('usd', new HoverProvider())); + context.subscriptions.push(vscode.languages.registerDefinitionProvider('usd', new DefinitionProvider())); + context.subscriptions.push(vscode.languages.registerDocumentLinkProvider('usd', new DocumentLinkProvider())); + context.subscriptions.push(vscode.workspace.registerTextDocumentContentProvider("usdc", new TextDocumentContentProvider())); +} +exports.activate = activate; + +async function deactivate() { + +} exports.deactivate = deactivate; \ No newline at end of file