Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IDEA]spec(rails): Interactive rails g arguments #2142

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
99 changes: 97 additions & 2 deletions src/rails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,11 +477,18 @@ const defaultCommands: Fig.Subcommand[] = [
],
},
{
name: "generate",
name: ["g", "generate"],
description: "Use templates to generate Rails resources",
args: [
{
name: "generator",
name: "generate",
suggestions: [
"model",
"resource",
"scaffold",
"controller",
"migration",
],
generators: {
script: ["rails", "g", "--help"],
postProcess(out) {
Expand Down Expand Up @@ -517,6 +524,94 @@ const defaultCommands: Fig.Subcommand[] = [
{
name: "args",
isVariadic: true,
generators: {
custom: async (tokens, executeShellCommand) => {
const handleFieldAutocomplete = (arg: string) => {
const types = [
"integer",
"primary_key",
"decimal",
"float",
"boolean",
"binary",
"string",
"text",
"date",
"time",
"datetime",
"belongs_to",
];

const cantIndex = ["belongs_to"];
if (arg.length === 0) {
return [{ name: "__column_name__", priority: 80 }];
}

if (arg.match(/^\w+$/)) {
return types.map((type) => ({ name: `${arg}:${type}` }));
}

if (arg.match(/^\w+:\w*/)) {
const [name, startOfType] = arg.split(":");

return types
.filter((type) => type.startsWith(startOfType))
.flatMap((type) => [
{ name: `${name}:${type}` },
...(cantIndex.includes(type)
? []
: [
{ name: `${name}:${type}:uniq` },
{ name: `${name}:${type}:index` },
]),
]);
}

return undefined;
};

const [generator, ...args] = tokens.slice(2);
if (["model", "resource", "scaffold"].includes(generator)) {
if (args.length === 1) {
return [{ name: "__model_name__", priority: 80 }];
}

const completions = handleFieldAutocomplete(args.at(-1));
return completions || [];

return [];
}

if (generator === "migration") {
if (args.length === 1) {
return [{ name: "__migration__name", priority: 80 }];
}

const completions = handleFieldAutocomplete(args.at(-1));
return completions || [];
}

if (generator === "controller") {
if (args.length === 1) {
return [{ name: "__controller__name", priority: 80 }];
}

return [
"index",
"show",
"new",
"create",
"edit",
"update",
"destroy",
]
.filter((action) => !args.includes(action))
.map((action) => ({ name: action }));
}

return [];
},
},
},
],
options: [
Expand Down