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

Offline christo

  • Multiple posting newcomer
  • *
  • Posts: 32
Re: Code completion using LSP and clangd
« Reply #375 on: March 19, 2024, 06:21:49 pm »
Hi Pecan,

I found two issues with applying fix in my setup. I'm using clangd version 18.0.0
1. "fixes available" along with "fix available"
2. Multi line fixes, eg: unused header warnings.

Below modification helped to apply these fixes.
Code
diff --git a/src/plugins/contrib/clangd_client/src/LSPclient/lspdiagresultslog.cpp b/src/plugins/contrib/clangd_client/src/LSPclient/lspdiagresultslog.cpp
index b64e7d760..425268724 100644
--- a/src/plugins/contrib/clangd_client/src/LSPclient/lspdiagresultslog.cpp
+++ b/src/plugins/contrib/clangd_client/src/LSPclient/lspdiagresultslog.cpp
@@ -256,7 +256,7 @@ void LSPDiagnosticsResultsLog::OnApplyFixIfAvailable(wxCommandEvent& event) //(p
     {
         // Got the selected item index
         selectedLineText = GetItemAsText(itemIndex);
-        if (not selectedLineText.Contains(" (fix available) "))
+        if (not (selectedLineText.Contains(" (fix available) ") or (selectedLineText.Contains("(fixes available)"))))
         {
             wxString msg = wxString::Format(_("No Fix available for logLine(%d)"), int(itemIndex) );
             InfoWindow::Display(_("NO fix"), msg);
diff --git a/src/plugins/contrib/clangd_client/src/codecompletion/parser/parser.cpp b/src/plugins/contrib/clangd_client/src/codecompletion/parser/parser.cpp
index b97e46a30..2ef369352 100644
--- a/src/plugins/contrib/clangd_client/src/codecompletion/parser/parser.cpp
+++ b/src/plugins/contrib/clangd_client/src/codecompletion/parser/parser.cpp
@@ -3688,15 +3688,17 @@ void Parser::OnRequestCodeActionApply(wxCommandEvent& event) //(ph 2024/02/12)
         int startLine; // 1 origin; needs to be changed to zero origin
         int lineStartCol;
         int lineEndCol;
+        int endLine;
 
         codeActionStr  = FixesFound[ii];
         try {
             // std::string testData = "{\"newText\":\"int\",\"range\":{\"end\":{\"character\":8,\"line\":275},\"start\":{\"character\":4,\"line\":275}}}"; // **Debugging**
             nlohmann::json jCodeAction = nlohmann::json::parse(codeActionStr.ToStdString());
             newText      = jCodeAction["newText"].get<std::string>();
-            startLine    = lineNumInt; // it's already 1 origin
+            startLine    = jCodeAction["range"]["start"]["line"] ;
             lineStartCol = jCodeAction["range"]["start"]["character"] ;
             lineEndCol   = jCodeAction["range"]["end"]["character"] ;
+            endLine      = jCodeAction["range"]["end"]["line"] ;
         }
         catch(std::exception &err)
         {
@@ -3708,9 +3710,12 @@ void Parser::OnRequestCodeActionApply(wxCommandEvent& event) //(ph 2024/02/12)
         // pEd contains the cbEditor ptr from above
         cbStyledTextCtrl* pControl = pEd->GetControl();
          // Replace text; note that the startLine is from the log msg line, so it's 1 origin
-        int linePosn = pControl->PositionFromLine(startLine-1); // use zero origin for line
-        pControl->SetTargetStart(linePosn + lineStartCol);
-        pControl->SetTargetEnd(linePosn + lineEndCol );
+        int linePosn = pControl->PositionFromLine(startLine); // use zero origin for line
+        int targetStart = linePosn + lineStartCol;
+        pControl->SetTargetStart(targetStart);
+        int lineEndPosn = pControl->PositionFromLine(endLine);
+        int targetEnd = lineEndPosn + lineEndCol;
+        pControl->SetTargetEnd(targetEnd);
         pControl->ReplaceTarget(newText);
     }//endfor FixesFound
 

Thanks, Christo

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2802
Re: Code completion using LSP and clangd
« Reply #376 on: March 20, 2024, 08:33:50 pm »
Hi Pecan,

I found two issues with applying fix in my setup. I'm using clangd version 18.0.0
1. "fixes available" along with "fix available"
2. Multi line fixes, eg: unused header warnings.

Below modification helped to apply these fixes.
Code
diff --git a/src/plugins/contrib/clangd_client/src/LSPclient/lspdiagresultslog.cpp b/src/plugins/contrib/clangd_client/src/LSPclient/lspdiagresultslog.cpp
index b64e7d760..425268724 100644
--- a/src/plugins/contrib/clangd_client/src/LSPclient/lspdiagresultslog.cpp
+++ b/src/plugins/contrib/clangd_client/src/LSPclient/lspdiagresultslog.cpp
@@ -256,7 +256,7 @@ void LSPDiagnosticsResultsLog::OnApplyFixIfAvailable(wxCommandEvent& event) //(p
     {
         // Got the selected item index
         selectedLineText = GetItemAsText(itemIndex);
-        if (not selectedLineText.Contains(" (fix available) "))
+        if (not (selectedLineText.Contains(" (fix available) ") or (selectedLineText.Contains("(fixes available)"))))
         {
             wxString msg = wxString::Format(_("No Fix available for logLine(%d)"), int(itemIndex) );
             InfoWindow::Display(_("NO fix"), msg);
diff --git a/src/plugins/contrib/clangd_client/src/codecompletion/parser/parser.cpp b/src/plugins/contrib/clangd_client/src/codecompletion/parser/parser.cpp
index b97e46a30..2ef369352 100644
--- a/src/plugins/contrib/clangd_client/src/codecompletion/parser/parser.cpp
+++ b/src/plugins/contrib/clangd_client/src/codecompletion/parser/parser.cpp
@@ -3688,15 +3688,17 @@ void Parser::OnRequestCodeActionApply(wxCommandEvent& event) //(ph 2024/02/12)
         int startLine; // 1 origin; needs to be changed to zero origin
         int lineStartCol;
         int lineEndCol;
+        int endLine;
 
         codeActionStr  = FixesFound[ii];
         try {
             // std::string testData = "{\"newText\":\"int\",\"range\":{\"end\":{\"character\":8,\"line\":275},\"start\":{\"character\":4,\"line\":275}}}"; // **Debugging**
             nlohmann::json jCodeAction = nlohmann::json::parse(codeActionStr.ToStdString());
             newText      = jCodeAction["newText"].get<std::string>();
-            startLine    = lineNumInt; // it's already 1 origin
+            startLine    = jCodeAction["range"]["start"]["line"] ;
             lineStartCol = jCodeAction["range"]["start"]["character"] ;
             lineEndCol   = jCodeAction["range"]["end"]["character"] ;
+            endLine      = jCodeAction["range"]["end"]["line"] ;
         }
         catch(std::exception &err)
         {
@@ -3708,9 +3710,12 @@ void Parser::OnRequestCodeActionApply(wxCommandEvent& event) //(ph 2024/02/12)
         // pEd contains the cbEditor ptr from above
         cbStyledTextCtrl* pControl = pEd->GetControl();
          // Replace text; note that the startLine is from the log msg line, so it's 1 origin
-        int linePosn = pControl->PositionFromLine(startLine-1); // use zero origin for line
-        pControl->SetTargetStart(linePosn + lineStartCol);
-        pControl->SetTargetEnd(linePosn + lineEndCol );
+        int linePosn = pControl->PositionFromLine(startLine); // use zero origin for line
+        int targetStart = linePosn + lineStartCol;
+        pControl->SetTargetStart(targetStart);
+        int lineEndPosn = pControl->PositionFromLine(endLine);
+        int targetEnd = lineEndPosn + lineEndCol;
+        pControl->SetTargetEnd(targetEnd);
         pControl->ReplaceTarget(newText);
     }//endfor FixesFound
 

Thanks, Christo

@ christo
Thanks for your work on this.
When there are multiple extraneous #includes in the same file, the patch is taking out the wrong lines after the first one is successful.

So I'll hold onto this until I have time to find out why.

« Last Edit: March 20, 2024, 10:06:49 pm by Pecan »

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2802
Re: Code completion using LSP and clangd
« Reply #377 on: March 22, 2024, 06:25:57 pm »
Applied modified christo patch to Clangd_client at Head rev 13493.
The patch supports additional fixes from Clangd version 18.
Thanks christo.

Offline gd_on

  • Lives here!
  • ****
  • Posts: 799
Re: Code completion using LSP and clangd
« Reply #378 on: March 22, 2024, 06:47:38 pm »
are you planning to introduce other patches by christo as :
https://forums.codeblocks.org/index.php/topic,25570.msg174618.html#msg174618 to mark warnings in yellow, errors still in red
and
https://forums.codeblocks.org/index.php/topic,25659.msg174619.html#msg174619 to show dignostics by clicking

I think the fisrt one is useful, but of course need to be updated
Windows 11 64 bits (23H2), svn C::B (last version or almost!), wxWidgets 3.2.5 (tests with 3.3), Msys2 Compilers 14.1.0, 64 bits (seh, posix : gcc, g++ and gfortran in C:\msys64\mingw64) or 32 bits (dwarf2, posix  in C:\msys64\mingw32).

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2802
Re: Code completion using LSP and clangd
« Reply #379 on: March 22, 2024, 08:38:45 pm »
are you planning to introduce other patches by christo as :
https://forums.codeblocks.org/index.php/topic,25570.msg174618.html#msg174618 to mark warnings in yellow, errors still in red
and
https://forums.codeblocks.org/index.php/topic,25659.msg174619.html#msg174619 to show dignostics by clicking

I think the fisrt one is useful, but of course need to be updated

I'll have a look at them. Thanks for the reminder.

Offline christo

  • Multiple posting newcomer
  • *
  • Posts: 32
Re: Code completion using LSP and clangd
« Reply #380 on: March 24, 2024, 11:58:39 am »
Thanks Pecan.

I face another issue now - regarding errors with fixes in different line, like errors due to missing includes,  fix is not applied. It shows "No available fixes found" on applying. In Parser::OnRequestCodeActionApply() , there is a check if the fix is in same line that of the line causing the error. This might be causing the issue.

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2802
Re: Code completion using LSP and clangd
« Reply #381 on: March 24, 2024, 05:57:53 pm »
Thanks Pecan.

I face another issue now - regarding errors with fixes in different line, like errors due to missing includes,  fix is not applied. It shows "No available fixes found" on applying. In Parser::OnRequestCodeActionApply() , there is a check if the fix is in same line that of the line causing the error. This might be causing the issue.

Can you give me a 1,2,3 list of a way to re-create the situation?

Offline christo

  • Multiple posting newcomer
  • *
  • Posts: 32
Re: Code completion using LSP and clangd
« Reply #382 on: March 25, 2024, 02:59:56 am »
Thanks Pecan.

I face another issue now - regarding errors with fixes in different line, like errors due to missing includes,  fix is not applied. It shows "No available fixes found" on applying. In Parser::OnRequestCodeActionApply() , there is a check if the fix is in same line that of the line causing the error. This might be causing the issue.

Can you give me a 1,2,3 list of a way to re-create the situation?

Hi Pecan,

1. Create a new console application project
2. replace code in main.cpp with below code
Code
int main()
{
    std::cout << "Hello";
    return 0;
}

3. Clangd (I'm using v18) gives below diagnostic
Quote
/home/christo/projects/Test/test2/main.cpp|3|error:Use of undeclared identifier 'std' (fix available) Include <iostream> for symbol std::cout|
4. Right click and select "Apply fix if available"
5. Error pop-up is shown instead of applying the fix  "No available fixes found"

Offline Grit Clef

  • Multiple posting newcomer
  • *
  • Posts: 92
  • Where there is a will, there is a way.
Re: Code completion using LSP and clangd
« Reply #383 on: March 25, 2024, 11:56:01 am »
Yes, I use Clangd 18 and got the same problem, too.
-Windows 7, 32-bit
-CodeBlocks r13542, gcc 14.2.0, debug version

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2802
Re: Code completion using LSP and clangd
« Reply #384 on: March 25, 2024, 06:44:55 pm »
Yes, I use Clangd 18 and got the same problem, too.

Yep, my assumption error. The error is from line 2 but the fix is to line 0, and my code is looking for a fix to line 2.
Wups

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2802
Re: Code completion using LSP and clangd
« Reply #385 on: March 26, 2024, 02:01:45 am »
Thanks Pecan.

I face another issue now - regarding errors with fixes in different line, like errors due to missing includes,  fix is not applied. It shows "No available fixes found" on applying. In Parser::OnRequestCodeActionApply() , there is a check if the fix is in same line that of the line causing the error. This might be causing the issue.

Fixed Head rev. 13494
Thanks for the reports.

Offline christo

  • Multiple posting newcomer
  • *
  • Posts: 32
Re: Code completion using LSP and clangd
« Reply #386 on: March 26, 2024, 04:57:47 am »
Fixed Head rev. 13494
Thanks for the reports.
It works now, thank you Pecan for the quick response as always.

Offline tomay3000

  • Multiple posting newcomer
  • *
  • Posts: 61
Re: Code completion using LSP and clangd
« Reply #387 on: April 04, 2024, 03:48:27 pm »
- "Find declaration of: ..." does not work for me.
- "Reparse this project" has become a must so that the menu entry "Find declaration of: ..." appears.

What is going wrong ?

OS : Windows 7 with SP1 x64
C::B : Nightly build 13496
clangd : 18.1.2

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2802
Re: Code completion using LSP and clangd
« Reply #388 on: April 04, 2024, 06:09:29 pm »
- "Find declaration of: ..." does not work for me.
- "Reparse this project" has become a must so that the menu entry "Find declaration of: ..." appears.

What is going wrong ?

OS : Windows 7 with SP1 x64
C::B : Nightly build 13496
clangd : 18.1.2

Not enough info.
Can you give us a simple example (code) to recreate the problem?
What steps can we perform to recreate the problem?

Offline tomay3000

  • Multiple posting newcomer
  • *
  • Posts: 61
Re: Code completion using LSP and clangd
« Reply #389 on: April 07, 2024, 01:16:05 pm »
- "Find declaration of: ..." does not work for me.
- "Reparse this project" has become a must so that the menu entry "Find declaration of: ..." appears.

What is going wrong ?

OS : Windows 7 with SP1 x64
C::B : Nightly build 13496
clangd : 18.1.2

Not enough info.
Can you give us a simple example (code) to recreate the problem?
What steps can we perform to recreate the problem?

Here is my C::B project
Note that if I revert to using Code Completion back, then "Find declaration of: ..." starts working as expected.