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