|
| 1 | +import { Autowired } from '@ali/common-di'; |
| 2 | +import { |
| 3 | + CommandContribution, |
| 4 | + Domain, |
| 5 | + CommandRegistry, |
| 6 | + Command, |
| 7 | + Disposable, |
| 8 | +} from '@ali/ide-core-common'; |
| 9 | +import { IOpenerService } from '@ali/ide-core-browser'; |
| 10 | +import { CODE_PLATFORM_CONFIG, CodePlatform } from '@alipay/alex-code-api'; |
| 11 | +import { CodeModelService } from './code-model.service'; |
| 12 | + |
| 13 | +// 对外暴露的服务命令 |
| 14 | +namespace CODE_SERVICE_COMMANDS { |
| 15 | + const CATEGORY = 'CodeService'; |
| 16 | + |
| 17 | + export const BLAME: Command = { |
| 18 | + id: 'codeService.getFileBlame', |
| 19 | + category: CATEGORY, |
| 20 | + }; |
| 21 | + |
| 22 | + export type BLAME = (filepath: string) => Promise<Uint8Array | void>; |
| 23 | + |
| 24 | + export const OPEN_IN_REMOTE: Command = { |
| 25 | + id: 'codeService.openInRemote', |
| 26 | + category: CATEGORY, |
| 27 | + }; |
| 28 | + |
| 29 | + export type OPEN_IN_REMOTE = (filepath: string, res: RemoteResource) => void; |
| 30 | +} |
| 31 | + |
| 32 | +export enum RemoteResourceType { |
| 33 | + Branch = 'branch', |
| 34 | + Branches = 'branches', |
| 35 | + Commit = 'commit', |
| 36 | + File = 'file', |
| 37 | + Repo = 'repo', |
| 38 | + Revision = 'revision', |
| 39 | +} |
| 40 | + |
| 41 | +export type RemoteResource = |
| 42 | + | { |
| 43 | + type: RemoteResourceType.Branch; |
| 44 | + branch: string; |
| 45 | + } |
| 46 | + | { |
| 47 | + type: RemoteResourceType.Branches; |
| 48 | + } |
| 49 | + | { |
| 50 | + type: RemoteResourceType.Commit; |
| 51 | + sha: string; |
| 52 | + } |
| 53 | + | { |
| 54 | + type: RemoteResourceType.File; |
| 55 | + branch?: string; |
| 56 | + fileName: string; |
| 57 | + range?: Range; |
| 58 | + } |
| 59 | + | { |
| 60 | + type: RemoteResourceType.Repo; |
| 61 | + }; |
| 62 | + |
| 63 | +@Domain(CommandContribution) |
| 64 | +export class CommandsContribution extends Disposable implements CommandContribution { |
| 65 | + @Autowired(CodeModelService) |
| 66 | + private readonly codeModel: CodeModelService; |
| 67 | + |
| 68 | + @Autowired(IOpenerService) |
| 69 | + private readonly openerService: IOpenerService; |
| 70 | + |
| 71 | + registerCommands(registry: CommandRegistry) { |
| 72 | + this.addDispose([ |
| 73 | + // TODO: 这里直接返回 Uint8Array,减少序列化耗时,但考虑到多平台又需转换成统一格式数据 |
| 74 | + registry.registerCommand<CODE_SERVICE_COMMANDS.BLAME>(CODE_SERVICE_COMMANDS.BLAME, { |
| 75 | + execute: async (filepath) => { |
| 76 | + const repo = this.codeModel.getRepository(filepath); |
| 77 | + if (repo && repo.platform === CodePlatform.antcode) { |
| 78 | + return repo.request.getFileBlame(repo.asRelativePath(filepath)); |
| 79 | + } |
| 80 | + }, |
| 81 | + }), |
| 82 | + |
| 83 | + registry.registerCommand<CODE_SERVICE_COMMANDS.OPEN_IN_REMOTE>( |
| 84 | + CODE_SERVICE_COMMANDS.OPEN_IN_REMOTE, |
| 85 | + { |
| 86 | + execute: (filepath, res) => { |
| 87 | + const repo = this.codeModel.getRepository(filepath); |
| 88 | + if (repo) { |
| 89 | + if (res.type === RemoteResourceType.Commit) { |
| 90 | + const { origin } = CODE_PLATFORM_CONFIG[repo.platform]; |
| 91 | + this.openerService.open(`${origin}/${repo.owner}/${repo.name}/commit/${res.sha}`); |
| 92 | + } |
| 93 | + } |
| 94 | + }, |
| 95 | + } |
| 96 | + ), |
| 97 | + ]); |
| 98 | + } |
| 99 | +} |
0 commit comments