-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Nikola Hristov <[email protected]>
- Loading branch information
1 parent
f24d1fe
commit 9d902a3
Showing
12 changed files
with
5,645 additions
and
2,841 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
import { Emitter } from "../../../../base/common/event.js"; | ||
import { Disposable, IDisposable } from "../../../../base/common/lifecycle.js"; | ||
import { isEqualOrParent } from "../../../../base/common/resources.js"; | ||
import { URI } from "../../../../base/common/uri.js"; | ||
import { score } from "../../../../editor/common/languageSelector.js"; | ||
import { IUriIdentityService } from "../../../../platform/uriIdentity/common/uriIdentity.js"; | ||
import { | ||
IQuickDiffService, | ||
QuickDiff, | ||
QuickDiffProvider, | ||
} from "./quickDiff.js"; | ||
|
||
function createProviderComparer( | ||
uri: URI, | ||
): (a: QuickDiffProvider, b: QuickDiffProvider) => number { | ||
return (a, b) => { | ||
if (a.rootUri && !b.rootUri) { | ||
return -1; | ||
} else if (!a.rootUri && b.rootUri) { | ||
return 1; | ||
} else if (!a.rootUri && !b.rootUri) { | ||
return 0; | ||
} | ||
const aIsParent = isEqualOrParent(uri, a.rootUri!); | ||
const bIsParent = isEqualOrParent(uri, b.rootUri!); | ||
if (aIsParent && bIsParent) { | ||
return a.rootUri!.fsPath.length - b.rootUri!.fsPath.length; | ||
} else if (aIsParent) { | ||
return -1; | ||
} else if (bIsParent) { | ||
return 1; | ||
} else { | ||
return 0; | ||
} | ||
}; | ||
} | ||
export class QuickDiffService extends Disposable implements IQuickDiffService { | ||
declare readonly _serviceBrand: undefined; | ||
private quickDiffProviders: Set<QuickDiffProvider> = new Set(); | ||
private readonly _onDidChangeQuickDiffProviders = this._register( | ||
new Emitter<void>(), | ||
); | ||
readonly onDidChangeQuickDiffProviders = | ||
this._onDidChangeQuickDiffProviders.event; | ||
constructor( | ||
@IUriIdentityService | ||
private readonly uriIdentityService: IUriIdentityService, | ||
) { | ||
super(); | ||
} | ||
addQuickDiffProvider(quickDiff: QuickDiffProvider): IDisposable { | ||
this.quickDiffProviders.add(quickDiff); | ||
this._onDidChangeQuickDiffProviders.fire(); | ||
return { | ||
dispose: () => { | ||
this.quickDiffProviders.delete(quickDiff); | ||
this._onDidChangeQuickDiffProviders.fire(); | ||
}, | ||
}; | ||
} | ||
private isQuickDiff(diff: { | ||
originalResource?: URI; | ||
label?: string; | ||
isSCM?: boolean; | ||
}): diff is QuickDiff { | ||
return ( | ||
!!diff.originalResource && | ||
typeof diff.label === "string" && | ||
typeof diff.isSCM === "boolean" | ||
); | ||
} | ||
async getQuickDiffs( | ||
uri: URI, | ||
language: string = "", | ||
isSynchronized: boolean = false, | ||
): Promise<QuickDiff[]> { | ||
const providers = Array.from(this.quickDiffProviders) | ||
.filter( | ||
(provider) => | ||
!provider.rootUri || | ||
this.uriIdentityService.extUri.isEqualOrParent( | ||
uri, | ||
provider.rootUri, | ||
), | ||
) | ||
.sort(createProviderComparer(uri)); | ||
const diffs = await Promise.all( | ||
providers.map(async (provider) => { | ||
const scoreValue = provider.selector | ||
? score( | ||
provider.selector, | ||
uri, | ||
language, | ||
isSynchronized, | ||
undefined, | ||
undefined, | ||
) | ||
: 10; | ||
const diff: Partial<QuickDiff> = { | ||
originalResource: | ||
scoreValue > 0 | ||
? ((await provider.getOriginalResource(uri)) ?? | ||
undefined) | ||
: undefined, | ||
label: provider.label, | ||
isSCM: provider.isSCM, | ||
}; | ||
return diff; | ||
}), | ||
); | ||
return diffs.filter<QuickDiff>(this.isQuickDiff); | ||
} | ||
} |
Oops, something went wrong.