-
Notifications
You must be signed in to change notification settings - Fork 29.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract remote source provider registry into the vscode.git-base exte…
…nsion (#137656)
- Loading branch information
Showing
35 changed files
with
946 additions
and
258 deletions.
There are no files selected for viewing
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
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
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,17 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
//@ts-check | ||
|
||
'use strict'; | ||
|
||
const withDefaults = require('../shared.webpack.config'); | ||
|
||
module.exports = withDefaults({ | ||
context: __dirname, | ||
entry: { | ||
extension: './src/extension.ts' | ||
} | ||
}); |
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
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{ | ||
"displayName": "Git Base", | ||
"description": "Git static contributions and pickers." | ||
"description": "Git static contributions and pickers.", | ||
"command.api.getRemoteSources": "Get Remote Sources" | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,37 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { Disposable, commands } from 'vscode'; | ||
import { Model } from '../model'; | ||
import { pickRemoteSource } from '../remoteSource'; | ||
import { GitBaseExtensionImpl } from './extension'; | ||
import { API, PickRemoteSourceOptions, PickRemoteSourceResult, RemoteSourceProvider } from './git-base'; | ||
|
||
export class ApiImpl implements API { | ||
|
||
constructor(private _model: Model) { } | ||
|
||
pickRemoteSource(options: PickRemoteSourceOptions): Promise<PickRemoteSourceResult | string | undefined> { | ||
return pickRemoteSource(this._model, options as any); | ||
} | ||
|
||
registerRemoteSourceProvider(provider: RemoteSourceProvider): Disposable { | ||
return this._model.registerRemoteSourceProvider(provider); | ||
} | ||
} | ||
|
||
export function registerAPICommands(extension: GitBaseExtensionImpl): Disposable { | ||
const disposables: Disposable[] = []; | ||
|
||
disposables.push(commands.registerCommand('git-base.api.getRemoteSources', (opts?: PickRemoteSourceOptions) => { | ||
if (!extension.model) { | ||
return; | ||
} | ||
|
||
return pickRemoteSource(extension.model, opts as any); | ||
})); | ||
|
||
return Disposable.from(...disposables); | ||
} |
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,55 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { Model } from '../model'; | ||
import { GitBaseExtension, API } from './git-base'; | ||
import { Event, EventEmitter } from 'vscode'; | ||
import { ApiImpl } from './api1'; | ||
|
||
export class GitBaseExtensionImpl implements GitBaseExtension { | ||
|
||
enabled: boolean = false; | ||
|
||
private _onDidChangeEnablement = new EventEmitter<boolean>(); | ||
readonly onDidChangeEnablement: Event<boolean> = this._onDidChangeEnablement.event; | ||
|
||
private _model: Model | undefined = undefined; | ||
|
||
set model(model: Model | undefined) { | ||
this._model = model; | ||
|
||
const enabled = !!model; | ||
|
||
if (this.enabled === enabled) { | ||
return; | ||
} | ||
|
||
this.enabled = enabled; | ||
this._onDidChangeEnablement.fire(this.enabled); | ||
} | ||
|
||
get model(): Model | undefined { | ||
return this._model; | ||
} | ||
|
||
constructor(model?: Model) { | ||
if (model) { | ||
this.enabled = true; | ||
this._model = model; | ||
} | ||
} | ||
|
||
getAPI(version: number): API { | ||
if (!this._model) { | ||
throw new Error('Git model not found'); | ||
} | ||
|
||
if (version !== 1) { | ||
throw new Error(`No API version ${version} found.`); | ||
} | ||
|
||
return new ApiImpl(this._model); | ||
} | ||
} |
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,60 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { Disposable, Event, ProviderResult, Uri } from 'vscode'; | ||
export { ProviderResult } from 'vscode'; | ||
|
||
export interface API { | ||
registerRemoteSourceProvider(provider: RemoteSourceProvider): Disposable; | ||
pickRemoteSource(options: PickRemoteSourceOptions): Promise<string | PickRemoteSourceResult | undefined>; | ||
} | ||
|
||
export interface GitBaseExtension { | ||
|
||
readonly enabled: boolean; | ||
readonly onDidChangeEnablement: Event<boolean>; | ||
|
||
/** | ||
* Returns a specific API version. | ||
* | ||
* Throws error if git-base extension is disabled. You can listed to the | ||
* [GitBaseExtension.onDidChangeEnablement](#GitBaseExtension.onDidChangeEnablement) | ||
* event to know when the extension becomes enabled/disabled. | ||
* | ||
* @param version Version number. | ||
* @returns API instance | ||
*/ | ||
getAPI(version: 1): API; | ||
} | ||
|
||
export interface PickRemoteSourceOptions { | ||
readonly providerLabel?: (provider: RemoteSourceProvider) => string; | ||
readonly urlLabel?: string; | ||
readonly providerName?: string; | ||
readonly branch?: boolean; // then result is PickRemoteSourceResult | ||
} | ||
|
||
export interface PickRemoteSourceResult { | ||
readonly url: string; | ||
readonly branch?: string; | ||
} | ||
|
||
export interface RemoteSource { | ||
readonly name: string; | ||
readonly description?: string; | ||
readonly url: string | string[]; | ||
} | ||
|
||
export interface RemoteSourceProvider { | ||
readonly name: string; | ||
/** | ||
* Codicon name | ||
*/ | ||
readonly icon?: string; | ||
readonly supportsQuery?: boolean; | ||
|
||
getBranches?(url: string): ProviderResult<string[]>; | ||
getRemoteSources(query?: string): ProviderResult<RemoteSource[]>; | ||
} |
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,69 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { done } from './util'; | ||
|
||
export function debounce(delay: number): Function { | ||
return decorate((fn, key) => { | ||
const timerKey = `$debounce$${key}`; | ||
|
||
return function (this: any, ...args: any[]) { | ||
clearTimeout(this[timerKey]); | ||
this[timerKey] = setTimeout(() => fn.apply(this, args), delay); | ||
}; | ||
}); | ||
} | ||
|
||
export const throttle = decorate(_throttle); | ||
|
||
function _throttle<T>(fn: Function, key: string): Function { | ||
const currentKey = `$throttle$current$${key}`; | ||
const nextKey = `$throttle$next$${key}`; | ||
|
||
const trigger = function (this: any, ...args: any[]) { | ||
if (this[nextKey]) { | ||
return this[nextKey]; | ||
} | ||
|
||
if (this[currentKey]) { | ||
this[nextKey] = done(this[currentKey]).then(() => { | ||
this[nextKey] = undefined; | ||
return trigger.apply(this, args); | ||
}); | ||
|
||
return this[nextKey]; | ||
} | ||
|
||
this[currentKey] = fn.apply(this, args) as Promise<T>; | ||
|
||
const clear = () => this[currentKey] = undefined; | ||
done(this[currentKey]).then(clear, clear); | ||
|
||
return this[currentKey]; | ||
}; | ||
|
||
return trigger; | ||
} | ||
|
||
function decorate(decorator: (fn: Function, key: string) => Function): Function { | ||
return (_target: any, key: string, descriptor: any) => { | ||
let fnKey: string | null = null; | ||
let fn: Function | null = null; | ||
|
||
if (typeof descriptor.value === 'function') { | ||
fnKey = 'value'; | ||
fn = descriptor.value; | ||
} else if (typeof descriptor.get === 'function') { | ||
fnKey = 'get'; | ||
fn = descriptor.get; | ||
} | ||
|
||
if (!fn || !fnKey) { | ||
throw new Error('not supported'); | ||
} | ||
|
||
descriptor[fnKey] = decorator(fn, key); | ||
}; | ||
} |
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,16 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { ExtensionContext } from 'vscode'; | ||
import { registerAPICommands } from './api/api1'; | ||
import { GitBaseExtensionImpl } from './api/extension'; | ||
import { Model } from './model'; | ||
|
||
export function activate(context: ExtensionContext): GitBaseExtensionImpl { | ||
const apiImpl = new GitBaseExtensionImpl(new Model()); | ||
context.subscriptions.push(registerAPICommands(apiImpl)); | ||
|
||
return apiImpl; | ||
} |
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,34 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { EventEmitter, Disposable } from 'vscode'; | ||
import { toDisposable } from './util'; | ||
import { RemoteSourceProvider } from './api/git-base'; | ||
import { IRemoteSourceProviderRegistry } from './remoteProvider'; | ||
|
||
export class Model implements IRemoteSourceProviderRegistry { | ||
|
||
private remoteSourceProviders = new Set<RemoteSourceProvider>(); | ||
|
||
private _onDidAddRemoteSourceProvider = new EventEmitter<RemoteSourceProvider>(); | ||
readonly onDidAddRemoteSourceProvider = this._onDidAddRemoteSourceProvider.event; | ||
|
||
private _onDidRemoveRemoteSourceProvider = new EventEmitter<RemoteSourceProvider>(); | ||
readonly onDidRemoveRemoteSourceProvider = this._onDidRemoveRemoteSourceProvider.event; | ||
|
||
registerRemoteSourceProvider(provider: RemoteSourceProvider): Disposable { | ||
this.remoteSourceProviders.add(provider); | ||
this._onDidAddRemoteSourceProvider.fire(provider); | ||
|
||
return toDisposable(() => { | ||
this.remoteSourceProviders.delete(provider); | ||
this._onDidRemoveRemoteSourceProvider.fire(provider); | ||
}); | ||
} | ||
|
||
getRemoteProviders(): RemoteSourceProvider[] { | ||
return [...this.remoteSourceProviders.values()]; | ||
} | ||
} |
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
Oops, something went wrong.