|
From: | John W. Eaton |
Subject: | Re: Indentation of current line or selected blocks in the code editor? |
Date: | Wed, 8 Nov 2017 14:25:14 -0500 |
User-agent: | Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 |
On 11/07/2017 06:13 PM, siko1056 wrote:
John W. Eaton wroteI'm looking for something that will indent the code according to syntax, so that the bodies of loops and conditionals will be neatly aligned automatically, as a pretty printer would.Regarding this, I once stumbled about the `tree_print_code` class [1], used in `__get_cmdline_fcn_txt__` [2], used in `type.m`. It does pretty printing of command line functions, for example:% Ugly one line function with preceding comment function y = f(x); y = x; endfunction; type ff is the command-line function: ## Ugly one line function with preceding comment function y = f (x) y = x; endfunction The code of [1] looks pretty much like doing this, even with indention. I don't know where this class is used/was used for. But as you see, the original code was not preserved, thus I did not investigate further.
The tree_print_code thing only works if there are no errors when parsing the code. Otherwise, you don't end up with a valid parse tree.
Also, the parse doesn't generally preserve any whitespace, so as it is now, using the parse tree to format the code wouldn't just indent code, it would also do things like change spacing around operators and reformat code blocks.
I think we need something that just indents and doesn't try to reformat everything and that will do a best-guess indentation even if there are some syntax errors.
It might be easier to pipe the text to Emacs and ask the Octave mode to indent the line or region. :-)
jwe
[Prev in Thread] | Current Thread | [Next in Thread] |