greghendershott / racket-mode

Emacs major and minor modes for Racket: edit, REPL, check-syntax, debug, profile, and more.

Home Page:https://www.racket-mode.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

run main module

MarcRohrer opened this issue · comments

Hi,

racket-mode looks awesome! Thanx a lot!
But from the documentation for the life of me cannot figure out how
I can do a racket-run for the main module.

Currently I have a server.rkt which ist to be extended in the future, but
currently I work on the hl7 module in a dirrerent file, that is called from server.rkt.
To run the project I have to always switch to server.rkt.

How can I run the main module from the file I am currebtly working on!

Keep up the great work!

Sincerely

Marc

I'd call shell-command/compile to run racket server.rkt.

but does one have all of the nice relp functionality then?

There are a couple of ways to do this:

  1. make an elisp function that switches to the server.rkt buffer and calls racket-racket.
  2. define a keyboard macro to do so and save the macro somewhere.
  3. make an elisp function that somehow calls racket--shell-or-terminal with server.rkt.

Hi,

I was thinking about 1. and 2. but hoped for 3. :-)
So can I just call (racket--shell-or-terminal main-program-file)?
That would be perfect! Maybe I can implement that tomorrow.

Thanx a lot!

Marc

I think the general, "safe" answer is that you need to run server.rkt. So: Switch to its buffer, and run. In the general case, that's what I'd do. Or do racket server.rkt as @capfredf suggested.


Having said that, there can be other workflows. If server.rkt is just calling a function defined in hl7.rkt with a certain value? You can run hl7.rkt, and do the function call from there.

Let's say you have something like:

;; server.rkt
#lang racket
(require "hl7.rkt")
(connect-and-fetch "1.2.3.4")
;; hl7.rkt
#lang racket
(provide connect-and-fetch)
(define (connect-and-fetch ip-address)
  ___)

Here are several things you can do, solely from hl7.rkt. (These are real things I do sometimes in my own workflow.)

REPL

If I'm working in hl7.rkt, and developing connect-and-fetch, and wanting to try it out and see if it's working? I would probably just run hl7.rkt, and in the REPL, enter something like what server.rkt does: (connect-and-fetch "1.2.3.4").

"Example" submodule

If I get tired of entering (connect-and-fetch "1.2.3.4") repeatedly, I might tuck that in a little "example" submodule in hl7.rkt:

;; hl7.rkt
#lang racket
(provide connect-and-fetch)
(define (connect-and-fetch ip-address)
  ___)

(module+ example
  (connect-and-fetch "1.2.3.4"))

Now I can put cursor in there and C-c C-c to run. Saves time.

test submodule

Taking this a step further, maybe (connect-and-fetch "1.2.3.4") is supposed to return "foobar" if it's working correctly. OK. Instead of an "example" submodule, why not make that a test submodule and with a unit test:

;; hl7.rkt
#lang racket
(provide connect-and-fetch)
(define (connect-and-fetch ip-address)
  ___)

(module+ test
  (require rackunit)
  (check-equal? (connect-and-fetch "1.2.3.4")
                "foobar"))

I can still do the thing where I put the cursor in there and C-c C-c. But submodules named test are special. So I could just C-c C-t from anywhere in the file. Also at the command line raco test hl7.rkt will run this test submodule.

Oops. The page hadn't refreshed when I posted that, so I didn't see the more recent ones. It sounds like you already know what you want to do.

Hi Greg,

works perfectly, thank you!

I now have

(defun run-racket-project ()
(racket--shell-or-terminal racket-main))

whereas racket-main is set from .dir-locals.el in the projct directory

((nil . ((racket-main . ".rkt"))))

very quick and easy :-)

Thanx!

Marc

Hi Greg,

I am not sure if it does not work anymore as I thought it was or never
did in the first place, which I did not recognize.

But racket--shell-or-terminal runs the code as expected, but the repl
does not continue to run, so that I cannot check variables or in the repl
kind of way run stuff.

How could this be achieved?

Best wishes!

Marc

racket--shell-or-terminal is a private helper function used by some Racket Mode commands to run command-line racket in a shell or terminal. It's as if you opened a shell yourself, and entered racket sever.rkt. Racket Mode and its REPL aren't involved in how the program runs, at all.


I'm not sure if you read my comment above? Maybe not, because it seemed like you had a simple solution, and my comment probably seemed long-winded. 😄

If server.rkt calls some function foo defined in foo.rkt? Then I suggest you racket-run foo.rkt, putting the REPL inside that file where you can see all its definitions. At the REPL prompt, enter (foo) or (foo 1 2 3) or however server.rkt calls foo. Then do what you want.

If you get tired of entering (foo) or (foo 1 2 3) at the prompt all the time? Then see my comment above for a couple ideas, like putting that in an example or test submodule as a shortcut.

Does this help?

Hi Greg,

to be honest, a bit confusing :-)

I now changed it t:

(defun run-racket-project ()
"run the project of current file. The main program file ist specified in as racket-main in .dir-locals.el"
(interactive)
(let ((buffer (or (get-buffer racket-main)
(find-file-noselect racket-main))))
(with-current-buffer buffer
(racket-run))))

which seems to do the trick. The named modules are just a start
and will hopefully grow quite quickly in size. As it will be a server project,
the main module should always be run, I guess. Maybe not? I will start with
this and change it, if demands grow or whatever...

I am still using the console.log() "paradigma" from javascript mostly, but
am very curious, how this can be imporoved.

Predominantly I should focus on progressing the application though.
I am focussing on tooling way too much anyway, I guess.

Thank you for your help, it is highly appreciated!

Best wishes!

Marc