|
| 1 | +/*---------------------------------------------------------------------------- |
| 2 | +-- Language Server Protocol -- |
| 3 | +-- -- |
| 4 | +-- Copyright (C) 2018-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 * as vscode from 'vscode'; |
| 19 | + |
| 20 | +enum taskKinds { |
| 21 | + proveProject, // Run gnatprove for the project |
| 22 | + proveFile, // Run gnatprove for the single file |
| 23 | +} |
| 24 | + |
| 25 | +export default class gnatproveTaskProvider implements vscode.TaskProvider<vscode.Task> { |
| 26 | + public static gnatproveType = 'gnatprove'; |
| 27 | + gnatproveTasks: vscode.Task[] | undefined; |
| 28 | + |
| 29 | + constructor() { |
| 30 | + this.gnatproveTasks = undefined; |
| 31 | + } |
| 32 | + |
| 33 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 34 | + provideTasks(_token: vscode.CancellationToken): vscode.Task[] { |
| 35 | + if (!this.gnatproveTasks) { |
| 36 | + this.gnatproveTasks = getTasks(); |
| 37 | + } |
| 38 | + return this.gnatproveTasks; |
| 39 | + } |
| 40 | + |
| 41 | + resolveTask( |
| 42 | + task: vscode.Task, |
| 43 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 44 | + _token: vscode.CancellationToken |
| 45 | + ): vscode.ProviderResult<vscode.Task> { |
| 46 | + return task; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +const getTasks = (): vscode.Task[] => { |
| 51 | + function makeTask(taskKind: taskKinds, args: string[], title: string) { |
| 52 | + const kind = { |
| 53 | + type: gnatproveTaskProvider.gnatproveType, |
| 54 | + projectFile: '${config:ada.projectFile}', |
| 55 | + taskKind: taskKind, |
| 56 | + }; |
| 57 | + const shell = new vscode.ShellExecution('gnatprove', args); |
| 58 | + const task = new vscode.Task(kind, vscode.TaskScope.Workspace, title, 'ada', shell, '$ada'); |
| 59 | + task.group = vscode.TaskGroup.Build; |
| 60 | + return task; |
| 61 | + } |
| 62 | + |
| 63 | + const result: vscode.Task[] = []; |
| 64 | + |
| 65 | + // Run gnatprove on current project |
| 66 | + const proveProject = makeTask( |
| 67 | + taskKinds.proveProject, |
| 68 | + getGnatproveArgs('${config:ada.projectFile}'), |
| 69 | + 'Run gnatprove on the current project' |
| 70 | + ); |
| 71 | + |
| 72 | + // Run gnatprove on a file |
| 73 | + const proveFile = makeTask( |
| 74 | + taskKinds.proveFile, |
| 75 | + getGnatproveArgs('${config:ada.projectFile}', '${fileBasename}'), |
| 76 | + 'Run gnatprove on the current file' |
| 77 | + ); |
| 78 | + result.push(proveProject); |
| 79 | + result.push(proveFile); |
| 80 | + |
| 81 | + return result; |
| 82 | +}; |
| 83 | + |
| 84 | +const getGnatproveArgs = (projectFile?: string, file?: string): string[] => { |
| 85 | + // Append file (if any) and `-gnatef` to generate full file names in errors/warnings |
| 86 | + const arg = file ? [file] : []; |
| 87 | + const p_gnatef = ['-cargs', '-gnatef']; |
| 88 | + return commonArgs(projectFile).concat(arg, p_gnatef); |
| 89 | +}; |
| 90 | + |
| 91 | +// return '-P project.gpr -XVAR=value` optiona |
| 92 | +const commonArgs = (projectFile?: string): string[] => { |
| 93 | + const vars: string[][] = Object.entries( |
| 94 | + vscode.workspace.getConfiguration('ada').get('scenarioVariables') ?? [] |
| 95 | + ); |
| 96 | + const fold = (args: string[], item: string[]): string[] => { |
| 97 | + const option = '-X' + item[0] + '=' + item[1]; |
| 98 | + return args.concat([option]); |
| 99 | + }; |
| 100 | + // Set projectFile is any |
| 101 | + const prj = projectFile ? ['-P', projectFile] : []; |
| 102 | + // for each scenarioVariables put `-Xname=value` option |
| 103 | + return vars.reduce(fold, prj); |
| 104 | +}; |
0 commit comments