| 11 | * the newer "Interactive Forms" controlled by interactiveResolve capabilities. |
| 12 | */ |
| 13 | export class InteractiveRefactors implements IAmDisposable { |
| 14 | static readonly commandName = "_dart.interactiveRefactor"; |
| 15 | |
| 16 | private disposables: IAmDisposable[] = []; |
| 17 | |
| 18 | constructor(private readonly logger: Logger, client: LanguageClient) { |
| 19 | this.disposables.push(commands.registerCommand(InteractiveRefactors.commandName, this.handleRefactor.bind(this))); |
| 20 | this.addMiddleware(client); |
| 21 | } |
| 22 | |
| 23 | public get feature(): StaticFeature { |
| 24 | return { |
| 25 | clear() { }, |
| 26 | fillClientCapabilities(capabilities: ClientCapabilities) { |
| 27 | capabilities.experimental ??= {}; |
| 28 | capabilities.experimental.dartCodeAction ??= {}; |
| 29 | capabilities.experimental.dartCodeAction.commandParameterSupport = { |
| 30 | supportedKinds: Object.values(SupportedParameterKind), |
| 31 | }; |
| 32 | }, |
| 33 | getState(): FeatureState { |
| 34 | return { kind: "static" }; |
| 35 | }, |
| 36 | initialize() { }, |
| 37 | }; |
| 38 | } |
| 39 | |
| 40 | /// Rewrites any commands in `actions` that are interactive refactors to go through |
| 41 | /// our own local command that can prompt the user before calling the server. |
| 42 | public rewriteCommands(actions: Array<Command | CodeAction>) { |
| 43 | for (const action of actions) { |
| 44 | if (!("kind" in action)) |
| 45 | continue; |
| 46 | |
| 47 | const command = action.command; |
| 48 | if (!command || !("command" in command)) |
| 49 | continue; |
| 50 | |
| 51 | if (!action.kind || !CodeActionKind.Refactor.contains(action.kind)) |
| 52 | continue; |
| 53 | |
| 54 | const originalCommandName = command.command; |
| 55 | const argObject = this.getCommandArgumentObject(command.arguments); |
| 56 | if (!argObject) |
| 57 | continue; |
| 58 | |
| 59 | const parameters = this.getCommandParameters(action, argObject); |
| 60 | if (!parameters) |
| 61 | continue; |
| 62 | |
| 63 | command.command = InteractiveRefactors.commandName; |
| 64 | command.arguments = [originalCommandName, parameters, argObject]; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | private addMiddleware(client: LanguageClient) { |
| 69 | const middleware = client.clientOptions.middleware ??= {}; |
| 70 | const previousProvideCodeActions = middleware.provideCodeActions; |
nothing calls this directly
no outgoing calls
no test coverage detected