jscheid / dtrt-indent

A minor mode that guesses the indentation offset originally used for creating source code files and transparently adjusts the corresponding settings in Emacs, making it more convenient to edit foreign files.
187 stars 31 forks source link

check adjacent-files like vim-sleuth #43

Open deviantfero opened 6 years ago

deviantfero commented 6 years ago

More often than not I find myself creating a new file, it would be useful that dtrt adjusts indentation settings based on a few files of the same type adjacent to it, or maybe in the parent directories, would this be possible with the existing code-base? if so, how?

thanks for such a nice plugin, btw

rrthomas commented 6 years ago

I think this would need some more work, but it's mainly a matter of selecting suitable files and then feeding them to dtrt to get the settings, then applying them to the new buffer; I don't know vim-sleuth, but presumably its algorithm could be copied.

As far as the code goes, the guts of dtrt-indent-try-set-offset would need to be extracted into a function that, given a buffer, returns the results of analyzing it. I'm also not sure whether it's easily possible to merge the results of analyzing two buffers.

So realistically this would require some work, but it'd be great to have!

An alternative solution is to write out the current settings to a .dir-locals.el file, or some other per-project mechanism. This might be better in that it would have more predictable results.

deviantfero commented 6 years ago

It would definitely be appreciated, the problem with per project mechanisms is that one usually manages many file-types in one project and one might have different indentation settings for them, sure you could specify them, but then dtrt wouldn't make much sense then, since it wouldn't need to actually analyze anything.

but it's rare when two files of the same file-type have different indentation, so that's why I suggested that, I guess it wouldn't have to scan all the buffers available, I imagine something like this:

.
..
new.js
old.js
older.js

If this is my folder layout, dtrt would first try with old.js, then if no information is found (empty lines, no indentation found etc.) it would go on to scan older.js, and if still no information is found, it would go one level up and search for .js files, you would be able to adjust this behavior by setting two flags max-depth and scan-other-files for turning this behavior on/off

Thanks for your thoughtful response!