Skip to content

Commit 1cd3b7b

Browse files
committed
fix: leanOptions are not used
1 parent 48e3d83 commit 1cd3b7b

File tree

8 files changed

+72
-19
lines changed

8 files changed

+72
-19
lines changed

Diff for: cypress/e2e/spec.cy.ts

+11
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,15 @@ describe('Editor Test', () => {
7171
cy.contains('#check 0').click()
7272
cy.iframe().contains('0 : Nat')
7373
})
74+
75+
it('check leanOptions', () => {
76+
cy.on('uncaught:exception', (err, runnable) => {
77+
// Note: see note about console errors above
78+
return false
79+
})
80+
cy.visit('http://localhost:5173/')
81+
cy.contains('#print f')
82+
cy.contains('#print f').click()
83+
cy.iframe().contains('def f : Nat → Nat := fun x ↦ x + 1')
84+
})
7485
})

Diff for: demo/server/LeanProject/LeanProject.lean

-1
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
def hello := "world"

Diff for: demo/server/LeanProject/LeanProject/Test0.lean

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- demonstrates that the 1st and 2nd editor use the `leanOptions` as they specify
2+
-- and existing file, while all further do not as they reference a fictional file.

Diff for: demo/server/LeanProject/LeanProject/Test1.lean

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- demonstrates that the 1st and 2nd editor use the `leanOptions` as they specify
2+
-- and existing file, while all further do not as they reference a fictional file.

Diff for: demo/server/LeanProject/Main.lean

-4
This file was deleted.

Diff for: demo/server/LeanProject/lakefile.lean

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
import Lake
22
open Lake DSL
33

4-
package leanProject {
4+
package leanProject where
55
-- add package configuration options here
6-
}
7-
8-
lean_lib LeanProject {
9-
-- add library configuration options here
10-
}
6+
leanOptions := #[
7+
`pp.unicode.fun, true⟩ ]
118

129
@[default_target]
13-
lean_exe leanProject {
14-
root := `Main
15-
}
10+
lean_lib LeanProject where
11+
-- add library configuration options here
12+
leanOptions := #[
13+
`pp.unicode.fun, true⟩ ]

Diff for: demo/server/index.mjs

+43-4
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ const server = app.listen(PORT, () => console.log(`Listening on ${PORT}`));
1818

1919
const wss = new WebSocketServer({ server })
2020

21+
const leanProjectPath = path.join(__dirname, 'LeanProject')
22+
2123
function startServerProcess() {
2224

23-
const serverProcess = cp.spawn("lake", ["serve", "--"], { cwd: path.join(__dirname, 'LeanProject') })
25+
const serverProcess = cp.spawn("lean", ["--server"], { cwd: leanProjectPath })
2426

2527
serverProcess.on('error', error =>
2628
console.error(`Launching Lean Server failed: ${error}`)
@@ -35,6 +37,40 @@ function startServerProcess() {
3537
return serverProcess
3638
}
3739

40+
/** Transform client URI to valid file on the server */
41+
function urisToFilenames(prefix, obj) {
42+
for (let key in obj) {
43+
if (obj.hasOwnProperty(key)) {
44+
if (key === 'uri') {
45+
obj[key] = obj[key].replace('file://', `file://${prefix}`)
46+
} else if (key === 'rootUri') {
47+
obj[key] = obj[key].replace('file://', `file://${prefix}`)
48+
} else if (key === 'rootPath') {
49+
obj[key] = path.join(prefix, obj[key])
50+
}
51+
if (typeof obj[key] === 'object' && obj[key] !== null) {
52+
urisToFilenames(prefix, obj[key]);
53+
}
54+
}
55+
}
56+
return obj;
57+
}
58+
59+
/** Transform server file back into client URI */
60+
function FilenamesToUri(prefix, obj) {
61+
for (let key in obj) {
62+
if (obj.hasOwnProperty(key)) {
63+
if (key === 'uri') {
64+
obj[key] = obj[key].replace(prefix, '')
65+
}
66+
if (typeof obj[key] === 'object' && obj[key] !== null) {
67+
FilenamesToUri(prefix, obj[key]);
68+
}
69+
}
70+
}
71+
return obj;
72+
}
73+
3874
wss.addListener("connection", function(ws, req) {
3975
const ps = startServerProcess()
4076
console.log(`[${new Date()}] Socket opened`)
@@ -50,8 +86,9 @@ wss.addListener("connection", function(ws, req) {
5086
const socketConnection = jsonrpcserver.createConnection(reader, writer, () => ws.close())
5187
const serverConnection = jsonrpcserver.createProcessStreamConnection(ps)
5288
socketConnection.forward(serverConnection, message => {
53-
// console.log(`CLIENT: ${JSON.stringify(message)}`)
89+
urisToFilenames(leanProjectPath, message)
5490

91+
console.log(`CLIENT: ${JSON.stringify(message)}`)
5592
// Here would be the first place we could determine the server OS
5693
// and modify the data sent.
5794
// TODO: Maybe it could be an option to search `message` recursively for `uri` fields
@@ -60,7 +97,9 @@ wss.addListener("connection", function(ws, req) {
6097
return message;
6198
})
6299
serverConnection.forward(socketConnection, message => {
63-
// console.log(`SERVER: ${JSON.stringify(message)}`)
100+
FilenamesToUri(leanProjectPath, message)
101+
102+
console.log(`SERVER: ${JSON.stringify(message)}`)
64103
return message;
65104
});
66105

@@ -75,7 +114,7 @@ wss.addListener("connection", function(ws, req) {
75114
"code": "-1"
76115
}
77116
}
78-
// console.log(`SERVER: ${JSON.stringify(msg)}`)
117+
console.log(`SERVER: ${JSON.stringify(msg)}`)
79118
ws.send(JSON.stringify(msg))
80119
})
81120

Diff for: demo/src/LeanMonaco.tsx

+7-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ function LeanMonacoComponent({options, numberEditors} : {options: LeanMonacoOpti
2929
<>
3030
<LeanMonacoContext.Provider value={leanMonaco}>
3131
{[...Array(numberEditors)].map((_x, i) =>
32-
<LeanMonacoEditorComponent key={i} fileName={`/project/test${i}.lean`} value={`#check ${i}`}/>
32+
<LeanMonacoEditorComponent
33+
key={i}
34+
// fileName: must be a valid file inside the Lean project
35+
// (or lake does not read the `leanOptions` in the `lakefile`), AND
36+
// must be inside a folder, i.e. 'LeanProject.lean' does not work (monaco bug?) :(
37+
fileName={`LeanProject/Test${i}.lean`}
38+
value={`#check ${i}\ndef f : Nat → Nat := fun x ↦ x + 1\n#print f`}/>
3339
)}
3440
<div className='infoview' ref={infoviewRef}></div>
3541
</LeanMonacoContext.Provider>

0 commit comments

Comments
 (0)