-
Notifications
You must be signed in to change notification settings - Fork 22
/
init.wls
251 lines (187 loc) · 7 KB
/
init.wls
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
243
244
245
246
247
248
249
250
251
#!/usr/bin/env wolframscript
(* ::Package:: *)
(* Copyright 2018 lsp-wl Authors *)
(* SPDX-License-Identifier: MIT *)
(* init.wls
This is a script file to initialize the Wolfram Language Server.
Please see the help info below.
*)
(* For Windows, this should be set for Print to correctly format outputs. *)
If[$OperatingSystem == "Windows" && $FrontEnd === Null,
SetOptions[$Output, FormatType -> OutputForm]
]
(* Messaage pinted to stdout should be encoded in UTF-8. *)
$CharacterEncoding = "UTF-8"
(* ::Subsection:: *)
(*RootDirectory*)
(* If $ScriptCommandLine is empty, use $CommandLine instead *)
WolframLanguageServer`CommandLine = Replace[$ScriptCommandLine, {} -> $CommandLine ~Drop~ 2];
WolframLanguageServer`RootDirectory = (
$InputFileName
// Replace["" -> First @ WolframLanguageServer`CommandLine] (* If empty, use CommandLine *)
// DirectoryName (* root folder *)
// Replace[{ (* relative path to absolute path *)
"" -> Directory[],
relpath:(("./"|"../") ~~ ___ ):> FileNameJoin[{Directory[], relpath}]
}]
);
WolframLanguageServer`SourceDirectories = {
FileNameJoin[{WolframLanguageServer`RootDirectory, "src"}],
FileNameJoin[{WolframLanguageServer`RootDirectory, "external", "Matypetica", "src"}]
}
WolframLanguageServer`TestDirectories = {
FileNameJoin[{WolframLanguageServer`RootDirectory, "test"}],
FileNameJoin[{WolframLanguageServer`RootDirectory, "external", "Matypetica", "test"}]
}
WolframLanguageServer`$DefaultPath = $Path
$Path = Join[
WolframLanguageServer`SourceDirectories,
WolframLanguageServer`TestDirectories,
$Path
]
(* ::Subsection:: *)
(*Argument Parsing*)
WolframLanguageServer`CommandLine = Flatten[Rest[WolframLanguageServer`CommandLine] ~StringSplit~ "="];
ArgumentValue[keyPattern_, valuePattern_:Blank[]] := Module[
{
},
FirstPosition[WolframLanguageServer`CommandLine, keyPattern]
// Replace[{
_Missing -> Missing["NotSpecified"],
{Length[WolframLanguageServer`CommandLine]} -> Missing["ValueMissing"], (* last argument *)
{keyPos_} :> (
Part[WolframLanguageServer`CommandLine, keyPos + 1]
// Replace[{Except[_?(MatchQ[valuePattern])] -> Missing["PatternNotMatch"]}]
)
}]
];
(* ::Subsection:: *)
(*Help Information*)
If[MemberQ[WolframLanguageServer`CommandLine, "-h" | "--help"],
Print[
"
This is a script file to initialize the Wolfram Language Server.
You can execute the script from commandline:
wolfram -script path/to/init.wls [args]
wolframscript -f path/to/init.wls [args]
or inside a notebook via:
Get[\"path/to/init.wls\"];
Options:
--test, -t Run all tests
--log=loglevel, -l loglevel Specifiy logging level as debug, info, warn or error (default: info)
--no-start Do not run the language server. (Useful when running in notebook)
--socket=port Connect to a socket server on port (default: 6536)
--tcp-server=port Start a socket server on port (default: 6536)
--pipe=pipeName Connect via a named pipe (Windows only)
--generate-table Re-generate the tables (unicode and completion tables)
--version, -v Print the version.
--help, -h Print this help message
\"initializationOptions\": {
}
"
];
Quit[];
];
(* ::Subsection:: *)
(*Version*)
WolframLanguageServer`$Name = "Wolfram Language Server"
WolframLanguageServer`$Version = "0.3.1"
If[MemberQ[WolframLanguageServer`CommandLine, "-v" | "--version"],
Print["
Wolfram Language Server " <> WolframLanguageServer`$Version <> " running on
Wolfram Language " <> $Version <> "\n"];
Quit[];
];
(* ::Subsection:: *)
(*Test*)
If[MemberQ[WolframLanguageServer`CommandLine, "-t" | "--test"],
Print["\nRunning tests for all.\n"];
Off[General::shdw];
<< RunTest`;
RunTest`TestRunAll[]
// Replace[{testAllPass_, testResult_} :> (
Print[testResult];
Exit[If[testAllPass, 0, 1]]
)]
]
(* ::Subsection:: *)
(*Generate Table*)
If[MemberQ[WolframLanguageServer`CommandLine, "--generate-table"],
Needs["WolframLanguageServer`TableGenerator`"];
Print["\nGenerating UnicodeTable.\n"];
FindFile["WolframLanguageServer`UnicodeTable`"]
// WolframLanguageServer`TableGenerator`GenerateUnicodeTable;
Print["\nGenerating CompletionsTable.\n"];
FindFile["WolframLanguageServer`CompletionTable`"]
// WolframLanguageServer`TableGenerator`GenerateCompletionTable;
Print["\nDone.\n"];
Quit[]
]
(* ::Subsection:: *)
(*Run Server*)
Needs["WolframLanguageServer`Logger`"];
Module[
{
stream, loglevel, logstreams, clientPid, port, pipe,
ContextPattern, ExportedFunctions
},
stream = Which[
MemberQ[WolframLanguageServer`CommandLine, "--stdio"],
"stdio",
MemberQ[WolframLanguageServer`CommandLine, "--pipe"],
"pipe",
MemberQ[WolframLanguageServer`CommandLine, "--tcp-server"],
"tcp-server",
True,
"socket"
];
clientPid = ArgumentValue["--clientProcessId", _?(StringMatchQ[NumberString])]
// Replace[{
p:Except[_Missing] :> ToExpression[p]
}];
port = ArgumentValue["--socket", _?(StringMatchQ[NumberString])]
// Replace[_Missing :> ArgumentValue["--tcp-server", _?(StringMatchQ[NumberString])]]
// Replace[{
_Missing -> 6536,
p_ :> ToExpression[p]
}];
pipe = ArgumentValue["--pipe", _String]
// Replace[{
_Missing :> Null
}];
loglevel = ArgumentValue["--log"|"-l", Alternatives @@ LoggingLevels]
// Replace[_Missing -> "info"];
(*stream = ArgumentValue["--stream"|"-s", "stdio"|"socket"]
// Replace[_Missing, "stdio"];*)
logstreams = {
(* OpenWrite[WolframLanguageServer`RootDirectory <> "wlserver.log"], *)
First @ Streams["stderr"]
};
LoggerStart[loglevel, logstreams];
$Messages = logstreams;
LogInfo @ "Initializing Wolfram Language Server";
(* ContextPattern = "WolframLanguageServer`*";
ExportedFunctions ={"WLServerStart", "WLServerVersion", "WLServerDebug"};
$ContextPath = DeleteCases[$ContextPath, _?(StringMatchQ["WolframLanguageServer`*"])];
Unprotect[$Packages];
$Packages = DeleteCases[$Packages, _?(StringMatchQ[ContextPattern])];
Protect[$Packages];
Off[Remove::rmnsm];
Remove/@ ExportedFunctions;
On[Remove::rmnsm];
DeclarePackage["WolframLanguageServer`Server`", {"WLServerStart", "WLServerVersion", "WLServerDebug"}];
*)
<< WolframLanguageServer`Server`;
WolframLanguageServer`CheckReturnTypeQ = True;
If[!MemberQ[WolframLanguageServer`CommandLine, "--no-start"],
LogDebug @ WolframLanguageServer`Server`WLServerStart[
"Stream" -> stream,
"ClientPid" -> clientPid,
"Port" -> port,
"Pipe" -> pipe,
"WorkingDir" -> WolframLanguageServer`RootDirectory
]
];
InputString[];
Quit[0]
];