Skip to content

Commit 7a93003

Browse files
committed
Spring initializer frontend for lsp-java
1 parent e2acb04 commit 7a93003

File tree

3 files changed

+73
-4
lines changed

3 files changed

+73
-4
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ LSP java mode supports the following JDT Features:
1919
* Visual debugger - [dap-mode](https://github.com/yyoncho/dap-mode/)
2020
* Test runner - [dap-mode](https://github.com/yyoncho/dap-mode/)
2121
* Project explorer integration - [treemacs](https://github.com/Alexander-Miller/treemacs)
22+
* Integration with [Spring Initializr](https://start.spring.io/)
2223
## Screenshot
2324
![demo](images/demo.png)
2425
## Installation
@@ -115,6 +116,10 @@ some of them are bound to Emacs commands:
115116
#### Classpath browsing
116117
[lsp-java](https://github.com/emacs-lsp/lsp-java) the command `lsp-java-classpath-browse` which allows users to browse the structure of current projects classpath. From that view the users could go to the particular item.
117118
![Classpath](images/classpath.png)
119+
120+
#### Spring Initializr
121+
`lsp-java` provides a frontend for [Spring Initializr](https://start.spring.io/) which simplifies the creation of Spring Boot projects directly from Emacs via `lsp-java-spring-initializr`.
122+
![Create Spring boot project](images/boot.png)
118123
## Supported settings
119124
* `lsp-java-server-install-dir` - Install directory for eclipsejdtls-server
120125
* `lsp-java-java-path` - Path of the java executable

images/boot.png

117 KB
Loading

lsp-java.el

+68-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
;;; lsp-java.el --- Java support for lsp-mode
22

33
;; Version: 1.0
4-
;; Package-Requires: ((emacs "25.1") (lsp-mode "3.0") (markdown-mode "2.3") (dash "2.14.1") (f "0.20.0") (ht "2.0") (dash-functional "1.2.0"))
4+
;; Package-Requires: ((emacs "25.1") (lsp-mode "3.0") (markdown-mode "2.3") (dash "2.14.1") (f "0.20.0") (ht "2.0") (dash-functional "1.2.0") (request "0.3.0"))
55
;; Keywords: java
66
;; URL: https://github.com/emacs-lsp/lsp-java
77

@@ -28,6 +28,7 @@
2828
(require 'ht)
2929
(require 'f)
3030
(require 'tree-widget)
31+
(require 'request)
3132

3233
(defgroup lsp-java nil
3334
"JDT emacs frontend."
@@ -667,9 +668,7 @@ server."
667668
(lsp-find-workspace 'jdtls)
668669
lsp-java--get-project-uris
669670
(--filter (s-starts-with? (lsp--uri-to-path it) (lsp--uri-to-path file-uri)))
670-
(-max-by (lambda (project-a project-b)
671-
(> (length project-a)
672-
(length project-b))))))
671+
(--max-by (> (length it) (length other)))))
673672

674673
(defun lsp-java--nearest-widget ()
675674
"Return widget at point or next nearest widget."
@@ -847,6 +846,8 @@ PROJECT-URI uri of the item."
847846
("language/actionableNotification" 'lsp-java--actionable-notification-callback)
848847
("language/progressReport" 'lsp-java--progress-report)
849848
("workspace/notify" 'lsp-java--workspace-notify))
849+
850+
:request-handlers (lsp-ht ("workspace/executeClientCommand" 'lsp-java--boot-workspace-execute-client-command))
850851
:action-handlers (lsp-ht ("java.apply.workspaceEdit" 'lsp-java--apply-workspace-edit))
851852
:uri-handlers (lsp-ht ("jdt" 'lsp-java--resolve-uri)
852853
("chelib" 'lsp-java--resolve-uri))
@@ -866,5 +867,68 @@ PROJECT-URI uri of the item."
866867
(with-lsp-workspace workspace
867868
(lsp-java-update-project-uris)))))
868869

870+
(defun lsp-java-spring-initializr ()
871+
"Emacs frontend for https://start.spring.io/ ."
872+
(interactive)
873+
(let ((base-url "https://start.spring.io/"))
874+
(message "Requesting spring initializr data...")
875+
(request
876+
base-url
877+
:type "GET"
878+
:parser (lambda () (let ((json-array-type 'list)) (json-read)))
879+
:headers '(("Accept" . "application/vnd.initializr.v2.1+json"))
880+
:success (cl-function
881+
(lambda (&key data &allow-other-keys)
882+
(flet ((ask (message key) (alist-get 'id
883+
(lsp--completing-read message
884+
(alist-get 'values (alist-get key data))
885+
(-partial 'alist-get 'name)))))
886+
(condition-case _err
887+
(-let* ((group-id (read-string "Enter group name: " "com.example"))
888+
(artifact-id (read-string "Enter artifactId: " "demo"))
889+
(description (read-string "Enter description: " "Demo project for Spring Boot"))
890+
(boot-version (ask "Select boot-version: " 'bootVersion))
891+
(java-version (ask "Select java-version: " 'javaVersion))
892+
(language (ask "Select language: " 'language))
893+
(packaging (ask "Select packaging: " 'packaging))
894+
(base-url "https://start.spring.io/")
895+
(package-name (read-string "Select package name: " "com.example.demo"))
896+
(type (ask "Select type: " 'type))
897+
(target-directory (read-directory-name "Select project directory: " default-directory))
898+
(dependenciles-list (->> data
899+
(alist-get 'dependencies)
900+
(alist-get 'values)
901+
(-map (-lambda ((&alist 'name 'values))
902+
(-map (-lambda ((&alist 'id 'name dep-name 'description))
903+
(cons (format "%s / %s (%s)" name dep-name description) id)) values)))
904+
(apply 'append)))
905+
(temp-file (make-temp-file "spring-project" nil ".zip"))
906+
deps dep)
907+
(while (setq dep (-> (lsp--completing-read
908+
(if deps
909+
(format "Select dependency (M-RET to finish) (selected %s): " (length deps))
910+
"Select dependency (M-RET to finish): ")
911+
dependenciles-list
912+
(-lambda ((name . id))
913+
(if (-contains? deps id)
914+
(concat "(Added) " name)
915+
name)))
916+
rest))
917+
(if (-contains? deps dep)
918+
(setq deps (remove dep deps))
919+
(pushnew dep deps)))
920+
(let ((download-url (format "%sstarter.zip?type=%s&language=%s&groupId=%s&artifactId=%s&packaging=%s&bootVersion=%s&baseDir=%s&dependencies=%s"
921+
base-url type language group-id artifact-id packaging boot-version artifact-id (s-join "," deps))))
922+
(message "Downloading template from %s" download-url)
923+
(url-copy-file download-url temp-file t)
924+
(if (executable-find "unzip")
925+
(progn
926+
(shell-command (format "unzip %s -d %s" temp-file target-directory))
927+
(when (yes-or-no-p "Do you want to import the project?")
928+
(lsp-workspace-folders-add (f-join target-directory artifact-id)))
929+
(find-file (f-join target-directory artifact-id)))
930+
(user-error "Unable to unzip tool - file %s cannot be extracted, extract it manually" temp-file))))
931+
('quit))))))))
932+
869933
(provide 'lsp-java)
870934
;;; lsp-java.el ends here

0 commit comments

Comments
 (0)