|
| 1 | +import { Subscription } from 'rxjs' |
| 2 | +import sourcegraph from 'sourcegraph' |
| 3 | +import { evaluateAndCreateCampaignSpec } from '@sourcegraph/campaigns-client' |
| 4 | +import slugify from 'slugify' |
| 5 | +import { getCurrentUser } from './util' |
| 6 | +import { editFile } from './edit-file' |
| 7 | + |
| 8 | +// TODO: sanitize this for real, it gets used in the description of the campaign |
| 9 | +const escapedMarkdownCode = (text: string): string => '`' + text.replace(/`/g, '\\`') + '`' |
| 10 | + |
| 11 | +export const registerFindReplaceAction = (): Subscription => { |
| 12 | + const subscription = new Subscription() |
| 13 | + subscription.add( |
| 14 | + sourcegraph.commands.registerCommand('findReplace.startFindReplace', async (searchQuery: string) => { |
| 15 | + // TODO: in the future, use the search query to get the list of matching files. |
| 16 | + console.log('context.searchQuery', searchQuery) |
| 17 | + |
| 18 | + if (!searchQuery) { |
| 19 | + return |
| 20 | + } |
| 21 | + |
| 22 | + // To create campaigns, a namespace is used, which can be the current user's username. |
| 23 | + const currentUser = await getCurrentUser() |
| 24 | + if (!currentUser) { |
| 25 | + throw new Error('No current user') |
| 26 | + } |
| 27 | + const namespaceName = currentUser.username |
| 28 | + |
| 29 | + const findString = await sourcegraph.app.activeWindow!.showInputBox({ |
| 30 | + prompt: 'Find all matches of:', |
| 31 | + }) |
| 32 | + if (!findString) { |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + const replacementString = await sourcegraph.app.activeWindow!.showInputBox({ |
| 37 | + prompt: 'Replace with:', |
| 38 | + }) |
| 39 | + // Empty string is a valid replacement, so compare directly with undefined. |
| 40 | + if (replacementString === undefined) { |
| 41 | + return |
| 42 | + } |
| 43 | + |
| 44 | + const campaignName = `replace-${slugify(findString)}-with-${slugify(replacementString)}` |
| 45 | + const description = `Replace ${escapedMarkdownCode(findString)} with ${escapedMarkdownCode( |
| 46 | + replacementString |
| 47 | + )}` |
| 48 | + |
| 49 | + let percentage = 0 |
| 50 | + const { applyURL, diffStat } = await sourcegraph.app.activeWindow!.withProgress( |
| 51 | + { title: '**Find-replace**' }, |
| 52 | + reporter => |
| 53 | + evaluateAndCreateCampaignSpec(namespaceName, { |
| 54 | + name: campaignName, |
| 55 | + on: [ |
| 56 | + { |
| 57 | + repositoriesMatchingQuery: searchQuery, |
| 58 | + }, |
| 59 | + ], |
| 60 | + description, |
| 61 | + steps: [ |
| 62 | + { |
| 63 | + // fileFilter is a required property, but we want it to be a noop. |
| 64 | + fileFilter: () => true, |
| 65 | + editFile: (path, text) => { |
| 66 | + if (!text.includes(findString)) { |
| 67 | + // skip the file by returning null |
| 68 | + return null |
| 69 | + } |
| 70 | + |
| 71 | + percentage += (100 - percentage) / 100 |
| 72 | + reporter.next({ message: `Computing changes in ${path}`, percentage }) |
| 73 | + |
| 74 | + return editFile(text, findString, replacementString) |
| 75 | + }, |
| 76 | + }, |
| 77 | + ], |
| 78 | + changesetTemplate: { |
| 79 | + title: description, |
| 80 | + branch: `campaign/${campaignName}`, |
| 81 | + commit: { |
| 82 | + message: description, |
| 83 | + author: { |
| 84 | + name: currentUser.username, |
| 85 | + email: currentUser.email, |
| 86 | + }, |
| 87 | + }, |
| 88 | + published: false, |
| 89 | + }, |
| 90 | + }) |
| 91 | + ) |
| 92 | + sourcegraph.app.activeWindow!.showNotification( |
| 93 | + `[**Find-replace changes**](${applyURL}) are ready to preview and apply.\n\n<small>${diffStat.changed} changes made.</small>`, |
| 94 | + sourcegraph.NotificationType.Success |
| 95 | + ) |
| 96 | + }) |
| 97 | + ) |
| 98 | + return subscription |
| 99 | +} |
0 commit comments