lsp-mode icon indicating copy to clipboard operation
lsp-mode copied to clipboard

Add Solidity (solc) support

Open otavioschwanck opened this issue 4 years ago • 2 comments

Would be nice to have solidity support, on nvim it already has with:

https://github.com/qiuxiang/coc-solidity

EDIT:

I Created a initial LSP configuration:

;;; lsp-solidity.el --- description -*- lexical-binding: t; -*-

;; Copyright (C) 2020 emacs-lsp maintainers

;; Author: emacs-lsp maintainers
;; Keywords: lsp, solidity, solidity

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; LSP Clients for the Solidity Programming Language.

;;; Code:

(require 'lsp-mode)

(defgroup lsp-solidity nil
  "LSP support for solidityl using solidity-language-server."
  :group 'lsp-mode
  :link '(url-link "https://www.npmjs.com/package/solidity-language-server"))

(defcustom lsp-clients-solidity-executable '("solidity-language-server"  "--stdio")
  "Command to start the solidity language server."
  :group 'lsp-solidity
  :risky t
  :type 'file)

(defcustom lsp-clients-solidity-initialization-options '()
  "Initialization options for solidity language server."
  :group 'lsp-solidity
  :type 'alist)

(lsp-dependency 'solidity-language-server
                '(:system "solidity-language-server")
                '(:npm :package "solidity-ls"
                       :path "solidity-ls"))

(lsp-register-client
 (make-lsp-client :new-connection (lsp-stdio-connection
                                   (lambda ()
                                     `(,(or (executable-find (cl-first lsp-clients-solidity-executable))
                                            (lsp-package-path 'solidity-language-server))
                                       ,@(cl-rest lsp-clients-solidity-executable))))
                  :major-modes '(solidity-mode)
                  :priority -1
                  :server-id 'solidity-ls
                  :initialization-options (lambda () lsp-clients-solidity-initialization-options)
                  :download-server-fn (lambda (_client callback error-callback _update?)
                                        (lsp-package-ensure 'solidity-language-server
                                                            callback error-callback))))

(lsp-consistency-check lsp-solidity)

(provide 'lsp-solidity)
;;; lsp-solidity.el ends here

But have some problems: Sometimes the completion stop working:

Company: backend (:separate company-capf company-yasnippet) error "Wrong type argument: stringp, nil" with args (candidates jun)

Go to definition doesn't work to other contracts (when using is at contract definition).

On contract:

lsp-request: Request textDocument/definition failed with message: Cannot read property ’absolutePath’ of undefined```

otavioschwanck avatar Mar 30 '22 15:03 otavioschwanck

Could you share your lsp-mode config? This is somewhat working for me, except I can't get past some server startup errors.

Also, did you take any other steps to get lsp support working, like manually installing the npm package? When I first run lsp, it seems to fetch and install solidity-ls from npm (after a confirmation prompt). I'm testing this code as well, and getting an error when opening a Solidity file:

LSP :: solidity-ls has exited (exited abnormally with code 1)
Server solidity-ls:14704 exited with status exit(check corresponding stderr buffer for details). Do you want to restart it? (y or n)

The *solidity-ls::stderr* buffer has some more info, but I'm not sure how to interpret it:

undefined:1


SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at Socket.<anonymous> (/Users/user/.emacs.d/.cache/lsp/npm/solidity-ls/lib/node_modules/solidity-ls/dist/index.js:53:50)
    at Socket.emit (node:events:538:35)
    at endReadableNT (node:internal/streams/readable:1345:12)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)

Process solidity-ls stderr finished
undefined:1


SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at Socket.<anonymous> (/Users/user/.emacs.d/.cache/lsp/npm/solidity-ls/lib/node_modules/solidity-ls/dist/index.js:53:50)
    at Socket.emit (node:events:538:35)
    at endReadableNT (node:internal/streams/readable:1345:12)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)

Process solidity-ls stderr finished

clarkhenry avatar Jul 11 '22 21:07 clarkhenry

This works better for me if I use solidity-language-server instead of solidity-ls throughout lsp-solidity.el.

Also, separately I need to add solidity-mode to lsp-language-id-configuration with (add-to-list 'lsp-language-id-configuration '(solidity-mode . "solidity")).

Even so, I can now connect to the solidity language server, but I'm not seeing any solidity specific UI features in Emacs. I suspect I'm missing some key parts of the lsp-mode config settings. When I look at lsp-describe-session, it shows a tree with some capabilities listed I believe:

[-] /Users/user/repositories/token
 `-[-] solidity-language-server:26436
    |-[-] Buffers
    |  `-[X] DebtToken.sol
    `-[-] Capabilities
       |-[-] workspace:
       |  `-[-] workspaceFolders:
       |     `-[X] supported: t
       |-[X] textDocumentSync: 1
       |-[X] definitionProvider: t
       `-[-] completionProvider:
          |-[X] triggerCharacters: [.]
          `-[X] resolveProvider: nil

clarkhenry avatar Jul 12 '22 20:07 clarkhenry

Hi! Any chance this will be part of lsp-mode soon? Thanks for the initiatives.

dorneanu avatar Mar 28 '23 09:03 dorneanu