2

I am an avid hippie-expand user but I find it annoying that it natively doesn't complete ruby symbols without the prefix. For example (cursor is |):

class ApplicationController < ActionController::Base
  before_action :some_action

  protected

  def some_a|

  end
end

If I trigger hippie-expand here, it doesn't know that :some_action is also a completion option without the : prefix.

What is the cleanest way to extend hippie-expand (or perhaps dabbrev-expand) to do this?

  • 1
    Yeah, it annoys me as well. So far, though, the best idea I've got is to change the syntax of : in ruby-mode back to punctuation. It will require quite a few changes to the code, in different places. – Dmitry Jun 10 '15 at 22:51
2

@Dmitry made a great point that hippie-expand will do what I'm desiring when : is considered a punctuation character.

It's obviously not ideal to just set (modify-syntax-entry ?: "." ruby-mode-syntax-table) in your init and call it a day, because then only some_action in :some_action is considered a symbol which would break expectations for other plugins. This seems ripe for re-defining with advice though.

Here's what I came up with:

(defun hippie-expand-ruby-symbols (orig-fun &rest args)
  (if (eq major-mode 'ruby-mode)
      (let ((table (make-syntax-table ruby-mode-syntax-table)))
        (modify-syntax-entry ?: "." table)
        (with-syntax-table table (apply orig-fun args)))
    (apply orig-fun args)))

(advice-add 'hippie-expand :around #'hippie-expand-ruby-symbols)

If hippie-expand is called when the major-mode is ruby-mode, define a cloned temporary syntax table where : is a punctuation character and call hippie-expand with it. Otherwise, just call hippie-expand as normal.

Any downside to this approach?

| improve this answer | |
  • This is a pretty great approach, actually. Here's a possible tweak: instead of major-mode comparison, you should be able to call advice-add in ruby-mode-hook, if you replace 'hippy-expand with (local 'hippy-expand). – Dmitry Jun 11 '15 at 19:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.