MaskRay / ccls

C/C++/ObjC language server supporting cross references, hierarchies, completion and semantic highlighting
Apache License 2.0
3.73k stars 254 forks source link

Add implement function feature #223

Open Yanpas opened 5 years ago

Yanpas commented 5 years ago

Cquery: https://github.com/cquery-project/cquery/blob/236118e5cfaf0fef38b496b124c99adc1e880aca/src/messages/text_document_code_action.cc#L148

I was talking about it in gitter today.

MaskRay commented 5 years ago

myrgy: "It might be great to have ability "create implementation" call. It should create method in corresponding cpp file.

Another useful refactoring tool is "Move to cpp"

Thank you in advance!"

SavchenkoValeriy commented 5 years ago

@MaskRay @myrgy I want to help implementing this issue.

Do you have any directions or original thoughts of how you see it? Here is a list of things it would be good to know so I can devise a good solution:

PS> I have good experience with Clang libTooling and LLVM, but none with LSPs and ccls.

MaskRay commented 5 years ago

These are good questions.

should it be a separate endpoint in the API (or even two)?

Assuming you have the definition in a .cc file, and you want to create its declaration in .h (or .hpp .hh), one extension request should be sufficient.

should it infer implementation file only based on its name or should it check where the implementations of other functions are? (what should we do if there is more than one file?)

The inference of the header file is difficult. We can use the basename, but there can be many false positives. Detecting the first #include in the .cc may be more robust.

is it a good approach to find a least enclosing function for a location (using FindSymbolsAtLocation) to find a function to extract? Or is it there a better function to do the job?

If the cursor is within the body, and you want to locate the function. messages/ccls_navigate.cc:FindParent has an example how this can be done.

what should it do with qualified names? Put fully-qualified name or shorten them based on using directives?

There can be many problems regarding this. If the header has namespace a { namespace b { and you want to insert a declaration a::b::f. You should probably nest it in a::b. However, if namespace a { namespace b { doesn't exist, you need to add namespace a { namespace b {, then declare f.

Given a declaration, add an implementation is even more problematic, because you can place b::f in namespace a {, f in namespace a::b {, or just f if using namespace a::b is used in the file. There is lots of uncertainty.

This is yet another good question:) I really don't know. Note, sometimes .cc can using something so the parameters in .cc can be simpler than the declaration in .hh.

FlexW commented 4 years ago

What is the status on this?