|
1 | | -//// Copyright 2013-2014 François de Campredon |
2 | | -//// |
3 | | -//// Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | -//// you may not use this file except in compliance with the License. |
5 | | -//// You may obtain a copy of the License at |
6 | | -//// |
7 | | -//// http://www.apache.org/licenses/LICENSE-2.0 |
8 | | -//// |
9 | | -//// Unless required by applicable law or agreed to in writing, software |
10 | | -//// distributed under the License is distributed on an "AS IS" BASIS, |
11 | | -//// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | -//// See the License for the specific language governing permissions and |
13 | | -//// limitations under the License. |
| 1 | +// Copyright 2013-2014 François de Campredon |
14 | 2 | // |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
15 | 6 | // |
16 | | -//'use strict'; |
17 | | -// |
18 | | -//import ServiceConsumer = require('./serviceConsumer'); |
19 | | -//import IFormatingService = require('../commons/formattingService'); |
20 | | -// |
21 | | -//var EditorManager = brackets.getModule('editor/EditorManager'), |
22 | | -// Editor = brackets.getModule('editor/Editor').Editor; |
23 | | -// |
24 | | -// |
25 | | -//class FormattingManager extends ServiceConsumer<IFormatingService> { |
26 | | -// format = () => { |
27 | | -// var editor = EditorManager.getCurrentFullEditor(); |
28 | | -// if (!editor) { |
29 | | -// return; |
30 | | -// } |
31 | | -// var useTabs = Editor.getUseTabChar(); |
32 | | -// |
33 | | -// var options: TypeScript.Services.FormatCodeOptions = { |
34 | | -// IndentSize: Editor.getSpaceUnits(), |
35 | | -// TabSize: Editor.getTabSize(), |
36 | | -// NewLineCharacter: '\n', |
37 | | -// ConvertTabsToSpaces: !useTabs, |
38 | | -// InsertSpaceAfterSemicolonInForStatements: true, |
39 | | -// InsertSpaceAfterCommaDelimiter: true, |
40 | | -// InsertSpaceBeforeAndAfterBinaryOperators: true, |
41 | | -// InsertSpaceAfterKeywordsInControlFlowStatements: true, |
42 | | -// InsertSpaceAfterFunctionKeywordForAnonymousFunctions: true, |
43 | | -// InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: false, |
44 | | -// PlaceOpenBraceOnNewLineForFunctions: false, |
45 | | -// PlaceOpenBraceOnNewLineForControlBlocks: false |
46 | | -// }; |
47 | | -// var currentRange = editor.getSelection(true), |
48 | | -// startPos = currentRange ? currentRange.start : undefined, |
49 | | -// endPos = currentRange ? currentRange.end : undefined; |
50 | | -// |
51 | | -// if (startPos && endPos && startPos.line === endPos.line && startPos.ch === endPos.ch) { |
52 | | -// startPos = endPos = undefined; |
53 | | -// } |
54 | | -// |
55 | | -// |
56 | | -// this.getService().then(service => { |
57 | | -// service.getFormatingForFile(editor.document.file.fullPath, options, startPos, endPos).then(textEdits => { |
58 | | -// if (EditorManager.getCurrentFullEditor() !== editor) { |
59 | | -// return; |
60 | | -// } |
61 | | -// editor.document.setText( |
62 | | -// textEdits.reduce((text: string, edit: TypeScript.Services.TextEdit) => { |
63 | | -// return text.substr(0, edit.minChar) + edit.text + text.substr(edit.limChar); |
64 | | -// }, editor.document.getText()) |
65 | | -// ); |
66 | | -// }); |
67 | | -// }); |
68 | | -// }; |
69 | | -// |
70 | | -// static FORMAT_COMMAND_ID = 'fdecampred.brackets-typescript.format'; |
71 | | -// |
72 | | -// static FORMAT_LABEL = 'Format'; |
73 | | -//} |
74 | | -// |
75 | | -//export = FormattingManager; |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
76 | 8 | // |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +'use strict'; |
| 17 | + |
| 18 | +import ServiceConsumer = require('./serviceConsumer'); |
| 19 | +import ts = require('typescript'); |
| 20 | + |
| 21 | +var EditorManager = brackets.getModule('editor/EditorManager'), |
| 22 | + Editor = brackets.getModule('editor/Editor').Editor; |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +export function format() { |
| 27 | + var editor = EditorManager.getCurrentFullEditor(); |
| 28 | + if (!editor) { |
| 29 | + return; |
| 30 | + } |
| 31 | + var useTabs = Editor.getUseTabChar(); |
| 32 | + |
| 33 | + var options: ts.FormatCodeOptions = { |
| 34 | + InsertSpaceAfterCommaDelimiter: true, |
| 35 | + InsertSpaceAfterSemicolonInForStatements: true, |
| 36 | + InsertSpaceBeforeAndAfterBinaryOperators: true, |
| 37 | + InsertSpaceAfterKeywordsInControlFlowStatements: true, |
| 38 | + InsertSpaceAfterFunctionKeywordForAnonymousFunctions: true, |
| 39 | + InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: false, |
| 40 | + PlaceOpenBraceOnNewLineForFunctions: false, |
| 41 | + PlaceOpenBraceOnNewLineForControlBlocks: false, |
| 42 | + |
| 43 | + IndentSize: Editor.getSpaceUnits(), |
| 44 | + TabSize: Editor.getTabSize(), |
| 45 | + NewLineCharacter: '\n', |
| 46 | + ConvertTabsToSpaces: !Editor.getUseTabChar() |
| 47 | + }; |
| 48 | + var currentRange = editor.getSelection(true), |
| 49 | + startPos = currentRange ? currentRange.start : undefined, |
| 50 | + endPos = currentRange ? currentRange.end : undefined; |
| 51 | + |
| 52 | + if (startPos && endPos && startPos.line === endPos.line && startPos.ch === endPos.ch) { |
| 53 | + startPos = endPos = undefined; |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + ServiceConsumer.getService().then(service => { |
| 58 | + service.getFormatingForFile(editor.document.file.fullPath, options, startPos, endPos).then(textEdits => { |
| 59 | + if (EditorManager.getCurrentFullEditor() !== editor) { |
| 60 | + return; |
| 61 | + } |
| 62 | + editor.document.setText( |
| 63 | + textEdits.reduce((text, edit) => { |
| 64 | + return text.substr(0, edit.start) + edit.newText + text.substr(edit.end); |
| 65 | + }, editor.document.getText()) |
| 66 | + ); |
| 67 | + }); |
| 68 | + }); |
| 69 | +}; |
| 70 | + |
| 71 | +export var FORMAT_COMMAND_ID = 'fdecampred.brackets-typescript.format'; |
| 72 | + |
| 73 | +export var FORMAT_LABEL = 'Format'; |
0 commit comments