help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Ruby and RubyOn Rails setup - Cannot enable indentation and snippets


From: Dmitry Gutov
Subject: Re: Ruby and RubyOn Rails setup - Cannot enable indentation and snippets
Date: Tue, 01 Jan 2013 13:24:57 -0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (windows-nt)

Hi Sayth,

Sayth Renshaw <address@hidden> writes:
> I have been trying to configure emacs24 now for sometime to be good using 
> ruby and later rails. I am on Fedora 18.
>
> Well the biggest issue I have is that emacs will not indent ruby correctly, 
> well it doesn't indent at all.

Please open an .rb file in Emacs, evaluate (M-:) the following
expressions and send the values returned (for long values, you can look
in the *Messages* buffer, or use esk-eval-and-replace from emacs-starter-kit):

major-mode
indent-line-function
(find-library-name "ruby-mode")
emacs-version

> I have tried several variations all done by hand by myself, emacs-starter-kit 
> with ruby loaded, prelude emacs(indents end keyword correctly but does not 
> indent the block) with ruby loaded and cannot get proper indentation, 
> snippets and/or autocompletion. I can get some bits in the installs but it is 
> not ideal.

That sounds positively broken.

> By default Sublime text2 does all this and I cannot manage to emacs 
> configured.
>
> This is my .emacs(left out references to marmalade etc) I put together using 
> ruby-mode, rinari, rsense and yasnippet.
> I get the features of rsense, however I cannot get it to indent correctly or 
> have yasnippet insert snippet from tab press.
>
> Example indentation
>
> Correct
> multi_array.each do
>   x.each do |y|
>     puts y
>
>   end
> end
>
> Emacs
> multi_array.each do
> x.each do |y|
> puts y
> end
> end

ruby-mode indents that fine out of the box (emacs -Q), no setup
required. No other packages you mentioned mess with indentation, so
there must be something wrong with the setup.

> If anyone can advise on what to improve it would be greatly appreciated.
>
> (setq-default indent-tabs-mode nil)    ; use only spaces and no tabs
> (setq default-tab-width 4)
>
> (require 'pep8)
> (require 'pylint)
>
> ;; org mode
> (add-to-list 'auto-mode-alist '("\\.org\\'" . org-mode))
>
>  (global-set-key "\C-cl" 'org-store-link)
>  (global-set-key "\C-cc" 'org-capture)
>  (global-set-key "\C-ca" 'org-agenda)
>  (global-set-key "\C-cb" 'org-iswitchb)

The stuff above is largely irrelevant.

> (require 'yasnippet-bundle)

Yasnippet bundles are largely deprecated. I'm not sure if that's the
source of your problem, but if all else fails, try to install yasnippet
from git (https://github.com/capitaomorte/yasnippet/), or from Melpa,
mentioned in the README there.

Also note that in recent versions, all variable and function names have
had slashes replaces with dashes.

> (yas/global-mode)
> (add-hook 'rinari-minor-mode-hook
>           #'(lambda ()
>               (setq yas/mode-symbol 'rails-mode)))
>
> (add-hook 'ruby-mode-hook
>           #'(lambda ()
>               (setq yas/mode-symbol 'ruby-mode)))

There's no reason to set yas/mode-symbol to 'ruby-mode, the major mode
name is already included in the list of symbols to look up.
And since rinari-minor-mode-hook runs after ruby-mode-hook, the value
just gets overwritten.

Do you have a separate rails-mode snippet directory? If not, you can
remove both `add-hook' forms above.

If you do, on the other hand, you need to add its parent directory to
yas-snippet-dirs.

> (setq ruby-indent-level 4)

The example at the beginning of your email has a 2-space indentation.
Why are you setting this to 4?

> (require 'rinari)
> (add-to-list 'load-path "/home/sayth/.emacs.d/rhtml")
> (require 'rhtml-mode)

rhtml-mode is also irrelevant.

> (autoload 'ruby-mode "ruby-mode"
>     "Mode for editing ruby source files")
> (add-to-list 'auto-mode-alist '("\\.rb$" . ruby-mode))
> (add-to-list 'interpreter-mode-alist '("ruby" . ruby-mode))

The above settings are already included in the default Emacs installation.

> (autoload 'run-ruby "inf-ruby"
>     "Run an inferior Ruby process")
> (autoload 'inf-ruby-keys "inf-ruby"
>     "Set local key defs for inf-ruby in ruby-mode")

If you installed inf-ruby from Elpa, you can remove these, too.

> (add-hook 'ruby-mode-hook
>     '(lambda ()
>         (inf-ruby-keys)))
>
> (setq load-path (cons (expand-file-name "~/.emacs.d/rails-reloaded") 
> load-path))
>   (require 'rails-autoload)

I'd like know what gave you the impression that installing
rails-reloaded is a good idea. Here's a recent user complaint:

http://stackoverflow.com/questions/13890831/emacs-rails-reloaded-does-not-work

Anyway, since you're having problems with your setup, you should build
it gradually, first make sure the indentation works, then yasnippet,
then code completion, which is the most problematic of the three.

emacs-rails-reloaded, or the original emacs-rails, don't help with any
of those (except for having an additional set of snippets, I guess), so
there's no reason to complicate your config, for now.

> (setq rsense-home "/home/sayth/opt/rsense-0.3")
> (add-to-list 'load-path (concat rsense-home "/etc"))
> (require 'rsense)
> (add-hook 'ruby-mode-hook
>           (lambda ()
>             (add-to-list 'ac-sources 'ac-source-rsense-method)
>             (add-to-list 'ac-sources 'ac-source-rsense-constant)))

This looks good, but in my experience, RSense doesn't work with Rails
projects (or, rather, usually doesn't show any useful completions).
I guess this is listed as "Large project" in the "Limitations" section of
RSense user manual.

*shameless plug warning*

You can try Robe (https://github.com/dgutov/robe) for a less clever, but
more comprehensive completion. It falls back to "all methods in all
classes" approach much more often, though, and should still be
considered WIP.

--Dmitry



reply via email to

[Prev in Thread] Current Thread [Next in Thread]