|
| 1 | +/*---------------------------------------------------------------------------- |
| 2 | +-- Language Server Protocol -- |
| 3 | +-- -- |
| 4 | +-- Copyright (C) 2021, AdaCore -- |
| 5 | +-- -- |
| 6 | +-- This is free software; you can redistribute it and/or modify it under -- |
| 7 | +-- terms of the GNU General Public License as published by the Free Soft- -- |
| 8 | +-- ware Foundation; either version 3, or (at your option) any later ver- -- |
| 9 | +-- sion. This software is distributed in the hope that it will be useful, -- |
| 10 | +-- but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- -- |
| 11 | +-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public -- |
| 12 | +-- License for more details. You should have received a copy of the GNU -- |
| 13 | +-- General Public License distributed with this software; see file -- |
| 14 | +-- COPYING3. If not, go to http://www.gnu.org/licenses for a complete copy -- |
| 15 | +-- of the license. -- |
| 16 | +----------------------------------------------------------------------------*/ |
| 17 | + |
| 18 | +import { window, InputBoxOptions } from 'vscode'; |
| 19 | +import { LanguageClient } from 'vscode-languageclient/node'; |
| 20 | + |
| 21 | +import { AdaGrammarRule, AdaSyntaxCheckProvider } from '../alsProtocolExtensions'; |
| 22 | + |
| 23 | +export type AddParameterCommandArgs = { |
| 24 | + newParameter: string; |
| 25 | + requiresFullSpecification: boolean; |
| 26 | +}; |
| 27 | + |
| 28 | +export const alsAddParameterCommandExecutor = async ( |
| 29 | + client: LanguageClient, |
| 30 | + args: AddParameterCommandArgs |
| 31 | +): Promise<boolean> => { |
| 32 | + // If the server command attributes changed, some of args fields might be undefined |
| 33 | + if (args.requiresFullSpecification === undefined || args.newParameter === undefined) { |
| 34 | + return Promise.reject( |
| 35 | + 'Invalid als-refactor-add-parameters command: missing "requiresFullSpecification" field' |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + // Create an input box with the messages adjusted according to if we require a full parameter |
| 40 | + // specification or not |
| 41 | + |
| 42 | + const prompt = args.requiresFullSpecification |
| 43 | + ? 'Insert a full parameter specification' |
| 44 | + : 'Insert one or more comma-separated parameter names or a full parameter specification'; |
| 45 | + |
| 46 | + const rules = args.requiresFullSpecification |
| 47 | + ? [AdaGrammarRule.Param_Spec_Rule] |
| 48 | + : [ |
| 49 | + AdaGrammarRule.Defining_Id_Rule, |
| 50 | + AdaGrammarRule.Defining_Id_List_Rule, |
| 51 | + AdaGrammarRule.Param_Spec_Rule, |
| 52 | + ]; |
| 53 | + |
| 54 | + const diagnostic = args.requiresFullSpecification |
| 55 | + ? 'This is not a syntactically valid full parameter specification' |
| 56 | + : 'This is not a syntactically valid parameter name or full parameter specification'; |
| 57 | + |
| 58 | + const adaSyntaxCheckProvider = new AdaSyntaxCheckProvider(client, rules, diagnostic); |
| 59 | + const { sendCheckSyntaxRequest } = adaSyntaxCheckProvider; |
| 60 | + |
| 61 | + const inputBoxOptions: InputBoxOptions = { |
| 62 | + title: 'Add Parameter(s)', |
| 63 | + prompt: prompt, |
| 64 | + ignoreFocusOut: true, |
| 65 | + validateInput: sendCheckSyntaxRequest, |
| 66 | + }; |
| 67 | + |
| 68 | + const input = await window.showInputBox(inputBoxOptions); |
| 69 | + |
| 70 | + // If input is undefined, then the user cancelled the operation. Return false |
| 71 | + // to indicate that the executor should no proced. Otherwise, set the |
| 72 | + // newParameter. |
| 73 | + |
| 74 | + if (input !== undefined) { |
| 75 | + args.newParameter = input; |
| 76 | + return Promise.resolve(true); |
| 77 | + } else { |
| 78 | + return Promise.resolve(false); |
| 79 | + } |
| 80 | +}; |
0 commit comments