-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathls.py
242 lines (194 loc) · 7.83 KB
/
ls.py
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# # KIELER - Kiel Integrated Environment for Layout Eclipse RichClient
#
# http://rtsys.informatik.uni-kiel.de/kieler
#
# Copyright 2024 by
# + Kiel University
# + Department of Computer Science
# + Real-Time and Embedded Systems Group
#
# This program and the accompanying materials are made available under the
# terms of the MIT License which is available at
# https://opensource.org/license/mit.
# # SPDX-License-Identifier: MIT
import argparse
from lsprotocol import types as lsp
from pygls.server import LanguageServer
import synthesis as plyghd_synthesis
import PlyghdOptions
from synthesis_options import SynthesisOption
class KlighdLanguageServer(LanguageServer):
CMD_SET_PREFERENCES = "keith/preferences/setPreferences"
CMD_ACCEPT = "diagram/accept"
CMD_SET_SYNTHESIS_OPTIONS = "keith/diagramOptions/setSynthesisOptions"
def __init__(self, *args):
super().__init__(*args)
klighd_server = KlighdLanguageServer("klighd-ls-example", "v0.1")
current_options = {}
def getOption(current_options: dict, option: SynthesisOption):
if (option.id in current_options):
return current_options[option.id]
else:
return option.initialValue
@klighd_server.feature(lsp.INITIALIZE)
def initialize(params: lsp.InitializeParams):
init_synthesis_options = params.initialization_options["clientDiagramOptions"]["synthesis"]
for key in PlyghdOptions.plyghd_options:
try:
PlyghdOptions.plyghd_options[key] = init_synthesis_options[key]
except:
continue
@klighd_server.feature(KlighdLanguageServer.CMD_SET_PREFERENCES)
def set_preferences(ls: KlighdLanguageServer, preferences: dict):
# we don't care about the preferences set currently.
# We expect a message such as this one:
# {
# "jsonrpc": "2.0",
# "method": "keith/preferences/setPreferences",
# "params": {
# "diagram.shouldSelectDiagram": false,
# "diagram.shouldSelectText": true,
# "diagram.incrementalDiagramGenerator": false
# }
# }
# TODO: cross-reference and explain what these mean and how these could be handled.
pass
@klighd_server.feature(KlighdLanguageServer.CMD_ACCEPT)
def accept(ls: KlighdLanguageServer, *args):
print(args[0])
options = {
"requestModel": requestModel,
"performAction": performAction
}
try:
function = options[args[0].action.kind]
except:
print("action kind not supported yet: " + args[0].action.kind)
return
function(args[0].action)
@klighd_server.feature(KlighdLanguageServer.CMD_SET_SYNTHESIS_OPTIONS)
def set_synthesis_options(ls: KlighdLanguageServer, params: dict):
print(params)
# store the current synthesis options.
for option in params.synthesisOptions:
current_options[option.id] = option.currentValue
doRequestModel(params.uri)
def requestModel(action):
print(action)
doRequestModel(action.options.sourceUri)
model_uri = ""
def doRequestModel(sourceUri):
# we expect to receive an action such as this one:
# {
# "kind": "requestModel",
# "options": {
# "needsClientLayout": true,
# "needsServerLayout": false,
# "sourceUri": "file:///home/nre/git/models/graphs/small.kgt",
# "diagramType": "keith-diagram"
# },
# "requestId": "client_1"
# }
# For now, we just remember the model to be generated.
global model_uri
model_uri = sourceUri
# Find out the model file extension to find the available syntheses.
file_extension = model_uri.split(".")[-1]
synthesis_ids = PlyghdOptions.plyghd_syntheses[file_extension]
# Send a message to the client containing the known syntheses.
send_syntheses(synthesis_ids)
# We just use the first one as our synthesis.
synthesis = synthesis_ids[0]
known_syntheses = {
plyghd_synthesis.ID: plyghd_synthesis.PlyghdKgraphSynthesis
}
if not synthesis in known_syntheses:
print("synthesis not known. " + synthesis)
return
# Send the synthesis-available options.
synthesis_instance = known_syntheses[synthesis]()
send_available_options(synthesis_instance)
# Finally, generate the kgraph from the model file (hardcoded for now) and send it the client.
model = synthesis_instance.transform(model_uri, current_options) # TODO: should load the model.
print("sending requestBounds")
klighd_server.send_notification(KlighdLanguageServer.CMD_ACCEPT, {"clientId":"sprotty","action":{"kind":"requestBounds","newRoot": model}})
def send_syntheses(synthesis_ids):
# Send a message like this one, containing the known syntheses.
# {
# "jsonrpc": "2.0",
# "method": "diagram/accept",
# "params": {
# "clientId": "sprotty",
# "action": {
# "kind": "setSyntheses",
# "syntheses": [
# {
# "id": "de.cau.cs.kieler.graphs.klighd.syntheses.KGraphDiagramSynthesis",
# "displayName": "KGraphDiagramSynthesis"
# }
# ]
# }
# }
# }
syntheses = list(map(lambda id: {"id": id, "displayName": id.split(".")[-1]}, synthesis_ids))
print("sending setSynthesis")
klighd_server.send_notification(KlighdLanguageServer.CMD_ACCEPT, {"clientId":"sprotty","action":{"kind":"setSyntheses","syntheses": syntheses}})
def send_available_options(synthesis):
# Send a message like this one, containing the options of the chosen synthesis:
# {
# "clientId": "sprotty",
# "action": {
# "kind": "updateOptions",
# "valuedSynthesisOptions": [
# {
# "synthesisOption": {
# "id": "e.cau.cs.kieler.plyghd.PlyghdKgraphSynthesis.color",
# "name": "Color",
# "type": 3,
# "sourceHash": 42,
# "initialValue": "0x000000"
# },
# "currentValue": "0x000000"
# },
# {
# "synthesisOption": {
# "id": "de.cau.cs.kieler.plyghd.PlyghdKgraphSynthesis.labels",
# "name": "Labels",
# "type": 0,
# "sourceHash": 43,
# "initialValue": true
# },
# "currentValue": true
# }
# ],
# "layoutOptions": [],
# "actions": [],
# "modelUri": "file:///home/nre/git/models/graphs/small.kgt"
# }
# }
global model_uri
synthesis_options_json = list(map(lambda option: {
"synthesisOption": {
"id": option.id,
"name": option.name,
"type": option.type,
"sourceHash": option.id, # TODO: should be some unique hash value, might not be needed with better API.
"initialValue": option.initialValue
},
"currentValue": getOption(current_options, option) # TODO: should be stored and the current value be sent here instead.
}, synthesis.getDisplayedSynthesisOptions()))
print("sending updateOptions")
klighd_server.send_notification(KlighdLanguageServer.CMD_ACCEPT, {"clientId":"sprotty","action":{"kind":"updateOptions","valuedSynthesisOptions": synthesis_options_json, "layoutOptions":[],"actions":[],"modelUri": model_uri}})
def performAction(action):
print("action performed! action ID: " + action.actionId + ", KGraph element ID: " + action.kGraphElementId + ", KRendering ID: " + action.kRenderingId)
def add_arguments(parser):
parser.description = "simple KLighD diagram server example"
parser.add_argument("--host", default="127.0.0.1", help="Bind to this address")
parser.add_argument("--port", type=int, default=5007, help="Bind to this port")
def main():
parser = argparse.ArgumentParser()
add_arguments(parser)
args = parser.parse_args()
klighd_server.start_tcp(args.host, args.port)
if __name__ == "__main__":
main()