Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements block decorations feature. #152740

Merged
merged 2 commits into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/vs/editor/browser/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import { IViewModel } from 'vs/editor/common/viewModel';
import { getThemeTypeSelector, IColorTheme } from 'vs/platform/theme/common/themeService';
import { EditorOption } from 'vs/editor/common/config/editorOptions';
import { PointerHandlerLastRenderData } from 'vs/editor/browser/controller/mouseTarget';
import { BlockDecorations } from 'vs/editor/browser/viewParts/blockDecorations/blockDecorations';


export interface IContentWidgetData {
Expand Down Expand Up @@ -180,6 +181,9 @@ export class View extends ViewEventHandler {
const rulers = new Rulers(this._context);
this._viewParts.push(rulers);

const blockOutline = new BlockDecorations(this._context);
this._viewParts.push(blockOutline);

const minimap = new Minimap(this._context);
this._viewParts.push(minimap);

Expand All @@ -192,6 +196,7 @@ export class View extends ViewEventHandler {

this._linesContent.appendChild(contentViewOverlays.getDomNode());
this._linesContent.appendChild(rulers.domNode);
this._linesContent.appendChild(blockOutline.domNode);
this._linesContent.appendChild(this._viewZones.domNode);
this._linesContent.appendChild(this._viewLines.getDomNode());
this._linesContent.appendChild(this._contentWidgets.domNode);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

.monaco-editor .blockDecorations-container {
position: absolute;
top: 0;
}

.monaco-editor .blockDecorations-block {
position: absolute;
box-sizing: border-box;
}
103 changes: 103 additions & 0 deletions src/vs/editor/browser/viewParts/blockDecorations/blockDecorations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { createFastDomNode, FastDomNode } from 'vs/base/browser/fastDomNode';
import 'vs/css!./blockDecorations';
import { RenderingContext, RestrictedRenderingContext } from 'vs/editor/browser/view/renderingContext';
import { ViewPart } from 'vs/editor/browser/view/viewPart';
import { EditorOption } from 'vs/editor/common/config/editorOptions';
import * as viewEvents from 'vs/editor/common/viewEvents';
import { ViewContext } from 'vs/editor/common/viewModel/viewContext';

export class BlockDecorations extends ViewPart {

public domNode: FastDomNode<HTMLElement>;

private readonly blocks: FastDomNode<HTMLElement>[] = [];

private contentWidth: number = -1;

constructor(context: ViewContext) {
super(context);

this.domNode = createFastDomNode<HTMLElement>(document.createElement('div'));
this.domNode.setAttribute('role', 'presentation');
this.domNode.setAttribute('aria-hidden', 'true');
this.domNode.setClassName('blockDecorations-container');

this.update();
}

private update(): boolean {
let didChange = false;
const options = this._context.configuration.options;
const layoutInfo = options.get(EditorOption.layoutInfo);
const newContentWidth = layoutInfo.contentWidth - layoutInfo.verticalScrollbarWidth;

if (this.contentWidth !== newContentWidth) {
this.contentWidth = newContentWidth;
didChange = true;
}

return didChange;
}

public override dispose(): void {
super.dispose();
}

// --- begin event handlers

public override onConfigurationChanged(e: viewEvents.ViewConfigurationChangedEvent): boolean {
return this.update();
}
public override onScrollChanged(e: viewEvents.ViewScrollChangedEvent): boolean {
return e.scrollTopChanged || e.scrollLeftChanged;
}
public override onDecorationsChanged(e: viewEvents.ViewDecorationsChangedEvent): boolean {
return true;
}

hediet marked this conversation as resolved.
Show resolved Hide resolved
public override onZonesChanged(e: viewEvents.ViewZonesChangedEvent): boolean {
return true;
}

// --- end event handlers
public prepareRender(ctx: RenderingContext): void {
// Nothing to read
}

public render(ctx: RestrictedRenderingContext): void {
let count = 0;
const decorations = ctx.getDecorationsInViewport();
for (const decoration of decorations) {
if (!decoration.options.blockClassName) {
continue;
}

let block = this.blocks[count];
if (!block) {
block = this.blocks[count] = createFastDomNode(document.createElement('div'));
this.domNode.appendChild(block);
}
const top = ctx.getVerticalOffsetForLineNumber(decoration.range.startLineNumber);
// See https://github.com/microsoft/vscode/pull/152740#discussion_r902661546
const bottom = ctx.getVerticalOffsetForLineNumber(decoration.range.endLineNumber + 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will contain any view zone immediately following this line. If you would like to limit the block strictly to the end line number, you would need to use something like ctx.getVerticalOffsetForLineNumber(decoration.range.endLineNumber) + lineHeight. lineHeight can be found in the editor options.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, good catch. Unfortunately, this is the behavior I need, to cover deleted ranges in the 3wm editor.

I suggest to add a comment and merge it as this and solve the problem when someone does not want to include view zones.


block.setClassName('blockDecorations-block ' + decoration.options.blockClassName);
block.setLeft(ctx.scrollLeft);
block.setWidth(this.contentWidth);
block.setTop(top);
block.setHeight(bottom - top);

count++;
}

for (let i = count; i < this.blocks.length; i++) {
this.blocks[i].domNode.remove();
}
this.blocks.length = count;
}
}
1 change: 1 addition & 0 deletions src/vs/editor/common/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export interface IModelDecorationOptions {
* CSS class name describing the decoration.
*/
className?: string | null;
blockClassName?: string | null;
/**
* Message to be rendered when hovering over the glyph margin decoration.
*/
Expand Down
2 changes: 2 additions & 0 deletions src/vs/editor/common/model/textModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2227,6 +2227,7 @@ export class ModelDecorationOptions implements model.IModelDecorationOptions {
}

readonly description: string;
readonly blockClassName: string | null;
readonly stickiness: model.TrackedRangeStickiness;
readonly zIndex: number;
readonly className: string | null;
Expand All @@ -2253,6 +2254,7 @@ export class ModelDecorationOptions implements model.IModelDecorationOptions {

private constructor(options: model.IModelDecorationOptions) {
this.description = options.description;
this.blockClassName = options.blockClassName ? cleanClassName(options.blockClassName) : null;
this.stickiness = options.stickiness || model.TrackedRangeStickiness.AlwaysGrowsWhenTypingAtEdges;
this.zIndex = options.zIndex || 0;
this.className = options.className ? cleanClassName(options.className) : null;
Expand Down
1 change: 1 addition & 0 deletions src/vs/monaco.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1483,6 +1483,7 @@ declare namespace monaco.editor {
* CSS class name describing the decoration.
*/
className?: string | null;
blockClassName?: string | null;
/**
* Message to be rendered when hovering over the glyph margin decoration.
*/
Expand Down