Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lua-jump-to-define #146

Open
ChongQing520 opened this issue Apr 1, 2019 · 0 comments
Open

lua-jump-to-define #146

ChongQing520 opened this issue Apr 1, 2019 · 0 comments

Comments

@ChongQing520
Copy link

(defun lua-get-symbol ()
  (let* ((line-start (if (derived-mode-p 'inferior-lua-mode)
                         ;; Working on a shell buffer: use prompt end.
                         (cdr (lua-util-comint-last-prompt))
                       (line-beginning-position)))
         (start
          (save-excursion
            (if (not (re-search-backward
                      (lua-rx   ;; find the current-word
                       (or whitespace open-paren close-paren string-delimiter))
                      line-start
                      t 1))
                line-start
              (forward-char (length (match-string-no-properties 0)))
              (point))))
        (end (save-excursion
               (if (not (re-search-forward
                      (lua-rx   ;; find the current-word
                       (or whitespace open-paren close-paren string-delimiter))
                      (point-max)
                      t 1))
                (line-end-position)
              (backward-char (length (match-string-no-properties 0)))
              (point))
               ))
        (sym (buffer-substring start end))
        )
    sym))

(defun lua-jump-to-define-command (sym)
  (format
   (concat "if(type(%s) ~= \"function\")then print(\"no\") "
           "else local info = debug.getinfo(%s);"
           "local src = info.source; "
           "local line = info.linedefined ;"
           "local ret = src ..\":\".. line;print(ret) end \n")
   sym sym))

(defun lua-jump-to-define ()
  (interactive)
  (let* ((proc (get-buffer-process (current-buffer)))
         (sym (lua-get-symbol))
         ret source line)
    (setq ret (lua-shell-run proc (lua-jump-to-define-command sym)))
    (if (equal (car ret) "no")
        (message "not found function define or not a function")
      (setq source (car ret))
      (cond
       ((string-match "^@\\([^:]+\\):\\([0-9]+\\)" source)
        (setq line (match-string 2 source)
              source (match-string 1 source))
        (find-file source)
        (goto-line (string-to-number line)))
       ((string-match "=\\[C\\]:" "=[C]:")
        (message "C Function"))
       (t
        (print (format "not Know Pattern:%s" ret)))
       ))))

;;(switch-to-buffer lua-shell-completion-native-redirect-buffer)

(defun lua-shell-run (process input-to-send)
  "Get completions using native readline for PROCESS.
When IMPORT is non-nil takes precedence over INPUT for
completion."
  (with-current-buffer (process-buffer process )
    (let* ((original-filter-fn (process-filter process))
           (redirect-buffer (get-buffer-create
                             lua-shell-completion-native-redirect-buffer))
           )
      ;; Ensure restoring the process filter, even if the user quits
      ;; or there's some other error.
      (unwind-protect
          (with-current-buffer redirect-buffer
            ;; Cleanup the redirect buffer
            (erase-buffer)
            ;; Mimic `comint-redirect-send-command', unfortunately it
            ;; can't be used here because it expects a newline in the
            ;; command and that's exactly what we are trying to avoid.
            (let ((comint-redirect-echo-input nil)
                  (comint-redirect-completed nil)
                  (comint-redirect-perform-sanity-check nil)
                  (comint-redirect-insert-matching-regexp t)
                  (comint-redirect-finished-regexp
                   "> ")
                  (comint-redirect-output-buffer redirect-buffer))
              ;; Compatibility with Emacs 24.x.  Comint changed and
              ;; now `comint-redirect-filter' gets 3 args.  This
              ;; checks which version of `comint-redirect-filter' is
              ;; in use based on its args and uses `apply-partially'
              ;; to make it up for the 3 args case.
              (if (= (length
                      (help-function-arglist 'comint-redirect-filter)) 3)
                  (set-process-filter
                   process (apply-partially
                            #'comint-redirect-filter original-filter-fn))
                (set-process-filter process #'comint-redirect-filter))
              (process-send-string process input-to-send)
              ;; Grab output until our dummy completion used as
              ;; output end marker is found.
              (when (lua-shell-accept-process-output
                     process lua-shell-completion-native-output-timeout
                     comint-redirect-finished-regexp)
                (cl-remove-duplicates
                 (split-string
                  (buffer-substring-no-properties
                   (line-beginning-position) (point-min))
                  "[ \f\t\n\r\v()]+" t)
                 :test #'string=))
              )
            )
        (set-process-filter process original-filter-fn)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant