Code Formatter stdin/stdout

98 views
Skip to first unread message

rgre...@gmail.com

unread,
Oct 12, 2017, 4:35:45 PM10/12/17
to elixir-lang-core
I apologize if this topic has already been covered.

Have there been any discussions about adding support for reading/writing via stdin/stdout to the new code formatter coming v1.6? 

I've release an Atom package that formats Elixir code automatically on save using the new formatter. If the formatter supported stdin/stout it would eliminate the need to write to a tmp file, which would greatly reduce latency.

I'm happy to help implement this, but wanted to see if there had been any discussion already.

Thanks in advance!

Ron Green

OvermindDL1

unread,
Oct 12, 2017, 5:42:28 PM10/12/17
to elixir-lang-core
You can just call the formatting function directly.  :-)

iex(1)> Code.format_string!("blah 42")                                                                                               
["blah", "(", "", "42", "", ")"]
iex(2)> Code.format_string!("blah 42") |> :erlang.iolist_to_binary()                                                                 
"blah(42)"

Allen Madsen

unread,
Oct 12, 2017, 6:09:38 PM10/12/17
to elixir-l...@googlegroups.com
I suspect the Atom package can't have elixir in it, which is probably why he was hoping there'd be the stdin/stdout support in the mix format command itself.

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/c7102ea4-d4fe-4a83-b0f2-07feea48f577%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

OvermindDL1

unread,
Oct 12, 2017, 6:18:45 PM10/12/17
to elixir-lang-core
The atom packages I've seen for elixir run an elixir node to do syntax coloring, highlighting, intellisense, and more.  They should either use one of those connections directly (best speed) or they could just run it on the command-line.


On Thursday, October 12, 2017 at 4:09:38 PM UTC-6, Allen Madsen wrote:
I suspect the Atom package can't have elixir in it, which is probably why he was hoping there'd be the stdin/stdout support in the mix format command itself.
On Thu, Oct 12, 2017 at 5:42 PM, OvermindDL1 <overm...@gmail.com> wrote:
You can just call the formatting function directly.  :-)

iex(1)> Code.format_string!("blah 42")                                                                                               
["blah", "(", "", "42", "", ")"]
iex(2)> Code.format_string!("blah 42") |> :erlang.iolist_to_binary()                                                                 
"blah(42)"



On Thursday, October 12, 2017 at 2:35:45 PM UTC-6, rgre...@gmail.com wrote:
I apologize if this topic has already been covered.

Have there been any discussions about adding support for reading/writing via stdin/stdout to the new code formatter coming v1.6? 

I've release an Atom package that formats Elixir code automatically on save using the new formatter. If the formatter supported stdin/stout it would eliminate the need to write to a tmp file, which would greatly reduce latency.

I'm happy to help implement this, but wanted to see if there had been any discussion already.

Thanks in advance!

Ron Green

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.

rgre...@gmail.com

unread,
Oct 13, 2017, 11:58:25 AM10/13/17
to elixir-lang-core
Thanks for the responses. 

