Skip to content

Commit d087d53

Browse files
Improve tidy-requires
Motivated by jackfirth/resyntax#432, as well as inspired by racket-review's used of syntax classes. Define syntax classes closely following the require grammar. Use them to drive the matching and simplification, producing attribute values that are `sortable` structs, from which we reconstruct the syntax. Because the macro-debugger-lib drop analysis works in terms of <module phase> tuples, the root-module-path syntax class is the locus -- it may produce #f instead of a `sortable` to indicate elision. This can percolate up through other specs, since they all build directly or transitively on root-module-path. Simplify gratuitous specs either reducing to the simpler equivalent spec or eliding entirely. e.g. (only-in m) => m or (combine-in) => #f. No longer "explode" multi-in forms when de-normalizing and attempt to recover them when normalizing. Instead: Preserve multi-in forms, modifying only for drops. In simple cases like (multi-in a b (c d)), we can modify the multi-in form itself. In the general case -- any Cartesian product -- we must wrap in a subtract-in spec. Improve some doc strings in substance, and prefer active to passive voice.
1 parent f4fe6dd commit d087d53

File tree

4 files changed

+707
-478
lines changed

4 files changed

+707
-478
lines changed

doc/racket-mode.texi

Lines changed: 57 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,37 +1069,24 @@ Unfold (show) all test submodules.
10691069

10701070
@kbd{M-x} @code{racket-tidy-requires}
10711071

1072-
Make a single ``require'' form, modules sorted, one per line.
1072+
Make a single, sorted ``require'' form.
10731073

1074-
The scope of this command is the innermost module around point,
1075-
including the outermost module for a file using a ``#lang'' line.
1076-
All require forms within that module are combined into a single
1077-
form. Within that form:
1074+
The scope of this command is the innermost module around point --
1075+
whether an explicit submodule form or the outermost module for a
1076+
file that has a ``#lang'' line.
10781077

1079-
@itemize
1080-
@item
1081-
A single subform is used for each phase level, sorted in this
1078+
Merge all require forms within that module to one form.
1079+
1080+
Use a single require-spec for each phase-level, sorted in this
10821081
order: for-syntax, for-template, for-label, for-meta, and
10831082
plain (phase 0).
10841083

1085-
@itemize
1086-
@item
1087-
Within each level subform, the modules are sorted:
1088-
1089-
@itemize
1090-
@item
1091-
Collection path modules -- sorted alphabetically.
1092-
1093-
@item
1094-
Subforms such as only-in.
1084+
Within each phase-level, sort require-specs by module name.
10951085

1096-
@item
1097-
Quoted relative requires -- sorted alphabetically.
1098-
@end itemize
1099-
@end itemize
1100-
@end itemize
1086+
Format at most one module per line.
11011087

1102-
At most one required module is listed per line.
1088+
Simplify gratuitous require-specs. For example reduce (only-in m)
1089+
to m and elide (combine-in).
11031090

11041091
See also: @ref{racket-trim-requires} and @ref{racket-base-requires}.
11051092

@@ -1108,16 +1095,28 @@ See also: @ref{racket-trim-requires} and @ref{racket-base-requires}.
11081095

11091096
@kbd{M-x} @code{racket-trim-requires}
11101097

1111-
Like @ref{racket-tidy-requires} but also deletes unnecessary requires.
1098+
Like @ref{racket-tidy-requires} but also delete unnecessary requires.
1099+
1100+
Use macro-debugger/analysis/check-requires to analyze.
1101+
1102+
The analysis:
1103+
1104+
@itemize
1105+
@item
1106+
Needs the @code{macro-debugger-lib} package.
11121107

1113-
Note: This only works when the source file can be fully expanded
1114-
with no errors.
1108+
@item
1109+
Only works when the source file can be fully expanded with no
1110+
@end itemize
1111+
errors.
11151112

1116-
Note: This only works for requires at the top level of a source
1117-
file using #lang. It does NOT work for require forms inside
1118-
module forms. Furthermore, it is not smart about module+ or
1119-
module* forms -- it might delete top level requires that are
1120-
actually needed by such submodules.
1113+
@itemize
1114+
@item
1115+
Only works for requires at the top level of a source file using
1116+
@end itemize
1117+
#lang -- not for requires inside submodule forms. Furthermore, it
1118+
is not smart about module+ or module* forms -- it might delete
1119+
outer requires that are actually needed by such submodules.
11211120

11221121
See also: @ref{racket-base-requires}.
11231122

@@ -1128,24 +1127,16 @@ See also: @ref{racket-base-requires}.
11281127

