reddit / emacs

How Do I Force 'here doc' Strings In Ruby Files To Actually Be Indented Properly In Emacs?

BackToPlebbit69 · 2022-08-31 20:36
Hey there, Does anyone know why 'here doc' syntax ([https://en.wikipedia.org/wiki/Here\_document](https://en.wikipedia.org/wiki/Here_document)) with the '<<-' symbol does not automatically get indented in Ruby files with Emacs? **Here's an example:** This gets indented in other editors just fine: def self.create_table sql = <<-SQL CREATE TABLE IF NOT EXISTS songs ( id INTEGER PRIMARY KEY, name TEXT, album TEXT ) SQL DB[:conn].execute(sql) end This same block in Emacs does not get indented at all: def self.create_table sql = <<-SQL CREATE TABLE IF NOT EXISTS songs ( id INTEGER PRIMARY KEY, name TEXT, album TEXT ) SQL **Here's my config snippet for my Emacs with LSP config:** * Here's a link to a snippet of my Emacs config that I have using a literate Emacs config in a '\~/.emacs.d/configuration.org' file: * [https://bpa.st/7BVA](https://bpa.st/7BVA)
flexibeast · 2022-09-01 04:53
Well, the Wikipedia link you shared says, in its opening para, > In computing, a here document (here-document, here-text, heredoc, hereis, here-string or here-script) is a file literal or input stream literal: it is a section of a source code file that is treated as if it were a separate file. The term is also used for a form of multiline string literals that use similar syntax, preserving line breaks and other whitespace (including indentation) in the text. That is, a heredoc contains text that is to be interpreted literally. If an editor inserted indentation itself, it would be changing the literal contents of the text. i would argue that whatever is handling indentation in your Emacs setup - ruby-mode? - is doing The Right Thing, i.e. not doing indentation of heredocs automatically, but leaving such indentation to be done manually at the discretion of the code author, as appropriate for the situation. Additionally, it might or might not be relevant that [the POSIX definition of heredocs](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_07_04) - which Ruby heredocs might ultimately be based on, via Perl heredocs? i only have a basic knowledge of Ruby, sorry! - states: > If the redirection operator is "<<-", all leading <tab> characters shall be stripped from input lines and the line containing the trailing delimiter. In the context of the Ruby snippet you shared, the indentation doesn't matter for what the code is doing, but in other contexts, the indentation *can* matter. For example, i recently wrote [a shell script to create a minimal EPUB](https://github.com/flexibeast/epub-create/blob/main/epub-create), which uses heredocs to create XML and XHTML files. If Emacs indented the 'headers' and 'footers' according to their position in the code structure, their indentation in the resulting files would be misleading (even though this wouldn't affect the validity of the markup).
patrick_thomson · 2022-09-01 13:06
As per /u/flexibeast’s comment, I’d argue that this is correct behavior: Emacs can’t, by default, know that the contents of a heredoc are SQL. However, with a little elbow grease, you can use [mmm-mode](https://github.com/purcell/mmm-mode) to engage sql-mode in heredocs, which will give you syntax highlighting and (I believe) correct indentation. There doesn’t appear to be support for this baked into mmm-mode, but I found [this comment](https://github.com/purcell/mmm-mode/issues/25#issuecomment-24036688) that you should be able to adapt.
BackToPlebbit69 · 2022-09-02 04:39
u/flexibeast u/patrick_thomson I appreciate the answers on this, however, even with u/patrick_thomson's idea to use that hacky version of that GitHub comment, the problem is that it probably will change drastically over time. My use case is that I have to utilize several SQL statements within my current web dev bootcamp work within my Ruby projects, so truth be told, I'm kind of shocked no one else has thought to maybe force an indent even if it shouldn't have one to begin with. I understand the semantic of why you wouldn't want to have this, but maybe to make it have a fake indent just in the buffer itself would be helpful anyway and not so jarring in comparison. I just get the feeling that VS Code can do this from the get-go, yet Emacs can't... and stuff like this just bothers me where you really have to force Emacs to play nicely when it comes to indentation and working with certain types of web dev projects. I'm also bothered since a slightly mid-tier user like myself has to scour through so many peoples Emacs's configs online just to maybe get a glimpse of an okay setup for web dev stuff in the first place. I just wish it were easier to be completely honest. I honestly have hated configuring Emacs for LSP type stuff since its such a shitty, jenky experience. I might be better off just forking the Prelude distribution of Emacs and just adding my own existing config on top since I just hate the LSP side of stuff so much as I've never had anything but frustration with it since it never 'just works'.
flexibeast · 2022-09-02 05:01
Well, VS Code's development is funded by Microsoft. Emacs' development is done by volunteers, who work on what's important to them/us. i wanted an Emacs UI for the buku Web bookmark manager, but one didn't already exist, so rather than hoping that someone else would make one for it, i gradually put together one myself. Your particular use-case sounds reasonable to me, and of course it's possible to add a ruby-mode user option to request indentation of heredocs as appropriate for the heredoc contents. (i couldn't see such an option via a quick skim of the `ruby` customization-group.) You could request this feature via `M-x report-emacs-bug`, if someone hasn't already done so - check [here](https://debbugs.gnu.org/cgi/pkgreport.cgi?package=emacs). (Maybe someone else has wanted it previously, but didn't open a feature request because doing so didn't help them at that moment, but had they done so, that feature might now have been added by now.) All that said, a superficial search via `package-list-packages` with the MELPA repo enabled shows the existence of [emh-ruby-mode](https://github.com/zenspider/Enhanced-Ruby-Mode), which might do what you require; but even if it doesn't, there's [poly-ruby](https://github.com/knu/poly-ruby.el): > This package defines poly-ruby-mode, which introduces polymode for here-documents in a ruby script.
BackToPlebbit69 · 2022-09-02 16:57
Neat, so any time they use 'heredocs', they really mean 'poly' mode. I see, gotcha. This is coming from a complete newbie, so this is super helpful. &#x200B; The closest analog to me that I've ever worked with is like docstrings in Python that need to span over the course of 3+ lines. &#x200B; Thanks a ton for this explanation. u/flexibeast