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.tools.analysis_tool : AnalysisTool; 73 import dls.util.json : convertFromJSON; 74 import dls.util.uri : Uri; 75 import std.json : JSONException; 76 import std.format : format; 77 78 logger.info("Executing command %s with arguments %s", commandName, arguments); 79 80 try 81 { 82 final switch (convertFromJSON!Commands(JSONValue(commandName))) 83 { 84 case Commands.workspaceEdit: 85 send(Workspace.applyEdit, 86 new ApplyWorkspaceEditParams(convertFromJSON!WorkspaceEdit(arguments[0]))); 87 break; 88 89 case Commands.codeAction_analysis_disableCheck: 90 AnalysisTool.instance.disableCheck(new Uri(convertFromJSON!string(arguments[0])), 91 convertFromJSON!string(arguments[1])); 92 break; 93 } 94 } 95 catch (JSONException e) 96 { 97 throw new InvalidParamsException(format!"unknown command: %s"(commandName)); 98 } 99 100 return JSONValue(null); 101 } 102 }