Skip to content

Commit

Permalink
Feature: change font family of inline completions (#202671)
Browse files Browse the repository at this point in the history
Feature: change font family of inline completions

---------

Co-authored-by: Henning Dieterichs <[email protected]>
  • Loading branch information
aiday-mar and hediet authored Jan 18, 2024
1 parent 04f554b commit 284417d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/vs/base/browser/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,38 @@ export function isGlobalStylesheet(node: Node): boolean {
return globalStylesheets.has(node as HTMLStyleElement);
}

/**
* A version of createStyleSheet which has a unified API to initialize/set the style content.
*/
export function createStyleSheet2(): WrappedStyleElement {
return new WrappedStyleElement();
}

class WrappedStyleElement {
private _currentCssStyle = '';
private _styleSheet: HTMLStyleElement | undefined = undefined;

public setStyle(cssStyle: string): void {
if (cssStyle !== this._currentCssStyle) {
return;
}
this._currentCssStyle = cssStyle;

if (!this._styleSheet) {
this._styleSheet = createStyleSheet(mainWindow.document.head, (s) => s.innerText = cssStyle);
} else {
this._styleSheet.innerText = cssStyle;
}
}

public dispose(): void {
if (this._styleSheet) {
clearNode(this._styleSheet);
this._styleSheet = undefined;
}
}
}

export function createStyleSheet(container: HTMLElement = mainWindow.document.head, beforeAppend?: (style: HTMLStyleElement) => void, disposableStore?: DisposableStore): HTMLStyleElement {
const style = document.createElement('style');
style.type = 'text/css';
Expand Down
12 changes: 12 additions & 0 deletions src/vs/editor/common/config/editorOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3993,6 +3993,11 @@ export interface IInlineSuggestOptions {
* Does not clear active inline suggestions when the editor loses focus.
*/
keepOnBlur?: boolean;

/**
* Font family for inline suggestions.
*/
fontFamily?: string | 'default';
}

/**
Expand All @@ -4011,6 +4016,7 @@ class InlineEditorSuggest extends BaseEditorOption<EditorOption.inlineSuggest, I
showToolbar: 'onHover',
suppressSuggestions: false,
keepOnBlur: false,
fontFamily: 'default'
};

super(
Expand All @@ -4037,6 +4043,11 @@ class InlineEditorSuggest extends BaseEditorOption<EditorOption.inlineSuggest, I
default: defaults.suppressSuggestions,
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.")
},
'editor.inlineSuggest.fontFamily': {
type: 'string',
default: defaults.fontFamily,
description: nls.localize('inlineSuggest.fontFamily', "Controls the font family of the inline suggestions.")
},
}
);
}
Expand All @@ -4052,6 +4063,7 @@ class InlineEditorSuggest extends BaseEditorOption<EditorOption.inlineSuggest, I
showToolbar: stringSet(input.showToolbar, this.defaultValue.showToolbar, ['always', 'onHover', 'never']),
suppressSuggestions: boolean(input.suppressSuggestions, this.defaultValue.suppressSuggestions),
keepOnBlur: boolean(input.keepOnBlur, this.defaultValue.keepOnBlur),
fontFamily: EditorStringOption.string(input.fontFamily, this.defaultValue.fontFamily)
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

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

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

const styleElement = this._register(createStyleSheet2());
this._register(autorun(reader => {
const fontFamily = this._fontFamily.read(reader);
styleElement.setStyle(fontFamily === '' || fontFamily === 'default' ? `` : `
.monaco-editor .ghost-text-decoration,
.monaco-editor .ghost-text-decoration-preview,
.monaco-editor .ghost-text {
font-family: ${fontFamily};
}`);
}));

const getReason = (e: IModelContentChangedEvent): VersionIdChangeReason => {
if (e.isUndoing) { return VersionIdChangeReason.Undo; }
if (e.isRedoing) { return VersionIdChangeReason.Redo; }
Expand Down
4 changes: 4 additions & 0 deletions src/vs/monaco.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4502,6 +4502,10 @@ declare namespace monaco.editor {
* Does not clear active inline suggestions when the editor loses focus.
*/
keepOnBlur?: boolean;
/**
* Font family for inline suggestions.
*/
fontFamily?: string | 'default';
}

export interface IBracketPairColorizationOptions {
Expand Down

0 comments on commit 284417d

Please sign in to comment.