Custom Project FIle/Directory Path Completion Contributor and Provider for YAML Files

Answered

I'm trying to create a custom completion contributor and provider where I can make it so certain YAML keys have completion with available file/directory paths in the current project.

E.g. If I have the following project directory structure:

dir1/
	dir2/
file1
somefile.yaml

then if I have a “path” key in the in somefile.yaml:

someparentkey:
	path: <...>

then autocomplete after “path: ” should have completion results of “dir1/” and “file1”, and if I choose “dir1/” then it should next provide completion results of “dir2/” and all other directories/files in that directory, etc.

Are there any resources on how to do this? I saw there was a FilePathCompletionContributor class but it doesn't entirely fit my needs since its Provider is not public, so I can't use it

Also, I was wondering if there was a way to have this contributor only apply when a certain condition is met (e.g., if the file has a specific name)?

0
4 comments

Hi Karol Lewandowski ,

Thank you for the reply! I had come across that post but I wasn't sure if it was out of date since it's a decade old, and I didn't quite understand some of the follow up replies at the end since the examples are no longer available.

Just to confirm, is the following reply still accurate:

The short answer is no, there is no such standard completion contributor.

There is a standard mechanism for path completion, using the FileReference/FileReferenceSet classes, but you need to do a little more work in order to use it (you need to implement the getReferences() method in your dummy PSI element, locate the dummy identifier inserted into your file by the completion mechanism, and return the references collected from a FileReferenceSet created at that location). Depending on your requirements, this approach may or may not be easier than simply hand-coding a completion contributor.

that is, it's likely more feasible to hand-coding a completion contributor by traversing the project structure to show a list of files/directories?

0

Hi Marcus,

I understand it might look outdated, but it doesn't seem so in this case.

Regarding the pasted examples, you can find them in older Git branches. As the answer containing examples was posted in January 2017, I suggest checking the 171 branch (171 means version 2017.1):

I'm not sure if it is the easiest approach in your case - using PathReferenceManager or FileReferenceSet. I would try with the simpler one, which seems to be PathReferenceManager and see if it's sufficient.

1

Okay, understood, thank you so much Karol, I'll look into the resources you provided!

0

Please sign in to leave a comment.