Skip to content

Commit

Permalink
Merge pull request ocaml#1364 from ocaml/locate-state-reset
Browse files Browse the repository at this point in the history
[locate] reset state from entry points
Tests adapted to >4.08 <4.11
  • Loading branch information
voodoos committed Jul 20, 2021
1 parent da14f19 commit 52c6605
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 31 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ git version

+ merlin binary
- fix -cmt-path dirs mistakenly added to build path (#1330)
+ editor modes
to show more or less deep results. (#1318)
- vim: add support for the `merlin-locate-type` command:
`MerlinLocateType` (#1359)
- emacs: add support for the `merlin-locate-type` command. (#1359)

merlin 3.5.0
============
Expand Down
11 changes: 11 additions & 0 deletions emacs/merlin.el
Original file line number Diff line number Diff line change
Expand Up @@ -1396,6 +1396,17 @@ loading"
(interactive)
(merlin--locate-result (merlin-call-locate)))

(defun merlin-locate-type ()
"Locate the type of the expression under point."
(interactive)
(let ((result (merlin/call "locate-type"
"-position" (merlin/unmake-point (point)))))
(unless result
(error "Not found. (Check *Messages* for potential errors)"))
(unless (listp result)
(error "%S" result))
(merlin--goto-file-and-point result)))

(defun merlin-pop-stack ()
"Go back to the last position where the user did a locate."
(interactive)
Expand Down
75 changes: 45 additions & 30 deletions vim/merlin/autoload/merlin.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,38 @@ def differs_from_current_file(path):
def vim_fnameescape(s):
return vim.eval("fnameescape('%s')" % s.replace("'","''"))

def goto_file_and_point(pos_or_err):
if not isinstance(pos_or_err, dict):
print(pos_or_err)
else:
l = pos_or_err['pos']['line']
c = pos_or_err['pos']['col']
split_method = vim.eval('g:merlin_split_method')
# save the current position in the jump list
vim.command("normal! m'")
if "file" in pos_or_err and differs_from_current_file(pos_or_err['file']):
fname = vim_fnameescape(pos_or_err['file'])
if split_method == "never":
vim.command(":keepjumps e %s" % fname)
elif "tab" in split_method:
if "always" in split_method:
vim.command(":keepjumps tab split %s" % fname)
else:
vim.command(":keepjumps tab drop %s" % fname)
elif "vertical" in split_method:
vim.command(":keepjumps vsplit %s" % fname)
else:
vim.command(":keepjumps split %s" % fname)
elif "always" in split_method:
if "tab" in split_method:
vim.command(":tab split")
elif "vertical" in split_method:
vim.command(":vsplit")
else:
vim.command(":split")
# TODO: move the cursor using vimscript, so we can :keepjumps?
vim.current.window.cursor = (l, c)

def command_locate(path, pos):
try:
choice = vim.eval('g:merlin_locate_preference')
Expand All @@ -260,36 +292,16 @@ def command_locate(path, pos):
pos_or_err = command("locate", "-look-for", choice, "-position", fmtpos(pos))
else:
pos_or_err = command("locate", "-prefix", path, "-look-for", choice, "-position", fmtpos(pos))
if not isinstance(pos_or_err, dict):
print(pos_or_err)
else:
l = pos_or_err['pos']['line']
c = pos_or_err['pos']['col']
split_method = vim.eval('g:merlin_split_method')
# save the current position in the jump list
vim.command("normal! m'")
if "file" in pos_or_err and differs_from_current_file(pos_or_err['file']):
fname = vim_fnameescape(pos_or_err['file'])
if split_method == "never":
vim.command(":keepjumps e %s" % fname)
elif "tab" in split_method:
if "always" in split_method:
vim.command(":keepjumps tab split %s" % fname)
else:
vim.command(":keepjumps tab drop %s" % fname)
elif "vertical" in split_method:
vim.command(":keepjumps vsplit %s" % fname)
else:
vim.command(":keepjumps split %s" % fname)
elif "always" in split_method:
if "tab" in split_method:
vim.command(":tab split")
elif "vertical" in split_method:
vim.command(":vsplit")
else:
vim.command(":split")
# TODO: move the cursor using vimscript, so we can :keepjumps?
vim.current.window.cursor = (l, c)
goto_file_and_point(pos_or_err)
except MerlinExc as e:
try_print_error(e)


def command_locate_type(pos):
try:
pos_or_err = command("locate-type", "-position", fmtpos(pos))
goto_file_and_point(pos_or_err)

except MerlinExc as e:
try_print_error(e)

Expand Down Expand Up @@ -434,6 +446,9 @@ def vim_locate_at_cursor(path):
def vim_locate_under_cursor():
vim_locate_at_cursor(None)

def vim_locate_type_at_cursor():
command_locate_type(vim.current.window.cursor)

# Jump and Phrase motion
def vim_jump_to(target):
command_motion("jump", target, vim.current.window.cursor)
Expand Down
6 changes: 5 additions & 1 deletion vim/merlin/autoload/merlin.vim
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,10 @@ function! merlin#Locate(...)
endif
endfunction

function! merlin#LocateType()
MerlinPy merlin.vim_locate_type_at_cursor()
endfunction

function! merlin#LocateImpl(...)
let l:pref = g:merlin_locate_preference
let g:merlin_locate_preference = 'implementation'
Expand Down Expand Up @@ -596,7 +600,7 @@ function! merlin#Register()
command! -buffer -complete=customlist,merlin#ExpandPrefix -nargs=? MerlinLocateImpl call merlin#LocateImpl(<q-args>)
command! -buffer -complete=customlist,merlin#ExpandPrefix -nargs=? MerlinLocateIntf call merlin#LocateIntf(<q-args>)
command! -buffer -nargs=0 MerlinILocate call merlin#InteractiveLocate()

command! -buffer -nargs=0 MerlinLocateType call merlin#LocateType()

if !exists('g:merlin_disable_default_keybindings') || !g:merlin_disable_default_keybindings
nmap <silent><buffer> gd :MerlinLocate<return>
Expand Down

0 comments on commit 52c6605

Please sign in to comment.