diff --git a/README.md b/README.md index a4928d6ce..b5454ea51 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ JavaScript developers can now just open a `.ts` file and start hacking away like * Project Build Support * `package.json` Support * React Support -* Format code +* Format code (configurable to be on save) * Goto Declaration * Find References * Block comment and uncomment @@ -107,6 +107,8 @@ Covered here : http://basarat.gitbooks.io/typescript/content/docs/jsx/tsx.html ## Format Code Shortcut : `ctrl+alt+l` or `cmd+alt+l`. Will format just the selection if you have something selected otherwise it will format the entire file. +Format on save is covered [here](https://github.com/TypeStrong/atom-typescript/blob/master/docs/tsconfig.md#formatOnSave) + ## Go to Declaration Shortcut : `F12`. Will open the *first* declaration of the said item for now. (Note: some people call it Go to Definition) diff --git a/dist/main/atom/onSaveHandler.js b/dist/main/atom/onSaveHandler.js index 0fb427d5d..32b255b9b 100644 --- a/dist/main/atom/onSaveHandler.js +++ b/dist/main/atom/onSaveHandler.js @@ -27,6 +27,9 @@ function handle(event) { if (fileDetails.project.buildOnSave) { atom.commands.dispatch(atom.views.getView(event.editor), 'typescript:build'); } + if (fileDetails.project.atom.formatOnSave) { + atom.commands.dispatch(atom.views.getView(event.editor), 'typescript:format-code'); + } }); } exports.handle = handle; diff --git a/dist/main/tsconfig/tsconfig.js b/dist/main/tsconfig/tsconfig.js index 5c02e707b..4009ac86a 100644 --- a/dist/main/tsconfig/tsconfig.js +++ b/dist/main/tsconfig/tsconfig.js @@ -181,7 +181,7 @@ function getDefaultInMemoryProject(srcFile) { compileOnSave: true, buildOnSave: false, scripts: {}, - atom: { rewriteTsconfig: true }, + atom: { rewriteTsconfig: true, formatOnSave: false }, }; return { projectFileDirectory: dir, @@ -253,7 +253,7 @@ function getProjectSync(pathOrSrcFile) { externalTranspiler: projectSpec.externalTranspiler == undefined ? undefined : projectSpec.externalTranspiler, scripts: projectSpec.scripts || {}, buildOnSave: !!projectSpec.buildOnSave, - atom: { rewriteTsconfig: true } + atom: { rewriteTsconfig: true, formatOnSave: !!projectSpec.atom.formatOnSave } }; var validationResult = validator.validate(projectSpec.compilerOptions); if (validationResult.errorMessage) { diff --git a/docs/tsconfig.md b/docs/tsconfig.md index 50e443e33..0e5af2ab7 100644 --- a/docs/tsconfig.md +++ b/docs/tsconfig.md @@ -20,8 +20,9 @@ i.e. an empty JSON file at the *root* of your project :heart: This will be suffi * [`compileOnSave`](https://github.com/TypeStrong/atom-typescript/blob/master/docs/tsconfig.md#compileonsave) : Should AtomTS compile on save * [`buildOnSave`](https://github.com/TypeStrong/atom-typescript/blob/master/docs/tsconfig.md#buildonsave) : Should AtomTS build on save * [`scripts`](https://github.com/TypeStrong/atom-typescript/blob/master/docs/tsconfig.md#scripts) : Sometimes its useful to have post build scripts -* [`atom`](https://github.com/TypeStrong/atom-typescript/blob/master/docs/tsconfig.md#rewriteTsconfig) : Configuration specific to Atom. Currently -only contains `rewriteTsconfig` which prevents Atom from rewriting a project's `tsconfig.json`. +* [`atom`](https://github.com/TypeStrong/atom-typescript/blob/master/docs/tsconfig.md#atom) : Configuration specific to Atom. + * [`rewriteTsconfig`](https://github.com/TypeStrong/atom-typescript/blob/master/docs/tsconfig.md#rewriteTsconfig) which prevents Atom from rewriting a project's `tsconfig.json` + * [`formatOnSave`](https://github.com/TypeStrong/atom-typescript/blob/master/docs/tsconfig.md#formatOnSave) which will format the Typescript file on save. ## Examples @@ -134,5 +135,17 @@ to `false` (this defaults to `true`). } ``` +**formatOnSave** + +Setting this to `true` will format the entire file exactly like `ctrl+alt+l` or `cmd+alt+l` does on save. (this defaults to `false`) + +```json +{ + "atom": { + "formatOnSave": true + } +} +``` + ## Additional Notes FWIW [a json schema is also available](http://json.schemastore.org/tsconfig) diff --git a/lib/main/atom/onSaveHandler.ts b/lib/main/atom/onSaveHandler.ts index c9b6b7909..cb39676e8 100644 --- a/lib/main/atom/onSaveHandler.ts +++ b/lib/main/atom/onSaveHandler.ts @@ -53,5 +53,12 @@ export function handle(event: { filePath: string; editor: AtomCore.IEditor }) { 'typescript:build'); } + if (fileDetails.project.atom.formatOnSave) { + // Trigger a format + atom.commands.dispatch( + atom.views.getView(event.editor), + 'typescript:format-code'); + } + }); } diff --git a/lib/main/tsconfig/tsconfig.ts b/lib/main/tsconfig/tsconfig.ts index ab1baebbd..abf5e4d3a 100644 --- a/lib/main/tsconfig/tsconfig.ts +++ b/lib/main/tsconfig/tsconfig.ts @@ -139,7 +139,7 @@ interface TypeScriptProjectRawSpecification { buildOnSave?: boolean; externalTranspiler?: string | { name: string; options?: any }; scripts?: { postbuild?: string }; - atom?: { rewriteTsconfig?: boolean }; + atom?: { rewriteTsconfig?: boolean, formatOnSave?: boolean }; } /** @@ -157,7 +157,7 @@ export interface TypeScriptProjectSpecification { package?: UsefulFromPackageJson; externalTranspiler?: string | { name: string; options?: any }; scripts: { postbuild?: string }; - atom: { rewriteTsconfig: boolean }; + atom: { rewriteTsconfig: boolean, formatOnSave: boolean }; } ///////// FOR USE WITH THE API ///////////// @@ -290,7 +290,7 @@ function mixin(target: any, source: any): any { function rawToTsCompilerOptions(jsonOptions: CompilerOptions, projectDir: string): ts.CompilerOptions { // Cannot use Object.create because the compiler checks hasOwnProperty - var compilerOptions = mixin({}, defaults); + var compilerOptions = mixin({}, defaults); for (var key in jsonOptions) { if (typescriptEnumMap[key]) { compilerOptions[key] = typescriptEnumMap[key][jsonOptions[key].toLowerCase()]; @@ -322,7 +322,7 @@ function rawToTsCompilerOptions(jsonOptions: CompilerOptions, projectDir: string function tsToRawCompilerOptions(compilerOptions: ts.CompilerOptions): CompilerOptions { // Cannot use Object.create because JSON.stringify will only serialize own properties - var jsonOptions = mixin({}, compilerOptions); + var jsonOptions = mixin({}, compilerOptions); Object.keys(compilerOptions).forEach((key) => { if (jsonEnumMap[key] && compilerOptions[key]) { @@ -350,7 +350,7 @@ export function getDefaultInMemoryProject(srcFile: string): TypeScriptProjectFil compileOnSave: true, buildOnSave: false, scripts: {}, - atom: { rewriteTsconfig: true }, + atom: { rewriteTsconfig: true, formatOnSave: false }, }; return { @@ -375,8 +375,8 @@ export function getProjectSync(pathOrSrcFile: string): TypeScriptProjectFileDeta var projectFile = tsconfig.resolveSync(dir); if (!projectFile) { - throw errorWithDetails( - new Error(errors.GET_PROJECT_NO_PROJECT_FOUND), { projectFilePath: fsu.consistentPath(pathOrSrcFile), errorMessage: 'not found' }); + throw errorWithDetails( + new Error(errors.GET_PROJECT_NO_PROJECT_FOUND), { projectFilePath: fsu.consistentPath(pathOrSrcFile), errorMessage: 'not found' }); } var projectFileDirectory = path.dirname(projectFile) + path.sep; @@ -442,7 +442,7 @@ export function getProjectSync(pathOrSrcFile: string): TypeScriptProjectFileDeta externalTranspiler: projectSpec.externalTranspiler == undefined ? undefined : projectSpec.externalTranspiler, scripts: projectSpec.scripts || {}, buildOnSave: !!projectSpec.buildOnSave, - atom: { rewriteTsconfig: true } + atom: { rewriteTsconfig: true, formatOnSave: !!projectSpec.atom.formatOnSave } }; // Validate the raw compiler options before converting them to TS compiler options @@ -521,7 +521,7 @@ function increaseProjectForReferenceAndImports(files: string[]): string[] { } } - var getReferencedOrImportedFiles = (files: string[]): string[]=> { + var getReferencedOrImportedFiles = (files: string[]): string[] => { var referenced: string[][] = []; files.forEach(file => { @@ -553,7 +553,7 @@ function increaseProjectForReferenceAndImports(files: string[]): string[] { return file; } return getIfExists(file); - }).filter(file=> !!file) + }).filter(file => !!file) .concat( preProcessedFileInfo.importedFiles .filter((fileReference) => pathIsRelative(fileReference.fileName)) @@ -564,7 +564,7 @@ function increaseProjectForReferenceAndImports(files: string[]): string[] { file = getIfExists(`${file}/index`); } return file; - }).filter(file=> !!file) + }).filter(file => !!file) ) ); }); @@ -610,9 +610,9 @@ function getDefinitionsForNodeModules(projectDir: string, files: string[]): { ou // Find our `typings` (anything in a typings folder with extension `.d.ts` is considered a typing) // These are INF powerful var ourTypings = files - .filter(f=> path.basename(path.dirname(f)) == 'typings' && endsWith(f, '.d.ts') + .filter(f => path.basename(path.dirname(f)) == 'typings' && endsWith(f, '.d.ts') || path.basename(path.dirname(path.dirname(f))) == 'typings' && endsWith(f, '.d.ts')); - ourTypings.forEach(f=> typings[path.basename(f)] = { filePath: f, version: Infinity }); + ourTypings.forEach(f => typings[path.basename(f)] = { filePath: f, version: Infinity }); var existing = createMap(files.map(fsu.consistentPath)); function addAllReferencedFilesWithMaxVersion(file: string) { @@ -635,7 +635,7 @@ function getDefinitionsForNodeModules(projectDir: string, files: string[]): { ou if (fs.existsSync(file + '.d.ts')) { return file + '.d.ts'; } - }).filter(f=> !!f); + }).filter(f => !!f); // Only ones we don't have by name yet // TODO: replace INF with an actual version @@ -644,7 +644,7 @@ function getDefinitionsForNodeModules(projectDir: string, files: string[]): { ou // Add these files.forEach(f => typings[path.basename(f)] = { filePath: f, version: Infinity }); // Keep expanding - files.forEach(f=> addAllReferencedFilesWithMaxVersion(f)); + files.forEach(f => addAllReferencedFilesWithMaxVersion(f)); } // Keep going up till we find node_modules @@ -691,11 +691,11 @@ function getDefinitionsForNodeModules(projectDir: string, files: string[]): { ou var all = Object.keys(typings) .map(typing => typings[typing].filePath) - .map(x=> fsu.consistentPath(x)); + .map(x => fsu.consistentPath(x)); var implicit = all - .filter(x=> !existing[x]); + .filter(x => !existing[x]); var ours = all - .filter(x=> existing[x]); + .filter(x => existing[x]); return { implicit, ours, packagejson }; } diff --git a/lib/tsconfig.json b/lib/tsconfig.json index bc01d87bd..bf8d7a401 100644 --- a/lib/tsconfig.json +++ b/lib/tsconfig.json @@ -134,6 +134,7 @@ ], "exclude": [], "atom": { - "rewriteTsconfig": true + "rewriteTsconfig": true, + "formatOnSave": false } }