Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/comments/browser/simpleCommentEditor.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { EditorOption, IEditorOptions } from '../../../../editor/common/config/editorOptions.js';
7
import { EditorAction, EditorContributionInstantiation, EditorExtensionsRegistry, IEditorContributionDescription } from '../../../../editor/browser/editorExtensions.js';
8
import { ICodeEditorService } from '../../../../editor/browser/services/codeEditorService.js';
9
import { CodeEditorWidget, ICodeEditorWidgetOptions } from '../../../../editor/browser/widget/codeEditor/codeEditorWidget.js';
10
import { IContextKeyService, RawContextKey, IContextKey } from '../../../../platform/contextkey/common/contextkey.js';
11
import { IInstantiationService } from '../../../../platform/instantiation/common/instantiation.js';
12
import { ICommandService } from '../../../../platform/commands/common/commands.js';
13
14
// Allowed Editor Contributions:
15
import { MenuPreventer } from '../../codeEditor/browser/menuPreventer.js';
16
import { EditorDictation } from '../../codeEditor/browser/dictation/editorDictation.js';
17
import { ContextMenuController } from '../../../../editor/contrib/contextmenu/browser/contextmenu.js';
18
import { SuggestController } from '../../../../editor/contrib/suggest/browser/suggestController.js';
19
import { SnippetController2 } from '../../../../editor/contrib/snippet/browser/snippetController2.js';
20
import { TabCompletionController } from '../../snippets/browser/tabCompletion.js';
21
import { IThemeService } from '../../../../platform/theme/common/themeService.js';
22
import { INotificationService } from '../../../../platform/notification/common/notification.js';
23
import { IAccessibilityService } from '../../../../platform/accessibility/common/accessibility.js';
24
import { ICommentThreadWidget } from '../common/commentThreadWidget.js';
25
import { CommentContextKeys } from '../common/commentContextKeys.js';
26
import { ILanguageConfigurationService } from '../../../../editor/common/languages/languageConfigurationRegistry.js';
27
import { ILanguageFeaturesService } from '../../../../editor/common/services/languageFeatures.js';
28
import { IConfigurationService } from '../../../../platform/configuration/common/configuration.js';
29
import { ICodeEditor } from '../../../../editor/browser/editorBrowser.js';
30
import { clamp } from '../../../../base/common/numbers.js';
31
import { CopyPasteController } from '../../../../editor/contrib/dropOrPasteInto/browser/copyPasteController.js';
32
import { CodeActionController } from '../../../../editor/contrib/codeAction/browser/codeActionController.js';
33
import { DropIntoEditorController } from '../../../../editor/contrib/dropOrPasteInto/browser/dropIntoEditorController.js';
34
import { InlineCompletionsController } from '../../../../editor/contrib/inlineCompletions/browser/controller/inlineCompletionsController.js';
35
import { LinkDetector } from '../../../../editor/contrib/links/browser/links.js';
36
import { MessageController } from '../../../../editor/contrib/message/browser/messageController.js';
37
import { SelectionClipboardContributionID } from '../../codeEditor/browser/selectionClipboard.js';
38
import { MenuId } from '../../../../platform/actions/common/actions.js';
39
import { ContentHoverController } from '../../../../editor/contrib/hover/browser/contentHoverController.js';
40
import { GlyphHoverController } from '../../../../editor/contrib/hover/browser/glyphHoverController.js';
41
import { PlaceholderTextContribution } from '../../../../editor/contrib/placeholderText/browser/placeholderTextContribution.js';
42
43
export const ctxCommentEditorFocused = new RawContextKey<boolean>('commentEditorFocused', false);
44
export const MIN_EDITOR_HEIGHT = 5 * 18;
45
export const MAX_EDITOR_HEIGHT = 25 * 18;
46
47
export interface LayoutableEditor {
48
getLayoutInfo(): { height: number };
49
}
50
51
export class SimpleCommentEditor extends CodeEditorWidget {
52
private _parentThread: ICommentThreadWidget;
53
private _commentEditorFocused: IContextKey<boolean>;
54
private _commentEditorEmpty: IContextKey<boolean>;
55
56
constructor(
57
domElement: HTMLElement,
58
options: IEditorOptions,
59
scopedContextKeyService: IContextKeyService,
60
parentThread: ICommentThreadWidget,
61
@IInstantiationService instantiationService: IInstantiationService,
62
@ICodeEditorService codeEditorService: ICodeEditorService,
63
@ICommandService commandService: ICommandService,
64
@IThemeService themeService: IThemeService,
65
@INotificationService notificationService: INotificationService,
66
@IAccessibilityService accessibilityService: IAccessibilityService,
67
@ILanguageConfigurationService languageConfigurationService: ILanguageConfigurationService,
68
@ILanguageFeaturesService languageFeaturesService: ILanguageFeaturesService,
69
) {
70
const codeEditorWidgetOptions: ICodeEditorWidgetOptions = {
71
contributions: <IEditorContributionDescription[]>[
72
{ id: MenuPreventer.ID, ctor: MenuPreventer, instantiation: EditorContributionInstantiation.BeforeFirstInteraction },
73
{ id: ContextMenuController.ID, ctor: ContextMenuController, instantiation: EditorContributionInstantiation.BeforeFirstInteraction },
74
{ id: SuggestController.ID, ctor: SuggestController, instantiation: EditorContributionInstantiation.Eager },
75
{ id: SnippetController2.ID, ctor: SnippetController2, instantiation: EditorContributionInstantiation.Lazy },
76
{ id: TabCompletionController.ID, ctor: TabCompletionController, instantiation: EditorContributionInstantiation.Eager }, // eager because it needs to define a context key
77
{ id: EditorDictation.ID, ctor: EditorDictation, instantiation: EditorContributionInstantiation.Lazy },
78
...EditorExtensionsRegistry.getSomeEditorContributions([
79
CopyPasteController.ID,
80
DropIntoEditorController.ID,
81
LinkDetector.ID,
82
MessageController.ID,
83
ContentHoverController.ID,
84
GlyphHoverController.ID,
85
SelectionClipboardContributionID,
86
InlineCompletionsController.ID,
87
CodeActionController.ID,
88
PlaceholderTextContribution.ID
89
])
90
],
91
contextMenuId: MenuId.SimpleEditorContext
92
};
93
94
super(domElement, options, codeEditorWidgetOptions, instantiationService, codeEditorService, commandService, scopedContextKeyService, themeService, notificationService, accessibilityService, languageConfigurationService, languageFeaturesService);
95
96
this._commentEditorFocused = ctxCommentEditorFocused.bindTo(scopedContextKeyService);
97
this._commentEditorEmpty = CommentContextKeys.commentIsEmpty.bindTo(scopedContextKeyService);
98
this._commentEditorEmpty.set(!this.getModel()?.getValueLength());
99
this._parentThread = parentThread;
100
101
this._register(this.onDidFocusEditorWidget(_ => this._commentEditorFocused.set(true)));
102
103
this._register(this.onDidChangeModelContent(e => this._commentEditorEmpty.set(!this.getModel()?.getValueLength())));
104
this._register(this.onDidBlurEditorWidget(_ => this._commentEditorFocused.reset()));
105
}
106
107
getParentThread(): ICommentThreadWidget {
108
return this._parentThread;
109
}
110
111
protected _getActions(): Iterable<EditorAction> {
112
return EditorExtensionsRegistry.getEditorActions();
113
}
114
115
public override updateOptions(newOptions: Readonly<IEditorOptions> | undefined): void {
116
const withLineNumberRemoved: Readonly<IEditorOptions> = { ...newOptions, lineNumbers: 'off' };
117
super.updateOptions(withLineNumberRemoved);
118
}
119
120
public static getEditorOptions(configurationService: IConfigurationService): IEditorOptions {
121
return {
122
wordWrap: 'on',
123
glyphMargin: false,
124
lineNumbers: 'off',
125
folding: false,
126
selectOnLineNumbers: false,
127
scrollbar: {
128
vertical: 'visible',
129
verticalScrollbarSize: 14,
130
horizontal: 'auto',
131
useShadows: true,
132
verticalHasArrows: false,
133
horizontalHasArrows: false,
134
alwaysConsumeMouseWheel: false
135
},
136
overviewRulerLanes: 2,
137
lineDecorationsWidth: 0,
138
scrollBeyondLastLine: false,
139
renderLineHighlight: 'none',
140
fixedOverflowWidgets: true,
141
acceptSuggestionOnEnter: 'smart',
142
minimap: {
143
enabled: false
144
},
145
dropIntoEditor: { enabled: true },
146
autoClosingBrackets: configurationService.getValue('editor.autoClosingBrackets'),
147
quickSuggestions: false,
148
accessibilitySupport: configurationService.getValue<'auto' | 'off' | 'on'>('editor.accessibilitySupport'),
149
fontFamily: configurationService.getValue('editor.fontFamily'),
150
fontSize: configurationService.getValue('editor.fontSize'),
151
allowVariableLineHeights: false
152
};
153
}
154
}
155
156
export function calculateEditorHeight(parentEditor: LayoutableEditor, editor: ICodeEditor, currentHeight: number): number {
157
const layoutInfo = editor.getLayoutInfo();
158
const lineHeight = editor.getOption(EditorOption.lineHeight);
159
const contentHeight = (editor._getViewModel()?.getLineCount()! * lineHeight); // Can't just call getContentHeight() because it returns an incorrect, large, value when the editor is first created.
160
if ((contentHeight > layoutInfo.height) ||
161
(contentHeight < layoutInfo.height && currentHeight > MIN_EDITOR_HEIGHT)) {
162
const linesToAdd = Math.ceil((contentHeight - layoutInfo.height) / lineHeight);
163
const proposedHeight = layoutInfo.height + (lineHeight * linesToAdd);
164
return clamp(proposedHeight, MIN_EDITOR_HEIGHT, clamp(parentEditor.getLayoutInfo().height - 90, MIN_EDITOR_HEIGHT, MAX_EDITOR_HEIGHT));
165
}
166
return currentHeight;
167
}
168
169