Typescript VSCode扩展名:获取旧版本和新版本的文件
我正在尝试开发一个VSCode扩展,它需要当前打开的文件和以前git修订/提交的相同文件。这与在vs代码中单击“打开更改”按钮相同 我尝试使用SCM和QuickDiffProvider,如图所示,但在尝试用vscode打开旧文件时,它给出了“无法解析资源”Typescript VSCode扩展名:获取旧版本和新版本的文件,typescript,git,visual-studio-code,diff,vscode-extensions,Typescript,Git,Visual Studio Code,Diff,Vscode Extensions,我正在尝试开发一个VSCode扩展,它需要当前打开的文件和以前git修订/提交的相同文件。这与在vs代码中单击“打开更改”按钮相同 我尝试使用SCM和QuickDiffProvider,如图所示,但在尝试用vscode打开旧文件时,它给出了“无法解析资源” 来自extension.ts的代码片段 let folder: string = vscode.env.appRoot; let scm: vscode.SourceControl | undefined; if (vscode.wor
来自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);
}
}
}
}
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);
}
}
Repository.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);
}
}
}
}
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);
}
}
简而言之,我在寻找什么:
我想在左侧视图(旧)和右侧视图(新)中获取文件内容,如我的扩展名中的openChange所示。其目的是编写一个自定义比较方法,并以html格式存储结果,而不是以diff的形式显示并进行并排比较
在苦苦寻找替代解决方案以获得文件差异后,我发现我们可以使用
vscode.editors
访问所有打开的编辑器
示例:在git diff中,我们将有两个编辑器(左:旧和右:新),因此我们可以使用以下代码访问新旧内容
if (editors.length === 2) {
oldContent = editors[0].document.getText();
newContent = editors[1].document.getText();
}
如果我们使用git lens,那么我们也可以使用相同的代码获得旧提交的差异,这是一个额外的好处(至少对于我的扩展来说)
这种方法的唯一缺点是,如果在两列中打开两个编辑器,它也会工作(这对于我的扩展来说实际上很好)