From 706c73160473c6e2b7cfa3e99e9c85319b8138f1 Mon Sep 17 00:00:00 2001 From: Alexey Date: Thu, 27 Apr 2017 21:11:37 +0300 Subject: [PATCH] Formatting is not available if rustfmt is not installed (#205) --- .../formatting/formatting_manager.ts | 34 ++++++++---- src/extension.ts | 2 +- src/legacy_mode_manager.ts | 52 ++++++++----------- 3 files changed, 47 insertions(+), 41 deletions(-) diff --git a/src/components/formatting/formatting_manager.ts b/src/components/formatting/formatting_manager.ts index 3e17d8d..21112ae 100644 --- a/src/components/formatting/formatting_manager.ts +++ b/src/components/formatting/formatting_manager.ts @@ -18,6 +18,8 @@ import { Configuration } from '../configuration/Configuration'; import getDocumentFilter from '../configuration/mod'; +import { FileSystem } from '../file_system/FileSystem'; + const ansiRegex = /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g; interface RustFmtDiff { @@ -31,9 +33,29 @@ export default class FormattingManager implements DocumentFormattingEditProvider private newFormatRegex: RegExp = /^Diff in (.*) at line (\d+):$/; - public constructor(context: ExtensionContext, configuration: Configuration) { - this.configuration = configuration; + public static async create(context: ExtensionContext, configuration: Configuration): Promise { + const rustfmtPath: string | undefined = await FileSystem.findExecutablePath(configuration.getRustfmtPath()); + if (rustfmtPath === undefined) { + return undefined; + } + return new FormattingManager(context, configuration); + } + public provideDocumentFormattingEdits(document: TextDocument): Thenable { + return this.formattingEdits(document); + } + + public provideDocumentRangeFormattingEdits(document: TextDocument, range: Range): Thenable { + return this.formattingEdits(document, range); + } + + /** + * To create an instance of the class use the method `create` + * @param context The extension context + * @param configuration The configuration + */ + private constructor(context: ExtensionContext, configuration: Configuration) { + this.configuration = configuration; context.subscriptions.push( languages.registerDocumentFormattingEditProvider( getDocumentFilter(), @@ -46,14 +68,6 @@ export default class FormattingManager implements DocumentFormattingEditProvider ); } - public provideDocumentFormattingEdits(document: TextDocument): Thenable { - return this.formattingEdits(document); - } - - public provideDocumentRangeFormattingEdits(document: TextDocument, range: Range): Thenable { - return this.formattingEdits(document, range); - } - private formattingEdits(document: TextDocument, range?: Range): Thenable { return new Promise((resolve, reject) => { const fileName = document.fileName + '.fmt'; diff --git a/src/extension.ts b/src/extension.ts index 14e62fa..10ebddf 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -193,7 +193,7 @@ async function chooseModeAndRun( if (pathToRlsExecutable) { runInRlsMode(context, logger, configuration, pathToRlsExecutable); } else { - const legacyModeManager = new LegacyModeManager( + const legacyModeManager = await LegacyModeManager.create( context, configuration, currentWorkingDirectoryManager, diff --git a/src/legacy_mode_manager.ts b/src/legacy_mode_manager.ts index 64c47e1..6beba49 100644 --- a/src/legacy_mode_manager.ts +++ b/src/legacy_mode_manager.ts @@ -1,5 +1,4 @@ import { ExtensionContext } from 'vscode'; - import { Configuration } from './components/configuration/Configuration'; import CurrentWorkingDirectoryManager @@ -21,62 +20,55 @@ import MissingToolsInstallator from './components/tools_installation/installator export default class LegacyModeManager { private context: ExtensionContext; - private configuration: Configuration; - private completionManager: CompletionManager; - - private formattingManager: FormattingManager; - + private formattingManager: FormattingManager | undefined; private workspaceSymbolProvisionManager: WorkspaceSymbolProvisionManager; - private documentSymbolProvisionManager: DocumentSymbolProvisionManager; - private missingToolsInstallator: MissingToolsInstallator; - public constructor( + public static async create( context: ExtensionContext, configuration: Configuration, currentWorkingDirectoryManager: CurrentWorkingDirectoryManager, logger: ChildLogger + ): Promise { + const formattingManager: FormattingManager | undefined = await FormattingManager.create(context, configuration); + return new LegacyModeManager(context, configuration, currentWorkingDirectoryManager, logger, formattingManager); + } + + public async start(): Promise { + this.context.subscriptions.push(this.completionManager.disposable()); + await this.configuration.updatePathToRacer(); + await this.missingToolsInstallator.addStatusBarItemIfSomeToolsAreMissing(); + await this.completionManager.initialStart(); + } + + private constructor( + context: ExtensionContext, + configuration: Configuration, + currentWorkingDirectoryManager: CurrentWorkingDirectoryManager, + logger: ChildLogger, + formattingManager: FormattingManager | undefined ) { this.context = context; - this.configuration = configuration; - this.completionManager = new CompletionManager( context, configuration, logger.createChildLogger('CompletionManager: ') ); - - this.formattingManager = new FormattingManager(context, configuration); - + this.formattingManager = formattingManager; this.workspaceSymbolProvisionManager = new WorkspaceSymbolProvisionManager( context, configuration, currentWorkingDirectoryManager ); - - this.documentSymbolProvisionManager = new DocumentSymbolProvisionManager( - context, - configuration - ); - + this.documentSymbolProvisionManager = new DocumentSymbolProvisionManager(context, configuration); this.missingToolsInstallator = new MissingToolsInstallator( context, configuration, logger.createChildLogger('MissingToolsInstallator: ') ); } - - public async start(): Promise { - this.context.subscriptions.push(this.completionManager.disposable()); - - await this.configuration.updatePathToRacer(); - - await this.missingToolsInstallator.addStatusBarItemIfSomeToolsAreMissing(); - - await this.completionManager.initialStart(); - } }