More work will be needed to get this to properly synchronise between the command-line and editor.
7X2GGEHDEWGZCDFD2IAGUGHIPNFWY76TTVNKOIV7YL3D5PMAXX4AC
import { commands, ExtensionContext, Uri, window, workspace, scm, SourceControl, RelativePattern, SourceControlResourceGroup } from 'vscode';
import path from 'path';
import { commands, ExtensionContext, Uri, window, workspace, scm, SourceControl, RelativePattern, SourceControlResourceGroup, FileDecorationProvider, CancellationToken, Event, FileDecoration, ProviderResult, EventEmitter } from 'vscode';
import { join } from 'path';
import fs from 'fs';
function update_resource(repository: string, resource: { path: string, display: string, tooltip: string, untracked: boolean }) {
const repo = source_control_views.get(repository)!;
let resource_states = resource.untracked ? repo.resource_groups[1].resourceStates : repo.resource_groups[0].resourceStates;
let to_replace = resource_states.find((value, _index) => {
value.resourceUri === Uri.file(resource.path)
});
const diffs = shortDiff(path);
const resource_groups = source_control_views.get(repository)?.resource_groups!;
let modified = [];
let untracked = [];
const new_resource = {
resourceUri: Uri.file(path.join(repository, resource.path)),
decorations: {
tooltip: resource.tooltip,
// Add all the fresh diffs first
for (let diff of diffs) {
const to_add = {
resourceUri: Uri.file(join(repository, diff.path)),
decorations: {
tooltip: diff.tooltip,
}
}
console.log(`Resource URI: ${to_add.resourceUri}`);
if (diff.untracked) {
untracked.push(to_add);
} else {
modified.push(to_add);
}
class DecorationProvider implements FileDecorationProvider {
private readonly onDidFileDecorationsEmitter = new EventEmitter<Uri | Uri[] | undefined>();
get onDidChangeFileDecorations() {
return this.onDidFileDecorationsEmitter.event;
}
emitDecorationEvent(uri: Uri | Uri[]) {
this.onDidFileDecorationsEmitter.fire(uri);
}
provideFileDecoration(uri: Uri, token: CancellationToken): ProviderResult<FileDecoration> {
console.log(`Decorating file: ${uri.fsPath}`);
if (fs.lstatSync(uri.fsPath).isDirectory()) {
return;
} else {
const diffs = shortDiff(uri.fsPath);
console.assert(diffs.length <= 1);
if (diffs.length == 1) {
const diff = diffs[0];
return {
badge: diff.badge,
tooltip: diff.tooltip,
propagate: true, // TODO: no propogation for deletions
color: diff.color,
};
} else {
return new FileDecoration('', '');
}
}
}
try {
const repository = findClosestRoot(Array.from(source_control_views.keys()), event.fsPath);
if (!ignored(repository, event.fsPath)) {
console.log(`Responding to change event on file ${event.fsPath} with parent ${repository}`);
const diffs = shortDiff(event.fsPath);
console.assert(diffs.length <= 1);
// // TODO: check if file is tracked
if (diffs.length === 1) {
// File has changed, update diff in explorer tree
update_resource(repository, diffs[0]);
} else {
// File has no diff, clear any diff in explorer tree
}
} else {
console.log(`Ignored change event from ${event.fsPath}`);
}
} catch (e) {
console.error(e);
const repository = findClosestRoot(Array.from(source_control_views.keys()), event.fsPath);
if (!ignored(repository, event.fsPath)) {
console.log(`Responding to change event (repository: ${repo_path}) on file: ${event.fsPath}`);
update_view(repository, event.fsPath);
} else {
console.log(`Ignored change event from ${event.fsPath}`);