Skip to content

Commit

Permalink
generalize make-visible, locate-file, and handler:edit-file
Browse files Browse the repository at this point in the history
and then use that generalization inside srcloc-snip

related to racket/htdp#231
  • Loading branch information
rfindler committed Oct 29, 2024
1 parent 7b077bc commit fba98e3
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 47 deletions.
16 changes: 13 additions & 3 deletions gui-doc/scribblings/framework/frame.scrbl
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,21 @@
If @racket[temp] is a box, it is filled with @racket[#t] or @racket[#f],
depending if the filename is a temporary filename.
}
@defmethod*[(((make-visible (filename string?)) void?))]{
@defmethod[(make-visible [filename (or/c path-string? symbol?)]
[#:start-pos start-pos #f (or/c #f exact-nonnegative-integer?)]
[#:end-pos end-pos start-pos (or/c #f exact-nonnegative-integer?)])
void?]{
Makes the file named by @racket[filename] visible (intended for
use with tabbed editing).
use with tabbed editing), using @method[text:basic<%> port-name-matches?]
to find the editor if @racket[filename] is a @racket[symbol?].

}
If both @racket[start-pos] and @racket[end-pos]
are numbers, sets the insertion point to the range from
@racket[start-pos] and @racket[end-pos].

@history[#:changed "1.75" @list{generalized the @racket[filename] argument to allow
symbols and added the @racket[start-pos] and @racket[end-pos] arguments.}]
}
}
@defmixin[frame:basic-mixin (frame%) (frame:basic<%>)]{
This mixin provides the basic functionality that the framework expects. It
Expand Down
9 changes: 8 additions & 1 deletion gui-doc/scribblings/framework/group.scrbl
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,15 @@
Calls the @method[top-level-window<%> can-close?] method of each frame in
the group.
}
@defmethod*[(((locate-file [name path?]) (or/c false/c (is-a?/c frame:basic<%>))))]{
@defmethod*[(((locate-file [name (or/c path? symbol?)]) (or/c false/c (is-a?/c frame:basic<%>))))]{
Returns the frame that is editing or viewing the file @racket[name].

If @racket[name] is a @racket[symbol?], uses the
@method[text:basic<%> port-name-matches?] method to
find a window that's editing this file.

@history[#:changed "1.75" @list{generalized the @racket[filename] argument to allow
symbols and added the @racket[start-pos] and @racket[end-pos] arguments.}]
}
}

Expand Down
26 changes: 18 additions & 8 deletions gui-lib/framework/main.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -1004,31 +1004,41 @@ was never saved, then the first element of the list is @racket[#f].

(proc-doc/names
handler:edit-file
(->* ((or/c path? false/c))
((-> (is-a?/c frame:editor<%>)))
(->* ((or/c path? symbol? #f))
((-> (is-a?/c frame:editor<%>))
#:start-pos (or/c exact-nonnegative-integer? #f)
#:end-pos (or/c exact-nonnegative-integer? #f))
(or/c false/c (is-a?/c frame:editor<%>)))
((filename)
((make-default
(λ () ((handler:current-create-new-window) filename)))))
(λ () ((handler:current-create-new-window) (and (path? filename) filename))))
(start-pos #f)
(end-pos start-pos)))
@{This function invokes the appropriate format handler to open the file (see
@racket[handler:insert-format-handler]).

@itemize[
@item{If @racket[filename] is a string, this function checks the result
of @racket[group:get-the-frame-group] to see if the
@item{If @racket[filename] is a string or a symbol, this function checks the result
of @racket[group:get-the-frame-group]'s @method[group:% locate-file] method to see if the
@racket[filename] is already open by a frame in the group.
@itemize[
@item{If so, it returns the frame.}
@item{If not, this function calls
@item{If not and if @racket[filename] is a string, this function calls
@racket[handler:find-format-handler] with
@racket[filename].
@itemize[
@item{If a handler is found, it is applied to
@racket[filename] and its result is the final
result.}
@item{If not, @racket[make-default] is used.}]}]}
@item{If not, @racket[make-default] is used.}]}
@item{If the file is not already open by a frame in the group
and if @racket[filename] is a symbol,
@racket[make-default] is used.}]}
@item{If @racket[filename] is @racket[#f], @racket[make-default] is
used.}]})
used.}]

@history[#:changed "1.75" @list{generalized the @racket[filename] argument to allow
symbols and added the @racket[start-pos] and @racket[end-pos] arguments.}]})

