This repository has been archived by the owner on Oct 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(imports): Add organize on save via workspace hook (#291)
Closes #150. Adds a hook to actually organize the imports on the save event of a document. Needs editor.formatOnSave setting to be active as well.
- Loading branch information
Showing
9 changed files
with
214 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/extension/extensions/OrganizeImportsOnSaveExtension.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { inject, injectable } from 'inversify'; | ||
import { ExtensionContext, workspace } from 'vscode'; | ||
|
||
import { ExtensionConfig } from '../../common/config'; | ||
import { Logger, LoggerFactory } from '../../common/utilities'; | ||
import { iocSymbols } from '../IoCSymbols'; | ||
import { ImportManager } from '../managers'; | ||
import { BaseExtension } from './BaseExtension'; | ||
|
||
/** | ||
* Extension that does sort and organize the imports as soon as a document will be saved. | ||
* | ||
* @export | ||
* @class OrganizeImportsOnSaveExtension | ||
* @extends {BaseExtension} | ||
*/ | ||
@injectable() | ||
export class OrganizeImportsOnSaveExtension extends BaseExtension { | ||
private logger: Logger; | ||
|
||
constructor( | ||
@inject(iocSymbols.extensionContext) context: ExtensionContext, | ||
@inject(iocSymbols.loggerFactory) loggerFactory: LoggerFactory, | ||
@inject(iocSymbols.configuration) private config: ExtensionConfig, | ||
) { | ||
super(context); | ||
this.logger = loggerFactory('OrganizeImportsOnSaveExtension'); | ||
} | ||
|
||
/** | ||
* Initialized the extension. Registers the commands and other disposables to the context. | ||
* | ||
* @memberof OrganizeImportsOnSaveExtension | ||
*/ | ||
public initialize(): void { | ||
this.context.subscriptions.push(workspace.onWillSaveTextDocument((event) => { | ||
if (!this.config.resolver.organizeOnSave) { | ||
this.logger.info('Organize on save is deactivated through config.'); | ||
return; | ||
} | ||
|
||
this.logger.info(`Organize on save for document "${event.document.fileName}".`); | ||
event.waitUntil( | ||
ImportManager | ||
.create(event.document) | ||
.then(manager => manager.organizeImports().calculateTextEdits()), | ||
); | ||
})); | ||
|
||
this.logger.info('Initialized'); | ||
} | ||
|
||
/** | ||
* Disposes the extension. | ||
* | ||
* @memberof OrganizeImportsOnSaveExtension | ||
*/ | ||
public dispose(): void { | ||
this.logger.info('Disposed'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Oops, something went wrong.
46ebeb9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@buehler This is a nice feature but it should not be
true
by default in my opinion. It took me quite a bit of time understanding why my prettier settings were being overridden.46ebeb9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cbrevik fair point. Gonna change that in a future release.
46ebeb9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the sorting feature. But is there some way to turn off
organizeImports
removing unused imports? At the moment it is also removingReact
imports from my React Native-app. Which breaks rendering since it is a required import.(Optionally whitelisting imports?)
46ebeb9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea there is an option (setting) to whitelist certain imports so that they aren't removed:
typescriptHero.resolver.ignoreImportsForOrganize
46ebeb9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Seems like that feature is bugged though, it ignores:
But it does not ignore:
Which becomes:
I can open an issue?
46ebeb9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Of course you can open an issue ;-)
import React, { Component } from "react"
this syntax does not work though. You can write it as:import { default as React, Component } from "react"
which should work.Thats a bug in the syntax ;-)
in TypeScript, the following syntax works and is not removed:
import * as React from 'react';
46ebeb9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is allowed syntax if
"allowSyntheticDefaultImports": true
is set intsconfig.json
:)