Re: auto-indentation... off by one^H^H^Hsome?

23 views
Skip to first unread message

John Clements

unread,
Mar 20, 2020, 3:12:54 PM3/20/20
to Robby Findler, Racket Users
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 <clem...@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 <clem...@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 <clem...@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 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)"))
>>>>
>>>
>>>
>>>
>



Benjamin Lerner

unread,
Mar 20, 2020, 3:21:34 PM3/20/20
to John Clements, Robby Findler, Racket Users

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

Robby Findler

unread,
Mar 20, 2020, 4:03:47 PM3/20/20
to John Clements, Racket Users
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))

John Clements

unread,
Mar 20, 2020, 4:17:08 PM3/20/20
to Robby Findler, Racket Users
Ah! this solves the issue. many thanks.
> --
> 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 racket-users...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/CAL3TdONXaUZhCdF9aWentnGf028r%2ByODfS4Lx6mbf9pOdEY9jg%40mail.gmail.com.



John Clements

unread,
Mar 20, 2020, 4:21:46 PM3/20/20
to Benjamin Lerner, Robby Findler, 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

John Clements

unread,
Mar 20, 2020, 4:26:15 PM3/20/20
to Robby Findler, 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 <clem...@brinckerhoff.org> wrote:
>
> Ah! this solves the issue. many thanks.
>

Robby Findler

unread,
Mar 20, 2020, 4:37:09 PM3/20/20
to John Clements, Racket Users
Looks right to me!

John Clements

unread,
Mar 20, 2020, 5:32:46 PM3/20/20
to Robby Findler, Racket Users
Made a pull request, many thanks!

John
> To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/CAL3TdOO4oHxOsZKDSrH3Tk%3Dd92aKoZVAbRzH_wZKb55tfQUpKA%40mail.gmail.com.



Robby Findler

unread,
Mar 21, 2020, 9:34:54 PM3/21/20
to John Clements, Racket Users
I've pushed something so that racket:text% will indent better when it
doesn't have a display.

Robby

John Clements

unread,
Mar 21, 2020, 10:12:04 PM3/21/20
to Robby Findler, Racket Users
Many thanks! Should I cancel my pull request?

John

Robby Findler

unread,
Mar 21, 2020, 10:15:44 PM3/21/20
to John Clements, Racket Users
I had already merged it but I agree that it could probably stand to
have a little elaboration now. I'll do it.

Robby
Reply all
Reply to author
Forward
0 new messages