Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for C-famaily languages #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 24 additions & 16 deletions lsp-sourcekit.el
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
;;; Commentary:

;;
;; Call (lsp) after visiting a file in swift-mode major mode.
;;
;; TODO: Configure the Objective-C/C++ LSP client (requires clangd).
;; Call (lsp) after visiting a file in a major mode supported by
;; sourcekit-lsp.


;;; Code:

Expand All @@ -41,31 +41,39 @@
;; Customization
;; ---------------------------------------------------------------------

(defcustom lsp-sourcekit-executable
"sourcekit"
"Path of the lsp-sourcekit executable."

(defgroup lsp-sourcekit nil
"LSP support for Swift & C-family languages (C, C++,
Objective-C, Objective-C++) using sourcekit-lsp."
:group 'lsp-mode
:tag "Language Server"
:link '(url-link "https://github.com/apple/sourcekit-lsp"))

(defcustom lsp-sourcekit-executable "sourcekit-lsp"
"The sourcekit-lsp executable to use.
Leave as just the executable name to use the default behavior of
finding the executable with `exec-path'."
:type 'file
:group 'sourcekit)
:group 'lsp-sourcekit)

(defcustom lsp-sourcekit-extra-args
nil
"Additional command line options passed to the lsp-sourcekit executable."
(defcustom lsp-sourcekit-extra-args nil
"Extra arguments for the sourcekit-lsp executable."
:type '(repeat string)
:group 'sourcekit)
:group 'lsp-sourcekit)

;; ---------------------------------------------------------------------
;; Register lsp client
;; ---------------------------------------------------------------------

(defun lsp-sourcekit--lsp-command ()
"Generate the language server startup command."
`(,lsp-sourcekit-executable
,@lsp-sourcekit-extra-args))
`(,lsp-sourcekit-executable ,@lsp-sourcekit-extra-args))

(lsp-register-client
(make-lsp-client :new-connection (lsp-stdio-connection 'lsp-sourcekit--lsp-command)
:major-modes '(swift-mode)
:server-id 'sourcekit-ls))
(make-lsp-client :new-connection (lsp-stdio-connection
'lsp-sourcekit--lsp-command)
:major-modes '(swift-mode c-mode c++-mode objc-mode)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking that, instead of using :major-modes, we could use :activation-fn with a custom function. Inside that custom function we can be more precise for when sourcekit-lsp should activate:

Require that the major mode is either swift-mode, c-mode, c++-mode, or objc-mode AND that the project contains a Package.swift or .xcodeproj/.xcworkspace file. You can use Emacs's locate-dominating-file function to do this.

I think this will do the right thing in almost all cases, without the user having to configure existing or new projects that contain C++ code. What do you think?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds like the best option. Thanks for the pointers.

:server-id 'sourcekit-lsp))

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