Yes, I was trying to avoid running an Elixir node to keep things as simple as possible. Vim, Emacs, Sublime, VS Code, etc will each need to integrate with the new code formatter. Supporting stdin/stdout via a command line option would make integration simple and consistent across all editors. Otherwise, each editor will need to run an Elixir node or use a tmp file (to support formatting files that haven't been saved to the file system yet).

Thanks again for the feedback.

Ron

Jan Fornoff

unread,
Oct 15, 2017, 9:43:44 AM10/15/17
to elixir-lang-core
Agree with Ron. It'd be a great and editor-agnostic addition to have an option to utilize STDIN for quicker formatting experience on save!

Would be willing to help if a hand is needed!
Best,
Jan

José Valim

unread,
Oct 15, 2017, 10:36:10 AM10/15/17
to elixir-l...@googlegroups.com
That can't be done without running an Elixir node. It is your choice if you want to have a node always running or not, but you need to run Elixir one way or the other. In the most basic setup, it is as simple as:

echo "foo( )" | elixir -e 'IO.puts IO.stream(:stdio, :line) |> Enum.join("") |> Code.format_string!()'



José Valim
Founder and 
Director of R&D

To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/b2d7edb0-81be-4a75-903e-b35dfb7a3247%40googlegroups.com.

Pedro Medeiros

unread,
Oct 15, 2017, 10:54:19 AM10/15/17
to elixir-l...@googlegroups.com
Hi Jan.

The new version of the formatter tool already can format directly to stdout. Me and other vim users are already hacking our editors to format the code before the write. the command is $ mix format --print

To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/b2d7edb0-81be-4a75-903e-b35dfb7a3247%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Pedro Henrique de Souza Medeiros
----------------------------------
Cel: +55 (21) 9914-86898
Email: pedr...@gmail.com

Beautiful is better than ugly,
Explicit is better than implicit,
Simple is better than complex,
Complex is better than complicated.

The Zen of Python, by Tim Peters

Jan Fornoff

unread,
Oct 15, 2017, 11:15:47 AM10/15/17
to elixir-l...@googlegroups.com
Hi Pedro,

Thanks for your remark, I think we're using the same setup then :-) 

Jose, would you be open to accepting such a --stdin flag into the Mix formatter task? 

I'm sure we could find someone to get that done, if it's generally welcome. 

Best, 
Jan




--
Pedro Henrique de Souza Medeiros
----------------------------------
Cel: +55 (21) 9914-86898
Email: pedr...@gmail.com

Beautiful is better than ugly,
Explicit is better than implicit,
Simple is better than complex,
Complex is better than complicated.

The Zen of Python, by Tim Peters

--
You received this message because you are subscribed to a topic in the Google Groups "elixir-lang-core" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elixir-lang-core/JU0a80TWFJA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAJbPmJNdupnHBLYx1zo8vSRpkHQVuqgfvDu11npimLP_xXKBJw%40mail.gmail.com.

rgre...@gmail.com

unread,
Oct 15, 2017, 3:48:31 PM10/15/17
to elixir-lang-core
Hi José,

Thanks for the response. Running Elixir is certainly necessary and your code example would solve the stdin/stdout issue. 

It's not immediately clear to me, however, how to support .formatter.exs files using this approach (aside from eval-ing the file separately and passing a keyword list to format_string!(). 

Please let me know if I misread the docs and there is a way to do this. If not, then adding a --stdin flag to Mix formatter task would address both issues.

Thanks in advance.

Norbert Melzer

unread,
Oct 15, 2017, 3:57:15 PM10/15/17
to elixir-l...@googlegroups.com
On 10/15/2017 09:48 PM, rgre...@gmail.com wrote:
> Please let me know if I misread the docs and there is a way to do this. If
> not, then adding a --stdin flag to Mix formatter task would address both
> issues.

I like the STDIN to STDOUT thing in general, since this allows piping,
chaining and composing of various tools, and I'm not sure, why we would
discourage a pattern which is common inside of the language, in the
tools of the langauge.

Instead of a special flag though, I'd prefer `-` as a filename, which is
pretty common in linux. When used as a “filename” to read from, its
meaning is “read from STDIN”, if though its used as a “filename” for the
output its meaning is ”write to STDOUT”.

Bye
Norbert

signature.asc

José Valim

unread,
Oct 15, 2017, 4:04:33 PM10/15/17
to elixir-l...@googlegroups.com
I like the "-" approach, it plays better with how we handle inputs. Could you please open up an issue?



José Valim
Founder and 
Director of R&D

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/4f425e3f-8675-1c3c-9999-583074ee3004%40gmail.com.

rgre...@gmail.com

unread,
Oct 15, 2017, 5:02:47 PM10/15/17
to elixir-lang-core
Yes, I'd be happy to open an issue.

Thanks.


On Sunday, October 15, 2017 at 3:04:33 PM UTC-5, José Valim wrote:
I like the "-" approach, it plays better with how we handle inputs. Could you please open up an issue?



José Valim
Founder and 
Director of R&D

On Sun, Oct 15, 2017 at 9:58 PM, Norbert Melzer <timm...@gmail.com> wrote:
On 10/15/2017 09:48 PM, rgre...@gmail.com wrote:
> Please let me know if I misread the docs and there is a way to do this. If
> not, then adding a --stdin flag to Mix formatter task would address both
> issues.

I like the STDIN to STDOUT thing in general, since this allows piping,
chaining and composing of various tools, and I'm not sure, why we would
discourage a pattern which is common inside of the language, in the
tools of the langauge.

Instead of a special flag though, I'd prefer `-` as a filename, which is
pretty common in linux. When used as a “filename” to read from, its
meaning is “read from STDIN”, if though its used as a “filename” for the
output its meaning is ”write to STDOUT”.

Bye
  Norbert

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages