reddit / emacs

Why hints for C# are so poor?

EmilySeville7cfg · 2022-08-04 11:07
I've installed .NET 6 and the following packages in Emacs: `soft-stone-theme`, `flycheck`, `markdown-mode`, `json-mode`, `yaml-mode`, `csharp-mode`, `format-all`, `lsp-mode`, `lsp-pyright`, `company`. Then I've created a new C# project via `dotnet new console` and opened `Program.cs` via `emacs -nw Program.cs`. I've started company mode and lsp one via these consecutive commands: `M-x company-mode`, `M-x lsp`. I've expected to see list of available methods of `Console` class but nothing is shown except `WriteLine`. I use [this](https://emacs-lsp.github.io/lsp-mode/page/lsp-csharp-omnisharp/) LSP for C#. Is it normal behavior? Why Python support is better here? Maybe I have to configure smth to fix it? [No methods are shown](https://preview.redd.it/uyv4re0piof91.png?width=1920&format=png&auto=webp&s=7db3ec324ebd831731b2d30bb953539249347fd0)
unhammer · 2022-08-04 12:14
Hm, I still use [omnisharp-mode](https://github.com/OmniSharp/omnisharp-emacs) instead of lsp, with (setq omnisharp-expected-server-version "1.32.0") ; https://github.com/OmniSharp/omnisharp-vscode/issues/1450#issuecomment-432516876 (let ((dotnet-version (string-trim (shell-command-to-string "dotnet --version")))) ;; https://github.com/OmniSharp/omnisharp-emacs/issues/459#issuecomment-452656947 (setenv "MSBuildSDKsPath" (format "/usr/share/dotnet/sdk/%s/Sdks" dotnet-version))) which gives me [https://i.imgur.com/2heqHcr.png](https://i.imgur.com/2heqHcr.png) (could be that the MSBuildSDKsPath variable is the only thing you need though)
sebhoagie · 2022-08-04 15:27
It's been a few months now since I used C# in Emacs, but two things to try: 1. Build the project at least once, that helped me in many cases (mostly with custom types, not BCL items like in this case) 2. I never had to set the MSBuild path like /u/unhammer does, but I sure did have to install the Mono MSBuild packages (in Linux) for LSP to work properly. In my experience, you can definitely work with C#/dotnet in Emacs daily, I did it for 3+ years. The only thing I sometimes missed was the Visual Studio debugger, but the kind of things we developed at job-- favored deploying and checking out logs (hard to test locally because of external services dependencies). If it helps, my C# and LSP setup is here: [https://github.com/sebasmonia/dotfiles/blob/d88bb5f28ead7fb2593caaf5964bd9e15e382954/.emacs/init.el#L127](https://github.com/sebasmonia/dotfiles/blob/d88bb5f28ead7fb2593caaf5964bd9e15e382954/.emacs/init.el#L127) ​ [https://github.com/sebasmonia/dotfiles/blob/d88bb5f28ead7fb2593caaf5964bd9e15e382954/.emacs/init.el#L488-L539](https://github.com/sebasmonia/dotfiles/blob/d88bb5f28ead7fb2593caaf5964bd9e15e382954/.emacs/init.el#L488-L539) And, shameless plug :) you might find [https://github.com/sebasmonia/sharper](https://github.com/sebasmonia/sharper) useful (I should probably add a disclaimer that I stopped using it daily...)
[deleted] · 2022-08-04 17:11
[removed]
EmilySeville7cfg · 2022-08-04 17:27
I've tried using Visual Studio/Rider, but they are not so lightweight as terminal editors. As an alternative I consider VS Code or Sublime Text. 🤔
elizadev · 2022-08-04 18:05
I've personally had weird issues with the version of omnisharp that LSP mode downloads, and a lot of trouble with C# and Emacs in general (though usually due to Unity). Installing mono and setting `FrameworkPathOverride` may help, as in this article: https://eliza.sh/2021-06-01-using-unity-editor-with-emacs.html (but I'm not sure, it may just be a unity quirk) My journey has ultimately led me to using Eglot over lsp-mode and installing omnisharp via Nix package manager. With that setup, I don't have to do any weird environment hacks. I don't know if this will help you, but I hope it does. I really dislike C# and .NET personally, but I have to admit, once the LSP is set up correctly, its a pretty solid experience.
sebhoagie · 2022-08-04 18:18
That's another good tip, before I tried setting up the debugger, I used Eglot and it was a bit more stable than lsp-mode. If you check out older commits in my config (shared in another comment) or the issues in GH for Eglot you can find sample configurations for it, very simple to setup.
Saikyun · 2022-08-04 19:18
I had good experiences with omnisharp-mode iirc.
elizadev · 2022-08-04 22:30
Oh, to follow up on this: make sure that whatever LSP package you use can correctly recognize the root directory of the project, usually by making it recognizable by project.el. My blog goes over that bit too.
jsled · 2022-08-05 11:52
This has been removed, as it is trolling, and not very civil.
EmilySeville7cfg · 2022-08-06 07:53
I don't know why it doesn't work as expected. I've done the following steps: 1. add `omnisharp` to my `packages` variable in `~/.emacs` 2. `M-x load-file RET ~/.emacs` 3. `dotnet new console --name Test` 4. `dotnet new sln --name Test` 5. `dotnet sln add ./Test/Test.csproj` 6. `git init` 7. `emacs -nw ./Test/Program.cs` 8. `M-x omnisharp-install-server` 9. `M-x omnisharp-start-omnisharp-server` 10. `M-x company-mode RET` Note that I've tried to build project via `dotnet build` after no completion appeared and restarted Emacs with 7-10 steps repeated with no result... No completion is shown for `Console.`. I wanna manually start omnisharp server to check whether it works and after it hook it start to opening C# files. My `~/.emacs` is: (require 'package) (add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t) (setq packages '(soft-stone-theme flycheck markdown-mode json-mode yaml-mode format-all lsp-mode csharp-mode omnisharp lsp-pyright company)) (dolist (package packages) (package-install package)) (add-hook 'after-init-hook (lambda () (load-theme 'soft-stone t))) (add-hook 'after-init-hook #'global-flycheck-mode) (custom-set-variables ;; custom-set-variables was added by Custom. ;; If you edit it by hand, you could mess it up, so be careful. ;; Your init file should contain only one such instance. ;; If there is more than one, they won't work right. '(display-line-numbers 'relative) '(package-selected-packages '(omnisharp csharp-mode company lsp-pyright lsp-mode format-all yaml-mode json-mode markdown-mode flycheck soft-stone-theme))) (custom-set-faces ;; custom-set-faces was added by Custom. ;; If you edit it by hand, you could mess it up, so be careful. ;; Your init file should contain only one such instance. ;; If there is more than one, they won't work right. ) The last failed attempt to fix this problem was adding: (setq omnisharp-expected-server-version "1.32.0") ; https://github.com/OmniSharp/omnisharp-vscode/issues/1450#issuecomment-432516876 (let ((dotnet-version (string-trim (shell-command-to-string "dotnet --version")))) ;; https://github.com/OmniSharp/omnisharp-emacs/issues/459#issuecomment-452656947 (setenv "MSBuildSDKsPath" (format "/usr/share/dotnet/sdk/%s/Sdks" dotnet-version))) right before `custom-set-variables`. I restarted Emacs, `M-x omnisharp-start-omnisharp-server` asked me to install omnisharp server (again?). I installed it, started via `M-x omnisharp-start-omnisharp-server` but no completion appeared. Sometimes I obtain `Package cl is deprepated` error. It seems it happens only at first server start. Maybe it's a [bug](https://github.com/OmniSharp/omnisharp-emacs/issues/527).
EmilySeville7cfg · 2022-08-06 08:45
I've reported the bug [here](https://github.com/emacs-lsp/lsp-mode/issues/3661). I can't make LSP work.
Saikyun · 2022-08-06 15:40
I see the docs mentioning "company-omnisharp" which I don't find in your .emacs. Maybe that's what's missing? https://github.com/OmniSharp/omnisharp-emacs#autocompletion
EmilySeville7cfg · 2022-08-06 16:09
I've tried to place these lines to `~/.emacs` and then to `~/.emacs.d/init.el` but [nothing helped](https://github.com/OmniSharp/omnisharp-emacs/issues/527#issuecomment-1207237839).
Saikyun · 2022-08-06 16:19
Ah okay, sorry to hear that. It was 4-5 years ago since I tried last time. I hope they can help you. :)
EmilySeville7cfg · 2022-08-07 23:10
I've tried [csharp-ls](https://emacs-lsp.github.io/lsp-mode/page/lsp-csharp-ls/) LSP and it works! ;) Oh yeah
Saikyun · 2022-08-08 11:45
Nice!