;;
;; development assistance packages
;;

;; dev layer toggles
(defcustom whomp-development nil
	"Whether or not to enable development assistance packages."
	:type '(set (const :tag "None" nil)
							(const :tag "lsp" lspmode) ;; generally required for other modes
							(const :tag "magit" magitmode) ;; git integration
							(const :tag "projectile" projectilemode) ;; project management
							(const :tag "go" gomode) ;; go language support
							(const :tag "python" pythonmode) ;; python language support
							(const :tag "elpy" elpymode) ;; python language support (elpy)							
							(const :tag "yaml" yamlmode) ;; yaml language support
							(const :tag "rainbow-delimiters" rainbowmode) ;; rainbow-delimiters support
							(const :tag "terraform" terraformmode) ;; terraform language support
							(const :tag "z80" z80mode) ;; z80 assembly language support
							(const :tag "typescript" typescriptmode) ;; typescript language support
							(const :tag "web" webmodes) ;; web-related modes install
							)
	:group 'whomp-config)

;; terraform-mode
(when (memq 'terraformmode whomp-development)
	(use-package terraform-mode
		:straight t
		:defer t)
	(when (eq 'company whomp-completion)
		(use-package company-terraform
			:straight t
			:defer t
			:config
			(add-hook 'terraform-mode-hook #'company-terraform-init))
		(add-hook 'terraform-mode-hook #'terraform-format-on-save-mode)
		(add-hook 'terraform-mode-hook #'terraform-format-on-save-mode))
	(message "terraform-mode loaded"))

;; lsp-mode
(when (memq 'lspmode whomp-development)
	(straight-use-package 'lsp-mode)
	(straight-use-package 'lsp-ui)
	(when (eq 'treemacs whomp-dirbrowser)
		(straight-use-package 'lsp-treemacs))
	(setq gc-cons-threshold 100000000)
	(setq read-process-output-max (* 1024 1024))
	(whomp/add-key-binding "l" 'lsp-keymap-prefix "lsp-keymap-prefix")
	(straight-use-package 'eglot)
	(require 'eglot)
	(message "lsp-mode loaded"))

;; magit
(when (memq 'magitmode whomp-development)
	(use-package magit
		:straight t
		:defer t)
	(whomp/add-key-binding "g s" 'magit-status "Git status (magit entrypoint)")
	(whomp/add-key-binding "g l" 'magit-log "Git log (magit entrypoint)")
	(whomp/add-key-binding "g b" 'magit-blame "Git blame (magit entrypoint)")
	(whomp/add-key-binding "g f" 'magit-find-file "Git find file (magit entrypoint)")
	(message "magit loaded"))

;; projectile
(when (memq 'projectilemode whomp-development)
	(straight-use-package 'projectile)
	(whomp/add-key-binding "p" 'projectile-command-map "Projectile commands")
	(message "projectile loaded"))

;; go-mode
(when (memq 'gomode whomp-development)
	(straight-use-package 'go-mode)
	(straight-use-package 'go-eldoc)
	(straight-use-package 'go-projectile)
	(straight-use-package 'go-snippets)
	(setq lsp-go-analyses '((shadow . t)
													(simplifycompositelit . :json-false)))
	(when (executable-find "gopls")
		(add-hook 'go-mode-hook #'lsp-deferred)
		(add-hook 'go-mode-hook 'eglot-ensure))
	(when (executable-find "goimports")
		(setq gofmt-command "goimports"))
	(add-hook 'before-save-hook 'gofmt-before-save)
	(message "go-mode loaded"))

;; typescript-mode
(when (memq 'typescriptmode whomp-development)
	(use-package typescript-mode
		:straight t
		:defer t
		:hook (typescript-mode-hook . turn-on-font-lock))
	(message "typescript-mode loaded"))

;; typescript-mode
(when (memq 'webmodes whomp-development)
	(use-package css-mode
		:straight t
		:defer t
		)
	(use-package typescript-mode
		:straight t
		:defer t
		:hook (typescript-mode-hook . turn-on-font-lock))
	(message "web-modes loaded"))

;; python-mode
(when (memq 'pythonmode whomp-development)
	(when (memq 'elpymode whomp-development)
		(straight-use-package 'elpy)
		(elpy-enable))
	(when (not (memq 'elpymode whomp-development))
		(add-hook 'python-mode-hook (lambda ()
																	(setq-local tab-width 2)))
		(add-hook 'python-mode-hook #'lsp-deferred))
	(when (executable-find "pylsp")
		(add-hook 'python-mode-hook 'eglot-ensure))
	(use-package python-black
		:demand t
		:straight t
		:after python
		:hook (python-mode . python-black-on-save-mode))
	(message "python-mode loaded"))

;; yaml-mode
(when (memq 'yamlmode whomp-development)
	(use-package yaml-mode
		:straight t
		:defer t)
	(add-to-list 'auto-mode-alist '("\\.yml\\'" . yaml-mode))
	(add-to-list 'auto-mode-alist '("\\.yaml\\'" . yaml-mode))
	;; yaml-mode is a bit too aggressive with its indentation
	;; so we'll disable it and use the default
	(add-hook 'yaml-mode-hook
						(lambda ()
							(setq-local electric-indent-inhibit t)
							(setq-local electric-layout-rules nil)))
	(message "yaml-mode loaded"))

;; rainbow-delimiters-mode
(when (memq 'rainbowmode whomp-development)
	(use-package rainbow-delimiters
		:straight t
		:defer t
		:config
		(add-hook 'prog-mode-hook #'rainbow-delimiters-mode)
		(message "rainbow-delimiters-mode loaded")))

;; z80-mode
(when (memq 'z80mode whomp-development)
	(load "~/.emacs.d/whomp/z80-mode.el")
	(message "z80-mode loaded"))

(message "W.H.O.M.P. development assistance packages loaded")
(provide 'whomp-development)
;; whomp-development.el ends here