|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +import * as fs from 'fs'; |
| 4 | +import * as path from 'path'; |
| 5 | +import * as which from 'which'; |
| 6 | +import * as vscode from 'vscode'; |
| 7 | +import * as cp from 'child_process'; |
| 8 | + |
| 9 | +import { FORMATTERS } from '../lib/tools'; |
| 10 | +import { LoggingService } from '../services/logging-service'; |
| 11 | +import { EXTENSION_ID, promptForMissingTool } from '../lib/helper'; |
| 12 | + |
| 13 | +export class FortranFormattingProvider implements vscode.DocumentFormattingEditProvider { |
| 14 | + constructor(private logger: LoggingService) {} |
| 15 | + |
| 16 | + public provideDocumentFormattingEdits( |
| 17 | + document: vscode.TextDocument, |
| 18 | + options: vscode.FormattingOptions, |
| 19 | + token: vscode.CancellationToken |
| 20 | + ): vscode.ProviderResult<vscode.TextEdit[]> { |
| 21 | + const formatterName: string = this.getFormatter(); |
| 22 | + |
| 23 | + if (formatterName === 'fprettify') { |
| 24 | + this.doFormatFprettify(document); |
| 25 | + } else if (formatterName === 'findent') { |
| 26 | + this.doFormatFindent(document); |
| 27 | + } else { |
| 28 | + this.logger.logError('Cannot format document with formatter set to Disabled'); |
| 29 | + } |
| 30 | + |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Use `fprettify` to format a Fortran file. |
| 36 | + * |
| 37 | + * @param document vscode.TextDocument document to operate on |
| 38 | + */ |
| 39 | + private doFormatFprettify(document: vscode.TextDocument) { |
| 40 | + // fprettify can only do FortranFreeFrom |
| 41 | + if (document.languageId !== 'FortranFreeForm') { |
| 42 | + this.logger.logError(`fprettify can only format FortranFreeForm, change |
| 43 | + to findent for FortranFixedForm formatting`); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + const formatterName = 'fprettify'; |
| 48 | + const formatterPath: string = this.getFormatterPath(); |
| 49 | + // If no formatter path is present check that formatter is present in $PATH |
| 50 | + if (!formatterPath) { |
| 51 | + if (!which.sync(formatterName, { nothrow: true })) { |
| 52 | + this.logger.logWarning(`Formatter: ${formatterName} not detected in your system. |
| 53 | + Attempting to install now.`); |
| 54 | + const msg = `Installing ${formatterName} through pip with --user option`; |
| 55 | + promptForMissingTool(formatterName, msg, 'Python'); |
| 56 | + } |
| 57 | + } |
| 58 | + const formatter: string = path.join(formatterPath, formatterName); |
| 59 | + |
| 60 | + const args: string[] = [document.fileName, ...this.getFormatterArgs()]; |
| 61 | + // args.push('--silent'); // TODO: pass? |
| 62 | + |
| 63 | + // Get current file (name rel to path), run extension can be in a shell?? |
| 64 | + const process = cp.spawn(formatter, args); |
| 65 | + |
| 66 | + // if the findent then capture the output from that and parse it back to the file |
| 67 | + process.stdout.on('data', data => { |
| 68 | + this.logger.logInfo(`formatter stdout: ${data}`); |
| 69 | + }); |
| 70 | + process.stderr.on('data', data => { |
| 71 | + this.logger.logError(`formatter stderr: ${data}`); |
| 72 | + }); |
| 73 | + process.on('close', (code: number) => { |
| 74 | + if (code !== 0) this.logger.logInfo(`formatter exited with code: ${code}`); |
| 75 | + }); |
| 76 | + process.on('error', code => { |
| 77 | + this.logger.logInfo(`formatter exited with code: ${code}`); |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Use `findent` to format a Fortran file. |
| 83 | + * Creates a temporary file where the output is placed and then deleted |
| 84 | + * |
| 85 | + * @param document vscode.TextDocument document to operate on |
| 86 | + */ |
| 87 | + private doFormatFindent(document: vscode.TextDocument) { |
| 88 | + const formatterName = 'findent'; |
| 89 | + const formatterPath: string = this.getFormatterPath(); |
| 90 | + // If no formatter path is present check that formatter is present in $PATH |
| 91 | + if (!formatterPath) { |
| 92 | + if (!which.sync(formatterName, { nothrow: true })) { |
| 93 | + this.logger.logWarning(`Formatter: ${formatterName} not detected in your system. |
| 94 | + Attempting to install now.`); |
| 95 | + const msg = `Installing ${formatterName} through pip with --user option`; |
| 96 | + promptForMissingTool(formatterName, msg, 'Python'); |
| 97 | + } |
| 98 | + } |
| 99 | + let formatter: string = path.join(formatterPath, formatterName); |
| 100 | + |
| 101 | + // Annoyingly findent only outputs to a file and not to a stream so |
| 102 | + // let us go and create a temporary file |
| 103 | + const out = document.uri.path + '.findent.tmp'; |
| 104 | + const args: string = ['< ' + document.fileName + ' >', out, ...this.getFormatterArgs()].join( |
| 105 | + ' ' |
| 106 | + ); |
| 107 | + formatter = formatter + ' ' + args; |
| 108 | + |
| 109 | + // @note It is wise to have all IO operations being synchronous we don't |
| 110 | + // want to copy or delete the temp file before findent has completed. |
| 111 | + // I cannot forsee a situation where a performance bottleneck is created |
| 112 | + // since findent performs something like ~100k lines per second |
| 113 | + cp.execSync(formatter, { stdio: 'inherit' }); |
| 114 | + fs.copyFileSync(out, document.fileName); |
| 115 | + fs.unlinkSync(out); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Get the formatter type |
| 120 | + * Currently supporting: `findent` and `fprettify` |
| 121 | + * |
| 122 | + * Formatters are defined in FORMATTERS (./lib/tools.ts) |
| 123 | + * |
| 124 | + * @returns {string} formatter name or `Disabled` |
| 125 | + */ |
| 126 | + private getFormatter(): string { |
| 127 | + const config = vscode.workspace.getConfiguration(EXTENSION_ID); |
| 128 | + const formatter: string = config.get('formatting.formatter', 'Disabled'); |
| 129 | + |
| 130 | + if (!FORMATTERS.includes(formatter)) { |
| 131 | + this.logger.logError(`Unsupported formatter: ${formatter}`); |
| 132 | + } |
| 133 | + return formatter; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Read in any custom arguments for the formatter |
| 138 | + * |
| 139 | + * @returns {string[]} list of additional arguments |
| 140 | + */ |
| 141 | + private getFormatterArgs(): string[] { |
| 142 | + const config = vscode.workspace.getConfiguration(EXTENSION_ID); |
| 143 | + const args: string[] = config.get('formatting.args', []); |
| 144 | + |
| 145 | + return args; |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Installation directory for formatter (if not in PATH) |
| 150 | + * |
| 151 | + * @returns {string} path of formatter |
| 152 | + */ |
| 153 | + private getFormatterPath(): string { |
| 154 | + const config = vscode.workspace.getConfiguration(EXTENSION_ID); |
| 155 | + const formatterPath: string = config.get('formatting.path', ''); |
| 156 | + if (formatterPath !== '') { |
| 157 | + this.logger.logInfo(`Formatter located in: ${formatterPath}`); |
| 158 | + } |
| 159 | + |
| 160 | + return formatterPath; |
| 161 | + } |
| 162 | +} |
0 commit comments