Fix: PatchHelper not auto filling match field (#2338)

This commit is contained in:
Eric 2024-05-18 20:45:05 -04:00 committed by GitHub
parent caed7cd92c
commit d43731833a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -16,7 +16,6 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import { CheckedTextInput } from "@components/CheckedTextInput";
import { CodeBlock } from "@components/CodeBlock"; import { CodeBlock } from "@components/CodeBlock";
import { debounce } from "@shared/debounce"; import { debounce } from "@shared/debounce";
import { Margins } from "@utils/margins"; import { Margins } from "@utils/margins";
@ -47,7 +46,7 @@ const findCandidates = debounce(function ({ find, setModule, setError }) {
interface ReplacementComponentProps { interface ReplacementComponentProps {
module: [id: number, factory: Function]; module: [id: number, factory: Function];
match: string | RegExp; match: string;
replacement: string | ReplaceFn; replacement: string | ReplaceFn;
setReplacementError(error: any): void; setReplacementError(error: any): void;
} }
@ -58,7 +57,13 @@ function ReplacementComponent({ module, match, replacement, setReplacementError
const [patchedCode, matchResult, diff] = React.useMemo(() => { const [patchedCode, matchResult, diff] = React.useMemo(() => {
const src: string = fact.toString().replaceAll("\n", ""); const src: string = fact.toString().replaceAll("\n", "");
const canonicalMatch = canonicalizeMatch(match);
try {
new RegExp(match);
} catch (e) {
return ["", [], []];
}
const canonicalMatch = canonicalizeMatch(new RegExp(match));
try { try {
const canonicalReplace = canonicalizeReplace(replacement, "YourPlugin"); const canonicalReplace = canonicalizeReplace(replacement, "YourPlugin");
var patched = src.replace(canonicalMatch, canonicalReplace as string); var patched = src.replace(canonicalMatch, canonicalReplace as string);
@ -286,6 +291,7 @@ function PatchHelper() {
const [module, setModule] = React.useState<[number, Function]>(); const [module, setModule] = React.useState<[number, Function]>();
const [findError, setFindError] = React.useState<string>(); const [findError, setFindError] = React.useState<string>();
const [matchError, setMatchError] = React.useState<string>();
const code = React.useMemo(() => { const code = React.useMemo(() => {
return ` return `
@ -322,12 +328,17 @@ function PatchHelper() {
} }
function onMatchChange(v: string) { function onMatchChange(v: string) {
setMatchError(void 0);
setMatch(v);
}
function onMatchBlur() {
try { try {
new RegExp(v); new RegExp(match);
setFindError(void 0); setMatchError(void 0);
setMatch(v); setMatch(match);
} catch (e: any) { } catch (e: any) {
setFindError((e as Error).message); setMatchError((e as Error).message);
} }
} }
@ -351,16 +362,12 @@ function PatchHelper() {
/> />
<Forms.FormTitle className={Margins.top8}>match</Forms.FormTitle> <Forms.FormTitle className={Margins.top8}>match</Forms.FormTitle>
<CheckedTextInput <TextInput
type="text"
value={match} value={match}
onChange={onMatchChange} onChange={onMatchChange}
validate={v => { onBlur={onMatchBlur}
try { error={matchError}
return (new RegExp(v), true);
} catch (e) {
return (e as Error).message;
}
}}
/> />
<div className={Margins.top8} /> <div className={Margins.top8} />
@ -374,7 +381,7 @@ function PatchHelper() {
{module && ( {module && (
<ReplacementComponent <ReplacementComponent
module={module} module={module}
match={new RegExp(match)} match={match}
replacement={replacement} replacement={replacement}
setReplacementError={setReplacementError} setReplacementError={setReplacementError}
/> />