11291128
Change from ``#lang racket'' to ``#lang racket/base''.
11301129

1131-
Adds explicit requires for imports that are provided by
1132-
``racket'' but not by ``racket/base''.
1133-
1134-
This is a recommended optimization for Racket applications.
1135-
Avoiding loading all of ``racket'' can reduce load time and
1136-
memory footprint.
1130+
Using ``racket/base'' is a recommended optimization for Racket
1131+
applications. Loading all of ``racket'' is slower and uses more
1132+
memory.
11371133

1138-
Also, as does @ref{racket-trim-requires}, this removes unneeded
1139-
modules and tidies everything into a single, sorted require form.
1134+
Add explicit requires for imports that are provided by ``racket''
1135+
but not by ``racket/base''.
11401136

1141-
Note: This only works when the source file can be fully expanded
1142-
with no errors.
1143-
1144-
Note: This only works for requires at the top level of a source
1145-
file using #lang. It does NOT work for require forms inside
1146-
module forms. Furthermore, it is not smart about module+ or
1147-
module* forms -- it might delete top level requires that are
1148-
actually needed by such submodules.
1137+
Also do the equivalent of @ref{racket-trim-requires} and
1138+
@code{nil}. See those commands for additional notes
1139+
and caveats.
11491140

11501141
Note: Currently this only helps change ``#lang racket'' to
11511142
``#lang racket/base''. It does not help with other similar
@@ -1162,28 +1153,31 @@ Add a require for an identifier.
11621153
Useful when you know the name of an export but don't remember
11631154
from what module it is exported.
11641155

1165-
At the prompt:
1156+
1 At the prompt, you may:
11661157

1158+
@itemize
1159+
@item
11671160
Use @kbd{<XF86Forward>} or @kbd{<next>} or @kbd{M-n} to load the identifier at point.
1168-
You may also need to @kbd{C-e} or @kbd{<end>} to see candidates.
1169-
1170-
Or type anything.
1161+
@end itemize
1162+
You might also need to @kbd{C-e} or @kbd{<end>} to see candidates.
11711163

1172-
After you choose:
1164+
@itemize
1165+
@item
1166+
Or, type anything.
11731167

1174-
The identifier you chose is inserted at point if not already
1175-
there.
1168+
@item
1169+
After you choose, this command will:
11761170

1177-
A ``require'' form is inserted, followed by doing a
1178-
@ref{racket-tidy-requires}.
1171+
@item
1172+
Insert the identifier at point if not already there.
11791173

1180-
When more than one module supplies an identifer with the same
1181-
name, the first is used -- for example ``racket/base'' instead of
1182-
``racket''.
1174+
@item
1175+
Insert a ``require'' form and do @ref{racket-tidy-requires}.
1176+
@end itemize
11831177

1184-
Caveat: This works in terms of identifiers that are documented.
1185-
The mechanism is similar to that used for Racket's ``Search
1186-
Manuals'' feature. Today there exists no system-wide database of
1178+
Caveat: This works only for identifiers that are documented. The
1179+
mechanism is similar to that used for Racket's ``Search Manuals''
1180+
feature. Today there exists no system-wide database of
11871181
identifiers that are exported but not documented.
11881182

11891183
@node racket-indent-line

racket-edit.el

Lines changed: 43 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -60,26 +60,24 @@
6060
;;; requires
6161

6262
(defun racket-tidy-requires ()
63-
"Make a single \"require\" form, modules sorted, one per line.
63+
"Make a single, sorted \"require\" form.
6464
65-
The scope of this command is the innermost module around point,
66-
including the outermost module for a file using a \"#lang\" line.
67-
All require forms within that module are combined into a single
68-
form. Within that form:
65+
The scope of this command is the innermost module around point --
66+
whether an explicit submodule form or the outermost module for a
67+
file that has a \"#lang\" line.
6968
70-
- A single subform is used for each phase level, sorted in this
71-
order: for-syntax, for-template, for-label, for-meta, and
72-
plain (phase 0).
69+
Merge all require forms within that module to one form.
7370
74-
- Within each level subform, the modules are sorted:
71+
Use a single require-spec for each phase-level, sorted in this
72+
order: for-syntax, for-template, for-label, for-meta, and
73+
plain (phase 0).
7574
76-
- Collection path modules -- sorted alphabetically.
75+
Within each phase-level, sort require-specs by module name.
7776
78-
- Subforms such as only-in.
77+
Format at most one module per line.
7978
80-
- Quoted relative requires -- sorted alphabetically.
81-
82-
At most one required module is listed per line.
79+
Simplify gratuitous require-specs. For example reduce (only-in m)
80+
to m and elide (combine-in).
8381
8482
See also: `racket-trim-requires' and `racket-base-requires'."
8583
(interactive)
@@ -110,16 +108,21 @@ See also: `racket-trim-requires' and `racket-base-requires'."
110108
(funcall callback result))))))
111109

