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

Offline AndrewCot

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 678
Re: Code completion using LSP and clangd
« Reply #30 on: January 13, 2022, 10:49:17 am »

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1549
Re: Code completion using LSP and clangd
« Reply #31 on: January 13, 2022, 11:47:05 am »
I have updated the Wiki with your suggestions

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5906
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Code completion using LSP and clangd
« Reply #32 on: January 13, 2022, 12:21:43 pm »
@ollydbg In the plugin manager you specify the zip file instead of a .cbplugin and it works

I have rename the .zip file to .cbplugin, because I can't select a zip file in the plugin manager to install the new plugin(NO zip files will be shown in the file select dialog).


In the Windows-LLVM-ClangD-Install-Readme.txt

Quote
MSYS2 Compiler - MinGW64
-------------------------
a) Install MSYS 2 clang packages via the msys2.exe bash shell:
    pacman -S mingw-w64-clang-x86_64-toolchain

Run this command, I got this:

Code
# pacman -S mingw-w64-clang-x86_64-toolchain
:: There are 22 members in group mingw-w64-clang-x86_64-toolchain:
:: Repository clang64
   1) mingw-w64-clang-x86_64-clang  2) mingw-w64-clang-x86_64-clang-analyzer
   3) mingw-w64-clang-x86_64-clang-tools-extra
   4) mingw-w64-clang-x86_64-compiler-rt  5) mingw-w64-clang-x86_64-crt-git
   6) mingw-w64-clang-x86_64-headers-git  7) mingw-w64-clang-x86_64-libc++
   8) mingw-w64-clang-x86_64-libc++abi  9) mingw-w64-clang-x86_64-libmangle-git
   10) mingw-w64-clang-x86_64-libunwind
   11) mingw-w64-clang-x86_64-libwinpthread-git  12) mingw-w64-clang-x86_64-lld
   13) mingw-w64-clang-x86_64-lldb  14) mingw-w64-clang-x86_64-llvm
   15) mingw-w64-clang-x86_64-make  16) mingw-w64-clang-x86_64-mlir
   17) mingw-w64-clang-x86_64-openmp  18) mingw-w64-clang-x86_64-pkgconf
   19) mingw-w64-clang-x86_64-polly  20) mingw-w64-clang-x86_64-tools-git
   21) mingw-w64-clang-x86_64-winpthreads-git
   22) mingw-w64-clang-x86_64-winstorecompat-git

Enter a selection (default=all):


Do I need to only select the option 1?
Or do I need to use the all?


EDIT:

clangd is contained in mingw-w64-clang-x86_64-clang-tools-extra, so normally we should use option 3.
« Last Edit: January 13, 2022, 12:25:21 pm 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: 5906
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Code completion using LSP and clangd
« Reply #33 on: January 13, 2022, 01:16:30 pm »
I also see the "LSP_DidOpen() error: [json.exception.type_error.316] invalid UTF-8 byte at index 1019: 0xB5" message box.

A quick search gives this related discussion:

[json.exception.type_error.316] invalid UTF-8 byte at index 1: 0xC3 Issue #1383 nlohmann/json

Invalid UTF-8 byte at index 1: 0x65 Issue #1831 nlohmann/json

I'm not sure how we pass the source file to clangd, maybe the file is not encoded in UTF8, but you pass the file to clangd as a UTF8 format file, and in this case, clangd return a string which is not encoded in UTF8.  And finally we get the exception message box.
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 BlueHazzard

  • Developer
  • Lives here!
  • *****
  • Posts: 3353
Re: Code completion using LSP and clangd
« Reply #34 on: January 13, 2022, 06:10:45 pm »
What is the encoding of your source file?

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2743
Re: Code completion using LSP and clangd
« Reply #35 on: January 13, 2022, 08:57:58 pm »
What is the encoding of your source file?

The data sent to clangd comes directly from the editors' wxTextctrl.
The encoding in the status bar for my CB shows windows-1252.
This problem only occurs on the incoming textDocument/completion response.
The data going out never gets a json exception. Only the data coming in.

How can I determine what the encoding is for the outgoing data?


Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2743
Re: Code completion using LSP and clangd
« Reply #36 on: January 13, 2022, 10:11:47 pm »
For the time being, I've coded a hack for the illegal utf8 problem.

