Skip to content

Commit 284417d

Browse files
aiday-marhediet
andauthored
Feature: change font family of inline completions (microsoft#202671)
Feature: change font family of inline completions --------- Co-authored-by: Henning Dieterichs <[email protected]>
1 parent 04f554b commit 284417d

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

src/vs/base/browser/dom.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,38 @@ export function isGlobalStylesheet(node: Node): boolean {
927927
return globalStylesheets.has(node as HTMLStyleElement);
928928
}
929929

930+
/**
931+
* A version of createStyleSheet which has a unified API to initialize/set the style content.
932+
*/
933+
export function createStyleSheet2(): WrappedStyleElement {
934+
return new WrappedStyleElement();
935+
}
936+
937+
class WrappedStyleElement {
938+
private _currentCssStyle = '';
939+
private _styleSheet: HTMLStyleElement | undefined = undefined;
940+
941+
public setStyle(cssStyle: string): void {
942+
if (cssStyle !== this._currentCssStyle) {
943+
return;
944+
}
945+
this._currentCssStyle = cssStyle;
946+
947+
if (!this._styleSheet) {
948+
this._styleSheet = createStyleSheet(mainWindow.document.head, (s) => s.innerText = cssStyle);
949+
} else {
950+
this._styleSheet.innerText = cssStyle;
951+
}
952+
}
953+
954+
public dispose(): void {
955+
if (this._styleSheet) {
956+
clearNode(this._styleSheet);
957+
this._styleSheet = undefined;
958+
}
959+
}
960+
}
961+
930962
export function createStyleSheet(container: HTMLElement = mainWindow.document.head, beforeAppend?: (style: HTMLStyleElement) => void, disposableStore?: DisposableStore): HTMLStyleElement {
931963
const style = document.createElement('style');
932964
style.type = 'text/css';

src/vs/editor/common/config/editorOptions.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3993,6 +3993,11 @@ export interface IInlineSuggestOptions {
39933993
* Does not clear active inline suggestions when the editor loses focus.
39943994
*/
39953995
keepOnBlur?: boolean;
3996+
3997+
/**
3998+
* Font family for inline suggestions.
3999+
*/
4000+
fontFamily?: string | 'default';
39964001
}
39974002

39984003
/**
@@ -4011,6 +4016,7 @@ class InlineEditorSuggest extends BaseEditorOption<EditorOption.inlineSuggest, I
40114016
showToolbar: 'onHover',
40124017
suppressSuggestions: false,
40134018
keepOnBlur: false,
4019+
fontFamily: 'default'
40144020
};
40154021

40164022
super(
@@ -4037,6 +4043,11 @@ class InlineEditorSuggest extends BaseEditorOption<EditorOption.inlineSuggest, I
40374043
default: defaults.suppressSuggestions,
40384044
description: nls.localize('inlineSuggest.suppressSuggestions', "Controls how inline suggestions interact with the suggest widget. If enabled, the suggest widget is not shown automatically when inline suggestions are available.")
40394045
},
4046+
'editor.inlineSuggest.fontFamily': {
4047+
type: 'string',
4048+
default: defaults.fontFamily,
4049+
description: nls.localize('inlineSuggest.fontFamily', "Controls the font family of the inline suggestions.")
4050+
},
40404051
}
40414052
);
40424053
}
@@ -4052,6 +4063,7 @@ class InlineEditorSuggest extends BaseEditorOption<EditorOption.inlineSuggest, I
40524063
showToolbar: stringSet(input.showToolbar, this.defaultValue.showToolbar, ['always', 'onHover', 'never']),
40534064
suppressSuggestions: boolean(input.suppressSuggestions, this.defaultValue.suppressSuggestions),
40544065
keepOnBlur: boolean(input.keepOnBlur, this.defaultValue.keepOnBlur),
4066+
fontFamily: EditorStringOption.string(input.fontFamily, this.defaultValue.fontFamily)
40554067
};
40564068
}
40574069
}

src/vs/editor/contrib/inlineCompletions/browser/inlineCompletionsController.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6+
import { createStyleSheet2 } from 'vs/base/browser/dom';
67
import { alert } from 'vs/base/browser/ui/aria/aria';
78
import { Disposable, toDisposable } from 'vs/base/common/lifecycle';
89
import { ITransaction, autorun, autorunHandleChanges, constObservable, derived, disposableObservableValue, observableFromEvent, observableSignal, observableValue, transaction } from 'vs/base/common/observable';
@@ -52,6 +53,7 @@ export class InlineCompletionsController extends Disposable {
5253
}
5354
));
5455
private readonly _enabled = observableFromEvent(this.editor.onDidChangeConfiguration, () => this.editor.getOption(EditorOption.inlineSuggest).enabled);
56+
private readonly _fontFamily = observableFromEvent(this.editor.onDidChangeConfiguration, () => this.editor.getOption(EditorOption.inlineSuggest).fontFamily);
5557

5658
private _ghostTextWidget = this._register(this._instantiationService.createInstance(GhostTextWidget, this.editor, {
5759
ghostText: this.model.map((v, reader) => /** ghostText */ v?.ghostText.read(reader)),
@@ -112,6 +114,17 @@ export class InlineCompletionsController extends Disposable {
112114
});
113115
}));
114116

117+
const styleElement = this._register(createStyleSheet2());
118+
this._register(autorun(reader => {
119+
const fontFamily = this._fontFamily.read(reader);
120+
styleElement.setStyle(fontFamily === '' || fontFamily === 'default' ? `` : `
121+
.monaco-editor .ghost-text-decoration,
122+
.monaco-editor .ghost-text-decoration-preview,
123+
.monaco-editor .ghost-text {
124+
font-family: ${fontFamily};
125+
}`);
126+
}));
127+
115128
const getReason = (e: IModelContentChangedEvent): VersionIdChangeReason => {
116129
if (e.isUndoing) { return VersionIdChangeReason.Undo; }
117130
if (e.isRedoing) { return VersionIdChangeReason.Redo; }

src/vs/monaco.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4502,6 +4502,10 @@ declare namespace monaco.editor {
45024502
* Does not clear active inline suggestions when the editor loses focus.
45034503
*/
45044504
keepOnBlur?: boolean;
4505+
/**
4506+
* Font family for inline suggestions.
4507+
*/
4508+
fontFamily?: string | 'default';
45054509
}
45064510

45074511
export interface IBracketPairColorizationOptions {

0 commit comments

Comments
 (0)