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 <[email protected]> wrote:
>
> Made a pull request, many thanks!
>
> John
>
> > On Mar 20, 2020, at 1:36 PM, Robby Findler <[email protected]> 
> > wrote:
> >
> > Looks right to me!
> >
> > On Fri, Mar 20, 2020 at 3:26 PM John Clements <[email protected]> 
> > 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 <[email protected]> 
> > > wrote:
> > >
> > > Ah! this solves the issue. many thanks.
> > >
> > >> On Mar 20, 2020, at 1:03 PM, Robby Findler <[email protected]> 
> > >> 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 
> > >> <[email protected]> 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 
> > >>>> <[email protected]> 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 “[email protected]” seems to 
> > >>>> be broken right now for me, sigh.)
> > >>>>
> > >>>> John
> > >>>>
> > >>>>> On Mar 20, 2020, at 12:01 PM, Robby Findler 
> > >>>>> <[email protected]> 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 
> > >>>>> <[email protected]> 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 
> > >>>>>>> <[email protected]> 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)])
> > >>>>>>>  (define par-start (send t paragraph-start-position n))
> > >>>>>>>  (list par-start
> > >>>>>>>        (send t compute-racket-amount-to-indent
> > >>>>>>>              par-start
> > >>>>>>>              head-sexp-type))))
> > >>>>>>> ;; NB: OBVIOUSLY WRONG, assumes all lines start with zero leading 
> > >>>>>>> spaces:
> > >>>>>>> ;; act in reverse order, to avoid messing up the meaning of char 
> > >>>>>>> posns:
> > >>>>>>> (for ([indent (in-list (reverse indents))])
> > >>>>>>> (send t set-position (first indent))
> > >>>>>>> (define space-string
> > >>>>>>>  (list->string (for/list ([i (in-range (second indent))]) #\space)))
> > >>>>>>> (send t insert space-string))
> > >>>>>>> (send t get-text))
> > >>>>>>>
> > >>>>>>> ;; for now, always return 'other
> > >>>>>>> (define (head-sexp-type str)
> > >>>>>>> 'other)
> > >>>>>>>
> > >>>>>>>
> > >>>>>>> (define text-to-indent
> > >>>>>>> #<<|
> > >>>>>>> ;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}}}
> > >>>>>>> |
> > >>>>>>> )
> > >>>>>>>
> > >>>>>>> (displayln (string-indent text-to-indent))
> > >>>>>>>
> > >>>>>>>
> > >>>>>>> (displayln (string-indent "
> > >>>>>>> (+ 3
> > >>>>>>> 4)"))
> > >>>>>>>
> > >>>>>>>
> > >>>>>>> (module+ test
> > >>>>>>> (require rackunit)
> > >>>>>>>
> > >>>>>>>
> > >>>>>>> (check-equal? (string-indent "
> > >>>>>>> (+ 3
> > >>>>>>> 4)")
> > >>>>>>>            "
> > >>>>>>> (+ 3
> > >>>>>>> 4)")
> > >>>>>>>
> > >>>>>>> (check-equal? (string-indent "
> > >>>>>>> (+  3
> > >>>>>>> 4)")
> > >>>>>>>            "
> > >>>>>>> (+  3
> > >>>>>>> 4)")
> > >>>>>>>
> > >>>>>>> (check-equal? (string-indent "
> > >>>>>>> (+        3
> > >>>>>>> 4)")
> > >>>>>>>            "
> > >>>>>>> (+        3
> > >>>>>>>      4)"))
> > >>>>>>>
> > >>>>>>
> > >>>>>>
> > >>>>>>
> > >>>>
> > >>>
> > >>>
> > >>>
> > >>
> > >> --
> > >> You received this message because you are subscribed to the Google 
> > >> Groups "Racket Users" group.
> > >> To unsubscribe from this group and stop receiving emails from it, send 
> > >> an email to [email protected].
> > >> To view this discussion on the web visit 
> > >> https://groups.google.com/d/msgid/racket-users/CAL3TdONXaUZhCdF9aWentnGf028r%2ByODfS4Lx6mbf9pOdEY9jg%40mail.gmail.com.
> > >
> >
> >
> >
> >
> > --
> > You received this message because you are subscribed to the Google Groups 
> > "Racket Users" group.
> > To unsubscribe from this group and stop receiving emails from it, send an 
> > email to [email protected].
> > To view this discussion on the web visit 
> > https://groups.google.com/d/msgid/racket-users/CAL3TdOO4oHxOsZKDSrH3Tk%3Dd92aKoZVAbRzH_wZKb55tfQUpKA%40mail.gmail.com.
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CAL3TdOMXtQLYok0nFn5yU5o%3D-dLggTq5wmo3k80Wi-gRqqcx-w%40mail.gmail.com.

Reply via email to