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

[FEATURE] Using Right Arrow Key to Auto Complete Virtual Text? #54539

Closed
TheCedarPrince opened this issue May 22, 2024 · 1 comment · Fixed by #54983
Closed

[FEATURE] Using Right Arrow Key to Auto Complete Virtual Text? #54539

TheCedarPrince opened this issue May 22, 2024 · 1 comment · Fixed by #54983
Labels
REPL Julia's REPL (Read Eval Print Loop)

Comments

@TheCedarPrince
Copy link
Member

TheCedarPrince commented May 22, 2024

Julia Information

Version info:

Julia Version 1.12.0-DEV.573
Commit 263928f9ad4 (2024-05-21 19:54 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 8 × 11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz
  WORD_SIZE: 64
  LLVM: libLLVM-17.0.6 (ORCJIT, tigerlake)
Threads: 1 default, 0 interactive, 1 GC (on 8 virtual cores)
Environment:
  JULIA_EDITOR = nvim

How I installed Julia: juliaup


Description: Would it be possible to allow auto-completions shown as virtual text in the Julia REPL complete by pressing the right arrow key as well? Currently on nightly I can hit tab, but I was hoping it would be alright to use the right arrow key to finish autocomplete as well. Currently, also pressing another arrow key (left or right in the written text) "hides" the virtual text instead.

Just for clarity, here is the functionality I am talking about:

image

@AshlinHarris
Copy link
Contributor

Hey @TheCedarPrince. I love this suggestion! I got the feature working locally by replacing the existing methods of edit_move_right with the following:

function edit_move_right(q::MIState)
    s = state(q)
    buf = s.input_buffer
    completions, partial, should_complete = complete_line(s.p.complete, s, q.active_module)::Tuple{Vector{String},String,Bool}
    if !eof(buf)
        # move to the next base UTF8 character to the right
        while true
            c = char_move_right(buf)
            eof(buf) && break
            pos = position(buf)
            nextc = read(buf,Char)
            seek(buf,pos)
            (textwidth(nextc) != 0 || nextc == '\n') && break
        end
        refresh_line(state(s)) 
        return true
    elseif should_complete && length(completions) == 1 && length(partial) > 1
        # Replace word by completion
        prev_pos = position(s)
        push_undo(s)
        edit_splice!(s, (prev_pos - sizeof(partial)) => prev_pos, completions[1])
        refresh_line(state(s)) 
        return true
    end
    return false
end

I had to rewrite the function to take an ::MIState input in order to access all the necessary variables. Then, I basically just inserted the relevant code for the Tab key behavior. Normally, the right arrow key does nothing at the end of a line, but now it should complete the line if an autocomplete option is displayed (i.e., there is one unique autocomplete option and more than 1 character has been typed).

This is a quick hack, and I don't fully understand what all the State variables are doing, so I'll revise it and add some tests. But I don't see any downside to your suggested feature, so I plan to make a pull request soon.

@fatteneder fatteneder added the REPL Julia's REPL (Read Eval Print Loop) label Jun 30, 2024
fingolfin pushed a commit that referenced this issue Jul 24, 2024
This commit adds a REPL feature: autocompletion when the right arrow key
is pressed at the end of a line (The current behavior is to do nothing).

This new effect only occurs when:
  - the cursor is at the end of the buffer,
  - there is only one possible completion, and
  - more than 1 character is in the buffer.

In this situation, the right arrow key behaves like `<Tab>`. Otherwise,
the right arrow key behaves as normal.

The feature was requested in #54539 and seems intuitive to me. One
useful side effect is that, by requiring that the cursor be at the end
of the line, it offers a way to avoid autcompletes within words, which I
assume are almost never helpful (For example, if I type "show" in the
REPL, move the cursor onto the 'o' and press `<Tab>`, I end up with
"showow"). One potential drawback is that the autocomplete could occur
when a user who simply wants to move the cursor to the end of the line
holds down the right arrow.
lazarusA pushed a commit to lazarusA/julia that referenced this issue Aug 17, 2024
This commit adds a REPL feature: autocompletion when the right arrow key
is pressed at the end of a line (The current behavior is to do nothing).

This new effect only occurs when:
  - the cursor is at the end of the buffer,
  - there is only one possible completion, and
  - more than 1 character is in the buffer.

In this situation, the right arrow key behaves like `<Tab>`. Otherwise,
the right arrow key behaves as normal.

The feature was requested in JuliaLang#54539 and seems intuitive to me. One
useful side effect is that, by requiring that the cursor be at the end
of the line, it offers a way to avoid autcompletes within words, which I
assume are almost never helpful (For example, if I type "show" in the
REPL, move the cursor onto the 'o' and press `<Tab>`, I end up with
"showow"). One potential drawback is that the autocomplete could occur
when a user who simply wants to move the cursor to the end of the line
holds down the right arrow.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
REPL Julia's REPL (Read Eval Print Loop)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants