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.tools.command_tool;
22 
23 import dls.tools.tool : Tool;
24 
25 enum Commands : string
26 {
27     workspaceEdit = "workspaceEdit",
28     codeAction_analysis_disableCheck = "codeAction.analysis.disableCheck"
29 }
30 
31 class CommandTool : Tool
32 {
33     import std.json : JSONValue;
34 
35     private static CommandTool _instance;
36 
37     static void initialize(CommandTool tool)
38     {
39         _instance = tool;
40     }
41 
42     static void shutdown()
43     {
44         destroy(_instance);
45     }
46 
47     @property static CommandTool instance()
48     {
49         return _instance;
50     }
51 
52     @property string[] commands()
53     {
54         string[] result;
55 
56         foreach (member; __traits(allMembers, Commands))
57         {
58             result ~= mixin("Commands." ~ member);
59         }
60 
61         return result;
62     }
63 
64     JSONValue executeCommand(const string commandName, const JSONValue[] arguments)
65     {
66         import dls.protocol.definitions : WorkspaceEdit;
67         import dls.protocol.errors : InvalidParamsException;
68         import dls.protocol.interfaces : ApplyWorkspaceEditParams;
69         import dls.protocol.jsonrpc : send;
70         import dls.protocol.logger : logger;
71         import dls.protocol.messages.methods : Workspace;
72         import dls.util.json : convertFromJSON;
73         import dls.util.uri : Uri;
74         import std.json : JSONException;
75         import std.format : format;
76 
77         logger.info("Executing command %s with arguments %s", commandName, arguments);
78 
79         try
80         {
81             final switch (convertFromJSON!Commands(JSONValue(commandName)))
82             {
83             case Commands.workspaceEdit:
84                 send(Workspace.applyEdit,
85                         new ApplyWorkspaceEditParams(convertFromJSON!WorkspaceEdit(arguments[0])));
86                 break;
87 
88             case Commands.codeAction_analysis_disableCheck:
89                 // TODO: disable check
90                 break;
91             }
92         }
93         catch (JSONException e)
94         {
95             throw new InvalidParamsException(format!"unknown command: %s"(commandName));
96         }
97 
98         return JSONValue(null);
99     }
100 }