Indentation of current line or selected blocks in the code editor?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

Indentation of current line or selected blocks in the code editor?

John W. Eaton
Administrator
Has anyone worked on adding an indentation feature for the code editor?

I would like to try to get some simple indentation feature working that
would at least handle re-indenting the current line or the selected region.

Also, how does the Matlab code editor work for indenting the current
line or blocks of code?  I expect new Octave users who are familiar with
Matlab would expect similar behavior.

Thanks,

jwe

Reply | Threaded
Open this post in threaded view
|

Re: Indentation of current line or selected blocks in the code editor?

bpabbott
Administrator
> On Nov 7, 2017, at 11:27 AM, John W. Eaton <[hidden email]> wrote:
>
> Has anyone worked on adding an indentation feature for the code editor?
>
> I would like to try to get some simple indentation feature working that would at least handle re-indenting the current line or the selected region.
>
> Also, how does the Matlab code editor work for indenting the current line or blocks of code?  I expect new Octave users who are familiar with Matlab would expect similar behavior.
>
> Thanks,
>
> jwe

Matlab has auto-indent, right-indent, and left-indect icons. Each are applied to the highlighted code or to the current line.

Ben



Reply | Threaded
Open this post in threaded view
|

Re: Indentation of current line or selected blocks in the code editor?

Torsten-3
On 07.11.2017 17:58, Ben Abbott wrote:

>> On Nov 7, 2017, at 11:27 AM, John W. Eaton <[hidden email]> wrote:
>>
>> Has anyone worked on adding an indentation feature for the code editor?
>>
>> I would like to try to get some simple indentation feature working that would at least handle re-indenting the current line or the selected region.
>>
>> Also, how does the Matlab code editor work for indenting the current line or blocks of code?  I expect new Octave users who are familiar with Matlab would expect similar behavior.
>>
>> Thanks,
>>
>> jwe
>
> Matlab has auto-indent, right-indent, and left-indect icons. Each are applied to the highlighted code or to the current line.
>
> Ben

In Octave editor, <Tab> indents line/selection, <Shift>+<Tab> unindents
line/selection.

Torsten



Reply | Threaded
Open this post in threaded view
|

Re: Indentation of current line or selected blocks in the code editor?

John W. Eaton
Administrator
On 11/07/2017 04:00 PM, Torsten wrote:

> In Octave editor, <Tab> indents line/selection, <Shift>+<Tab> unindents
> line/selection.

