Skip to content

Commit

Permalink
Add support for TCL and fix some bugs (#366)
Browse files Browse the repository at this point in the history
* Add support for tcl

* Fix issue when symbol is at the start of line

Since the current implementation always adds one before returning it can never
return 0, even when the start of the symbol is at the start of the line. This
means that if the symbol is at the start of the line the :left side will be the
first letter of the symbol instead of empty.

* Fix issue with git grep when no file extensions are specified

Currently if no file extensions are specified then git grep will not be given a
path and will therefore only search from the current directory instead of the
root. So in that case we add the magic path (:/) to search from the project
root. Note that this only works in Git versions greater then 1.7.6.
  • Loading branch information
CeleritasCelery committed Sep 14, 2020
1 parent b47774b commit fbbe6b0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 47 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ There is currently basic support for the following languages:
* SQL
* Swift
* SystemVerilog
* Tcl
* TypeScript
* Vala
* VHDL
Expand Down
80 changes: 41 additions & 39 deletions dumb-jump.el
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
(require 'dash)
(require 'popup)
(require 'cl-generic nil :noerror)
(require 'cl-lib)

(defgroup dumb-jump nil
"Easily jump to project function and variable definitions"
Expand Down Expand Up @@ -775,6 +776,19 @@ or most optimal searcher."
:regex "JJJ\\s*=\\s*"
:tests ("$test = 1234"))

;; Tcl
(:type "function" :supports ("ag" "grep" "rg" "git-grep") :language "tcl"
:regex "proc\\s+JJJ\\s*\\{"
:tests ("proc test{" "proc test {"))

(:type "variable" :supports ("ag" "grep" "rg" "git-grep") :language "tcl"
:regex "set\\s+JJJ"
:tests ("set test 1234"))

(:type "variable" :supports ("ag" "grep" "rg" "git-grep") :language "tcl"
:regex "(variable|global)\\s+JJJ"
:tests ("variable test" "global test"))

;; shell
(:type "function" :supports ("ag" "grep" "rg" "git-grep") :language "shell"
:regex "function\\s*JJJ\\s*"
Expand Down Expand Up @@ -1636,6 +1650,9 @@ or most optimal searcher."
(:language "javascript" :type "variable" :right "^;" :left nil)
(:language "typescript" :type "function" :right "^(" :left nil)
(:language "perl" :type "function" :right "^(" :left nil)
(:language "tcl" :type "function" :left "\\[$" :right nil)
(:language "tcl" :type "function" :left "^\s*$" :right nil)
(:language "tcl" :type "variable" :left "\\$$" :right nil)
(:language "php" :type "function" :right "^(" :left nil)
(:language "php" :type "class" :right nil :left "new\s+")
(:language "elisp" :type "function" :right nil :left "($")
Expand Down Expand Up @@ -1762,18 +1779,6 @@ inaccurate jump). If nil, jump without confirmation but print a warning."
(setq dumb-jump--grep-installed? variant))
dumb-jump--grep-installed?))

(defun dumb-jump-find-start-pos (line-in look-for cur-pos)
"Find start column position for LINE-IN of LOOK-FOR using CUR-POS as a hint."
(let ((is-found nil)
(line (s-replace "\t" (s-repeat tab-width " ") line-in)))
(while (and (> cur-pos 0) (not is-found))
(let* ((char (substring line cur-pos (1+ cur-pos)))
(is-at (s-index-of char look-for)))
(if (null is-at)
(setq is-found t)
(setq cur-pos (1- cur-pos)))))
(1+ cur-pos)))

(defun dumb-jump-run-test (test cmd)
"Use TEST as the standard input for the CMD."
(with-temp-buffer
Expand Down Expand Up @@ -1920,16 +1925,9 @@ Optionally pass t for RUN-NOT-TESTS to see a list of all failed rules"

(defun dumb-jump-get-point-context (line func cur-pos)
"Get the LINE context to the left and right of FUNC using CUR-POS as hint."
(let* ((loc (dumb-jump-find-start-pos line func cur-pos))
(func-len (length func))
(sen-len (length line))
(right-loc-start (+ loc func-len))
(right-loc-end (length line))
(left (substring line 0 loc))
(right (if (> right-loc-end sen-len)
""
(substring line right-loc-start right-loc-end))))
`(:left ,left :right ,right)))
(let ((loc (or (cl-search func line :start2 cur-pos) 0)))
(list :left (substring line 0 loc)
:right (substring line (+ loc (length func))))))

(defun dumb-jump-to-selected (results choices selected)
"With RESULTS use CHOICES to find the SELECTED choice from multiple options."
Expand Down Expand Up @@ -2113,6 +2111,13 @@ to keep looking for another root."
(thing-at-point 'symbol)
(thing-at-point 'symbol t))))

(defun dumb-jump--get-symbol-start ()
"Get the start of symbol at point"
(- (if (region-active-p)
(region-beginning)
(car (bounds-of-thing-at-point 'symbol)))
(line-beginning-position)))

(defun dumb-jump-get-lang-by-shell-contents (buffer)
"Return languages in BUFFER by checking if file extension is mentioned."
(let* ((buffer-contents (with-current-buffer buffer
Expand All @@ -2128,20 +2133,18 @@ CUR-FILE is the path of the current buffer.
PROJ-ROOT is that file's root project directory.
LANG is a string programming language with CONFIG a property list
of project configuration."
(let* ((cur-line (if prompt 0 (dumb-jump-get-point-line)))
(look-for-start (when (not prompt)
(- (car (bounds-of-thing-at-point 'symbol))
(point-at-bol))))
(cur-line-num (line-number-at-pos))
(let* ((cur-line-num (line-number-at-pos))
(proj-config (dumb-jump-get-config proj-root))
(config (when (s-ends-with? ".dumbjump" proj-config)
(dumb-jump-read-config proj-root proj-config)))
(found-symbol (or prompt (dumb-jump-get-point-symbol)))
(look-for (dumb-jump-process-symbol-by-lang lang found-symbol))
(pt-ctx (or (and prompt (get-text-property 0 :dumb-jump-ctx prompt))
(if (and (not prompt) (not (string= cur-line look-for)))
(dumb-jump-get-point-context cur-line look-for look-for-start)
nil)))
(pt-ctx (if prompt
(get-text-property 0 :dumb-jump-ctx prompt)
(dumb-jump-get-point-context
(dumb-jump-get-point-line)
look-for
(dumb-jump--get-symbol-start))))
(ctx-type
(dumb-jump-get-ctx-type-by-language lang pt-ctx))

Expand Down Expand Up @@ -2275,6 +2278,7 @@ current file."
(:comment "//" :language "go")
(:comment "//" :language "zig")
(:comment "#" :language "perl")
(:comment "#" :language "tcl")
(:comment "//" :language "php")
(:comment "#" :language "python")
(:comment "%" :language "matlab")
Expand Down Expand Up @@ -2884,7 +2888,7 @@ Using ag to search only the files found via git-grep literal symbol search."
(when (not (s-blank? dumb-jump-git-grep-search-args))
(concat " " dumb-jump-git-grep-search-args))
" -E"))
(fileexps (s-join " " (--map (shell-quote-argument (format "%s/*.%s" proj it)) ggtypes)))
(fileexps (s-join " " (or (--map (shell-quote-argument (format "%s/*.%s" proj it)) ggtypes) '(":/"))))
(exclude-args (s-join " "
(--map (shell-quote-argument (concat ":(exclude)" it))
exclude-paths)))
Expand Down Expand Up @@ -3002,13 +3006,11 @@ Using ag to search only the files found via git-grep literal symbol search."
"2020-06-26")

(cl-defmethod xref-backend-identifier-at-point ((_backend (eql dumb-jump)))
(let ((bounds (bounds-of-thing-at-point 'symbol)))
(and bounds (let* ((ident (dumb-jump-get-point-symbol))
(start (car bounds))
(col (- start (point-at-bol)))
(line (dumb-jump-get-point-line))
(ctx (dumb-jump-get-point-context line ident col)))
(propertize ident :dumb-jump-ctx ctx)))))
(let ((start (dumb-jump--get-symbol-start)))
(and start (let* ((ident (dumb-jump-get-point-symbol))
(line (dumb-jump-get-point-line))
(ctx (dumb-jump-get-point-context line ident start)))
(propertize ident :dumb-jump-ctx ctx)))))

(cl-defmethod xref-backend-definitions ((_backend (eql dumb-jump)) prompt)
(let* ((info (dumb-jump-get-results prompt))
Expand Down
10 changes: 2 additions & 8 deletions test/dumb-jump-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -424,14 +424,14 @@
(ert-deftest dumb-jump-context-point-test ()
(let* ((sentence "mainWindow.loadUrl('file://')")
(func "loadUrl")
(ctx (dumb-jump-get-point-context sentence func 15)))
(ctx (dumb-jump-get-point-context sentence func 11)))
(should (string= (plist-get ctx :left) "mainWindow."))
(should (string= (plist-get ctx :right) "('file://')"))))

(ert-deftest dumb-jump-context-point-type-test ()
(let* ((sentence "mainWindow.loadUrl('file://' + __dirname + '/dt/inspector.html?electron=true');")
(func "loadUrl")
(pt-ctx (dumb-jump-get-point-context sentence func 14))
(pt-ctx (dumb-jump-get-point-context sentence func 11))
(ctx-type (dumb-jump-get-ctx-type-by-language "javascript" pt-ctx)))
(should (string= ctx-type "function"))))

Expand Down Expand Up @@ -917,12 +917,6 @@
(mock (dumb-jump-goto-file-line "/usr/blah/test2.txt" 52 1))
(dumb-jump--result-follow data nil "/usr/blah"))))

(ert-deftest dumb-jump-find-start-pos-test ()
(let ((cur-pos 9)
(line "event event")
(word "event"))
(should (= (dumb-jump-find-start-pos line word cur-pos) 6))))

(ert-deftest dumb-jump-go-include-lib-test ()
(let ((el-file (f-join test-data-dir-elisp "fake2.el"))
(lib-file (f-join test-data-dir-elisp "../fake-library/lib.el")))
Expand Down

0 comments on commit fbbe6b0

Please sign in to comment.