Skip to content

Commit d961e10

Browse files
committed
Handling whitespace better after !...
This fixes the indentation of the follow line and a problem where, for example, !outputLua("a=b") foo() would result in a=bfoo()
1 parent 77254d8 commit d961e10

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

misc/testHandler.lua

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
1+
--[[============================================================
2+
--=
3+
--= Test message handler
4+
--= for use with preprocess-cl.lua
5+
--=
6+
--============================================================]]
7+
8+
--
9+
-- This module shares environment with the processed files,
10+
-- making it a good place to put things all files use/share.
11+
--
12+
13+
_G.IS_DEVELOPER = true
14+
15+
math.tau = 2*math.pi
16+
17+
function _G.flipTable(t)
18+
local flipped = {}
19+
for k, v in pairs(t) do
20+
flipped[v] = k
21+
end
22+
return flipped
23+
end
24+
25+
--
26+
-- The module is expected to return one or multiple message handlers.
27+
--
28+
129
-- [[ Alternative #1: Multiple specific message handlers.
230
return {
331
beforemeta = function(path)

preprocess-cl.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ exec lua "$0" "$@"
2828
table of functions. If it returns a function then it will be
2929
called with various messages as it's first argument. If it's
3030
a table, the keys should be the message names and the values
31-
should be functions to handle the respective message. (See
32-
'Handler messages'.) This file shares the same environment
33-
as the processed files.
31+
should be functions to handle the respective message.
32+
(See 'Handler messages' and misc/testHandler.lua)
33+
The file shares the same environment as the processed files.
3434
3535
--linenumbers
3636
Add comments with line numbers to the output.

preprocess.lua

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,10 +1526,9 @@ local function _processFileOrString(params, isFile)
15261526

15271527
while true do
15281528
local tok = tokens[tokenIndex]
1529-
if not tok then break end
1529+
if not tok then return end
15301530

15311531
local tokType = tok.type
1532-
15331532
if
15341533
bracketBalance == 0 and (
15351534
(tokType == "whitespace" and tok.value:find("\n", 1, true)) or
@@ -1546,7 +1545,18 @@ local function _processFileOrString(params, isFile)
15461545
outputFinalDualValueStatement(metaLineIndexStart, tokenIndex-1)
15471546
end
15481547

1549-
break
1548+
-- Fix whitespace after the line.
1549+
local tokNext = tokens[tokenIndex]
1550+
if not isDual and tokType == "whitespace" and not (tokNext and isToken(tokNext, "pp_entry")) then
1551+
local tokExtra = copyTable(tok)
1552+
tokExtra.value = tok.value:gsub("^[^\n]+", "")
1553+
tokExtra.representation = tokExtra.value
1554+
tokExtra.position = tokExtra.position+#tok.value-#tokExtra.value
1555+
1556+
table.insert(tokensToProcess, tokExtra)
1557+
end
1558+
1559+
return
15501560

15511561
elseif tokType == "pp_entry" then
15521562
errorInFile(

0 commit comments

Comments
 (0)