1 /*
2  *Copyright (C) 2018 Laurent Tréguier
3  *
4  *This file is part of DLS.
5  *
6  *DLS is free software: you can redistribute it and/or modify
7  *it under the terms of the GNU General Public License as published by
8  *the Free Software Foundation, either version 3 of the License, or
9  *(at your option) any later version.
10  *
11  *DLS is distributed in the hope that it will be useful,
12  *but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *GNU General Public License for more details.
15  *
16  *You should have received a copy of the GNU General Public License
17  *along with DLS.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 module dls.protocol.messages.workspace;
22 
23 import dls.protocol.interfaces : SymbolInformation;
24 import dls.protocol.interfaces.workspace;
25 import std.json : JSONValue;
26 import std.typecons : Nullable;
27 
28 void workspaceFolders(string id, Nullable!(WorkspaceFolder[]) folders)
29 {
30     import dls.protocol.jsonrpc : send;
31     import dls.protocol.messages.methods : Workspace;
32     import dls.protocol.state : initState;
33     import dls.tools.tool : Tool;
34     import dls.util.uri : Uri;
35     import std.typecons : nullable;
36 
37     if (!folders.isNull)
38     {
39         ConfigurationItem[] items;
40 
41         foreach (workspaceFolder; folders)
42         {
43             auto uri = new Uri(workspaceFolder.uri);
44             items ~= new ConfigurationItem(uri.toString().nullable);
45             Tool.instance.updateConfig(uri, JSONValue());
46         }
47 
48         immutable conf = !initState.capabilities.workspace.isNull
49             && !initState.capabilities.workspace.configuration.isNull
50             && initState.capabilities.workspace.configuration;
51 
52         if (conf)
53         {
54             send(Workspace.configuration, new ConfigurationParams(items));
55         }
56     }
57 }
58 
59 void didChangeWorkspaceFolders(DidChangeWorkspaceFoldersParams params)
60 {
61     import dls.tools.tool : Tool;
62     import dls.util.uri : Uri;
63     import std.typecons : nullable;
64 
65     workspaceFolders(null, params.event.added.nullable);
66 
67     foreach (folder; params.event.removed)
68     {
69         auto uri = new Uri(folder.uri);
70         Tool.instance.removeConfig(uri);
71     }
72 }
73 
74 void configuration(string id, JSONValue[] configs)
75 {
76     import dls.protocol.logger : logger;
77     import dls.tools.tool : Tool;
78 
79     auto uris = null ~ Tool.instance.workspacesUris;
80 
81     logger.info("Updating workspace configurations");
82 
83     for (size_t i; i < configs.length && i < uris.length; ++i)
84     {
85         auto config = configs[i];
86 
87         if ("d" in config && "dls" in config["d"])
88         {
89             Tool.instance.updateConfig(uris[i], config["d"]["dls"]);
90         }
91     }
92 }
93 
94 void didChangeConfiguration(DidChangeConfigurationParams params)
95 {
96     import dls.protocol.jsonrpc : send;
97     import dls.protocol.logger : logger;
98     import dls.protocol.messages.methods : Workspace;
99     import dls.protocol.state : initState;
100     import dls.tools.configuration : Configuration;
101     import dls.tools.tool : Tool;
102     import std.typecons : Nullable, nullable;
103 
104     logger.info("Configuration changed");
105 
106     immutable conf = !initState.capabilities.workspace.isNull
107         && !initState.capabilities.workspace.configuration.isNull
108         && initState.capabilities.workspace.configuration;
109 
110     if (conf)
111     {
112         auto items = [new ConfigurationItem(Nullable!string(null))];
113 
114         foreach (uri; Tool.instance.workspacesUris)
115         {
116             items ~= new ConfigurationItem(uri.toString().nullable);
117         }
118 
119         send(Workspace.configuration, new ConfigurationParams(items));
120     }
121     else if ("d" in params.settings && "dls" in params.settings["d"])
122     {
123         logger.info("Updating configuration");
124         Tool.instance.updateConfig(null, params.settings["d"]["dls"]);
125     }
126 }
127 
128 void didChangeWatchedFiles(DidChangeWatchedFilesParams params)
129 {
130 }
131 
132 SymbolInformation[] symbol(WorkspaceSymbolParams params)
133 {
134     return [];
135 }
136 
137 JSONValue executeCommand(ExecuteCommandParams params)
138 {
139     import dls.tools.command_tool : CommandTool;
140 
141     return CommandTool.instance.executeCommand(params.command,
142             params.arguments.isNull ? [] : params.arguments.get());
143 }
144 
145 void applyEdit(string id, ApplyWorkspaceEditResponse response)
146 {
147 }