Skip to content
This repository has been archived by the owner on Dec 8, 2020. It is now read-only.

Commit

Permalink
Formatting is not available if rustfmt is not installed (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
KalitaAlexey authored Apr 27, 2017
1 parent 00c81c3 commit 706c731
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 41 deletions.
34 changes: 24 additions & 10 deletions src/components/formatting/formatting_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<FormattingManager | undefined> {
const rustfmtPath: string | undefined = await FileSystem.findExecutablePath(configuration.getRustfmtPath());
if (rustfmtPath === undefined) {
return undefined;
}
return new FormattingManager(context, configuration);
}

public provideDocumentFormattingEdits(document: TextDocument): Thenable<TextEdit[]> {
return this.formattingEdits(document);
}

public provideDocumentRangeFormattingEdits(document: TextDocument, range: Range): Thenable<TextEdit[]> {
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(),
Expand All @@ -46,14 +68,6 @@ export default class FormattingManager implements DocumentFormattingEditProvider
);
}

public provideDocumentFormattingEdits(document: TextDocument): Thenable<TextEdit[]> {
return this.formattingEdits(document);
}

public provideDocumentRangeFormattingEdits(document: TextDocument, range: Range): Thenable<TextEdit[]> {
return this.formattingEdits(document, range);
}

private formattingEdits(document: TextDocument, range?: Range): Thenable<TextEdit[]> {
return new Promise((resolve, reject) => {
const fileName = document.fileName + '.fmt';
Expand Down
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
52 changes: 22 additions & 30 deletions src/legacy_mode_manager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ExtensionContext } from 'vscode';

import { Configuration } from './components/configuration/Configuration';

import CurrentWorkingDirectoryManager
Expand All @@ -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<LegacyModeManager> {
const formattingManager: FormattingManager | undefined = await FormattingManager.create(context, configuration);
return new LegacyModeManager(context, configuration, currentWorkingDirectoryManager, logger, formattingManager);
}

public async start(): Promise<void> {
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<void> {
this.context.subscriptions.push(this.completionManager.disposable());

await this.configuration.updatePathToRacer();

await this.missingToolsInstallator.addStatusBarItemIfSomeToolsAreMissing();

await this.completionManager.initialStart();
}
}

0 comments on commit 706c731

Please sign in to comment.