【发布时间】:2021-07-09 03:03:15
【问题描述】:
我正在尝试开发一个 VSCode 扩展,它需要当前打开的文件和来自先前 git 修订/提交的相同文件。这与在 vs 代码中单击打开更改按钮相同。
我尝试使用 SCM 和 QuickDiffProvider,如 sample-extension 所示,但在尝试在 vscode 中打开旧文件时,它给出“无法解析资源”。
sn-p 来自 extension.ts
let folder: string = vscode.env.appRoot;
let scm: vscode.SourceControl | undefined;
if (vscode.workspace.workspaceFolders) {
let rootUri = vscode.workspace.workspaceFolders[0].uri;
scm = vscode.scm.createSourceControl("MyDiff", "MyDiff", rootUri);
folder = rootUri.fsPath;
var repo = new Repository(vscode.workspace.workspaceFolders[0]);
scm.quickDiffProvider = repo;
let changedResources = scm.createResourceGroup("workingTree", "Changes");
// repo.getResourceStates().then((result) => {
// changedResources.resourceStates = result;
// });
// context.subscriptions.push(changedResources);
var currentlyOpenTabfileUri = vscode.window.activeTextEditor?.document.uri;
if(currentlyOpenTabfileUri){
if(repo.provideOriginalResource){
const repositoryUri = repo.provideOriginalResource(currentlyOpenTabfileUri, null);
console.log(repositoryUri);
console.log(currentlyOpenTabfileUri);
try{
vscode.commands.executeCommand('vscode.open', currentlyOpenTabfileUri);
vscode.commands.executeCommand('vscode.open', repositoryUri);
vscode.commands.executeCommand('vscode.diff', repositoryUri, currentlyOpenTabfileUri, `Old - New`);
}
catch(err){
console.error(err);
}
}
}
}
Repository.ts
export const JSFIDDLE_SCHEME = 'MyDiff';
import { QuickDiffProvider, Uri, CancellationToken, ProviderResult, WorkspaceFolder, workspace, window, env } from "vscode";
import * as path from 'path';
export class Repository implements QuickDiffProvider {
constructor(private workspaceFolder: WorkspaceFolder) { }
provideOriginalResource?(uri: Uri, token: CancellationToken|null): ProviderResult<Uri> {
// converts the local file uri to jsfiddle:file.ext
const relativePath = workspace.asRelativePath(uri.fsPath);
return Uri.parse(`${JSFIDDLE_SCHEME}:${relativePath}`);
}
/**
* Enumerates the resources under source control.
*/
provideSourceControlledResources(): Uri[] {
return [
Uri.file(this.createLocalResourcePath('json'))
];
}
/**
* Creates a local file path in the local workspace that corresponds to the part of the
* fiddle denoted by the given extension.
*
* @param extension fiddle part, which is also used as a file extension
* @returns path of the locally cloned fiddle resource ending with the given extension
*/
createLocalResourcePath(extension: string) {
return path.join(this.workspaceFolder.uri.fsPath, extension);
}
}
调试控制台输出:
Congratulations, your extension "vscode-test-diff" is now active!
h {scheme: 'MyDiff', authority: '', path: 'test', query: '', fragment: '', …}
h {scheme: 'file', authority: '', path: '/c:/dummy/test', query: '', fragment: '', …}
简而言之,我在寻找什么:
我想在左侧视图(旧)和右侧视图(新)中获取文件的文件内容,如我的扩展中的 openChange 所示。目的是编写自定义比较方法并将结果存储为 html 格式,而不是显示为 diff 与并排比较。
【问题讨论】:
标签: typescript git visual-studio-code diff vscode-extensions