Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indentation does not work for me #2

Open
petergacs opened this issue Nov 13, 2018 · 11 comments
Open

Indentation does not work for me #2

petergacs opened this issue Nov 13, 2018 · 11 comments

Comments

@petergacs
Copy link

petergacs commented Nov 13, 2018

Line-by-line indentation by tab does strange things. (I cannot give an example here since in the preview all indentation disappears.)
The netlogo-indent-region and netlogo-indent-line commands return errors.

@tgolsson
Copy link
Owner

Hi @petergacs ! Wrap your code in three backticks, newline, and then your code. End it with another three backticks. That should hopefully preserve the formatting.

Like this:

```
This is where my code goes
```

which gives

This is where my code goes

Regarding your issue, I haven't used netlogo in a long time but if you can post your snippet I can take a look and see if it works as expected. I faintly recall that there was some discrepancy between format region and per-line formatting. If the direct commands give errors, it's probably related.

@petergacs
Copy link
Author

petergacs commented Nov 15, 2018

Thank you! I tried the backticks, they had no effect. Here is a code snippet.
As I said just hitting the tab on each line does not format it correctly, and if I try to invoke M-x netlogo-indent-region I get an error message like wrong value: sequencep 1. Similarly invoking netlogo-indent-line.
Here is a snippet (the output on this web page also distorts the indentation):

to age-and-die

let new-age xcor + 1
ifelse (new-age <= 60)
[set xcor new-age]
[
if is-married?
[
ask spouse
[
set is-married? false
set color red
]
]
die
]

end

@tgolsson
Copy link
Owner

Hi again @petergacs, I tried reproducing this but cannot reproduce any issues with the version in this repo.

This is the output I get both from indenting the whole region, and indenting-per-line.

to age-and-die
  
  let new-age xcor + 1
  ifelse (new-age <= 60)
  [set xcor new-age]
  [
    if is-married?
    [
      ask spouse
      [
        set is-married? false
        set color red
      ]
    ]
    die
  ]  
end

Can you please post what version of emacs you are using, as well as a callstack for this issue? You should be able to generate one by toggling toggle-debug-on-error to enabled, and then triggering an indentation command to get the error.

@petergacs
Copy link
Author

petergacs commented Nov 17, 2018 via email

@petergacs
Copy link
Author

When the indent-region fails it crashes Aquamacs.
Here is a simple case where it fails, apparently it does not like long expressions. It indents the following code segment fine:

let x start-center-x - start-radius + random (2 * start-radius)
let y start-center-y - start-radius + random (2 * start-radius)
setxy x y

However, it crashes Aquamacs on

setxy (start-center-x - start-radius + random (2 * start-radius)) (y start-center-y - start-radius + random (2 * start-radius))

@tgolsson
Copy link
Owner

Thank you, I've got a reproducible crash with that line. Will see what I can do.

@tgolsson
Copy link
Owner

Hi @petergacs ,

Can you try the new version I've added here? I've fixed two bugs for indentation, the most severe hopefully being the crash you observed. I'm not sure if it fixes single-line tab, but it should be more consistent in indentation overall.

@petergacs
Copy link
Author

Hi, Tom,

thank you. I have installed this, so far it has not crashed Emacs. I did a couple of trivial changes to it to mute complaints by byte-compile-file in emacs. I marked those changes in the attached file by "; Gacs:". The netlogo-indent-line still does not work the way it should. At least when I changed the (interactive (point)) to (interactive "P"), it does not give an error message.
I have two additional questions. One is trivial: I don't understand why the triple backslash (instead of double, which seems to be enough) in front of [ and ] in netlog-indent-increase-regexp and netlogo-indent-decrease-regexp. The other is more substantial: I don't always understand the distinct treatment of the positive and negative indentation changes. I also suspect that this is the reason why netlogo-indent-line is not indenting correctly.

Peter
netlogo-mode.el.zip

@tgolsson
Copy link
Owner

tgolsson commented Nov 30, 2018

You're absolutely correct on the triple backslash being unnecessary. It works equally for me, but I'll change it next week since it seems unnecessary. Thanks!

The positive and negative indentation change is based on where you want to apply the indentation. Consider the below code, with the caret being shown at *.

if (bar < 10)
[ 
*print "Bar"
]

So, in my mind, if we press <Tab>, we cannot look at the whole script and to decide where the p should be. So we take one step back, and ask how much the indentation should change assuming that the previous (semantically interesting) line is correctly indented. In the case here, the previous line matches the netlogo-indent-increase-regexp. Thus, our positive change, after the previous line, is +2. Because the [ should not be indented, giving the below result:

if (bar < 10)
[ 
  print "Bar"
]

Now, let's take this example instead:

if (bar < 10)
[ 
  print "Bar" 
  *]

In this situation, we actually want to reduce the indentation on the ] itself, so we can set the indentation delta and apply it immediately. Consider that if we had been on the [ instead, we would not have applied the indentation.

Now, this is mostly irrelevant because we're just looking at a single line. However, this becomes very important for region-indentation, because you carry the indentation with you. So if you see something that increases indentation, you start applying that after the next linebreak. It you see something that decrements indentation, you want to apply that immediately. Note that this is actually fragile as well:

if (bar < 10)
[ 
  print "Bar" *]

Will move p to 0 indentation, which... may not be what you want. On the other hand, this code is horribly formatted either way, so I accepted that "bad formatting means bad indentation" as I felt the overall result was better.

With that said, I still cannot reproduce any issues with netlogo-indent-line, no matter whether I use tab or call the function manually (except the (point) -> "P" change for manual calls. Thanks!). As an example, I took this code:

to-report foo
if (bar < 10)
[ 
print "Bar"
]
report qux = 10
end

And going line-by-line and calling M-x netlogo-indent-line gives me:

to-report foo
  if (bar < 10)
  [ 
    print "Bar"
  ]
  report qux = 10
end

Which is what I expected.

@petergacs
Copy link
Author

petergacs commented Dec 1, 2018

Hi, Tom,
thank you for the explanation. I had understood that part, the problems were elsewhere. I tried to correct the indentation functions, you may want to look at it. At some point the corrections were too many to mark them individually. But there are three main important ones.

  1. I changed ^[\s-] to ^[\s-\s.\t] in the regular expression searching backward for a line with content.
  2. I simplified the netlogo-indent-previous-indent: no case distinction is needed there, it simply returns (current-indentation) (from within save-excursion).
  3. In netlogo-indent-line, I used also a variable netlogo-indent-previous-change.

This version of netlogo-mode seems to work for me (at least under a little testing).
netlogo-mode.el.zip

Peter

@petergacs
Copy link
Author

petergacs commented Dec 4, 2018

I made another slight correction in the netlogo-indent-increase-regexp and netlogo-indent-decrease-regexp, replacing the original "\s" with "\s-" (the double backslash does not seem to come through here on the web).
netlogo-mode.el.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants