编辑器edit模块分析
# 默认编辑
# 右击格式化
# 注册
core/packages/editor/src/browser/editor.contribution.ts
registerCommands(commands: CommandRegistry): void {
//....
commands.registerCommand(EDITOR_COMMANDS.FORMAT_DOCUMENT_WITH, {
execute: async () => {
const formatService = this.injector.get(DocumentFormatService);
formatService.formatDocumentWith();
},
});
commands.registerCommand(EDITOR_COMMANDS.FORMAT_SELECTION_WITH, {
execute: async () => {
const formatService = this.injector.get(DocumentFormatService);
formatService.formatSelectionWith();
},
});
}
registerMenus(menus: IMenuRegistry) {
menus.registerMenuItem(MenuId.EditorTitleContext, {
command: EDITOR_COMMANDS.COPY_PATH.id,
group: '10_path',
order: 1,
});
menus.registerMenuItem(MenuId.EditorTitleContext, {
command: EDITOR_COMMANDS.COPY_RELATIVE_PATH.id,
group: '10_path',
order: 2,
});
//.....
menus.registerMenuItem(MenuId.EditorContext, {
command: EDITOR_COMMANDS.FORMAT_DOCUMENT_WITH.id,
group: '1_modification',
order: 1.3,
when: ContextKeyExpr.and(EditorContextKeys.writable, EditorContextKeys.hasMultipleDocumentFormattingProvider),
});
menus.registerMenuItem(MenuId.EditorContext, {
command: EDITOR_COMMANDS.FORMAT_SELECTION_WITH.id,
when: ContextKeyExpr.and(
EditorContextKeys.writable,
EditorContextKeys.hasMultipleDocumentSelectionFormattingProvider,
EditorContextKeys.hasNonEmptySelection,
),
group: '1_modification',
order: 1.31,
});
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 实现
# formatDocumentWith
core/packages/editor/src/browser/format/format.service.ts
async formatDocumentWith() {
const model = this.workbenchEditorService.currentEditor?.monacoEditor.getModel();
if (model) {
const formatterProviders = getRealAndSyntheticDocumentFormattersOrdered(
languageFeaturesService.documentFormattingEditProvider,
languageFeaturesService.documentRangeFormattingEditProvider,
model,
);
const selector = this.injector.get(FormattingSelector);
const formatter = await selector.select(formatterProviders, model, FormattingMode.Explicit, true);
if (formatter) {
try {
const edits = await (formatter as DocumentFormattingEditProvider).provideDocumentFormattingEdits(
model,
model.getFormattingOptions(),
CancellationToken.None,
);
if (edits) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
FormattingEdit.execute(this.workbenchEditorService.currentEditor?.monacoEditor!, edits, true);
}
} catch (err) {
this.logger.error('execute format document with error', err);
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# formatSelectionWith
core/packages/editor/src/browser/format/format.service.ts
async formatSelectionWith() {
if (!this.workbenchEditorService.currentEditor?.monacoEditor.hasModel()) {
return;
}
const model = this.workbenchEditorService.currentEditor?.monacoEditor.getModel();
if (model) {
let range: Range | null | undefined = this.workbenchEditorService.currentEditor?.monacoEditor.getSelection();
if (range?.isEmpty()) {
range = new Range(
range.startLineNumber,
1,
range.startLineNumber,
model.getLineMaxColumn(range.startLineNumber),
);
}
const formatterProviders = languageFeaturesService.documentRangeFormattingEditProvider.ordered(model);
const selector = this.injector.get(FormattingSelector);
const formatter = await selector.select(formatterProviders, model, FormattingMode.Explicit, true);
if (formatter) {
try {
const edits = await (formatter as DocumentRangeFormattingEditProvider).provideDocumentRangeFormattingEdits(
model,
range!,
model.getFormattingOptions(),
CancellationToken.None,
);
if (edits) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
FormattingEdit.execute(this.workbenchEditorService.currentEditor?.monacoEditor!, edits, true);
}
} catch (err) {
this.logger.error('execute format document with error', err);
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# selector.select
core/packages/editor/src/browser/format/formatterSelect.ts
async select(
formatters: Array<
monaco.languages.DocumentFormattingEditProvider | monaco.languages.DocumentRangeFormattingEditProvider
>,
document: ITextModel,
mode: FormattingMode,
forceSelect = false,
): Promise<
monaco.languages.DocumentFormattingEditProvider | monaco.languages.DocumentRangeFormattingEditProvider | undefined
> {
const docRef = this.modelService.getModelReference(URI.from(document.uri.toJSON()));
if (!docRef) {
return;
}
const languageId = docRef.instance.languageId;
docRef.dispose();
let preferred;
if (!forceSelect) {
preferred = (this.preferenceService.get<{ [key: string]: string }>('editor.preferredFormatter') || {})[
languageId
];
}
const elements: { [key: string]: IProvider } = {};
formatters.forEach((provider: IProvider) => {
if (provider.extensionId) {
elements[provider.extensionId] = provider;
}
});
if (preferred && !forceSelect) {
const idx = formatters.findIndex((provider: IProvider) => provider.extensionId === preferred);
if (idx >= 0) {
return formatters[idx];
}
} else if (formatters.length < 2 && !forceSelect) {
return formatters[0];
}
if (mode === FormattingMode.Explicit) {
const selected = await this.quickPickService.show(
Object.keys(elements).map((k) => ({
label: elements[k].displayName!,
value: elements[k].extensionId,
})),
{ placeholder: localize('editor.format.chooseFormatter') },
);
if (selected) {
const config = this.preferenceService.get<{ [key: string]: string }>('editor.preferredFormatter') || {};
this.preferenceService.set(
'editor.preferredFormatter',
{ ...config, [languageId]: selected },
PreferenceScope.User,
);
return elements[selected];
}
} else {
return undefined;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 问题
目前发现的问题:
格式化文档功能是ide插件实现的;然后下的使用 ...格式化文档才是ide内部实现的;
'editor.formatDocument.label.multiple': '使用...格式化文档',
'editor.formatSelection.label.multiple': '格式化选定内容的方式...',
1
2
2
上次更新: 2023/11/17, 05:08:20