From 00b9f8090f1a1bd66cf0db9b72afb9936f50284d Mon Sep 17 00:00:00 2001 From: Tom Willemse Date: Wed, 10 Dec 2025 23:39:44 -0800 Subject: oni-js: Add function that will guess the javascript import location --- oni-js.el | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) 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 ;; 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 -- cgit v1.3-2-g0d8e