I see that tab does indent by a fixed amount when the cursor is before
the text on the line.  If the cursor is in the middle of the text, it
inserts spaces at the location of the cursor instead of indenting the
line (that's the behavior I expect from using Emacs, anyway).

For single lines, shift+tab just moves the cursor back, not the text on
the line.

For selections it does appear to work, but it is a fixed indent, not
sensitive to context.

I'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.

jwe



Reply | Threaded
Open this post in threaded view
|

Re: Indentation of current line or selected blocks in the code editor?

siko1056
John W. Eaton wrote
> I'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 f
f 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.

Kai

[1]:
https://hg.savannah.gnu.org/hgweb/octave/file/014f47089682/libinterp/parse-tree/pt-pr-code.h#l42
[2]:
https://hg.savannah.gnu.org/hgweb/octave/file/014f47089682/libinterp/corefcn/symtab.cc#l2058



--
Sent from: http://octave.1599824.n4.nabble.com/Octave-Maintainers-f1638794.html

Reply | Threaded
Open this post in threaded view
|

Re: Indentation of current line or selected blocks in the code editor?

John W. Eaton
Administrator
On 11/07/2017 06:13 PM, siko1056 wrote:

> John W. Eaton wrote
>> I'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 f
> f 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

Reply | Threaded
Open this post in threaded view
|

Re: Indentation of current line or selected blocks in the code editor?

Torsten-3
On 08.11.2017 20:25, John W. Eaton wrote:

> 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
>

There is some smart indentation while typing
(octave_qscintilla::smart_indent). Maybe we can use parts of it for
"offline" adjusting the indentation.

Torsten


Reply | Threaded
Open this post in threaded view
|

Re: Indentation of current line or selected blocks in the code editor?

John W. Eaton
Administrator
On 11/08/2017 03:56 PM, Torsten wrote:

> There is some smart indentation while typing
> (octave_qscintilla::smart_indent). Maybe we can use parts of it for
> "offline" adjusting the indentation.

Thanks for the pointer.

jwe



Reply | Threaded
Open this post in threaded view
|

Re: Indentation of current line or selected blocks in the code editor?

John W. Eaton
Administrator
On 11/08/2017 05:44 PM, John W. Eaton wrote:
> On 11/08/2017 03:56 PM, Torsten wrote:
>
>> There is some smart indentation while typing
>> (octave_qscintilla::smart_indent). Maybe we can use parts of it for
>> "offline" adjusting the indentation.
>
> Thanks for the pointer.

Here's what I have so far:

   http://hg.savannah.gnu.org/hgweb/octave/rev/e0bcd17ac070

I pushed it even though it is not complete so that it would be easier to
test.  It only adds code and doesn't modify an existing behavior.

Currently, this new indent feature is not connected to any keyboard
shortcut yet and is only accessible from the Edit -> Format menu in the
editor.  What keybinding should it use by default?

I'm sure there is a lot of room for improvement since this is very
simplistic.  You can easily fool it by having multiple block (begin or
end) keywords on a single line.  Only the first will be noticed, so
indentation will probably not be correct.  It does not attempt to detect
mismatched begin/end pairs (for matched by endif, for example).  But it
does appear to work correctly for me with well-formed code.  It will
work on a single line (where the cursor is) or on a block of selected text.

It probably should be limited to Octave code since it uses regexps that
are for Octave syntax, not other languages.  I'm not sure how the editor
works for different types of files.

Comments, help, and suggestions welcome.

jwe

Reply | Threaded
Open this post in threaded view
|

Re: Indentation of current line or selected blocks in the code editor?

Richard Crozier


On 14/11/17 22:01, John W. Eaton wrote:

> On 11/08/2017 05:44 PM, John W. Eaton wrote:
>> On 11/08/2017 03:56 PM, Torsten wrote:
>>
>>> There is some smart indentation while typing
>>> (octave_qscintilla::smart_indent). Maybe we can use parts of it for
>>> "offline" adjusting the indentation.
>>
>> Thanks for the pointer.
>
> Here's what I have so far:
>
>    http://hg.savannah.gnu.org/hgweb/octave/rev/e0bcd17ac070
>
> I pushed it even though it is not complete so that it would be easier to
> test.  It only adds code and doesn't modify an existing behavior.
>
> Currently, this new indent feature is not connected to any keyboard
> shortcut yet and is only accessible from the Edit -> Format menu in the
> editor.  What keybinding should it use by default?
>
Matlab uses CTRL+Ifor this, as good an option as any?

While on the subject, Matlab also has a useful function to wrap comment
paragraphs at 80 characters, preserving the indentation after the
comment character, e.g.

%       Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec
lobortis dolor sit amet eros feugiat tincidunt non vitae arcu. Duis
fringilla massa quis aliquet maximus. Suspendisse in pharetra dui.
Suspendisse potenti. In in diam at elit tempor sagittis sit amet.

becomes:

%       Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec
%       lobortis dolor sit amet eros feugiat tincidunt non vitae arcu. Duis
%       fringilla massa quis aliquet maximus. Suspendisse in pharetra dui.
%       Suspendisse potenti. In in diam at elit tempor sagittis sit amet.


It really is quite helpful. Is there any interest in implementing this?
As a fairly chunky start I have ported the python TextWrapper class to
classdef (see attached files). These already allow an indent to be
prepended to every wrapped line. This class also handles lots of text
wrapping edge cases nicely.

Also Matlab also isn't too clever about it, it just rewraps text comment
blocks with the same indentation.

Shortcut for this in Matlab is CTRL+J

Regards,

Richard





--
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.


TextWrapper.m (22K) Download Attachment
test_TextWrapper.m (7K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Indentation of current line or selected blocks in the code editor?

Torsten-3
On 14.11.2017 23:49, Richard Crozier wrote:

>
>
> On 14/11/17 22:01, John W. Eaton wrote:
>> On 11/08/2017 05:44 PM, John W. Eaton wrote:
>>> On 11/08/2017 03:56 PM, Torsten wrote:
>>>
>>>> There is some smart indentation while typing
>>>> (octave_qscintilla::smart_indent). Maybe we can use parts of it for
>>>> "offline" adjusting the indentation.
>>>
>>> Thanks for the pointer.
>>
>> Here's what I have so far:
>>
>>    http://hg.savannah.gnu.org/hgweb/octave/rev/e0bcd17ac070
>>
>> I pushed it even though it is not complete so that it would be easier
>> to test.  It only adds code and doesn't modify an existing behavior.
>>
>> Currently, this new indent feature is not connected to any keyboard
>> shortcut yet and is only accessible from the Edit -> Format menu in
>> the editor.  What keybinding should it use by default?
>>
>
> Matlab uses CTRL+Ifor this, as good an option as any?
>
> While on the subject, Matlab also has a useful function to wrap comment
> paragraphs at 80 characters, preserving the indentation after the
> comment character, e.g.
>
> %       Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec
> lobortis dolor sit amet eros feugiat tincidunt non vitae arcu. Duis
> fringilla massa quis aliquet maximus. Suspendisse in pharetra dui.
> Suspendisse potenti. In in diam at elit tempor sagittis sit amet.
>
> becomes:
>
> %       Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec
> %       lobortis dolor sit amet eros feugiat tincidunt non vitae arcu. Duis
> %       fringilla massa quis aliquet maximus. Suspendisse in pharetra dui.
> %       Suspendisse potenti. In in diam at elit tempor sagittis sit amet.
>
>
> It really is quite helpful. Is there any interest in implementing this?
> As a fairly chunky start I have ported the python TextWrapper class to
> classdef (see attached files). These already allow an indent to be
> prepended to every wrapped line. This class also handles lots of text
> wrapping edge cases nicely.
>
> Also Matlab also isn't too clever about it, it just rewraps text comment
> blocks with the same indentation.
>
> Shortcut for this in Matlab is CTRL+J
>
> Regards,
>
> Richard

The octave editor provides a similar feature for line breaking in
comments while typing with the editor's preference "break lines ..." &
"break lines in comments only".

Torsten