FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
modelscript/extensions/vscode/src/notebookController.ts at main · modelscript/modelscript · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
modelscript
/
modelscript
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
3
Star
12
Code
Issues
1
Pull requests
5
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
modelscript
/
extensions
/
vscode
/
src
/
notebookController.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
155 lines (134 loc) · 4.96 KB
Breadcrumbs
modelscript
/
extensions
/
vscode
/
src
/
notebookController.ts
Copy path
File metadata and controls
155 lines (134 loc) · 4.96 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import
*
as
vscode
from
"vscode"
;
import
type
{
LanguageClient
}
from
"vscode-languageclient/browser"
;
/** Notebook type must match the one registered in package.json contributes.notebooks */
const
NOTEBOOK_TYPE
=
"modelscript-notebook"
;
/**
* Notebook controller (kernel) for Modelica scripts.
*
* Each notebook gets a unique session ID so that variables defined in one cell
* persist into subsequent cells (shared ModelicaScriptScope on the server).
*/
export
class
ModelicaNotebookController
implements
vscode
.
Disposable
{
static
readonly
controllerId
=
"modelscript-notebook-kernel"
;
static
readonly
label
=
"Modelica"
;
private
readonly
_controller
:
vscode
.
NotebookController
;
private
_executionOrder
=
0
;
private
_client
:
LanguageClient
|
undefined
;
/** Map notebook URI → session ID for shared scope. */
private
_sessions
=
new
Map
<
string
,
string
>
(
)
;
constructor
(
)
{
this
.
_controller
=
vscode
.
notebooks
.
createNotebookController
(
ModelicaNotebookController
.
controllerId
,
NOTEBOOK_TYPE
,
ModelicaNotebookController
.
label
,
)
;
this
.
_controller
.
supportedLanguages
=
[
"modelica"
]
;
this
.
_controller
.
supportsExecutionOrder
=
true
;
this
.
_controller
.
executeHandler
=
this
.
_execute
.
bind
(
this
)
;
}
/** Set (or update) the language client used for execution. */
set
client
(
c
:
LanguageClient
|
undefined
)
{
this
.
_client
=
c
;
}
/** Get or create a session ID for a notebook. */
private
_getSessionId
(
notebook
:
vscode
.
NotebookDocument
)
:
string
{
const
key
=
notebook
.
uri
.
toString
(
)
;
let
id
=
this
.
_sessions
.
get
(
key
)
;
if
(
!
id
)
{
id
=
`nb-
${
Date
.
now
(
)
}
-
${
Math
.
random
(
)
.
toString
(
36
)
.
slice
(
2
,
8
)
}
`
;
this
.
_sessions
.
set
(
key
,
id
)
;
}
return
id
;
}
/** Reset the kernel session for a notebook (clears shared scope). */
async
resetSession
(
notebook
:
vscode
.
NotebookDocument
)
:
Promise
<
void
>
{
const
key
=
notebook
.
uri
.
toString
(
)
;
const
sessionId
=
this
.
_sessions
.
get
(
key
)
;
if
(
sessionId
&&
this
.
_client
)
{
try
{
await
this
.
_client
.
sendRequest
(
"modelscript/resetNotebookSession"
,
{
sessionId
}
)
;
}
catch
{
// ignore — session may not exist on server yet
}
}
// Generate a new session ID so subsequent cells start fresh
this
.
_sessions
.
delete
(
key
)
;
this
.
_executionOrder
=
0
;
}
private
_execute
(
cells
:
vscode
.
NotebookCell
[
]
,
notebook
:
vscode
.
NotebookDocument
)
:
void
{
for
(
const
cell
of
cells
)
{
this
.
_doExecution
(
cell
,
notebook
)
;
}
}
private
async
_doExecution
(
cell
:
vscode
.
NotebookCell
,
notebook
:
vscode
.
NotebookDocument
)
:
Promise
<
void
>
{
const
execution
=
this
.
_controller
.
createNotebookCellExecution
(
cell
)
;
execution
.
executionOrder
=
++
this
.
_executionOrder
;
execution
.
start
(
Date
.
now
(
)
)
;
if
(
!
this
.
_client
)
{
execution
.
replaceOutput
(
[
new
vscode
.
NotebookCellOutput
(
[
vscode
.
NotebookCellOutputItem
.
error
(
new
Error
(
"Language server not ready. Please wait for initialization."
)
)
,
]
)
,
]
)
;
execution
.
end
(
false
,
Date
.
now
(
)
)
;
return
;
}
const
sessionId
=
this
.
_getSessionId
(
notebook
)
;
const
code
=
cell
.
document
.
getText
(
)
;
try
{
const
result
=
await
this
.
_client
.
sendRequest
<
{
output
:
string
;
error
?:
string
;
diagrams
?:
{
name
:
string
;
data
:
unknown
}
[
]
;
}
>
(
"modelscript/runNotebookCell"
,
{
sessionId
,
code
,
}
)
;
const
outputs
:
vscode
.
NotebookCellOutput
[
]
=
[
]
;
if
(
result
.
diagrams
&&
result
.
diagrams
.
length
>
0
)
{
for
(
const
diagram
of
result
.
diagrams
)
{
outputs
.
push
(
new
vscode
.
NotebookCellOutput
(
[
vscode
.
NotebookCellOutputItem
.
json
(
diagram
.
data
,
"application/x.modelscript-diagram"
)
,
]
)
,
)
;
}
}
if
(
result
.
error
)
{
outputs
.
push
(
new
vscode
.
NotebookCellOutput
(
[
vscode
.
NotebookCellOutputItem
.
error
(
new
Error
(
result
.
error
)
)
]
)
)
;
}
if
(
result
.
output
)
{
// Try to parse as JSON for rich rendering
try
{
const
parsed
=
JSON
.
parse
(
result
.
output
)
;
outputs
.
push
(
new
vscode
.
NotebookCellOutput
(
[
vscode
.
NotebookCellOutputItem
.
json
(
parsed
,
"application/json"
)
,
vscode
.
NotebookCellOutputItem
.
text
(
result
.
output
)
,
]
)
,
)
;
}
catch
{
// Plain text output
outputs
.
push
(
new
vscode
.
NotebookCellOutput
(
[
vscode
.
NotebookCellOutputItem
.
text
(
result
.
output
)
]
)
)
;
}
}
if
(
outputs
.
length
===
0
)
{
// No output — still mark as successful
execution
.
replaceOutput
(
[
]
)
;
}
else
{
execution
.
replaceOutput
(
outputs
)
;
}
execution
.
end
(
!
result
.
error
,
Date
.
now
(
)
)
;
}
catch
(
e
)
{
execution
.
replaceOutput
(
[
new
vscode
.
NotebookCellOutput
(
[
vscode
.
NotebookCellOutputItem
.
error
(
e
instanceof
Error
?
e
:
new
Error
(
String
(
e
)
)
)
,
]
)
,
]
)
;
execution
.
end
(
false
,
Date
.
now
(
)
)
;
}
}
dispose
(
)
:
void
{
this
.
_controller
.
dispose
(
)
;
}
}
Back
|
FazBrowse Home
|
New Git URL