(parameter-doc
handler:current-create-new-window
Expand Down
18 changes: 14 additions & 4 deletions gui-lib/framework/private/frame.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@
(exit)
(exit:set-exiting #f))))

(define/public (make-visible filename) (void))
(define/public (make-visible filename #:start-pos [start-pos #f] #:end-pos [end-pos start-pos]) (void))
(define/public get-filename
(case-lambda
[() (get-filename #f)]
Expand Down Expand Up @@ -1449,9 +1449,19 @@
(with-handlers ((exn:fail? (λ (x) #f)))
(equal? (normal-case-path (normalize-path x))
(normal-case-path (normalize-path y)))))])
(let ([this-fn (get-filename)])
(and this-fn
(path-equal? filename (get-filename))))))
(define ed (get-editor))
(cond
[(let ([fn (get-filename)])
(and fn (path-equal? filename (get-filename))))
;; it would be nice to remove this case, as
;; port-name-matches? is doing something similar.
;; unfortunately, it isn't quite the same; here
;; normalize-path is used and there it isn't.
#t]
[(is-a? ed text:basic<%>)
(send ed port-name-matches? filename)]
[else
#f])))

(define/override (get-all-open-files)
(with-handlers ((exn:fail? (λ (x) '())))
Expand Down
13 changes: 9 additions & 4 deletions gui-lib/framework/private/handler.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@
frame))))

(define (edit-file filename [make-default
(λ () ((current-create-new-window) filename))])
(λ () ((current-create-new-window)
(and (path? filename) filename)))]
#:start-pos [start-pos #f]
#:end-pos [end-pos #f])
(with-handlers ([(λ (x) #f) ;exn:fail?
(λ (exn)
(message-box
Expand All @@ -97,14 +100,16 @@
filename)])
(cond
[already-open
(send already-open make-visible filename)
(send already-open make-visible filename
#:start-pos start-pos #:end-pos end-pos)
(send already-open show #t)
already-open]
[else
(let ([handler (and (path? filename)
(find-format-handler filename))])
(add-to-recent filename)
(if handler (handler filename) (make-default)))]))
(when (path? filename)
(add-to-recent filename))
(if (and (path? filename) handler) (handler filename) (make-default)))]))
(make-default))))))

;; type recent-list-item = (list/p string? number? number?)
Expand Down
35 changes: 9 additions & 26 deletions gui-lib/framework/private/srcloc-snip.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,18 @@
(import [prefix frame: framework:frame^]
[prefix group: framework:group^]
[prefix text: framework:text^]
[prefix editor: framework:editor^])
[prefix editor: framework:editor^]
[prefix handler: framework:handler^])
(export (rename framework:srcloc-snip^ [-snip% snip%]))

(define (select-srcloc srcloc)
(let frame-loop ([frames (send (group:get-the-frame-group) get-frames)])
(unless (null? frames)
(let ([frame (car frames)])
(cond
[(and (is-a? frame frame:editor<%>)
(send frame find-editor
(lambda (editor)
(send editor port-name-matches? (srcloc-source srcloc)))))
=> (lambda (editor)
(show-editor frame editor)
(send (send frame get-interactions-text)
highlight-error
editor (srcloc-position srcloc) (+ (srcloc-position srcloc) (srcloc-span srcloc))))]
[else
(frame-loop (cdr frames))])))))

(define (show-editor frame editor)
(let* ([current-tab (send editor get-tab)]
[frame (send current-tab get-frame)])
(let loop ([tabs (send frame get-tabs)] [i 0])
(unless (null? tabs)
(if (eq? (car tabs) current-tab)
(send frame change-to-nth-tab i)
(loop (cdr tabs) (+ i 1)))))
(send frame show #t)))
(define pos (srcloc-position srcloc))
(define span (srcloc-span srcloc))
(handler:edit-file (srcloc-source srcloc)
#:start-pos (and pos (- pos 1))
#:end-pos (cond
[(and pos span) (+ pos span -1)]
[else (and pos (- pos 1))])))

; honest attempt
(define (source->datum source)
Expand Down
2 changes: 1 addition & 1 deletion gui-lib/info.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

(define pkg-authors '(mflatt robby))

(define version "1.74")
(define version "1.75")

(define license
'(Apache-2.0 OR MIT))

0 comments on commit fba98e3

Please sign in to comment.