Re: [racket-users] auto-indentation... off by one^H^H^Hsome?

2020-03-21 Thread Robby Findler
I had already merged it but I agree that it could probably stand to
have a little elaboration now. I'll do it.

Robby

On Sat, Mar 21, 2020 at 9:12 PM John Clements  wrote:
>
> Many thanks! Should I cancel my pull request?
>
> John
>
> > On Mar 21, 2020, at 18:34, Robby Findler  wrote:
> >
> > I've pushed something so that racket:text% will indent better when it
> > doesn't have a display.
> >
> > Robby
> >
> > On Fri, Mar 20, 2020 at 4:32 PM John Clements  
> > wrote:
> >>
> >> Made a pull request, many thanks!
> >>
> >> John
> >>
> >>> On Mar 20, 2020, at 1:36 PM, Robby Findler  
> >>> wrote:
> >>>
> >>> Looks right to me!
> >>>
> >>> On Fri, Mar 20, 2020 at 3:26 PM John Clements  
> >>> wrote:
> >>> Could I add a note like this to the docs for the indentation function?
> >>>
> >>> “NB: indentation results depend on the graphical context associated with 
> >>> the object; it may be necessary to associate the object with an 
> >>> editor-canvas and frame in order to get the expected results."
> >>>
> >>> That text might not actually be accurate…
> >>>
> >>> John
> >>>
>  On Mar 20, 2020, at 1:16 PM, John Clements  
>  wrote:
> 
>  Ah! this solves the issue. many thanks.
> 
> > On Mar 20, 2020, at 1:03 PM, Robby Findler  
> > wrote:
> >
> > Looks like you need a display or the text gets confused about how big
> > (in pixels) characters really are:
> >
> > #lang racket/gui
> > (require framework)
> > (define f (new frame% [label ""]))
> > (define t (new racket:text%))
> > (define ec (new editor-canvas% [parent f] [editor t]))
> > (send t insert "(+3\n4)")
> > (send t freeze-colorer)
> > (send t tabify-all)
> > (display (send t get-text))
> >
> > On Fri, Mar 20, 2020 at 2:12 PM John Clements 
> >  wrote:
> >>
> >> That actually solves a bunch of problems for me… but strangely, not 
> >> the initial one. If, for instance, I tabify
> >>
> >> (+  3
> >> 4)
> >>
> >> using the tabify-all method, the line with the four gets an 
> >> indentation of 2, not 8. This is definitely different from DrRacket’s 
> >> behavior.
> >>
> >> Any idea how to fix this?
> >>
> >> (cc:ing racket-users without permission, hope that’s okay?)
> >>
> >> John
> >>
> >>
> >>> On Mar 20, 2020, at 12:05 PM, John Clements 
> >>>  wrote:
> >>>
> >>> Ah! That’s probably a better solution, I’ll just do that. I searched 
> >>> for “indent”, should I try to add that as a search term for the 
> >>> “tabify-all” method?
> >>>
> >>> Many thanks!
> >>>
> >>> (Also, sending mail to the list using “us...@racket-lang.org” seems 
> >>> to be broken right now for me, sigh.)
> >>>
> >>> John
> >>>
>  On Mar 20, 2020, at 12:01 PM, Robby Findler 
>   wrote:
> 
>  I'm not sure of the details but did you consider just inserting it
>  into a racket:text% and then calling the tabify-all method?
> 
>  Robby
> 
>  On Fri, Mar 20, 2020 at 1:26 PM John Clements 
>   wrote:
> >
> > Hmm… followup problem. If I try to indent the string “#|\nabc\n|#” 
> > (that is, a block comment), the compute-racket-amount-to-indent 
> > method returns #f. Is this a bug, or just undocumented behavior?
> >
> > John
> >
> >> On Mar 20, 2020, at 10:41 AM, John Clements 
> >>  wrote:
> >>
> >> I’m writing code to help me grade exams, and one of the issues 
> >> I’ve run into is that the code coming out of the LMS is all 
> >> totally unindented. So, for instance, a student’s response might 
> >> read:
> >>
> >> ;Closures help a function value remember what substitutions have 
> >> already been applied to it.
> >> '{vars {{x {lam {x} {+ 1 x}}}
> >> {y {lam {y} {+ 3 y
> >> {+ {x 2} {y 4}}}
> >>
> >> Obviously, it would be a lot easier to grade that if it was 
> >> indented.
> >>
> >> Excellent! A chance to whip up a quick-and-dirty auto-indenter, 
> >> using DrRacket’s indentation framework. Specifically, the 
> >> `compute-racket-amount-to-indent` method of racket:text<%>.
> >>
> >> I’m pleased to report almost total success, aside from one strange 
> >> off-by-one error that I’m looking for help with.  To see it, 
> >> here’s the result of my auto-indenter on the prior block:
> >>
> >> '{vars {{x {lam {x} {+ 1 x}}}
> >>   {y {lam {y} {+ 3 y
> >> {+ {x 2} {y 4}}}
> >>
> >> (if you’re viewing this in a proportional-width font, that’s going 
> >> to look terrible, sorry.)
> >>
> >> The issue is that the final plus line isn’t lined up with the 
> >> 

Re: [racket-users] auto-indentation... off by one^H^H^Hsome?

2020-03-21 Thread 'John Clements' via Racket Users
Many thanks! Should I cancel my pull request?

John

> On Mar 21, 2020, at 18:34, Robby Findler  wrote:
> 
> I've pushed something so that racket:text% will indent better when it
> doesn't have a display.
> 
> Robby
> 
> On Fri, Mar 20, 2020 at 4:32 PM John Clements  
> wrote:
>> 
>> Made a pull request, many thanks!
>> 
>> John
>> 
>>> On Mar 20, 2020, at 1:36 PM, Robby Findler  
>>> wrote:
>>> 
>>> Looks right to me!
>>> 
>>> On Fri, Mar 20, 2020 at 3:26 PM John Clements  
>>> wrote:
>>> Could I add a note like this to the docs for the indentation function?
>>> 
>>> “NB: indentation results depend on the graphical context associated with 
>>> the object; it may be necessary to associate the object with an 
>>> editor-canvas and frame in order to get the expected results."
>>> 
>>> That text might not actually be accurate…
>>> 
>>> John
>>> 
 On Mar 20, 2020, at 1:16 PM, John Clements  
 wrote:
 
 Ah! this solves the issue. many thanks.
 
> On Mar 20, 2020, at 1:03 PM, Robby Findler  
> wrote:
> 
> Looks like you need a display or the text gets confused about how big
> (in pixels) characters really are:
> 
> #lang racket/gui
> (require framework)
> (define f (new frame% [label ""]))
> (define t (new racket:text%))
> (define ec (new editor-canvas% [parent f] [editor t]))
> (send t insert "(+3\n4)")
> (send t freeze-colorer)
> (send t tabify-all)
> (display (send t get-text))
> 
> On Fri, Mar 20, 2020 at 2:12 PM John Clements  
> wrote:
>> 
>> That actually solves a bunch of problems for me… but strangely, not the 
>> initial one. If, for instance, I tabify
>> 
>> (+  3
>> 4)
>> 
>> using the tabify-all method, the line with the four gets an indentation 
>> of 2, not 8. This is definitely different from DrRacket’s behavior.
>> 
>> Any idea how to fix this?
>> 
>> (cc:ing racket-users without permission, hope that’s okay?)
>> 
>> John
>> 
>> 
>>> On Mar 20, 2020, at 12:05 PM, John Clements  
>>> wrote:
>>> 
>>> Ah! That’s probably a better solution, I’ll just do that. I searched 
>>> for “indent”, should I try to add that as a search term for the 
>>> “tabify-all” method?
>>> 
>>> Many thanks!
>>> 
>>> (Also, sending mail to the list using “us...@racket-lang.org” seems to 
>>> be broken right now for me, sigh.)
>>> 
>>> John
>>> 
 On Mar 20, 2020, at 12:01 PM, Robby Findler 
  wrote:
 
 I'm not sure of the details but did you consider just inserting it
 into a racket:text% and then calling the tabify-all method?
 
 Robby
 
 On Fri, Mar 20, 2020 at 1:26 PM John Clements 
  wrote:
> 
> Hmm… followup problem. If I try to indent the string “#|\nabc\n|#” 
> (that is, a block comment), the compute-racket-amount-to-indent 
> method returns #f. Is this a bug, or just undocumented behavior?
> 
> John
> 
>> On Mar 20, 2020, at 10:41 AM, John Clements 
>>  wrote:
>> 
>> I’m writing code to help me grade exams, and one of the issues I’ve 
>> run into is that the code coming out of the LMS is all totally 
>> unindented. So, for instance, a student’s response might read:
>> 
>> ;Closures help a function value remember what substitutions have 
>> already been applied to it.
>> '{vars {{x {lam {x} {+ 1 x}}}
>> {y {lam {y} {+ 3 y
>> {+ {x 2} {y 4}}}
>> 
>> Obviously, it would be a lot easier to grade that if it was indented.
>> 
>> Excellent! A chance to whip up a quick-and-dirty auto-indenter, 
>> using DrRacket’s indentation framework. Specifically, the 
>> `compute-racket-amount-to-indent` method of racket:text<%>.
>> 
>> I’m pleased to report almost total success, aside from one strange 
>> off-by-one error that I’m looking for help with.  To see it, here’s 
>> the result of my auto-indenter on the prior block:
>> 
>> '{vars {{x {lam {x} {+ 1 x}}}
>>   {y {lam {y} {+ 3 y
>> {+ {x 2} {y 4}}}
>> 
>> (if you’re viewing this in a proportional-width font, that’s going 
>> to look terrible, sorry.)
>> 
>> The issue is that the final plus line isn’t lined up with the 
>> curly-brace that’s two chars after the end of `vars`… instead, it’s 
>> one character to the left.
>> 
>> A much simpler test case is simply running on “(+ 3\n4)”. Rather 
>> than getting the 4 lined up with the 3, I get it one character to 
>> the left.
>> 
>> And… uh oh. A more illuminating test case occurs when I run my code 
>> on “(+  3\n4)”. That is, I put a bunch more 

Re: [racket-users] auto-indentation... off by one^H^H^Hsome?

2020-03-21 Thread Robby Findler
I've pushed something so that racket:text% will indent better when it
doesn't have a display.

Robby

On Fri, Mar 20, 2020 at 4:32 PM John Clements  wrote:
>
> Made a pull request, many thanks!
>
> John
>
> > On Mar 20, 2020, at 1:36 PM, Robby Findler  
> > wrote:
> >
> > Looks right to me!
> >
> > On Fri, Mar 20, 2020 at 3:26 PM John Clements  
> > wrote:
> > Could I add a note like this to the docs for the indentation function?
> >
> > “NB: indentation results depend on the graphical context associated with 
> > the object; it may be necessary to associate the object with an 
> > editor-canvas and frame in order to get the expected results."
> >
> > That text might not actually be accurate…
> >
> > John
> >
> > > On Mar 20, 2020, at 1:16 PM, John Clements  
> > > wrote:
> > >
> > > Ah! this solves the issue. many thanks.
> > >
> > >> On Mar 20, 2020, at 1:03 PM, Robby Findler  
> > >> wrote:
> > >>
> > >> Looks like you need a display or the text gets confused about how big
> > >> (in pixels) characters really are:
> > >>
> > >> #lang racket/gui
> > >> (require framework)
> > >> (define f (new frame% [label ""]))
> > >> (define t (new racket:text%))
> > >> (define ec (new editor-canvas% [parent f] [editor t]))
> > >> (send t insert "(+3\n4)")
> > >> (send t freeze-colorer)
> > >> (send t tabify-all)
> > >> (display (send t get-text))
> > >>
> > >> On Fri, Mar 20, 2020 at 2:12 PM John Clements 
> > >>  wrote:
> > >>>
> > >>> That actually solves a bunch of problems for me… but strangely, not the 
> > >>> initial one. If, for instance, I tabify
> > >>>
> > >>> (+  3
> > >>> 4)
> > >>>
> > >>> using the tabify-all method, the line with the four gets an indentation 
> > >>> of 2, not 8. This is definitely different from DrRacket’s behavior.
> > >>>
> > >>> Any idea how to fix this?
> > >>>
> > >>> (cc:ing racket-users without permission, hope that’s okay?)
> > >>>
> > >>> John
> > >>>
> > >>>
> >  On Mar 20, 2020, at 12:05 PM, John Clements 
> >   wrote:
> > 
> >  Ah! That’s probably a better solution, I’ll just do that. I searched 
> >  for “indent”, should I try to add that as a search term for the 
> >  “tabify-all” method?
> > 
> >  Many thanks!
> > 
> >  (Also, sending mail to the list using “us...@racket-lang.org” seems to 
> >  be broken right now for me, sigh.)
> > 
> >  John
> > 
> > > On Mar 20, 2020, at 12:01 PM, Robby Findler 
> > >  wrote:
> > >
> > > I'm not sure of the details but did you consider just inserting it
> > > into a racket:text% and then calling the tabify-all method?
> > >
> > > Robby
> > >
> > > On Fri, Mar 20, 2020 at 1:26 PM John Clements 
> > >  wrote:
> > >>
> > >> Hmm… followup problem. If I try to indent the string “#|\nabc\n|#” 
> > >> (that is, a block comment), the compute-racket-amount-to-indent 
> > >> method returns #f. Is this a bug, or just undocumented behavior?
> > >>
> > >> John
> > >>
> > >>> On Mar 20, 2020, at 10:41 AM, John Clements 
> > >>>  wrote:
> > >>>
> > >>> I’m writing code to help me grade exams, and one of the issues I’ve 
> > >>> run into is that the code coming out of the LMS is all totally 
> > >>> unindented. So, for instance, a student’s response might read:
> > >>>
> > >>> ;Closures help a function value remember what substitutions have 
> > >>> already been applied to it.
> > >>> '{vars {{x {lam {x} {+ 1 x}}}
> > >>> {y {lam {y} {+ 3 y
> > >>> {+ {x 2} {y 4}}}
> > >>>
> > >>> Obviously, it would be a lot easier to grade that if it was 
> > >>> indented.
> > >>>
> > >>> Excellent! A chance to whip up a quick-and-dirty auto-indenter, 
> > >>> using DrRacket’s indentation framework. Specifically, the 
> > >>> `compute-racket-amount-to-indent` method of racket:text<%>.
> > >>>
> > >>> I’m pleased to report almost total success, aside from one strange 
> > >>> off-by-one error that I’m looking for help with.  To see it, here’s 
> > >>> the result of my auto-indenter on the prior block:
> > >>>
> > >>> '{vars {{x {lam {x} {+ 1 x}}}
> > >>>{y {lam {y} {+ 3 y
> > >>>  {+ {x 2} {y 4}}}
> > >>>
> > >>> (if you’re viewing this in a proportional-width font, that’s going 
> > >>> to look terrible, sorry.)
> > >>>
> > >>> The issue is that the final plus line isn’t lined up with the 
> > >>> curly-brace that’s two chars after the end of `vars`… instead, it’s 
> > >>> one character to the left.
> > >>>
> > >>> A much simpler test case is simply running on “(+ 3\n4)”. Rather 
> > >>> than getting the 4 lined up with the 3, I get it one character to 
> > >>> the left.
> > >>>
> > >>> And… uh oh. A more illuminating test case occurs when I run my code 
> > >>> on “(+  3\n4)”. That is, I put a bunch more spaces before the 
> > >>> 3. After 

Re: [racket-users] auto-indentation... off by one^H^H^Hsome?

2020-03-20 Thread 'John Clements' via Racket Users
Made a pull request, many thanks!

John

> On Mar 20, 2020, at 1:36 PM, Robby Findler  wrote:
> 
> Looks right to me!
> 
> On Fri, Mar 20, 2020 at 3:26 PM John Clements  
> wrote:
> Could I add a note like this to the docs for the indentation function?
> 
> “NB: indentation results depend on the graphical context associated with the 
> object; it may be necessary to associate the object with an editor-canvas and 
> frame in order to get the expected results."
> 
> That text might not actually be accurate…
> 
> John
> 
> > On Mar 20, 2020, at 1:16 PM, John Clements  
> > wrote:
> > 
> > Ah! this solves the issue. many thanks.
> > 
> >> On Mar 20, 2020, at 1:03 PM, Robby Findler  
> >> wrote:
> >> 
> >> Looks like you need a display or the text gets confused about how big
> >> (in pixels) characters really are:
> >> 
> >> #lang racket/gui
> >> (require framework)
> >> (define f (new frame% [label ""]))
> >> (define t (new racket:text%))
> >> (define ec (new editor-canvas% [parent f] [editor t]))
> >> (send t insert "(+3\n4)")
> >> (send t freeze-colorer)
> >> (send t tabify-all)
> >> (display (send t get-text))
> >> 
> >> On Fri, Mar 20, 2020 at 2:12 PM John Clements  
> >> wrote:
> >>> 
> >>> That actually solves a bunch of problems for me… but strangely, not the 
> >>> initial one. If, for instance, I tabify
> >>> 
> >>> (+  3
> >>> 4)
> >>> 
> >>> using the tabify-all method, the line with the four gets an indentation 
> >>> of 2, not 8. This is definitely different from DrRacket’s behavior.
> >>> 
> >>> Any idea how to fix this?
> >>> 
> >>> (cc:ing racket-users without permission, hope that’s okay?)
> >>> 
> >>> John
> >>> 
> >>> 
>  On Mar 20, 2020, at 12:05 PM, John Clements  
>  wrote:
>  
>  Ah! That’s probably a better solution, I’ll just do that. I searched for 
>  “indent”, should I try to add that as a search term for the “tabify-all” 
>  method?
>  
>  Many thanks!
>  
>  (Also, sending mail to the list using “us...@racket-lang.org” seems to 
>  be broken right now for me, sigh.)
>  
>  John
>  
> > On Mar 20, 2020, at 12:01 PM, Robby Findler  
> > wrote:
> > 
> > I'm not sure of the details but did you consider just inserting it
> > into a racket:text% and then calling the tabify-all method?
> > 
> > Robby
> > 
> > On Fri, Mar 20, 2020 at 1:26 PM John Clements 
> >  wrote:
> >> 
> >> Hmm… followup problem. If I try to indent the string “#|\nabc\n|#” 
> >> (that is, a block comment), the compute-racket-amount-to-indent method 
> >> returns #f. Is this a bug, or just undocumented behavior?
> >> 
> >> John
> >> 
> >>> On Mar 20, 2020, at 10:41 AM, John Clements 
> >>>  wrote:
> >>> 
> >>> I’m writing code to help me grade exams, and one of the issues I’ve 
> >>> run into is that the code coming out of the LMS is all totally 
> >>> unindented. So, for instance, a student’s response might read:
> >>> 
> >>> ;Closures help a function value remember what substitutions have 
> >>> already been applied to it.
> >>> '{vars {{x {lam {x} {+ 1 x}}}
> >>> {y {lam {y} {+ 3 y
> >>> {+ {x 2} {y 4}}}
> >>> 
> >>> Obviously, it would be a lot easier to grade that if it was indented.
> >>> 
> >>> Excellent! A chance to whip up a quick-and-dirty auto-indenter, using 
> >>> DrRacket’s indentation framework. Specifically, the 
> >>> `compute-racket-amount-to-indent` method of racket:text<%>.
> >>> 
> >>> I’m pleased to report almost total success, aside from one strange 
> >>> off-by-one error that I’m looking for help with.  To see it, here’s 
> >>> the result of my auto-indenter on the prior block:
> >>> 
> >>> '{vars {{x {lam {x} {+ 1 x}}}
> >>>{y {lam {y} {+ 3 y
> >>>  {+ {x 2} {y 4}}}
> >>> 
> >>> (if you’re viewing this in a proportional-width font, that’s going to 
> >>> look terrible, sorry.)
> >>> 
> >>> The issue is that the final plus line isn’t lined up with the 
> >>> curly-brace that’s two chars after the end of `vars`… instead, it’s 
> >>> one character to the left.
> >>> 
> >>> A much simpler test case is simply running on “(+ 3\n4)”. Rather than 
> >>> getting the 4 lined up with the 3, I get it one character to the left.
> >>> 
> >>> And… uh oh. A more illuminating test case occurs when I run my code 
> >>> on “(+  3\n4)”. That is, I put a bunch more spaces before the 3. 
> >>> After indentation, the 4 is still only indented by two characters. So 
> >>> it looks like the `compute-racket-amount-to-indent` method is not 
> >>> looking for the token following the first one following the paren in 
> >>> the prior line (which would account for all the spaces), but just 
> >>> adding one to the last position of the first token following the 
> >>> paren. This makes me wonder 

Re: [racket-users] auto-indentation... off by one^H^H^Hsome?

2020-03-20 Thread Robby Findler
Looks right to me!

On Fri, Mar 20, 2020 at 3:26 PM John Clements 
wrote:

> Could I add a note like this to the docs for the indentation function?
>
> “NB: indentation results depend on the graphical context associated with
> the object; it may be necessary to associate the object with an
> editor-canvas and frame in order to get the expected results."
>
> That text might not actually be accurate…
>
> John
>
> > On Mar 20, 2020, at 1:16 PM, John Clements 
> wrote:
> >
> > Ah! this solves the issue. many thanks.
> >
> >> On Mar 20, 2020, at 1:03 PM, Robby Findler 
> wrote:
> >>
> >> Looks like you need a display or the text gets confused about how big
> >> (in pixels) characters really are:
> >>
> >> #lang racket/gui
> >> (require framework)
> >> (define f (new frame% [label ""]))
> >> (define t (new racket:text%))
> >> (define ec (new editor-canvas% [parent f] [editor t]))
> >> (send t insert "(+3\n4)")
> >> (send t freeze-colorer)
> >> (send t tabify-all)
> >> (display (send t get-text))
> >>
> >> On Fri, Mar 20, 2020 at 2:12 PM John Clements <
> cleme...@brinckerhoff.org> wrote:
> >>>
> >>> That actually solves a bunch of problems for me… but strangely, not
> the initial one. If, for instance, I tabify
> >>>
> >>> (+  3
> >>> 4)
> >>>
> >>> using the tabify-all method, the line with the four gets an
> indentation of 2, not 8. This is definitely different from DrRacket’s
> behavior.
> >>>
> >>> Any idea how to fix this?
> >>>
> >>> (cc:ing racket-users without permission, hope that’s okay?)
> >>>
> >>> John
> >>>
> >>>
>  On Mar 20, 2020, at 12:05 PM, John Clements <
> cleme...@brinckerhoff.org> wrote:
> 
>  Ah! That’s probably a better solution, I’ll just do that. I searched
> for “indent”, should I try to add that as a search term for the
> “tabify-all” method?
> 
>  Many thanks!
> 
>  (Also, sending mail to the list using “us...@racket-lang.org” seems
> to be broken right now for me, sigh.)
> 
>  John
> 
> > On Mar 20, 2020, at 12:01 PM, Robby Findler <
> ro...@cs.northwestern.edu> wrote:
> >
> > I'm not sure of the details but did you consider just inserting it
> > into a racket:text% and then calling the tabify-all method?
> >
> > Robby
> >
> > On Fri, Mar 20, 2020 at 1:26 PM John Clements <
> cleme...@brinckerhoff.org> wrote:
> >>
> >> Hmm… followup problem. If I try to indent the string “#|\nabc\n|#”
> (that is, a block comment), the compute-racket-amount-to-indent method
> returns #f. Is this a bug, or just undocumented behavior?
> >>
> >> John
> >>
> >>> On Mar 20, 2020, at 10:41 AM, John Clements <
> cleme...@brinckerhoff.org> wrote:
> >>>
> >>> I’m writing code to help me grade exams, and one of the issues
> I’ve run into is that the code coming out of the LMS is all totally
> unindented. So, for instance, a student’s response might read:
> >>>
> >>> ;Closures help a function value remember what substitutions have
> already been applied to it.
> >>> '{vars {{x {lam {x} {+ 1 x}}}
> >>> {y {lam {y} {+ 3 y
> >>> {+ {x 2} {y 4}}}
> >>>
> >>> Obviously, it would be a lot easier to grade that if it was
> indented.
> >>>
> >>> Excellent! A chance to whip up a quick-and-dirty auto-indenter,
> using DrRacket’s indentation framework. Specifically, the
> `compute-racket-amount-to-indent` method of racket:text<%>.
> >>>
> >>> I’m pleased to report almost total success, aside from one strange
> off-by-one error that I’m looking for help with.  To see it, here’s the
> result of my auto-indenter on the prior block:
> >>>
> >>> '{vars {{x {lam {x} {+ 1 x}}}
> >>>{y {lam {y} {+ 3 y
> >>>  {+ {x 2} {y 4}}}
> >>>
> >>> (if you’re viewing this in a proportional-width font, that’s going
> to look terrible, sorry.)
> >>>
> >>> The issue is that the final plus line isn’t lined up with the
> curly-brace that’s two chars after the end of `vars`… instead, it’s one
> character to the left.
> >>>
> >>> A much simpler test case is simply running on “(+ 3\n4)”. Rather
> than getting the 4 lined up with the 3, I get it one character to the left.
> >>>
> >>> And… uh oh. A more illuminating test case occurs when I run my
> code on “(+  3\n4)”. That is, I put a bunch more spaces before the 3.
> After indentation, the 4 is still only indented by two characters. So it
> looks like the `compute-racket-amount-to-indent` method is not looking for
> the token following the first one following the paren in the prior line
> (which would account for all the spaces), but just adding one to the last
> position of the first token following the paren. This makes me wonder
> whether DrRacket actually uses this method to indent.
> >>>
> >>> Hmm.
> >>>
> >>> Well, here’s the code to reproduce this. Note that there’s an
> obvious bug/assumption in that my code doesn’t account for any existing
> leading 

Re: [racket-users] auto-indentation... off by one^H^H^Hsome?

2020-03-20 Thread 'John Clements' via Racket Users
Could I add a note like this to the docs for the indentation function?

“NB: indentation results depend on the graphical context associated with the 
object; it may be necessary to associate the object with an editor-canvas and 
frame in order to get the expected results."

That text might not actually be accurate…

John

> On Mar 20, 2020, at 1:16 PM, John Clements  wrote:
> 
> Ah! this solves the issue. many thanks.
> 
>> On Mar 20, 2020, at 1:03 PM, Robby Findler  wrote:
>> 
>> Looks like you need a display or the text gets confused about how big
>> (in pixels) characters really are:
>> 
>> #lang racket/gui
>> (require framework)
>> (define f (new frame% [label ""]))
>> (define t (new racket:text%))
>> (define ec (new editor-canvas% [parent f] [editor t]))
>> (send t insert "(+3\n4)")
>> (send t freeze-colorer)
>> (send t tabify-all)
>> (display (send t get-text))
>> 
>> On Fri, Mar 20, 2020 at 2:12 PM John Clements  
>> wrote:
>>> 
>>> That actually solves a bunch of problems for me… but strangely, not the 
>>> initial one. If, for instance, I tabify
>>> 
>>> (+  3
>>> 4)
>>> 
>>> using the tabify-all method, the line with the four gets an indentation of 
>>> 2, not 8. This is definitely different from DrRacket’s behavior.
>>> 
>>> Any idea how to fix this?
>>> 
>>> (cc:ing racket-users without permission, hope that’s okay?)
>>> 
>>> John
>>> 
>>> 
 On Mar 20, 2020, at 12:05 PM, John Clements  
 wrote:
 
 Ah! That’s probably a better solution, I’ll just do that. I searched for 
 “indent”, should I try to add that as a search term for the “tabify-all” 
 method?
 
 Many thanks!
 
 (Also, sending mail to the list using “us...@racket-lang.org” seems to be 
 broken right now for me, sigh.)
 
 John
 
> On Mar 20, 2020, at 12:01 PM, Robby Findler  
> wrote:
> 
> I'm not sure of the details but did you consider just inserting it
> into a racket:text% and then calling the tabify-all method?
> 
> Robby
> 
> On Fri, Mar 20, 2020 at 1:26 PM John Clements  
> wrote:
>> 
>> Hmm… followup problem. If I try to indent the string “#|\nabc\n|#” (that 
>> is, a block comment), the compute-racket-amount-to-indent method returns 
>> #f. Is this a bug, or just undocumented behavior?
>> 
>> John
>> 
>>> On Mar 20, 2020, at 10:41 AM, John Clements  
>>> wrote:
>>> 
>>> I’m writing code to help me grade exams, and one of the issues I’ve run 
>>> into is that the code coming out of the LMS is all totally unindented. 
>>> So, for instance, a student’s response might read:
>>> 
>>> ;Closures help a function value remember what substitutions have 
>>> already been applied to it.
>>> '{vars {{x {lam {x} {+ 1 x}}}
>>> {y {lam {y} {+ 3 y
>>> {+ {x 2} {y 4}}}
>>> 
>>> Obviously, it would be a lot easier to grade that if it was indented.
>>> 
>>> Excellent! A chance to whip up a quick-and-dirty auto-indenter, using 
>>> DrRacket’s indentation framework. Specifically, the 
>>> `compute-racket-amount-to-indent` method of racket:text<%>.
>>> 
>>> I’m pleased to report almost total success, aside from one strange 
>>> off-by-one error that I’m looking for help with.  To see it, here’s the 
>>> result of my auto-indenter on the prior block:
>>> 
>>> '{vars {{x {lam {x} {+ 1 x}}}
>>>{y {lam {y} {+ 3 y
>>>  {+ {x 2} {y 4}}}
>>> 
>>> (if you’re viewing this in a proportional-width font, that’s going to 
>>> look terrible, sorry.)
>>> 
>>> The issue is that the final plus line isn’t lined up with the 
>>> curly-brace that’s two chars after the end of `vars`… instead, it’s one 
>>> character to the left.
>>> 
>>> A much simpler test case is simply running on “(+ 3\n4)”. Rather than 
>>> getting the 4 lined up with the 3, I get it one character to the left.
>>> 
>>> And… uh oh. A more illuminating test case occurs when I run my code on 
>>> “(+  3\n4)”. That is, I put a bunch more spaces before the 3. After 
>>> indentation, the 4 is still only indented by two characters. So it 
>>> looks like the `compute-racket-amount-to-indent` method is not looking 
>>> for the token following the first one following the paren in the prior 
>>> line (which would account for all the spaces), but just adding one to 
>>> the last position of the first token following the paren. This makes me 
>>> wonder whether DrRacket actually uses this method to indent.
>>> 
>>> Hmm.
>>> 
>>> Well, here’s the code to reproduce this. Note that there’s an obvious 
>>> bug/assumption in that my code doesn’t account for any existing leading 
>>> spaces on a line, but none of my examples have leading spaces following 
>>> newlines.
>>> 
>>> Advice appreciated!
>>> 
>>> John
>>> 
>>> 
>>> 
>>> 

Re: [racket-users] auto-indentation... off by one^H^H^Hsome?

2020-03-20 Thread 'John Clements' via Racket Users
Unimportant thing one: this code had the same issue as mine, but as Robby 
points out, the issue is the way the text% is constructed; I was just creating 
a racket:text% directly, which (IIUC) doesn’t have a graphics context, so the 
auto-indenter can’t figure out how wide characters are.

Less unimportant thing: It seems like a whole bunch of people are writing 
racket linting/style-checking code right now, which seems like an unfortunate 
duplication of resources. I’ve got one that I’m about to deploy to my students 
that uses a combination of raw-text and read-but-not-expanded syntax, and right 
now it’s looking for simple things like functions without purpose statements, 
lines with only trailing parens, and uses of ‘cast’ that aren’t in fairly 
specific places (to wit, the bodies of functions named “parse”, a kind of 
ridiculous hack).

If you released your code, then we could all use it, and shower you day and 
night with bug reports and irritating feature requests!

John

> On Mar 20, 2020, at 12:21 PM, Benjamin Lerner  wrote:
> 
> In my auto-indenter and style checker (not yet a package that’s widely 
> available): evidently, there are some preferences that control which 
> constructs get tabified differently than expected. So I tweak those 
> preferences before invoking tabify-all. (In my case, I’m trying to support 
> two plausible indentation styles, depending on whether students have 
> different defaults set, so I compute the indentation twice.)
> 
> #lang racket
> 
> ;; Read in a source file and tabify it according to the following three 
> tabbing styles:
> ;;
> ;; 1. Untabbed (as writen directly in the file)
> ;; 2. Standard DrRacket tabbing excluding the big-bang default.
> ;;big-bang indents as:
> ;;  (big-bang a
> ;;b)
> ;; 3. Standard DrRacket tabbing including the big-bang default.
> ;;big-bang indents as:
> ;;  (big-bang a
> ;;b)
> ;;
> ;; (-> racket:text%? (values string? string? string?))
> ;;
> ;; WARNING!!! This function likely has effects based on the framework 
> library. It should NOT touch
> ;; your filesystem. However, this module should not be instantiated alongside
> ;; other DrRacket preferences.
> ;;
> (provide tabify-text)
> 
> (require framework/preferences)
> 
> (define (tabify-text t)
>   (parameterize* ([preferences:low-level-put-preferences
>(λ _ (void))]
>   [preferences:low-level-get-preference
>(λ _ #f)])
> (define untabbed (send t get-text))
> (define tabbed
>   (let ()
> (match-define (list table rx1 rx2 rx3 rx4)
>   (preferences:get 'framework:tabify))
> (hash-remove! table 'big-bang)
> (preferences:set 'framework:tabify
>  (list table rx1 rx2 rx3 rx4))
> (send t tabify-all)
> (send t get-text)))
> (define lambda-tabbed
>   (let ()
> (match-define (list table rx1 rx2 rx3 rx4)
>   (preferences:get 'framework:tabify))
> (hash-set! table 'big-bang 'lambda)
> (preferences:set 'framework:tabify
>  (list table rx1 rx2 rx3 rx4))
> (send t tabify-all)
> (send t get-text)))
> (values untabbed tabbed lambda-tabbed)))
> 
> On 3/20/20 3:12 PM, ‘John Clements’ via Racket Users wrote:
> 
> 
> 
>> That actually solves a bunch of problems for me… but strangely, not the 
>> initial one. If, for instance, I tabify
>> 
>> (+  3
>>   4)
>> 
>> using the tabify-all method, the line with the four gets an indentation of 
>> 2, not 8. This is definitely different from DrRacket’s behavior.
>> 
>> Any idea how to fix this?
>> 
>> (cc:ing racket-users without permission, hope that’s okay?)
>> 
>> John
>> 
>> 
>> 
>>> On Mar 20, 2020, at 12:05 PM, John Clements 
>>>  wrote:
>>> 
>>> Ah! That’s probably a better solution, I’ll just do that. I searched for 
>>> “indent”, should I try to add that as a search term for the “tabify-all” 
>>> method?
>>> 
>>> Many thanks!
>>> 
>>> (Also, sending mail to the list using “
>>> us...@racket-lang.org
>>> ” seems to be broken right now for me, sigh.)
>>> 
>>> John
>>> 
>>> 
 On Mar 20, 2020, at 12:01 PM, Robby Findler 
  wrote:
 
 I'm not sure of the details but did you consider just inserting it
 into a racket:text% and then calling the tabify-all method?
 
 Robby
 
 On Fri, Mar 20, 2020 at 1:26 PM John Clements 
 
  wrote:
 
> Hmm… followup problem. If I try to indent the string “#|\nabc\n|#” (that 
> is, a block comment), the compute-racket-amount-to-indent method returns 
> #f. Is this a bug, or just undocumented behavior?
> 
> John
> 
> 
>> On Mar 20, 2020, at 10:41 AM, John Clements 
>>  wrote:
>> 
>> I’m writing code to help me grade exams, and one of the issues I’ve run 
>> into is that the code coming out of the LMS is all totally unindented. 
>> So, for instance, a student’s 

Re: [racket-users] auto-indentation... off by one^H^H^Hsome?

2020-03-20 Thread 'John Clements' via Racket Users
Ah! this solves the issue. many thanks.

> On Mar 20, 2020, at 1:03 PM, Robby Findler  wrote:
> 
> Looks like you need a display or the text gets confused about how big
> (in pixels) characters really are:
> 
> #lang racket/gui
> (require framework)
> (define f (new frame% [label ""]))
> (define t (new racket:text%))
> (define ec (new editor-canvas% [parent f] [editor t]))
> (send t insert "(+3\n4)")
> (send t freeze-colorer)
> (send t tabify-all)
> (display (send t get-text))
> 
> On Fri, Mar 20, 2020 at 2:12 PM John Clements  
> wrote:
>> 
>> That actually solves a bunch of problems for me… but strangely, not the 
>> initial one. If, for instance, I tabify
>> 
>> (+  3
>>  4)
>> 
>> using the tabify-all method, the line with the four gets an indentation of 
>> 2, not 8. This is definitely different from DrRacket’s behavior.
>> 
>> Any idea how to fix this?
>> 
>> (cc:ing racket-users without permission, hope that’s okay?)
>> 
>> John
>> 
>> 
>>> On Mar 20, 2020, at 12:05 PM, John Clements  
>>> wrote:
>>> 
>>> Ah! That’s probably a better solution, I’ll just do that. I searched for 
>>> “indent”, should I try to add that as a search term for the “tabify-all” 
>>> method?
>>> 
>>> Many thanks!
>>> 
>>> (Also, sending mail to the list using “us...@racket-lang.org” seems to be 
>>> broken right now for me, sigh.)
>>> 
>>> John
>>> 
 On Mar 20, 2020, at 12:01 PM, Robby Findler  
 wrote:
 
 I'm not sure of the details but did you consider just inserting it
 into a racket:text% and then calling the tabify-all method?
 
 Robby
 
 On Fri, Mar 20, 2020 at 1:26 PM John Clements  
 wrote:
> 
> Hmm… followup problem. If I try to indent the string “#|\nabc\n|#” (that 
> is, a block comment), the compute-racket-amount-to-indent method returns 
> #f. Is this a bug, or just undocumented behavior?
> 
> John
> 
>> On Mar 20, 2020, at 10:41 AM, John Clements  
>> wrote:
>> 
>> I’m writing code to help me grade exams, and one of the issues I’ve run 
>> into is that the code coming out of the LMS is all totally unindented. 
>> So, for instance, a student’s response might read:
>> 
>> ;Closures help a function value remember what substitutions have already 
>> been applied to it.
>> '{vars {{x {lam {x} {+ 1 x}}}
>> {y {lam {y} {+ 3 y
>> {+ {x 2} {y 4}}}
>> 
>> Obviously, it would be a lot easier to grade that if it was indented.
>> 
>> Excellent! A chance to whip up a quick-and-dirty auto-indenter, using 
>> DrRacket’s indentation framework. Specifically, the 
>> `compute-racket-amount-to-indent` method of racket:text<%>.
>> 
>> I’m pleased to report almost total success, aside from one strange 
>> off-by-one error that I’m looking for help with.  To see it, here’s the 
>> result of my auto-indenter on the prior block:
>> 
>> '{vars {{x {lam {x} {+ 1 x}}}
>> {y {lam {y} {+ 3 y
>>   {+ {x 2} {y 4}}}
>> 
>> (if you’re viewing this in a proportional-width font, that’s going to 
>> look terrible, sorry.)
>> 
>> The issue is that the final plus line isn’t lined up with the 
>> curly-brace that’s two chars after the end of `vars`… instead, it’s one 
>> character to the left.
>> 
>> A much simpler test case is simply running on “(+ 3\n4)”. Rather than 
>> getting the 4 lined up with the 3, I get it one character to the left.
>> 
>> And… uh oh. A more illuminating test case occurs when I run my code on 
>> “(+  3\n4)”. That is, I put a bunch more spaces before the 3. After 
>> indentation, the 4 is still only indented by two characters. So it looks 
>> like the `compute-racket-amount-to-indent` method is not looking for the 
>> token following the first one following the paren in the prior line 
>> (which would account for all the spaces), but just adding one to the 
>> last position of the first token following the paren. This makes me 
>> wonder whether DrRacket actually uses this method to indent.
>> 
>> Hmm.
>> 
>> Well, here’s the code to reproduce this. Note that there’s an obvious 
>> bug/assumption in that my code doesn’t account for any existing leading 
>> spaces on a line, but none of my examples have leading spaces following 
>> newlines.
>> 
>> Advice appreciated!
>> 
>> John
>> 
>> 
>> 
>> 
>> #lang racket
>> 
>> (require framework)
>> 
>> ;; given a string of racket text, return the corresponding indented 
>> string
>> ;; NB: CURRENTLY ASSUMES ALL LINES START WITH ZERO SPACES.
>> (define (string-indent text-to-indent)
>> (define t (new racket:text%))
>> (send t erase)
>> (send t insert text-to-indent 0)
>> (define num-paragraphs (add1 (send t last-paragraph)))
>> (define indents
>> (for/list ([n (in-range num-paragraphs)])
>>