1
0
mirror of https://github.com/postgres/postgres.git synced 2025-10-18 04:29:09 +03:00

psql: Add COMPLETE_WITH_FILES and COMPLETE_WITH_GENERATOR macros.

While most tab completions in match_previous_words() use
COMPLETE_WITH* macros to wrap rl_completion_matches(), some direct
calls to rl_completion_matches() still remained.

This commit introduces COMPLETE_WITH_FILES and COMPLETE_WITH_GENERATOR
macros to replace these direct calls, enhancing both code consistency
and readability.

Author: Yugo Nagata <nagata@sraoss.co.jp>
Reviewed-by: Masahiko Sawada <sawada.mshk@gmail.com>
Discussion: https://postgr.es/m/20250605100835.b396f9d656df1018f65a4556@sraoss.co.jp
This commit is contained in:
Masahiko Sawada
2025-09-25 14:28:01 -07:00
parent 02c4bc8830
commit 76418a0b67

View File

@@ -443,6 +443,16 @@ do { \
matches = rl_completion_matches(text, complete_from_schema_query); \ matches = rl_completion_matches(text, complete_from_schema_query); \
} while (0) } while (0)
#define COMPLETE_WITH_FILES(escape, force_quote) \
do { \
completion_charp = escape; \
completion_force_quote = force_quote; \
matches = rl_completion_matches(text, complete_from_files); \
} while (0)
#define COMPLETE_WITH_GENERATOR(generator) \
matches = rl_completion_matches(text, generator)
/* /*
* Assembly instructions for schema queries * Assembly instructions for schema queries
* *
@@ -2182,7 +2192,7 @@ match_previous_words(int pattern_id,
/* for INDEX and TABLE/SEQUENCE, respectively */ /* for INDEX and TABLE/SEQUENCE, respectively */
"UNIQUE", "UNLOGGED"); "UNIQUE", "UNLOGGED");
else else
matches = rl_completion_matches(text, create_command_generator); COMPLETE_WITH_GENERATOR(create_command_generator);
} }
/* complete with something you can create or replace */ /* complete with something you can create or replace */
else if (TailMatches("CREATE", "OR", "REPLACE")) else if (TailMatches("CREATE", "OR", "REPLACE"))
@@ -2192,7 +2202,7 @@ match_previous_words(int pattern_id,
/* DROP, but not DROP embedded in other commands */ /* DROP, but not DROP embedded in other commands */
/* complete with something you can drop */ /* complete with something you can drop */
else if (Matches("DROP")) else if (Matches("DROP"))
matches = rl_completion_matches(text, drop_command_generator); COMPLETE_WITH_GENERATOR(drop_command_generator);
/* ALTER */ /* ALTER */
@@ -2203,7 +2213,7 @@ match_previous_words(int pattern_id,
/* ALTER something */ /* ALTER something */
else if (Matches("ALTER")) else if (Matches("ALTER"))
matches = rl_completion_matches(text, alter_command_generator); COMPLETE_WITH_GENERATOR(alter_command_generator);
/* ALTER TABLE,INDEX,MATERIALIZED VIEW ALL IN TABLESPACE xxx */ /* ALTER TABLE,INDEX,MATERIALIZED VIEW ALL IN TABLESPACE xxx */
else if (TailMatches("ALL", "IN", "TABLESPACE", MatchAny)) else if (TailMatches("ALL", "IN", "TABLESPACE", MatchAny))
COMPLETE_WITH("SET TABLESPACE", "OWNED BY"); COMPLETE_WITH("SET TABLESPACE", "OWNED BY");
@@ -3316,17 +3326,9 @@ match_previous_words(int pattern_id,
COMPLETE_WITH("FROM", "TO"); COMPLETE_WITH("FROM", "TO");
/* Complete COPY <sth> FROM|TO with filename */ /* Complete COPY <sth> FROM|TO with filename */
else if (Matches("COPY", MatchAny, "FROM|TO")) else if (Matches("COPY", MatchAny, "FROM|TO"))
{ COMPLETE_WITH_FILES("", true); /* COPY requires quoted filename */
completion_charp = "";
completion_force_quote = true; /* COPY requires quoted filename */
matches = rl_completion_matches(text, complete_from_files);
}
else if (Matches("\\copy", MatchAny, "FROM|TO")) else if (Matches("\\copy", MatchAny, "FROM|TO"))
{ COMPLETE_WITH_FILES("", false);
completion_charp = "";
completion_force_quote = false;
matches = rl_completion_matches(text, complete_from_files);
}
/* Complete COPY <sth> TO <sth> */ /* Complete COPY <sth> TO <sth> */
else if (Matches("COPY|\\copy", MatchAny, "TO", MatchAny)) else if (Matches("COPY|\\copy", MatchAny, "TO", MatchAny))
@@ -5427,9 +5429,9 @@ match_previous_words(int pattern_id,
else if (TailMatchesCS("\\h|\\help", MatchAny)) else if (TailMatchesCS("\\h|\\help", MatchAny))
{ {
if (TailMatches("DROP")) if (TailMatches("DROP"))
matches = rl_completion_matches(text, drop_command_generator); COMPLETE_WITH_GENERATOR(drop_command_generator);
else if (TailMatches("ALTER")) else if (TailMatches("ALTER"))
matches = rl_completion_matches(text, alter_command_generator); COMPLETE_WITH_GENERATOR(alter_command_generator);
/* /*
* CREATE is recognized by tail match elsewhere, so doesn't need to be * CREATE is recognized by tail match elsewhere, so doesn't need to be
@@ -5529,11 +5531,7 @@ match_previous_words(int pattern_id,
else if (TailMatchesCS("\\cd|\\e|\\edit|\\g|\\gx|\\i|\\include|" else if (TailMatchesCS("\\cd|\\e|\\edit|\\g|\\gx|\\i|\\include|"
"\\ir|\\include_relative|\\o|\\out|" "\\ir|\\include_relative|\\o|\\out|"
"\\s|\\w|\\write|\\lo_import")) "\\s|\\w|\\write|\\lo_import"))
{ COMPLETE_WITH_FILES("\\", false);
completion_charp = "\\";
completion_force_quote = false;
matches = rl_completion_matches(text, complete_from_files);
}
/* gen_tabcomplete.pl ends special processing here */ /* gen_tabcomplete.pl ends special processing here */
/* END GEN_TABCOMPLETE */ /* END GEN_TABCOMPLETE */