Author Topic: Code completion using LSP and clangd  (Read 72218 times)

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5737
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Code completion using LSP and clangd
« Reply #285 on: December 26, 2022, 10:37:36 am »
I have some situation that there are many information messages showing: LSP: File not yet parsed.

The step is:

1, double click on a file in the source navigation tree, and a new editor is opened.
2, mouse hover on some symbols in the editor.
3, the information massage happens.

Sometimes, I got this information messages showing many times, I think there is a logic error.

... quote modified by pecan ...
Finally !! Caught and fixed in Head rev 13134
It seems clangd server started (at some point) sending an empty textDocument/publishDiagnostics
response (with a missing "version" entry) to a didClose() request, which really confused clangd_client.

Thanks ollydbg :>)

Hi, thanks, I will rebuild my C::B and test it.

EDIT:

It looks there is a typo in this commit.

Code
-#define VERSION wxT("1.2.60 22/12/22")
+#define VERSION wxT("1.2.611 22/12/24")
« Last Edit: December 26, 2022, 10:39:42 am by ollydbg »
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5737
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Code completion using LSP and clangd
« Reply #286 on: December 28, 2022, 09:43:03 am »
Hi, I see one issue today.

In the latest clangd_client (0.61), I see that "find declaration" and "find implementation" context menu item just do the same thing.

Here, I can see when I click on either item, it just jump between the function declaration and implementation.
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2530
Re: Code completion using LSP and clangd
« Reply #287 on: December 28, 2022, 08:24:34 pm »
Hi, I see one issue today.

In the latest clangd_client (0.61), I see that "find declaration" and "find implementation" context menu item just do the same thing.

Here, I can see when I click on either item, it just jump between the function declaration and implementation.

That's how clangd works.
Clangd doesn't seem to distinguish between the declaration and definition. It only has a "GoToDefinition" request.
If there's a missing .h or .cpp file, Find Declaration and Find Implementation mean the same thing to clangd.

I've tried adding code to determine which to display.
Given the request came from an .h file I try to display the .cpp file entry and vise versa.

Could you give me some example code that is causing the problem.
Maybe I can be more precise in showing the declaration vs the definition.
« Last Edit: December 28, 2022, 08:34:38 pm by Pecan »

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5387
Re: Code completion using LSP and clangd
« Reply #288 on: January 01, 2023, 08:05:49 pm »
I see that the plug-in sometimes put a read square on the left hand side of the editor (because it spots something which is not ok), but where can I see the "message/diagnostic", so I know what it is telling me ?

Offline BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3289
Re: Code completion using LSP and clangd
« Reply #289 on: January 01, 2023, 08:37:13 pm »
Quote
I see that the plug-in sometimes put a read square on the left hand side of the editor (because it spots something which is not ok), but where can I see the "message/diagnostic", so I know what it is telling me ?
In the LSP messages tab in the log pane

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5387
Re: Code completion using LSP and clangd
« Reply #290 on: January 01, 2023, 11:02:38 pm »
found it, thanks.

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5737
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Code completion using LSP and clangd
« Reply #291 on: January 08, 2023, 04:03:46 am »
Could you give me some example code that is causing the problem.
Maybe I can be more precise in showing the declaration vs the definition.

I think every C++ member function will demonstrate this issue.

For example, I just searched a simple C++ tutorial here: C++ Class Member Functions

And the test code is below:

Code
#include <iostream>

using namespace std;

class Box {
   public:
      double length;         // Length of a box
      double breadth;        // Breadth of a box
      double height;         // Height of a box

      // Member functions declaration
      double getVolume(void);
      void setLength( double len );
      void setBreadth( double bre );
      void setHeight( double hei );
};

// Member functions definitions
double Box::getVolume(void) {
   return length * breadth * height;
}

void Box::setLength( double len ) {
   length = len;
}
void Box::setBreadth( double bre ) {
   breadth = bre;
}
void Box::setHeight( double hei ) {
   height = hei;
}

// Main function for the program
int main() {
   Box Box1;                // Declare Box1 of type Box
   Box Box2;                // Declare Box2 of type Box
   double volume = 0.0;     // Store the volume of a box here

   // box 1 specification
   Box1.setLength(6.0);
   Box1.setBreadth(7.0);
   Box1.setHeight(5.0);

   // box 2 specification
   Box2.setLength(12.0);
   Box2.setBreadth(13.0);
   Box2.setHeight(10.0);

   // volume of box 1
   volume = Box1.getVolume();
   cout << "Volume of Box1 : " << volume <<endl;

   // volume of box 2
   volume = Box2.getVolume();
   cout << "Volume of Box2 : " << volume <<endl;
   return 0;
}

Find declaration of function "getVolume", will jump between from the function declaration in C++ class definition and function body "double Box::getVolume(void)".

BTW: sorry for the late reply, I just recovered from COVID-19 infection.

If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2530
Re: Code completion using LSP and clangd
« Reply #292 on: January 09, 2023, 06:22:50 pm »
Could you give me some example code that is causing the problem.
Maybe I can be more precise in showing the declaration vs the definition.

