1 module dls.protocol.messages.general;
2 
3 import logger = std.experimental.logger;
4 import dls.protocol.interfaces;
5 import dls.server : Server;
6 import dls.tools.tools : Tools;
7 
8 @("")
9 auto initialize(InitializeParams params)
10 {
11     import dls.tools.tools : Tools;
12     import dls.util.uri : Uri;
13     import std.algorithm : map;
14     import std.array : array;
15 
16     auto result = new InitializeResult();
17 
18     Server.initialized = true;
19     Server.initState = params;
20     logger.log("Initializing server");
21     Tools.initialize();
22 
23     Uri[] uris;
24 
25     if (!params.rootUri.isNull)
26     {
27         uris ~= new Uri(params.rootUri);
28     }
29 
30     if (!params.workspaceFolders.isNull)
31     {
32         uris ~= params.workspaceFolders.map!(wf => new Uri(wf.uri)).array;
33     }
34 
35     foreach (uri; uris)
36     {
37         Tools.codeCompleter.importPath(uri);
38         Tools.codeCompleter.importSelections(uri);
39     }
40 
41     with (result)
42     {
43         capabilities = new ServerCapabilities();
44 
45         with (capabilities)
46         {
47             textDocumentSync = new TextDocumentSyncOptions();
48             completionProvider = new CompletionOptions();
49             documentFormattingProvider = true;
50             workspace = new ServerCapabilities.Workspace();
51 
52             with (textDocumentSync)
53             {
54                 openClose = true;
55                 change = TextDocumentSyncKind.incremental;
56             }
57 
58             with (completionProvider)
59             {
60                 resolveProvider = false;
61                 triggerCharacters = ["."];
62             }
63 
64             with (workspace)
65             {
66                 workspaceFolders = new ServerCapabilities.Workspace.WorkspaceFolders();
67 
68                 with (workspaceFolders)
69                 {
70                     supported = true;
71                     changeNotifications = JSONValue(true);
72                 }
73             }
74         }
75     }
76 
77     return result;
78 }
79 
80 @("")
81 void initialized(JSONValue nothing)
82 {
83     import dls.updater : update;
84     import std.concurrency : spawn;
85 
86     spawn(&update);
87 
88     const didChangeWatchedFiles = Server.initState.capabilities.workspace.didChangeWatchedFiles;
89 
90     if (!didChangeWatchedFiles.isNull && didChangeWatchedFiles.dynamicRegistration)
91     {
92         auto params = new RegistrationParams!DidChangeWatchedFilesRegistrationOptions();
93         params.registrations ~= new Registration!DidChangeWatchedFilesRegistrationOptions();
94 
95         with (params.registrations[0])
96         {
97             id = "dls-registration-watch-dub-files";
98             method = "workspace/didChangeWatchedFiles";
99             registerOptions = new DidChangeWatchedFilesRegistrationOptions();
100 
101             with (registerOptions)
102             {
103                 watchers = [new FileSystemWatcher(), new FileSystemWatcher()];
104                 watchers[0].globPattern = "**/dub.selections.json";
105                 watchers[1].globPattern = "**/dub.{json,sdl}";
106             }
107         }
108 
109         Server.send("client/registerCapability", params);
110     }
111 }
112 
113 @("")
114 auto shutdown(JSONValue nothing)
115 {
116     logger.log("Shutting down server");
117     Server.shutdown = true;
118     return JSONValue(null);
119 }
120 
121 @("")
122 void exit(JSONValue nothing)
123 {
124     logger.log("Exiting server");
125     Server.exit = true;
126 }
127 
128 @("$")
129 void cancelRequest(JSONValue id)
130 {
131 }