forked from microsoft/vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hediet/b/placeholder-text (microsoft#210939)
Implements microsoft#208976
- Loading branch information
1 parent
493e180
commit 90faa9a
Showing
2 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
src/vs/editor/contrib/placeholderText/browser/placeholderText.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
.monaco-editor .placeholder-text { | ||
color: var(--vscode-editorGhostText-foreground) !important; | ||
} |
66 changes: 66 additions & 0 deletions
66
src/vs/editor/contrib/placeholderText/browser/placeholderTextContribution.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { structuralEquals } from 'vs/base/common/equals'; | ||
import { Disposable } from 'vs/base/common/lifecycle'; | ||
import { derived, derivedOpts, observableValue } from 'vs/base/common/observable'; | ||
import 'vs/css!./placeholderText'; | ||
import { ICodeEditor } from 'vs/editor/browser/editorBrowser'; | ||
import { EditorContributionInstantiation, registerEditorContribution } from 'vs/editor/browser/editorExtensions'; | ||
import { obsCodeEditor } from 'vs/editor/browser/observableUtilities'; | ||
import { Range } from 'vs/editor/common/core/range'; | ||
import { IEditorContribution } from 'vs/editor/common/editorCommon'; | ||
import { IModelDeltaDecoration, InjectedTextCursorStops } from 'vs/editor/common/model'; | ||
|
||
export class PlaceholderTextContribution extends Disposable implements IEditorContribution { | ||
public static get(editor: ICodeEditor): PlaceholderTextContribution { | ||
return editor.getContribution<PlaceholderTextContribution>(PlaceholderTextContribution.ID)!; | ||
} | ||
|
||
public static readonly ID = 'editor.contrib.placeholderText'; | ||
private readonly _editorObs = obsCodeEditor(this._editor); | ||
|
||
private readonly _placeholderText = observableValue<string | undefined>(this, undefined); | ||
|
||
private readonly _decorationOptions = derivedOpts<{ placeholder: string } | undefined>({ owner: this, equalsFn: structuralEquals }, reader => { | ||
const p = this._placeholderText.read(reader); | ||
if (!p) { return undefined; } | ||
if (!this._editorObs.valueIsEmpty.read(reader)) { return undefined; } | ||
|
||
return { placeholder: p }; | ||
}); | ||
|
||
private readonly _decorations = derived<IModelDeltaDecoration[]>(this, (reader) => { | ||
const options = this._decorationOptions.read(reader); | ||
if (!options) { return []; } | ||
|
||
return [{ | ||
range: new Range(1, 1, 1, 1), | ||
options: { | ||
description: 'placeholder', | ||
showIfCollapsed: true, | ||
after: { | ||
content: options.placeholder, | ||
cursorStops: InjectedTextCursorStops.None, | ||
inlineClassName: 'placeholder-text' | ||
} | ||
} | ||
}]; | ||
}); | ||
|
||
constructor( | ||
private readonly _editor: ICodeEditor, | ||
) { | ||
super(); | ||
|
||
this._register(this._editorObs.setDecorations(this._decorations)); | ||
} | ||
|
||
public setPlaceholderText(placeholder: string): void { | ||
this._placeholderText.set(placeholder, undefined); | ||
} | ||
} | ||
|
||
registerEditorContribution(PlaceholderTextContribution.ID, PlaceholderTextContribution, EditorContributionInstantiation.Lazy); |