It clears the illegal utf8 char and continues with the code completion.
The completion suggestions are correct so I'm ok with it.
I'll figure it out some other time.
« Last Edit: January 13, 2022, 10:17:04 pm by Pecan »

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5906
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Code completion using LSP and clangd
« Reply #37 on: January 15, 2022, 04:08:33 am »
I see your latest commit here in rev 27

* clangd_client - Code to remove invalid utf8 chars from clangd responses; cf., client.cpp/h DoValidateUTF8data()

I will test it, thanks!

EDIT updated:

The illegal utf8 problem still exits in rev 27.   :(
« Last Edit: January 15, 2022, 04:38:41 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 Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2743
Re: Code completion using LSP and clangd
« Reply #38 on: January 15, 2022, 06:53:26 am »
I see your latest commit here in rev 27

* clangd_client - Code to remove invalid utf8 chars from clangd responses; cf., client.cpp/h DoValidateUTF8data()

I will test it, thanks!

EDIT updated:

The illegal utf8 problem still exits in rev 27.   :(

Can you give me some guidance as to how you created the problem. I can't make it happen here.

What source are you using that got a utf8 invalid char?

What symbol were you trying to code complete.

What indication are you getting that there was an invalid utf8 char. A wxMessageBox or an error shown in the cb debug log?

If a message was in the CB debug log, what did it say?

Did you use the repo source or the download file ?
In plugin management, what is the version you see for clangd_client?

I don't think the download files have been updated yet.

Thanks for testing.
« Last Edit: January 15, 2022, 07:07:07 am by Pecan »

Offline AndrewCot

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 678
Re: Code completion using LSP and clangd
« Reply #39 on: January 15, 2022, 07:29:43 am »
The plugin download does not have the fix in it.

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5906
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Code completion using LSP and clangd
« Reply #40 on: January 15, 2022, 02:15:44 pm »
Hi, I try to debug this plugin, but I have always have this line hit:

Code
// ----------------------------------------------------------------------------
void CodeCompletion::OnAttach()
// ----------------------------------------------------------------------------
{
    AppVersion appVersion;
    appVersion.m_AppName = "clangd_client";
    // Set current plugin version
PluginInfo* pInfo = (PluginInfo*)(Manager::Get()->GetPluginManager()->GetPluginInfo(this));
pInfo->version = appVersion.GetVersion();

    // ccmanager's config obtained from Menu=>Settings=>Editor=>Code Completion (sub panel)
    // Get the CB config item that enables CodeCompletion
    ConfigManager* ccmcfg = Manager::Get()->GetConfigManager(_T("ccmanager"));
    m_CodeCompletionEnabled = ccmcfg->ReadBool(_T("/code_completion"), false);
    if (not m_CodeCompletionEnabled)
    {
        pInfo->version = pInfo->version.BeforeFirst(' ') + " Inactive";
        return;
    }


I have always get the m_CodeCompletionEnabled is false, and I have no idea how to enable it.

What I did is like below:

1, I have build a normal cbp:

Code
D:\code\cb\cb_sf_git\cccrash2019\src\CodeBlocks_wx31_64.cbp

I just build it.

2, I just open the downloaded clangd_client project, and I changed some build options:

In the build option custom variables, I set the variable:  TARGET_DEVEL_DIR
as the value: D:\code\cb\cb_sf_git\cccrash2019, which is the root folder of my local C::B git/svn

3, I changed the output filename as:
Code
$(TARGET_DEVEL_DIR)\src\devel31_64\share\CodeBlocks\plugins\clangd_client.dll

which means the build target(clangd_client.dll) will be put in the same folder as the other core plugins.

4,  I set the Execution working dir as:

Code
$(TARGET_DEVEL_DIR)\src\devel31_64

Which is the folder where codeblocks.exe locates.

5,  I change the post build script as:

Code
cmd /c if not exist $(TARGET_DEVEL_DIR)\src\devel31_64\share\CodeBlocks mkdir $(TARGET_DEVEL_DIR)\src\devel31_64\share\CodeBlocks
zip -jq9 $(TARGET_DEVEL_DIR)\src\devel31_64\share\CodeBlocks\clangd_client.zip src/resources/manifest.xml src/resources/*.xrc
zip -r9  $(TARGET_DEVEL_DIR)\src\devel31_64\share\CodeBlocks\clangd_client.zip src/resources/images > nul

This means I make a clangd_client.zip, and put it in the destination devel31_64 folder.




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: 2743
Re: Code completion using LSP and clangd
« Reply #41 on: January 15, 2022, 06:59:46 pm »
Hi, I try to debug this plugin, but I have always have this line hit:


I have always get the m_CodeCompletionEnabled is false, and I have no idea how to enable it.

What I did is like below:

1, I have build a normal cbp:

Code
D:\code\cb\cb_sf_git\cccrash2019\src\CodeBlocks_wx31_64.cbp

I just build it.

2, I just open the downloaded clangd_client project, and I changed some build options:

In the build option custom variables, I set the variable:  TARGET_DEVEL_DIR
as the value: D:\code\cb\cb_sf_git\cccrash2019, which is the root folder of my local C::B git/svn

3, I changed the output filename as:
Code
$(TARGET_DEVEL_DIR)\src\devel31_64\share\CodeBlocks\plugins\clangd_client.dll

which means the build target(clangd_client.dll) will be put in the same folder as the other core plugins.

4,  I set the Execution working dir as:

Code
$(TARGET_DEVEL_DIR)\src\devel31_64

Which is the folder where codeblocks.exe locates.

5,  I change the post build script as:

Code
cmd /c if not exist $(TARGET_DEVEL_DIR)\src\devel31_64\share\CodeBlocks mkdir $(TARGET_DEVEL_DIR)\src\devel31_64\share\CodeBlocks
zip -jq9 $(TARGET_DEVEL_DIR)\src\devel31_64\share\CodeBlocks\clangd_client.zip src/resources/manifest.xml src/resources/*.xrc
zip -r9  $(TARGET_DEVEL_DIR)\src\devel31_64\share\CodeBlocks\clangd_client.zip src/resources/images > nul

This means I make a clangd_client.zip, and put it in the destination devel31_64 folder.

The variable m_CodeCompletionEnabled should,in fact, be false.
Note that the line:     m_CodeCompletionEnabled = ccmcfg->ReadBool(_T("/code_completion"), false);

is asking if the old code_completion is enabled. It not asking if "/clangd_client" is enabled.
"/code_completion" must be disabled for clangd_client to run.
I'll change the comments to make that clearer.

Edit: 2022/01/15 10:30
I might be wrong about this. I'll do more checking.

The .cbp you've created look good.

Just a little more information.

Run or debug the CB you created the plugin for.
In the menu for the running debugged/run CB
Go to MainMenu=>Plugins=>Manage Plugins and tell us what the clangd_client plugin version is:


« Last Edit: January 15, 2022, 07:31:48 pm by Pecan »

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2743
Re: Code completion using LSP and clangd
« Reply #42 on: January 15, 2022, 09:51:15 pm »
@ollydbg

When you run the debugger, CB  ordinary uses the personality "default" which, for you, I'm guessing has CodeCompletion plugin enabled.

You should use a separate personality for debugging so that you can disable CodeCompletion in the debugged CB without disabling it in your production CB.

For me, in CB main menu/project/SetProgramArgument I have:
--debug-log --no-dde --no-check-associations --multiple-instance --no-splash-screen --verbose /p cbDebug315

Then I can disable CodeCompletion in the debugged CB MainMenu/plugins/managePlugins without it affecting the main CB personality "default".

I'd still like to see the "manage plugins" status of both clangd_client and Code Completion plugins to determine why your m_CodeCompletionEnabled variable is false.

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2743
Re: Code completion using LSP and clangd
« Reply #43 on: January 15, 2022, 10:32:40 pm »
@ollydbg

m_CodeCompletionEnabled variable refers to Settings/Editor/Code completion checkbox.

For your debugged CB, it must be currently unchecked which means neither Code completion nor clangd_client can run.


« Last Edit: January 15, 2022, 10:46:50 pm by Pecan »

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5906
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: Code completion using LSP and clangd
« Reply #44 on: January 16, 2022, 02:10:21 am »
@ollydbg

m_CodeCompletionEnabled variable refers to Settings/Editor/Code completion checkbox.

For your debugged CB, it must be currently unchecked which means neither Code completion nor clangd_client can run.

I have screen shots, see below as in attachment.

I have Settings/Editor/Code completion checkbox checked. If I member correctly this checkbox does not mean we should "disable" the plugin, instead, it means we don't show the suggestion prompt when we are editing.

The other screen shot shows the version latest version I'm using.

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.