I think every C++ member function will demonstrate this issue.

For example, I just searched a simple C++ tutorial here: C++ Class Member Functions

And the test code is below:

Code
#include <iostream>

using namespace std;

class Box {
   public:
      double length;         // Length of a box
      double breadth;        // Breadth of a box
      double height;         // Height of a box

      // Member functions declaration
      double getVolume(void);
      void setLength( double len );
      void setBreadth( double bre );
      void setHeight( double hei );
};

// Member functions definitions
double Box::getVolume(void) {
   return length * breadth * height;
}

void Box::setLength( double len ) {
   length = len;
}
void Box::setBreadth( double bre ) {
   breadth = bre;
}
void Box::setHeight( double hei ) {
   height = hei;
}

// Main function for the program
int main() {
   Box Box1;                // Declare Box1 of type Box
   Box Box2;                // Declare Box2 of type Box
   double volume = 0.0;     // Store the volume of a box here

   // box 1 specification
   Box1.setLength(6.0);
   Box1.setBreadth(7.0);
   Box1.setHeight(5.0);

   // box 2 specification
   Box2.setLength(12.0);
   Box2.setBreadth(13.0);
   Box2.setHeight(10.0);

   // volume of box 1
   volume = Box1.getVolume();
   cout << "Volume of Box1 : " << volume <<endl;

   // volume of box 2
   volume = Box2.getVolume();
   cout << "Volume of Box2 : " << volume <<endl;
   return 0;
}

Find declaration of function "getVolume", will jump between from the function declaration in C++ class definition and function body "double Box::getVolume(void)".

BTW: sorry for the late reply, I just recovered from COVID-19 infection.

Is this source all in one .cpp file or split between a .h and .cpp source file ?

Glad you made it through covid. I know how awful that suff is. I got it before there was any vaccine. It's dangerous.

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5737
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Code completion using LSP and clangd
« Reply #293 on: January 10, 2023, 03:41:07 am »
Is this source all in one .cpp file or split between a .h and .cpp source file ?
If you put all the source code in a single .cpp file, find declaration will jump only in a single file between two lines. First from one line to another, and return back.
If you split them in a .h and .cpp file, you will jump between the two files.


Quote
Glad you made it through covid. I know how awful that suff is. I got it before there was any vaccine. It's dangerous.
Thanks, I wish all our devs and C::B users good health, happy coding!
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2530
Re: Code completion using LSP and clangd
« Reply #294 on: January 14, 2023, 10:28:10 pm »
Hi, I see one issue today.

In the latest clangd_client (0.61), I see that "find declaration" and "find implementation" context menu item just do the same thing.

Here, I can see when I click on either item, it just jump between the function declaration and implementation.

This "bounce" behavior is how clangd is currently working.
If the user asks for a declaration while positioned on it, clangd will respond with the implementation location and vise versa.
 
They seem to think that's how a "smart editor" should act.
i.e., "Do what I meant to do" behavior. Read my mind, not my mouse.
https://github.com/clangd/clangd/issues/713

I've spent 25 hours trying to get this to behave how the old CodeCompletion behaved.
Alas, I've failed, since it would require parsing the file text of the "GoTo/Find"response. That would get us back to the problem we're trying to get away from, namely parsing c++ code.

I'm leaving it as is until (maybe) clangd gives us an option to circumvent this behavior.

Suggestions welcomed.
« Last Edit: January 15, 2023, 07:24:02 am by Pecan »

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5737
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Code completion using LSP and clangd
« Reply #295 on: January 16, 2023, 06:44:27 am »
Hi, I see one issue today.

In the latest clangd_client (0.61), I see that "find declaration" and "find implementation" context menu item just do the same thing.

Here, I can see when I click on either item, it just jump between the function declaration and implementation.

This "bounce" behavior is how clangd is currently working.
If the user asks for a declaration while positioned on it, clangd will respond with the implementation location and vise versa.
 
They seem to think that's how a "smart editor" should act.
i.e., "Do what I meant to do" behavior. Read my mind, not my mouse.
https://github.com/clangd/clangd/issues/713

I've spent 25 hours trying to get this to behave how the old CodeCompletion behaved.
Alas, I've failed, since it would require parsing the file text of the "GoTo/Find"response. That would get us back to the problem we're trying to get away from, namely parsing c++ code.

I'm leaving it as is until (maybe) clangd gives us an option to circumvent this behavior.

Suggestions welcomed.

Thanks, especially for your time and effort to try to fix the issue. I just add one comment on that clangd github issue.

Here are my guess:

To distinguish the definition and declaration. If we have to two locations: one in header file and the other in implementation file. This means the former is the declaration and the later is the definition.

If we can't distinguish by the file extension, then I guess the declaration have the ";" at the end of the statement, and the definition usually don't. Maybe a function definition may have a "{" at the beginning. (I think this is the way our build-in C++ parser parses the file, maybe ctags universal-ctags/ctags: A maintained ctags implementation, the one used in CodeLite for its native C++ parser also did that similar things.)

If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.