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.tools.analysis_tool : AnalysisTool;
31     import dls.tools.symbol_tool : SymbolTool;
32     import dls.util.uri : Uri;
33 
34     if (!folders.isNull)
35     {
36         foreach (workspaceFolder; folders)
37         {
38             auto uri = new Uri(workspaceFolder.uri);
39             SymbolTool.instance.importPath(uri);
40             AnalysisTool.instance.addAnalysisConfigPath(uri);
41         }
42     }
43 }
44 
45 void didChangeWorkspaceFolders(DidChangeWorkspaceFoldersParams params)
46 {
47     import dls.tools.analysis_tool : AnalysisTool;
48     import dls.tools.symbol_tool : SymbolTool;
49     import dls.util.uri : Uri;
50     import std.typecons : nullable;
51 
52     workspaceFolders(null, params.event.added.nullable);
53 
54     foreach (folder; params.event.removed)
55     {
56         auto uri = new Uri(folder.uri);
57         SymbolTool.instance.clearPath(uri);
58         AnalysisTool.instance.removeAnalysisConfigPath(uri);
59     }
60 }
61 
62 void configuration(string id, JSONValue[] config)
63 {
64 }
65 
66 void didChangeConfiguration(DidChangeConfigurationParams params)
67 {
68     import dls.tools.configuration : Configuration;
69     import dls.tools.tool : Tool;
70     import dls.util.json : convertFromJSON;
71     import dls.util.logger : logger;
72 
73     logger.info("Configuration changed");
74 
75     if ("d" in params.settings && "dls" in params.settings["d"])
76     {
77         logger.info("Applying new configuration");
78         Tool.configuration = convertFromJSON!Configuration(params.settings["d"]["dls"]);
79     }
80 }
81 
82 void didChangeWatchedFiles(DidChangeWatchedFilesParams params)
83 {
84     import dls.protocol.interfaces : FileChangeType;
85     import dls.tools.analysis_tool : AnalysisTool;
86     import dls.tools.symbol_tool : SymbolTool;
87     import dls.util.logger : logger;
88     import dls.util.uri : Uri;
89     import std.path : baseName, dirName;
90 
91     foreach (event; params.changes)
92     {
93         auto uri = new Uri(event.uri);
94         auto dirUri = Uri.fromPath(dirName(uri.path));
95 
96         logger.infof("File changed: %s", uri.path);
97 
98         switch (baseName(uri.path))
99         {
100         case "dub.json", "dub.sdl":
101             if (baseName(dirName(uri.path)) != ".dub"
102                     && event.type != FileChangeType.deleted)
103             {
104                 SymbolTool.instance.importPath(dirUri);
105             }
106 
107             break;
108 
109         case "dub.selections.json":
110             SymbolTool.instance.importSelections(dirUri);
111             break;
112 
113         default:
114             AnalysisTool.instance.updateAnalysisConfigPath(dirUri);
115             break;
116         }
117     }
118 }
119 
120 SymbolInformation[] symbol(WorkspaceSymbolParams params)
121 {
122     import dls.tools.symbol_tool : SymbolTool;
123 
124     return SymbolTool.instance.symbol(params.query);
125 }
126 
127 JSONValue executeCommand(ExecuteCommandParams params)
128 {
129     return JSONValue(null);
130 }
131 
132 void applyEdit(string id, ApplyWorkspaceEditResponse response)
133 {
134 }