Author Topic: Code format support in clangd_client plugin  (Read 2178 times)

Offline christo

  • Multiple posting newcomer
  • *
  • Posts: 50
Code format support in clangd_client plugin
« on: May 02, 2025, 07:04:53 pm »
Many of the projects now a days use clang-format for formatting code. clangd has support for clang-format as well using textDocument/rangeFormatting  LSP request. Attaching patch which adds a  context menu item " Format use Clangd" and does the formatting on selecting that.

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2872
Re: Code format support in clangd_client plugin
« Reply #1 on: May 13, 2025, 05:59:20 pm »
Many of the projects now a days use clang-format for formatting code. clangd has support for clang-format as well using textDocument/rangeFormatting  LSP request. Attaching patch which adds a  context menu item " Format use Clangd" and does the formatting on selecting that.

Applied svn 13663. Thanks

Offline christo

  • Multiple posting newcomer
  • *
  • Posts: 50
Re: Code format support in clangd_client plugin
« Reply #2 on: May 13, 2025, 06:13:26 pm »
Thank you Pecan.

In the change applied in svn, menu item is added after astyle menu item. But if I disable astyle plugin, this leads to context menu not working.

Edit:
Following change fixes the issue.
Code
diff --git a/src/plugins/contrib/clangd_client/src/codecompletion/codecompletion.cpp b/src/plugins/contrib/clangd_client/src/codecompletion/codecompletion.cpp
index 0964bbd37..e122e4823 100644
--- a/src/plugins/contrib/clangd_client/src/codecompletion/codecompletion.cpp
+++ b/src/plugins/contrib/clangd_client/src/codecompletion/codecompletion.cpp
@@ -1073,11 +1073,9 @@ void ClgdCompletion::BuildModuleMenu(const ModuleType type, wxMenu* menu, const
         const wxString labelAStyle = _("Format use AStyle");
         const wxString label = _("Format use Clangd"); // (christo 25/05/02)
         int position = Manager::Get()->GetPluginManager()->FindSortedMenuItemPosition(*menu, labelAStyle);
-        wxMenuItem* item = menu->FindItemByPosition(position);
-        if (item)  {
-            wxString itemLabel = item->GetItemLabelText();
-            if (itemLabel == labelAStyle)
-                position += 1;
+        if (position != menu->GetMenuItemCount())
+        {
+            position += 1;
         }
         menu->Insert(position, idCodeFormatterActiveFile, label, _("Format the selected source code (selected line) in the current file"));
« Last Edit: May 13, 2025, 06:50:08 pm by christo »

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2872
Re: Code format support in clangd_client plugin
« Reply #3 on: May 14, 2025, 05:48:58 pm »
@ christo

In what way does it not work?
For me, if AStyle is enabled, it places "Format use clangd" just after the AStyle context menu item, else it appends it to the context menu.

Offline christo

  • Multiple posting newcomer
  • *
  • Posts: 50
Re: Code format support in clangd_client plugin
« Reply #4 on: May 14, 2025, 06:48:24 pm »
Hi Pecan, I'm seeing below error on right clicking.

../src/common/menucmn.cpp(623): assert ""position < m_items.GetCount()"" failed in FindItemByPosition(): wxMenu::FindItemByPosition(): invalid menu index

https://github.com/wxWidgets/wxWidgets/blob/v3.2.4/src/common/menucmn.cpp#L623

I've enabled debug mode of wxWidgets, that might be the reason error is seen for me, and NULL for you.

Screenshot attached.

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2872
Re: Code format support in clangd_client plugin
« Reply #5 on: May 14, 2025, 09:20:07 pm »
@ Christo

Thanks, fixed in svn 13664