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

Add .rb extension to tempfile in vi mode #780

Closed

Conversation

DylanGriffith
Copy link

@DylanGriffith DylanGriffith commented Nov 15, 2024

Problem

When you are in VI mode and you enter normal mode and press v it opens up your $EDITOR to edit the current context. The issue is that your $EDITOR does not know that you are editing Ruby code so you don't get any syntax highlighting or formatting.

Solution

This change adds a .rb extension to the tempfile opened by the editor which then ensures you get the correct syntax highlighting and formatting.

Demo

See the following demo to see how this changes things:

vim-mode-edit-with-syntax.mp4

When you are in VI mode and you enter normal mode and press `v` it opens
up your `$EDITOR` to edit the current context. The issue is that your
`$EDITOR` does not know that you are editing Ruby code so you don't get
any syntax highlighting or formatting.

This change adds a `.rb` extension to the tempfile opened by the editor
which then ensures you get the correct syntax highlighting and
formatting.
@@ -2264,7 +2264,7 @@ def finish
end

private def vi_histedit(key)
path = Tempfile.open { |fp|
path = Tempfile.open(["reline", ".rb"]) { |fp|
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't find an obvious test to change for this. I'm happy to make such a change if necessary.

Also I wasn't sure about whether to add the reline to the file or some other static string. It seems that Tempfile requires me to put some file name in there if I want to add an extension.

@tompng
Copy link
Member

tompng commented Nov 16, 2024

Reline is a general purpose readline library compatible with GNU Readline. Input to Reline and Readline is not always a ruby code.

require 'json'
require 'readline' # loads 'reline' in Ruby >= 3.3 if readline-ext is not installed
p Readline == Reline #=> true

# Simple JSON colorization
Reline.output_modifier_proc=proc{|code|
  code.gsub(/("[^"]*")|(\d+(?:\.\d+)?)|(null|true|false)/){
    color = $1 ? 31 : $2 ? 34 : 36
    "\e[#{color}m#{$&}\e[0m"
  }
}

loop do
  puts "Enter JSON configuration"
  json = Readline.readline('> ', true) # input is JSON, not a ruby code
  puts JSON.parse(json)
end

This is why IRB's syntax highlighting logic is placed in IRB, not in Reline.

vi_histedit doesn't seem to exist in GNU Readline, so I don't want to add a new environment variable or an API to customize the file extension only for this feature.

@tompng tompng closed this Nov 16, 2024
@elfham
Copy link
Contributor

elfham commented Nov 16, 2024

FYI, vi_histedit probably comes from Editline.

Reline is mainly based on GNU Readline, but also takes a lot from Editline.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants