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

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2569
Re: Code completion using LSP and clangd
« Reply #300 on: March 20, 2023, 05:57:07 am »
since I build and installed clang16, the plug-in keep asking to specify the path to clangd, though it is in the same location as before, and even explicitly specifying the path does not help.


> which clangd
/opt/llvm/bin/clangd

> clangd --version
clangd version 16.0.0 (https://github.com/llvm/llvm-project.git 08d094a0e457360ad8b94b017d2dc277e697ca76)
Features: linux
Platform: x86_64-unknown-linux-gnu

Tell me how you installed it so I can do the same and reproduce the error.

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5429
Re: Code completion using LSP and clangd
« Reply #301 on: March 20, 2023, 05:06:03 pm »
I build it like this :

sudo rm -rf /opt/llvmTemp
rm -rf llvm-project
git clone --branch llvmorg-16.0.0 --depth=1 https://github.com/llvm/llvm-project.git

cd llvm-project
sed -i 's/set(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR_default ON)/set(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR_default OFF)/g' ./llvm/runtimes/CMakeLists.txt

mkdir build
cd build

cmake -G Ninja -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=/opt/llvmTemp -DLLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra;lld;lldb;openmp" -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libunwind;compiler-rt" ../llvm

ninja
sudo ninja install

#sudo 'echo "/opt/llvm/lib" > /etc/ld.so.conf.d/clanglib.conf' \
sudo ldconfig





note the echo line, you need to do this at least once, and off course also add /opt/llvm/bin yo your  PATH

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2569
Re: Code completion using LSP and clangd
« Reply #302 on: March 21, 2023, 06:14:30 pm »
since I build and installed clang16, the plug-in keep asking to specify the path to clangd, though it is in the same location as before, and even explicitly specifying the path does not help.


> which clangd
/opt/llvm/bin/clangd

> clangd --version
clangd version 16.0.0 (https://github.com/llvm/llvm-project.git 08d094a0e457360ad8b94b017d2dc277e697ca76)
Features: linux
Platform: x86_64-unknown-linux-gnu

Fixed svn Head rev 13241.

@Killerbot
Thanks for the instructions.
the fix works for me using Windows sublayer for Linux(WSL). Let us know if it works in your environment.
The problem was: The clangd version (16.0.0) no longer matches the clangd resources lib name (...\lib\clang\16\).
The version number used to be appended to the lib dir name.

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5429
Re: Code completion using LSP and clangd
« Reply #303 on: March 22, 2023, 06:23:41 pm »
confirmed solved,
and indeed on my 15.0.7 the dir is called /opt/llvm/lib/clang/15.0.7/

let's see when 16.0.1 comes ....

Offline Trigger

  • Single posting newcomer
  • *
  • Posts: 2
Re: Code completion using LSP and clangd
« Reply #304 on: May 09, 2023, 06:05:26 pm »
Hi!

I've recently started to work with SMT32 microcontrollers, and I discovered that standard Code::Blocks's code completion plugin doesn't work very well in some specific cases like this one:
Code
#define GPIOA               ((GPIO_TypeDef *) GPIOA_BASE)
GPIOA_BASE is not recognized correctly as a pointer to the structure of type GPIO_TypeDef, and Code::Blocks doesn't give any autocomplete suggestions after I type "GPIOA_BASE->" (see https://forums.codeblocks.org/index.php/topic,21823.0.html). So I decided to try this plugin.

So I installed the latest nightly build (06 May 2023 build (13268)) of C::B and clangd 16.0.2. Then I disabled the original code completion plugin, enabled clang_client and set the path to the clang executable. But when I tried to open my STM32 project, I got an error message (see the attached screenshot). It is obvious that there was some encoding problem caused by Cyrillic characters in the project path. Usually in such cases I create symbolic links/directory junctions with appropriate English-only directory names, but in this case I can't fix the problem this way because clangd doesn't work great with symlinks.

I decided to download the sources of C::B and plugins and try to fix the problem myself. And I think I managed to get it to work correctly. I've changed the file plugins/contrib/clangd_client/src/LSPclient/client.cpp. Here is the diff output:
Code
Index: client.cpp
===================================================================
--- client.cpp (revision 13268)
+++ client.cpp (working copy)
@@ -1890,7 +1890,7 @@
         writeClientLog(StdString_Format("<<< Initialize(): %s", stdDirName.c_str()) );
 
         // Set the project folder as the folder containing the commands file. //(ollydbg 2022/10/19) Ticket #75
-        try { Initialize(string_ref(fileUtils.FilePathToURI(dirname)), string_ref(dirname.ToUTF8())); } //(ollydbg 2022/10/19) ticket #75
+        try { Initialize(string_ref(fileUtils.FilePathToURI(dirname.ToUTF8())), string_ref(dirname.ToUTF8())); } //(ollydbg 2022/10/19) ticket #75
         catch(std::exception &err)
         {
             //printf("read error -> %s\nread -> %s\n ", e.what(), read.c_str());

I'm not sure if I've done it right, but it seems that the plugin is working fine. Maybe this information will be helpful for the further development of the clangd-client plugin.

Best regards,
Arseniy.

P.S. Thank you @Pecan and the other developers who works on this plugin. It's a great idea to integrate C::B with clangd!
« Last Edit: May 09, 2023, 08:05:21 pm by Trigger »

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2569
Re: Code completion using LSP and clangd
« Reply #305 on: May 11, 2023, 08:23:30 pm »
@ Trigger
Your fix applied in head rev 13271. Thanks.