Skip to content

Commit

Permalink
more on company and js; restclient
Browse files Browse the repository at this point in the history
  • Loading branch information
JJPandari committed Jul 14, 2020
1 parent dbb677c commit d017829
Show file tree
Hide file tree
Showing 22 changed files with 148 additions and 102 deletions.
2 changes: 1 addition & 1 deletion init.el
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@
(require 'init-json)
(require 'init-coffeescript)
(require 'init-css)
(require 'init-http)
(require 'init-java)
;; (require 'init-python)
;; ;; (require 'init-haskell)
Expand All @@ -113,6 +112,7 @@
(require 'init-ahk)
(require 'init-dotenv)
(require 'init-shell)
(require 'init-http)

(require 'init-chinese)
(require 'init-keyfreq)
Expand Down
8 changes: 4 additions & 4 deletions modes/init-buffer.el
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
"Switch to the `*scratch*' buffer, creating it first if needed.
if prefix argument ARG is given, switch to it in an other, possibly new window."
(interactive "P")
(let ((exists (get-buffer "*scratch*")))
(if arg
(switch-to-buffer-other-window (get-buffer-create "*scratch*"))
(switch-to-buffer (get-buffer-create "*scratch*")))))
(if arg
(switch-to-buffer-other-window (get-buffer-create "*scratch*"))
(switch-to-buffer (get-buffer-create "*scratch*")))
(lisp-interaction-mode))

;;----------------------------------------------------------------------------
;; switch to last buffer
Expand Down
37 changes: 11 additions & 26 deletions modes/init-company.el
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"C-n" #'company-select-next
"C-p" #'company-select-previous
"<C-m>" 'company-complete-common
"<tab>" 'expand-snippet-or-complete-selection
"<tab>" 'jester/expand-yas-or-complete-company
"C-g" 'company-abort
"<escape>" (lambda! (company-abort) (evil-normal-state)))
;; M-1 ~ M-0 to select candidate 1 ~ 10
Expand All @@ -49,27 +49,25 @@

(general-define-key
:states '(insert emacs)
"<tab>" 'tab-indent-or-complete
))
"<tab>" 'jester/yas-or-company-or-hippie))


(use-package company-tabnine
:demand t
:config
(setq jester-company-backends-with-tabnine
'((company-lsp :with company-tabnine) (company-capf :with company-tabnine)
company-files
'(company-files
company-tabnine
(company-dabbrev-code company-gtags company-etags company-keywords)
company-dabbrev))
(defun jester/use-tabnine-for-major-mode (major-mode)
"add tabnine to `COMPANY-BACKENDS' in `MAJOR-MODE'."
(add-hook (intern (format "%s-hook" major-mode))
(lambda () (setq-local company-backends jester-company-backends-with-tabnine)) t))
(dolist (mode '(js2-mode web-mode typescript-mode css-mode less-css-mode scss-mode))
(dolist (mode '(js2-mode web-mode typescript-mode css-mode less-css-mode scss-mode sass-mode))
(jester/use-tabnine-for-major-mode mode))

(add-to-list 'company-transformers 'jester/company-merge-tabnine-with-other t)
;; (add-to-list 'company-transformers 'jester/company-merge-tabnine-with-other t)
(defun jester/company-merge-tabnine-with-other (candidates)
"Show first 5 of tabnine's candidates, followed by the other backend's candidates.
\"the other\" means company-foo when the group is (company-foo :with company-tabnine)."
Expand Down Expand Up @@ -103,42 +101,29 @@
;; Make tab do both yas expand and company.
;;----------------------------------------------------------------------------
;; https://emacs.stackexchange.com/a/7925/12854
(defun check-expansion ()
;; (save-excursion
;; (if (looking-at "\\_>") t
;; (backward-char 1)
;; (if (looking-at "\\.") t
;; (backward-char 1)
;; (if (looking-at "->") t nil))))
t)

(defun tab-indent-or-complete ()
(defun jester/yas-or-company-or-hippie ()
(interactive)
(cond
((minibufferp)
(minibuffer-complete))
(t
;; (indent-for-tab-command)
(if (and (or (not yas-minor-mode)
(null (jester/yas-expand-no-prompt)))
(check-expansion))
(if (or (not yas-minor-mode)
(null (jester/yas-expand-no-prompt)))
(progn
(company-manual-begin)
(if (null company-candidates)
(progn
(company-abort)
(hippie-expand nil)
;; (indent-for-tab-command)
)))))))
(hippie-expand nil))))))))

(defun expand-snippet-or-complete-selection ()
(defun jester/expand-yas-or-complete-company ()
(interactive)
(if (or (not yas-minor-mode)
(null (jester/yas-expand-no-prompt))
(company-abort))
(company-complete-selection)))

(defun abort-company-or-yas ()
(defun jester/abort-company-or-yas ()
(interactive)
(if (null company-candidates)
(yas-abort-snippet)
Expand Down
22 changes: 22 additions & 0 deletions modes/init-css.el
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,27 @@
(after-load 'css-mode
(modify-syntax-entry ?. "." css-mode-syntax-table))

;; TODO need also consider when I'm on the ".class {" line
(defun jester/css-emmet-or-normal-tab ()
"Do `emmet-expand-line' if current line doesn't have a \": \" yet, otherwise behave like normal tab."
(interactive)
(let ((has-divider))
(save-excursion
(move-beginning-of-line 1)
(setq has-divider (search-forward ": " (line-end-position) t)))
(if has-divider (jester/yas-or-company-or-hippie)
(call-interactively 'emmet-expand-line))))
;; (general-define-key
;; :states '(insert emacs)
;; :keymaps '(css-mode-map sass-mode-map)
;; "<tab>" 'jester/css-emmet-or-normal-tab)
;; TODO how to make a buffer-local company-active-map?
(add-hook! 'css-mode-hook
;; (make-local-variable 'company-active-map)
;; (general-define-key
;; :keymaps 'company-active-map
;; "<tab>" 'jester/css-emmet-or-normal-tab)
;; (setf (alist-get 'tab (cdr company-active-map)) 'jester/css-emmet-or-normal-tab)
)

(provide 'init-css)
27 changes: 20 additions & 7 deletions modes/init-editing-utils.el
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@
"Define a function with the name of `FUN-NAME'.
`FORMS-FOR-BOLT' is some forms evaluated when point is at beginning of line text,
who decides which type of assignment we currently have, and change it to the next one.
Use `eol-str' as the tail."
Use `EOL-STR' as the tail."
`(defun ,fun-name ()
"Make a mode specific assignment statement,
using things left of point as left value, things right as right value.
Expand Down Expand Up @@ -315,8 +315,6 @@ If this line is already an assignment (has a \"=\"), cycle through styles in thi
"Make a assignment statement,
using things left of point as left value, things right as right value.
Additional formats are added to this line as declared in `jester-assignment-format-alist'.
If nothing is at left, move point to the left value's position,
otherwise move to before semicolon."
(interactive)
Expand Down Expand Up @@ -379,19 +377,34 @@ otherwise move to before semicolon."
;;----------------------------------------------------------------------------
;; move to bracket
;;----------------------------------------------------------------------------
(defvar jester-buffer-lang-has-generic-p-list nil
"Decides whether `jester/backward-bracket' and `jester/forward-bracket' will move to \"<\" and \">\",
by telling whether the language in current buffer has generic types (which are usually denoted by \"<T>\")")
(push (lambda () (eq major-mode 'rust-mode)) jester-buffer-lang-has-generic-p-list)
(push (lambda () (eq major-mode 'typescript-mode)) jester-buffer-lang-has-generic-p-list)
(push (lambda () (and (buffer-file-name)
(member (file-name-extension (buffer-file-name))
'("ts" "tsx"))))
jester-buffer-lang-has-generic-p-list)
(evil-define-motion jester/backward-bracket (count)
"Move backward to a (, [ or {."
"Move backward to a (, [ or {. Maybe <, if any of `jester-buffer-lang-has-generic-p-list' yields t."
;; TODO enable lispyville everywhere
:type exclusive
(setq count (or count 1))
(search-backward-regexp "[([{]" nil t count))
(search-backward-regexp (if (cl-some (lambda (p) (funcall p)) jester-buffer-lang-has-generic-p-list)
"[([{<]"
"[([{]")
nil t count))

(evil-define-motion jester/forward-bracket (count)
"Move forward to a ), ] or }."
"Move forward to a ), ] or }. Maybe >, if any of `jester-buffer-lang-has-generic-p-list' yields t."
:type exclusive
(setq count (or count 1))
(forward-char)
(search-forward-regexp "[]})]" nil t count)
(search-forward-regexp (if (cl-some (lambda (p) (funcall p)) jester-buffer-lang-has-generic-p-list)
"[]})>]"
"[]})]")
nil t count)
(backward-char))

(general-define-key
Expand Down
3 changes: 2 additions & 1 deletion modes/init-evil.el
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@
"C-e" 'evil-multiedit--end-of-line)
;; these have to be loaded before company so they don't shadow tab bindings in `company-active-map'
(define-key evil-multiedit-state-map (kbd "<tab>") 'evil-multiedit-next)
(define-key evil-multiedit-insert-state-map (kbd "<tab>") 'tab-indent-or-complete)
(define-key evil-multiedit-insert-state-map (kbd "<tab>") 'jester/yas-or-company-or-hippie)
)


Expand Down Expand Up @@ -409,6 +409,7 @@
:keymaps '(text-mode-map
conf-mode-map
prog-mode-map
restclient-mode-map

messages-buffer-mode-map

Expand Down
23 changes: 21 additions & 2 deletions modes/init-http.el
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
(use-package httprepl
:commands httprepl)
(use-package restclient
:init
(defun jester/restclient ()
(interactive)
(with-current-buffer (get-buffer-create "*restclient*")
(restclient-mode)
(switch-to-buffer (current-buffer))))
:mode ("\\.rest\\'" . restclient-mode)
:hook (restclient-mode . display-line-numbers-mode)
:config
(general-define-key
:keymaps 'restclient-mode-map
"C-c C-c" (lambda! (restclient-http-send-current nil t)))
(general-define-key
:states '(normal visual)
:keymaps 'restclient-mode-map
"<tab>" 'restclient-toggle-body-visibility)
(general-define-key
:states '(insert emacs)
:keymaps 'restclient-mode-map
"C-j" 'jester/make-simple-assignment))


(provide 'init-http)
2 changes: 1 addition & 1 deletion modes/init-ivy.el
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
;; --no-sort is much faster
(setq counsel-fzf-cmd "fzf --no-sort -f \"%s\""
;; limit file size and line length to be faster. long lines doesn't matter when search but is laggy to display
counsel-rg-base-command "rg --no-heading --line-number --color never --max-filesize 1M --max-columns 233 --max-columns-preview %s .")
counsel-rg-base-command "rg --no-heading --line-number --color never --max-filesize 1M --max-columns 233 --max-columns-preview %s")

(jester/with-leader
"p f" 'jester/open-project-file
Expand Down
46 changes: 25 additions & 21 deletions modes/init-javascript.el
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@

(use-package rjsx-mode
:init
(defun jester/jsx-file-p ()
"Check whether current buffer is a jsx file."
(defun jester/_sx-file-p (file-ext)
"Check whether current buffer file is a jsx/tsx file.
`FILE-EXT' is file extension, can be \"js\" or \"ts\"."
(let ((ext (file-name-extension (buffer-file-name))))
(when (or (string-equal ext "js")
(string-equal ext "jsx"))
(when (or (string-equal ext file-ext))
(point-min)
(while (looking-at "^//")
(beginning-of-line 2))
Expand All @@ -88,16 +88,26 @@
(or "from 'react'" "from \"react\"")
(optional ";")
eol))))))
(defun jester/jsx-file-p ()
"Check whether current buffer file is a jsx file."
(jester/_sx-file-p "js"))
(defun jester/tsx-file-p ()
"Check whether current buffer file is a jsx file."
(jester/_sx-file-p "ts"))
(setq magic-mode-alist
(append '((jester/jsx-file-p . rjsx-mode)) magic-mode-alist))
(setq magic-mode-alist
(append '((jester/tsx-file-p . web-mode)) magic-mode-alist))
(add-hook! 'rjsx-mode-hook
(add-hook 'post-command-hook 'jester/on-post-newline nil t)
(setq imenu-create-index-function (lambda () (jester/merge-imenu 'js2-mode-create-imenu-index))
imenu-generic-expression '((nil "^ *static \\(propTypes\\|defaultProps\\) = {$" 1))
mode-name "JSX"))
mode-name "JSX"
emmet-expand-jsx-className? t))
(require 'web-mode)
(custom-set-faces '(rjsx-tag
((t (:inherit web-mode-html-tag-face)))))
:mode "\\.jsx\\'"
:config
(evil-define-key 'insert rjsx-mode-map (kbd "C-b") #'rjsx-delete-creates-full-tag)
(modify-syntax-entry ?_ "w" rjsx-mode-syntax-table))
Expand Down Expand Up @@ -165,7 +175,9 @@
(defun jester/prettier-js-file-1 ()
"Call prettier on current file."
(interactive)
(call-process-shell-command (format "node %s/node_modules/.bin/prettier --write %s" (projectile-project-root) (buffer-file-name))))
(call-process-shell-command (format "node %s/node_modules/.bin/prettier --write %s"
(projectile-project-root)
(buffer-file-name))))

(defun jester/prettier-js-file-2 ()
"Call prettier on current file."
Expand Down Expand Up @@ -210,26 +222,18 @@
(add-hook 'js2-mode-hook 'jester/make-default-evil-makers-for-js t)


(defun jester/set-js-test-command ()
(defun jester/set-js-ts-test-command ()
"If file ends with \".test.js\" or \".spec.js\", set `jester-test-command' to \"node node_modules/.bin/jest ...\"."
(let ((file-name (buffer-file-name)))
(when (and file-name
(or (s-suffix-p ".test.js" file-name)
(s-suffix-p ".spec.js" file-name)))
(cl-some (lambda (suffix) (s-suffix-p suffix file-name))
'(".test.js" ".spec.js"
".test.jsx" ".spec.jsx"
".test.ts" ".spec.ts"
".test.tsx" ".spec.tsx")))
(setq jester-test-command (format "(cd %s && node node_modules/.bin/jest %s --collectCoverageOnlyFrom '')" (projectile-project-root) file-name)))))

(add-hook 'js2-mode-hook 'jester/set-js-test-command)


(defun jester/typescript-goto-typings-file ()
"Goto this project's typings.d.ts file."
(interactive)
(if-let ((root (projectile-project-root)))
(find-file (concat (file-name-as-directory root) "typings.d.ts"))
(user-error "Not in a project.")))

(jester/with-major-leader '(web-mode-map typescript-mode-map)
"t" 'jester/typescript-goto-typings-file)
(add-hook! '(js2-mode-hook typescript-mode-hook web-mode-hook) 'jester/set-js-ts-test-command)


(provide 'init-javascript)
9 changes: 0 additions & 9 deletions modes/init-misc.el
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,6 @@
;; Single space between sentences is more widespread than double
(setq-default sentence-end-double-space nil)

(use-package anzu
:demand t)
(use-package evil-anzu
:after anzu
:demand t)
;; use this in place of anzu when Emacs 27 is here
;; (setq isearch-lazy-count t
;; lazy-count-prefix-format "%s/%s ")

;; http://emacs.stackexchange.com/a/7745/12854
(defun browse-file-directory ()
"Open the current file's directory however the OS would."
Expand Down
6 changes: 3 additions & 3 deletions modes/init-mode-line.el
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@

" %+"

;; anzu
"%1"
'(:eval (when (and (featurep 'anzu) anzu--state) (concat " " (anzu--update-mode-line))))
;; TODO use this in place of anzu when Emacs 27 is here
;; (setq isearch-lazy-count t
;; lazy-count-prefix-format "%s/%s ")

"%1"
;; the buffer name; the file name as a tool tip
Expand Down
2 changes: 1 addition & 1 deletion modes/init-org.el
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
(setq org-log-done t
org-edit-timestamp-down-means-later t
org-archive-mark-done nil
org-hide-emphasis-markers t
org-hide-emphasis-markers nil
org-catch-invisible-edits 'show
org-export-coding-system 'utf-8
org-fast-tag-selection-single-key 'expert
Expand Down
Loading

0 comments on commit d017829

Please sign in to comment.