Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
kotlin-mode-overlay.el --- Missing bits for kotlin-mode
;;; kotlin-mode-overlay.el --- Missing bits for kotlin -*- lexical-binding: t; -*-
(defvar *kotlin-linter* "ktlint")
(defvar *kotlin-interpreter* "kotlin")
(defvar *kotlin-build-dir* "build")
(defvar *kotlin-main-args* "gest.xml")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; MAIN.
(defun kotlin/test ()
"run tests"
:todo)
;; format lint indent imports may be done by `ktlint`
(defun kotlin/format! ()
"whole buffer external formatting IN-PLACE"
:todo)
(defun kotlin/lint ()
"whole buffer external linting NON-DESTRUCTIVE"
(interactive)
(compile (concat *kotlin-linter* " " (buffer-name))))
(defun kotlin/indent! ()
"localized indentating/formatting"
:todo)
(defun kotlin/imports! ()
"organize buffer imports IN-PLACE"
:todo)
(defun kotlin/build ()
(interactive)
(let* ((fn (buffer-name))
(dir (if-let (d *kotlin-build-dir*) (concat "-d " d) " "))
(make (format "%s %s %s" kotlin-command dir fn)))
(compile make)))
(defun kotlin-mangle (s)
"<name>.<ext=kt> -> <name.capitalize>.<ext.capitalize>"
(let* ((p (split-string s "\\."))
(n (car p))
(e (cadr p)))
(concat (capitalize n) (capitalize e))))
(defun packaged (fn)
"open fn; re-search <package>"
(let ((b (find-file-noselect fn)))
(with-current-buffer b
(goto-char (point-min))
(if (re-search-forward "^package \\\(.*\\\)$" nil t)
(substring-no-properties (match-string 1))
""))))
(defun kotlin/run ()
"
TODO: a priori check for a main function ?
"
(interactive)
(let* ((fn (buffer-name))
(pk (packaged fn))
(cn (kotlin-mangle fn))
(dir (if-let (d *kotlin-build-dir*) (concat "-cp " d) " "))
(cmd (format "%s %s %s %s" *kotlin-interpreter* dir (concat pk "." cn) *kotlin-main-args*)))
(compile cmd)))
(define-key kotlin-mode-map (kbd "C-c g") #'kotlin/run)
(define-key kotlin-mode-map (kbd "C-c f") #'kotlin/lint)
(define-key kotlin-mode-map (kbd "C-c t") #'kotlin/test)
(define-key kotlin-mode-map (kbd "C-c m") #'kotlin/build)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; SNIPPETS.
"
- val: val $ = $
- fun: fun $($:$):$ {...}
- over: override fun $($:$):$ {...}
- class: class $:$ {...}
"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.