Skip to content

Commit 70d66b3

Browse files
committed
reenable formatter
1 parent 86c5d2d commit 70d66b3

File tree

3 files changed

+78
-83
lines changed

3 files changed

+78
-83
lines changed

src/declarations/typescript-project-services.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ declare module 'typescript-project-services' {
402402

403403
export function dispose():void;
404404

405-
405+
export function updateProjectConfigs(configs: { [projectId: string]: TypeScriptProjectConfig; }): Promise<void>;
406406
/**
407407
* Represent definition info of a symbol
408408
*/
@@ -478,8 +478,8 @@ declare module 'typescript-project-services' {
478478

479479

480480
export interface TextEdit {
481-
pos: Position;
482-
endPos: Position;
481+
start: number;
482+
end: number;
483483
newText: string;
484484
}
485485

src/main/formattingManager.ts

Lines changed: 70 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,73 @@
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
142
//
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
156
//
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
768
//
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';

src/main/index.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import inlineEditorProvider = require('./inlineEditorProvider');
2424
import jumpToDefProvider = require('./jumpToDefProvider');
2525
//import TypeScriptQuickFindDefitionProvider = require('./quickFindDefinition');
2626
import CodeHintProvider = require('./codeHintProvider');
27-
//import FormattingManager = require('./formattingManager');
27+
import FormattingManager = require('./formattingManager');
2828
import typeScriptModeFactory = require('./mode');
2929
import ServiceConsumer = require('./serviceConsumer');
3030

@@ -52,7 +52,6 @@ var LanguageManager = brackets.getModule('language/LanguageManager'),
5252

5353
// ,
5454
// quickFindDefinitionProvider: TypeScriptQuickFindDefitionProvider,
55-
// formattingManager: FormattingManager;
5655
//
5756

5857

@@ -93,11 +92,10 @@ function init(config: { logLevel: string; typeScriptLocation: string; workerLoca
9392
// QuickOpen.addQuickOpenPlugin(quickFindDefinitionProvider);
9493

9594
//Register formatting command
96-
// formattingManager = new FormattingManager();
97-
// CommandManager.register(FormattingManager.FORMAT_LABEL, FormattingManager.FORMAT_COMMAND_ID, formattingManager.format);
98-
// var contextMenu = Menus.getContextMenu(Menus.ContextMenuIds.EDITOR_MENU);
99-
// contextMenu.addMenuItem(FormattingManager.FORMAT_COMMAND_ID);
100-
//
95+
CommandManager.register(FormattingManager.FORMAT_LABEL, FormattingManager.FORMAT_COMMAND_ID, FormattingManager.format);
96+
var contextMenu = Menus.getContextMenu(Menus.ContextMenuIds.EDITOR_MENU);
97+
contextMenu.addMenuItem(FormattingManager.FORMAT_COMMAND_ID);
98+
10199
initServices(config.workerLocation, config.typeScriptLocation, config.logLevel);
102100

103101
$(ProjectManager).on('beforeProjectClose beforeAppClose', disposeServices);

0 commit comments

Comments
 (0)