Skip to content

Commit b532603

Browse files
authored
Merge pull request #1207 from JuliaLang/special-modes
Automatically strip comment lines from special mode commands
2 parents a424f9b + 6d36d1e commit b532603

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

docs/src/_changelog.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ Changelog](https://keepachangelog.com).
3333
minor release instead of each patch release.
3434
- Extended the precompilation workload to cover more calls, and added a
3535
workaround to minimize TTFX when Revise is used ([#1210]).
36+
- Previously running special mode commands like `] st` would not work if the
37+
cell contained comment lines:
38+
```julia
39+
# Check environment
40+
] st
41+
```
42+
43+
Now the comment lines are stripped from special mode commands so the above
44+
will work ([#1207]). Note that this only applies to *whole comment lines*, the
45+
inline comments in code like `] st # check environment` will still not be
46+
stripped.
3647

3748
## [v1.31.1] - 2025-10-20
3849

src/execute_request.jl

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,49 @@ function do_pkg_cmd(cmd::AbstractString)
3131
@invokelatest _do_pkg_cmd(cmd)
3232
end
3333

34+
# Helper function to check if `code` is a valid special mode command. If it is,
35+
# then comment lines will be stripped. If it isn't, then the original `code`
36+
# will be returned.
37+
function special_mode_strip(code::String)
38+
# Exit early if there are no comment or special mode characters at all
39+
if !contains(code, r"#|;|\]|\?")
40+
return code
41+
end
42+
43+
# Loop over the string and look for lines that aren't comments and aren't
44+
# all whitespace.
45+
uncommented_line = ""
46+
has_special_mode = false
47+
for line in eachline(IOBuffer(code))
48+
if isempty(line) || all(isspace, line)
49+
continue
50+
end
51+
52+
first_char = strip(line)[1]
53+
if first_char != '#'
54+
# If we've already found an uncommented line then bail out. We only
55+
# support special modes with one line of commands.
56+
if !isempty(uncommented_line)
57+
return code
58+
end
59+
60+
uncommented_line = line
61+
has_special_mode = first_char (';', ']', '?')
62+
end
63+
end
64+
65+
if isempty(uncommented_line)
66+
# If there are no uncommented lines then return the original code
67+
code
68+
elseif has_special_mode
69+
# If there's one and it is a special mode return just that line
70+
uncommented_line
71+
else
72+
# Otherwise return the original code
73+
code
74+
end
75+
end
76+
3477
"""
3578
execute_request(socket, kernel, msg)
3679
@@ -60,6 +103,8 @@ function execute_request(socket, kernel, msg)
60103
kernel.In[kernel.n] = code
61104
end
62105

106+
code = special_mode_strip(code)
107+
63108
# "; ..." cells are interpreted as shell commands for run
64109
code = replace(code, r"^\s*;.*$" =>
65110
m -> string(replace(m, r"^\s*;" => "Base.repl_cmd(`"),

test/execute_request.jl

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,45 @@ end
9191
@test !haskey(data, ANGRY_MIME_1)
9292
@test !haskey(data, ANGRY_MIME_3)
9393
end
94+
95+
@testset "Special REPL mode detection" begin
96+
code = "foo"
97+
@test IJulia.special_mode_strip(code) == code
98+
code = "foo # bar"
99+
@test IJulia.special_mode_strip(code) == code
100+
code = "# foo"
101+
@test IJulia.special_mode_strip(code) == code
102+
code = """
103+
# foo
104+
bar
105+
"""
106+
@test IJulia.special_mode_strip(code) == code
107+
code = """
108+
foo
109+
] st
110+
"""
111+
@test IJulia.special_mode_strip(code) == code
112+
113+
code = """
114+
# foo
115+
] st
116+
"""
117+
@test IJulia.special_mode_strip(code) == "] st"
118+
code = """
119+
# foo
120+
] st
121+
] st
122+
"""
123+
@test IJulia.special_mode_strip(code) == code
124+
code = "? foo"
125+
@test IJulia.special_mode_strip(code) == code
126+
code = "; foo # bar"
127+
@test IJulia.special_mode_strip(code) == code
128+
code = """
129+
? foo
130+
# foo
131+
"""
132+
@test IJulia.special_mode_strip(code) == " ? foo"
133+
code = " ] st "
134+
@test IJulia.special_mode_strip(code) == code
135+
end

0 commit comments

Comments
 (0)