112110
(defun racket-trim-requires ()
113-
"Like `racket-tidy-requires' but also deletes unnecessary requires.
111+
"Like `racket-tidy-requires' but also delete unnecessary requires.
112+
113+
Use macro-debugger/analysis/check-requires to analyze.
114+
115+
The analysis:
116+
117+
- Needs the `macro-debugger-lib` package.
114118
115-
Note: This only works when the source file can be fully expanded
116-
with no errors.
119+
- Only works when the source file can be fully expanded with no
120+
errors.
117121
118-
Note: This only works for requires at the top level of a source
119-
file using #lang. It does NOT work for require forms inside
120-
module forms. Furthermore, it is not smart about module+ or
121-
module* forms -- it might delete top level requires that are
122-
actually needed by such submodules.
122+
- Only works for requires at the top level of a source file using
123+
#lang -- not for requires inside submodule forms. Furthermore, it
124+
is not smart about module+ or module* forms -- it might delete
125+
outer requires that are actually needed by such submodules.
123126
124127
See also: `racket-base-requires'."
125128
(interactive)
@@ -143,24 +146,16 @@ See also: `racket-base-requires'."
143146
(defun racket-base-requires ()
144147
"Change from \"#lang racket\" to \"#lang racket/base\".
145148
146-
Adds explicit requires for imports that are provided by
147-
\"racket\" but not by \"racket/base\".
149+
Using \"racket/base\" is a recommended optimization for Racket
150+
applications. Loading all of \"racket\" is slower and uses more
151+
memory.
148152
149-
This is a recommended optimization for Racket applications.
150-
Avoiding loading all of \"racket\" can reduce load time and
151-
memory footprint.
153+
Add explicit requires for imports that are provided by \"racket\"
154+
but not by \"racket/base\".
152155
153-
Also, as does `racket-trim-requires', this removes unneeded
154-
modules and tidies everything into a single, sorted require form.
155-
156-
Note: This only works when the source file can be fully expanded
157-
with no errors.
158-
159-
Note: This only works for requires at the top level of a source
160-
file using #lang. It does NOT work for require forms inside
161-
module forms. Furthermore, it is not smart about module+ or
162-
module* forms -- it might delete top level requires that are
163-
actually needed by such submodules.
156+
Also do the equivalent of `racket-trim-requires' and
157+
`racket-tidy-require'. See those commands for additional notes
158+
and caveats.
164159
165160
Note: Currently this only helps change \"#lang racket\" to
166161
\"#lang racket/base\". It does not help with other similar
@@ -268,29 +263,23 @@ module form, meaning the outermost, file module."
268263
Useful when you know the name of an export but don't remember
269264
from what module it is exported.
270265
271-
At the prompt:
266+
1 At the prompt, you may:
272267
\\<minibuffer-local-map>
273268
274-
Use \\[next-history-element] to load the identifier at point.
275-
You may also need to \\[move-end-of-line] to see candidates.
276-
277-
Or type anything.
269+
- Use \\[next-history-element] to load the identifier at point.
270+
You might also need to \\[move-end-of-line] to see candidates.
278271
279-
After you choose:
272+
- Or, type anything.
280273
281-
The identifier you chose is inserted at point if not already
282-
there.
274+
2. After you choose, this command will:
283275
284-
A \"require\" form is inserted, followed by doing a
285-
`racket-tidy-requires'.
276+
- Insert the identifier at point if not already there.
286277
287-
When more than one module supplies an identifer with the same
288-
name, the first is used -- for example \"racket/base\" instead of
289-
\"racket\".
278+
- Insert a \"require\" form and do `racket-tidy-requires'.
290279
291-
Caveat: This works in terms of identifiers that are documented.
292-
The mechanism is similar to that used for Racket's \"Search
293-
Manuals\" feature. Today there exists no system-wide database of
280+
Caveat: This works only for identifiers that are documented. The
281+
mechanism is similar to that used for Racket's \"Search Manuals\"
282+
feature. Today there exists no system-wide database of
294283
identifiers that are exported but not documented."
295284
(interactive)
296285
(racket--assert-sexp-edit-mode)

0 commit comments

Comments
 (0)