-
Notifications
You must be signed in to change notification settings - Fork 21
/
model.js
222 lines (189 loc) · 5.25 KB
/
model.js
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
const IdrisIdeMode = require('./ide-mode')
const IdrisBuild = require('./idris-build')
const Rx = require('rx-lite')
const path = require('path')
class IdrisModel {
constructor() {
this.requestId = 0
this.ideModeRef = null
this.idrisBuildRef = null
this.idrisReplRef = null
this.subjects = {}
this.warnings = {}
this.idrisBuildSubject = null
this.idrisReplSubject = null
this.compilerOptions = {}
}
ideMode(compilerOptions) {
// Stop and nullify ideModeRef if it is already running but with
// outdated options, so that it can be restarted.
if (!this.ideModeRef) {
this.ideModeRef = new IdrisIdeMode()
this.ideModeRef.on('message', (obj) => { this.handleIdeModeCommand(obj) })
this.ideModeRef.start(compilerOptions)
}
return this.ideModeRef
}
idrisBuild(compilerOptions, ipkgFile) {
this.idrisBuildRef = new IdrisBuild(ipkgFile)
this.idrisBuildRef.on('message', (obj) => { this.handleIdrisBuildMessage(obj) })
this.idrisBuildRef.start(compilerOptions)
return this.idrisBuildRef
}
objectEqual(a, b) {
return JSON.stringify(a) === JSON.stringify(b)
}
stop() {
if (this.ideModeRef)
this.ideModeRef.stop()
if (this.idrisReplRef)
this.idrisReplRef.stop()
}
setCompilerOptions(options) {
this.compilerOptions = options
}
prepareCommand(cmd) {
let id = this.getUID()
let subject = new Rx.Subject
this.subjects[id] = subject
this.warnings[id] = []
this.ideMode(this.compilerOptions).send([cmd, id])
return subject
}
getUID() {
return ++this.requestId
}
handleIdrisBuildMessage(msg) {
this.idrisBuildSubject.onNext(msg)
}
handleIdeModeCommand(cmd) {
if (cmd.length > 0) {
let op = cmd[0]
let params = cmd.slice(1, cmd.length - 1)
let id = cmd[cmd.length - 1]
if (this.subjects[id] != null) {
let subject = this.subjects[id]
switch (op) {
case ':return':
let ret = params[0]
if (ret[0] === ':ok') {
let okparams = ret[1]
if (okparams[0] === ':metavariable-lemma') {
subject.onNext({
responseType: 'return',
msg: okparams
})
} else {
subject.onNext({
responseType: 'return',
msg: ret.slice(1)
})
}
} else {
subject.onError({
message: ret[1],
warnings: this.warnings[id],
highlightInformation: ret[2],
cwd: this.compilerOptions.src
})
}
subject.onCompleted()
delete this.subjects[id]
break
case ':write-string':
let msg = params[0]
subject.onNext({
responseType: 'write-string',
msg: msg
})
break
case ':warning':
let warning = params[0]
this.warnings[id].push(warning)
break
case ':set-prompt':
break
}
}
}
}
changeDirectory(dir) {
return this.interpret(`:cd ${dir}`)
}
getDirectory(uri) {
if (this.compilerOptions && this.compilerOptions.src) {
return this.compilerOptions.src
} else {
return path.dirname(uri)
}
}
build(ipkgFile) {
this.idrisBuildSubject = new Rx.Subject
this.idrisBuild(this.compilerOptions, ipkgFile)
return this.idrisBuildSubject
}
load(uri) {
let dir = this.getDirectory(uri)
let cd
if (dir != this.compilerOptions.src) {
this.compilerOptions.src = dir
cd = this.changeDirectory(dir).map((_) => {
return dir
})
} else {
cd = Rx.Observable.of(dir)
}
return cd.flatMap((_) => {
return this.prepareCommand([':load-file', uri])
})
}
getType(word) {
return this.prepareCommand([':type-of', word])
}
getDocs(word) {
return this.prepareCommand([':docs-for', word])
}
printDefinition(name) {
return this.prepareCommand([':print-definition', name])
}
interpret(code) {
return this.prepareCommand([':interpret', code])
}
getVersion() {
return this.prepareCommand(':version')
}
holes(width) {
return this.prepareCommand([':metavariables', width])
}
addClause(line, word) {
return this.prepareCommand([':add-clause', line, word])
}
addProofClause(line, word) {
return this.prepareCommand([':add-proof-clause', line, word])
}
caseSplit(line, word) {
return this.prepareCommand([':case-split', line, word])
}
proofSearch(line, word) {
return this.prepareCommand([':proof-search', line, word, []])
}
makeWith(line, word) {
return this.prepareCommand([':make-with', line, word])
}
makeLemma(line, word) {
return this.prepareCommand([':make-lemma', line, word])
}
makeCase(line, word) {
return this.prepareCommand([':make-case', line, word])
}
apropos(name) {
return this.prepareCommand([':apropos', name])
}
replCompletions(word) {
return this.prepareCommand([':repl-completions', word])
}
browseNamespace(moduleName) {
return this.prepareCommand([':browse-namespace', moduleName])
}
}
module.exports = IdrisModel