From 590302c425e422e6323a54288b71da38e0260cb7 Mon Sep 17 00:00:00 2001 From: Omer Ozarslan Date: Thu, 1 Aug 2019 12:09:15 -0500 Subject: [PATCH 1/2] Support for user provided template parameter --- config_gen.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/config_gen.py b/config_gen.py index 54e89f9..c0c7590 100755 --- a/config_gen.py +++ b/config_gen.py @@ -42,6 +42,7 @@ def main(): parser.add_argument("--out-of-tree", action="store_true", help="Build autotools projects out-of-tree. This is a no-op for other project types.") parser.add_argument("--qt-version", choices=["4", "5"], default="5", help="Use the given Qt version for qmake. (Default: 5)") parser.add_argument("-e", "--preserve-environment", action="store_true", help="Pass environment variables to build processes.") + parser.add_argument("-t", "--template", help="Custom template file to base YCM config file on") parser.add_argument("PROJECT_DIR", help="The root directory of the project.") args = vars(parser.parse_args()) project_dir = os.path.abspath(args["PROJECT_DIR"]) @@ -96,8 +97,14 @@ def main(): del args["output"] del args["PROJECT_DIR"] + # determine template filename for YCM config + default_template_file = os.path.join(ycm_generator_dir, "template.py") + template_file = args.pop("template", default_template_file) + template_file = os.path.expanduser(template_file) + template_file = os.path.expandvars(template_file) + generate_conf = { - "ycm": generate_ycm_conf, + "ycm": lambda *args: generate_ycm_conf(*args, template_file=template_file), "cc": generate_cc_conf, }[output_format] @@ -435,13 +442,12 @@ def generate_cc_conf(flags, config_file): output.write(f + "\n") -def generate_ycm_conf(flags, config_file): +def generate_ycm_conf(flags, config_file, template_file): '''Generates the .ycm_extra_conf.py. flags: the list of flags - config_file: the path to save the configuration file at''' - - template_file = os.path.join(ycm_generator_dir, "template.py") + config_file: the path to save the configuration file at + template_file: template file to base config on''' with open(template_file, "r") as template: with open(config_file, "w") as output: From 6959741c85df88c8aa1ee112f9365ddd54281c36 Mon Sep 17 00:00:00 2001 From: Omer Ozarslan Date: Thu, 1 Aug 2019 12:17:01 -0500 Subject: [PATCH 2/2] Use regex for matching placeholders --- config_gen.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/config_gen.py b/config_gen.py index c0c7590..241debb 100755 --- a/config_gen.py +++ b/config_gen.py @@ -453,14 +453,18 @@ def generate_ycm_conf(flags, config_file, template_file): with open(config_file, "w") as output: output.write("# Generated by YCM Generator at {}\n\n".format(str(datetime.datetime.today()))) + placeholder_regex = re.compile(r"^(\s*)# INSERT FLAGS HERE$") + for line in template: - if(line == " # INSERT FLAGS HERE\n"): + match = placeholder_regex.match(line) + if(match): # insert generated code + indent = match.group(1) for flag in flags: if(isinstance(flag, basestring)): - output.write(" '{}',\n".format(flag)) + output.write("{}'{}',\n".format(indent, flag)) else: # is tuple - output.write(" '{}', '{}',\n".format(*flag)) + output.write("{}'{}', '{}',\n".format(indent, *flag)) else: # copy template