aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Tom Willemse2025-12-10 23:39:44 -0800
committerGravatar Tom Willemse2025-12-10 23:39:44 -0800
commit00b9f8090f1a1bd66cf0db9b72afb9936f50284d (patch)
treeb9424d354338f21590562901d29f00231a22758d
parentab553a83bbe434d61400d7765117dfc1aab93118 (diff)
downloademacs-config-00b9f8090f1a1bd66cf0db9b72afb9936f50284d.tar.gz
emacs-config-00b9f8090f1a1bd66cf0db9b72afb9936f50284d.zip
oni-js: Add function that will guess the javascript import location
-rw-r--r--oni-js.el40
1 files changed, 39 insertions, 1 deletions
diff --git a/oni-js.el b/oni-js.el
index b2b9ba8..0af12be 100644
--- a/oni-js.el
+++ b/oni-js.el
@@ -4,7 +4,7 @@
;; Author: Tom Willemse <tom@ryuslash.org>
;; Keywords: local
-;; Version: 2025.1003.165923
+;; Version: 2025.1110.160632
;; Package-Requires: (oni-company js2-mode js2-refactor oni-flycheck fic-mode rjsx-mode prettier-js)
;; This program is free software; you can redistribute it and/or modify
@@ -97,5 +97,43 @@
(add-hook 'js2-mode-hook #'oni-js-set-imenu-expression)
+(defun oni-js-guess-import-filename-at-point ()
+ (save-excursion
+ (beginning-of-line)
+ (when (looking-at "import")
+
+ (let* ((bpos (progn
+ (search-forward-regexp (rx (or "'" "\"")))
+ (point)))
+ (epos (progn
+ (search-forward-regexp (rx (or "'" "\"")))
+ (point)))
+ (import-file (buffer-substring bpos (1- epos))))
+ (cond
+ ((string-prefix-p "@/" import-file)
+ (let ((file-name (expand-file-name (concat "src/" (substring import-file 2))
+ (project-root (project-current)))))
+ (cond
+ ((file-directory-p file-name)
+ (concat file-name "/index.js"))
+ ((null (file-name-extension file-name))
+ (concat file-name ".js"))
+ (t file-name))))
+ ((or (string-prefix-p "../" import-file)
+ (string-prefix-p "./" import-file))
+ (let ((file-name (expand-file-name (concat "src/" import-file)
+ (project-root (project-current)))))
+ (cond
+ ((file-directory-p file-name)
+ (concat file-name "/index.js"))
+ ((null (file-name-extension file-name))
+ (concat file-name ".js"))
+ (t file-name)))))))))
+
+(defun oni-js-add-ffap-function ()
+ (add-hook 'file-name-at-point-functions 'oni-js-guess-import-filename-at-point 0 t))
+
+(add-hook 'js2-mode-hook #'oni-js-add-ffap-function)
+
(provide 'oni-js)
;;; oni-js.el ends here