Skip to content

Commit

Permalink
- use strings for view zone ids
Browse files Browse the repository at this point in the history
- make it unlikely that a new view instance would accept a view zone id from a previous view instance
- remove that the find widget removes a view zone id from another view
(fixes #71745)
  • Loading branch information
alexdima committed Aug 14, 2019
1 parent c3fcaef commit 16051c7
Show file tree
Hide file tree
Showing 18 changed files with 88 additions and 99 deletions.
15 changes: 15 additions & 0 deletions src/vs/base/common/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -729,3 +729,18 @@ export function getNLines(str: string, n = 1): string {
str.substr(0, idx) :
str;
}

/**
* Produces 'a'-'z', followed by 'A'-'Z'... followed by 'a'-'z', etc.
*/
export function singleLetterHash(n: number): string {
const LETTERS_CNT = (CharCode.Z - CharCode.A + 1);

n = n % (2 * LETTERS_CNT);

if (n < LETTERS_CNT) {
return String.fromCharCode(CharCode.a + n);
}

return String.fromCharCode(CharCode.A + n - LETTERS_CNT);
}
2 changes: 1 addition & 1 deletion src/vs/editor/browser/controller/mouseHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export interface IPointerHandlerHelper {
*/
getLastViewCursorsRenderData(): IViewCursorRenderData[];

shouldSuppressMouseDownOnViewZone(viewZoneId: number): boolean;
shouldSuppressMouseDownOnViewZone(viewZoneId: string): boolean;
shouldSuppressMouseDownOnWidget(widgetId: string): boolean;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/vs/editor/browser/controller/mouseTarget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { IViewModel } from 'vs/editor/common/viewModel/viewModel';
import { CursorColumns } from 'vs/editor/common/controller/cursorCommon';

export interface IViewZoneData {
viewZoneId: number;
viewZoneId: string;
positionBefore: Position | null;
positionAfter: Position | null;
position: Position;
Expand Down
6 changes: 3 additions & 3 deletions src/vs/editor/browser/editorBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,17 @@ export interface IViewZoneChangeAccessor {
* @param zone Zone to create
* @return A unique identifier to the view zone.
*/
addZone(zone: IViewZone): number;
addZone(zone: IViewZone): string;
/**
* Remove a zone
* @param id A unique identifier to the view zone, as returned by the `addZone` call.
*/
removeZone(id: number): void;
removeZone(id: string): void;
/**
* Change a zone's position.
* The editor will rescan the `afterLineNumber` and `afterColumn` properties of a view zone.
*/
layoutZone(id: number): void;
layoutZone(id: string): void;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/vs/editor/browser/view/viewImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ export class View extends ViewEventHandler {
getLastViewCursorsRenderData: () => {
return this.viewCursors.getLastRenderData() || [];
},
shouldSuppressMouseDownOnViewZone: (viewZoneId: number) => {
shouldSuppressMouseDownOnViewZone: (viewZoneId: string) => {
return this.viewZones.shouldSuppressMouseDownOnViewZone(viewZoneId);
},
shouldSuppressMouseDownOnWidget: (widgetId: string) => {
Expand Down Expand Up @@ -473,17 +473,17 @@ export class View extends ViewEventHandler {

this._renderOnce(() => {
const changeAccessor: editorBrowser.IViewZoneChangeAccessor = {
addZone: (zone: editorBrowser.IViewZone): number => {
addZone: (zone: editorBrowser.IViewZone): string => {
zonesHaveChanged = true;
return this.viewZones.addZone(zone);
},
removeZone: (id: number): void => {
removeZone: (id: string): void => {
if (!id) {
return;
}
zonesHaveChanged = this.viewZones.removeZone(id) || zonesHaveChanged;
},
layoutZone: (id: number): void => {
layoutZone: (id: string): void => {
if (!id) {
return;
}
Expand Down
34 changes: 17 additions & 17 deletions src/vs/editor/browser/viewParts/viewZones/viewZones.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import * as viewEvents from 'vs/editor/common/view/viewEvents';
import { IViewWhitespaceViewportData } from 'vs/editor/common/viewModel/viewModel';

export interface IMyViewZone {
whitespaceId: number;
whitespaceId: string;
delegate: IViewZone;
isVisible: boolean;
domNode: FastDomNode<HTMLElement>;
Expand Down Expand Up @@ -74,7 +74,7 @@ export class ViewZones extends ViewPart {
const id = keys[i];
const zone = this._zones[id];
const props = this._computeWhitespaceProps(zone.delegate);
if (this._context.viewLayout.changeWhitespace(parseInt(id, 10), props.afterViewLineNumber, props.heightInPx)) {
if (this._context.viewLayout.changeWhitespace(id, props.afterViewLineNumber, props.heightInPx)) {
this._safeCallOnComputedHeight(zone.delegate, props.heightInPx);
hadAChange = true;
}
Expand Down Expand Up @@ -183,7 +183,7 @@ export class ViewZones extends ViewPart {
};
}

public addZone(zone: IViewZone): number {
public addZone(zone: IViewZone): string {
const props = this._computeWhitespaceProps(zone);
const whitespaceId = this._context.viewLayout.addWhitespace(props.afterViewLineNumber, this._getZoneOrdinal(zone), props.heightInPx, props.minWidthInPx);

Expand All @@ -200,29 +200,29 @@ export class ViewZones extends ViewPart {
myZone.domNode.setPosition('absolute');
myZone.domNode.domNode.style.width = '100%';
myZone.domNode.setDisplay('none');
myZone.domNode.setAttribute('monaco-view-zone', myZone.whitespaceId.toString());
myZone.domNode.setAttribute('monaco-view-zone', myZone.whitespaceId);
this.domNode.appendChild(myZone.domNode);

if (myZone.marginDomNode) {
myZone.marginDomNode.setPosition('absolute');
myZone.marginDomNode.domNode.style.width = '100%';
myZone.marginDomNode.setDisplay('none');
myZone.marginDomNode.setAttribute('monaco-view-zone', myZone.whitespaceId.toString());
myZone.marginDomNode.setAttribute('monaco-view-zone', myZone.whitespaceId);
this.marginDomNode.appendChild(myZone.marginDomNode);
}

this._zones[myZone.whitespaceId.toString()] = myZone;
this._zones[myZone.whitespaceId] = myZone;


this.setShouldRender();

return myZone.whitespaceId;
}

public removeZone(id: number): boolean {
if (this._zones.hasOwnProperty(id.toString())) {
const zone = this._zones[id.toString()];
delete this._zones[id.toString()];
public removeZone(id: string): boolean {
if (this._zones.hasOwnProperty(id)) {
const zone = this._zones[id];
delete this._zones[id];
this._context.viewLayout.removeWhitespace(zone.whitespaceId);

zone.domNode.removeAttribute('monaco-visible-view-zone');
Expand All @@ -242,10 +242,10 @@ export class ViewZones extends ViewPart {
return false;
}

public layoutZone(id: number): boolean {
public layoutZone(id: string): boolean {
let changed = false;
if (this._zones.hasOwnProperty(id.toString())) {
const zone = this._zones[id.toString()];
if (this._zones.hasOwnProperty(id)) {
const zone = this._zones[id];
const props = this._computeWhitespaceProps(zone.delegate);
// const newOrdinal = this._getZoneOrdinal(zone.delegate);
changed = this._context.viewLayout.changeWhitespace(zone.whitespaceId, props.afterViewLineNumber, props.heightInPx) || changed;
Expand All @@ -259,9 +259,9 @@ export class ViewZones extends ViewPart {
return changed;
}

public shouldSuppressMouseDownOnViewZone(id: number): boolean {
if (this._zones.hasOwnProperty(id.toString())) {
const zone = this._zones[id.toString()];
public shouldSuppressMouseDownOnViewZone(id: string): boolean {
if (this._zones.hasOwnProperty(id)) {
const zone = this._zones[id];
return Boolean(zone.delegate.suppressMouseDown);
}
return false;
Expand Down Expand Up @@ -314,7 +314,7 @@ export class ViewZones extends ViewPart {

let hasVisibleZone = false;
for (let i = 0, len = visibleWhitespaces.length; i < len; i++) {
visibleZones[visibleWhitespaces[i].id.toString()] = visibleWhitespaces[i];
visibleZones[visibleWhitespaces[i].id] = visibleWhitespaces[i];
hasVisibleZone = true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/vs/editor/browser/widget/diffEditorWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ interface IDiffEditorWidgetStyle {
}

class VisualEditorState {
private _zones: number[];
private _zones: string[];
private _zonesMap: { [zoneId: string]: boolean; };
private _decorations: string[];

Expand Down
17 changes: 1 addition & 16 deletions src/vs/editor/common/model/textModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,6 @@ export function createTextBuffer(value: string | model.ITextBufferFactory, defau

let MODEL_ID = 0;

/**
* Produces 'a'-'z', followed by 'A'-'Z'... followed by 'a'-'z', etc.
*/
function singleLetter(result: number): string {
const LETTERS_CNT = (CharCode.Z - CharCode.A + 1);

result = result % (2 * LETTERS_CNT);

if (result < LETTERS_CNT) {
return String.fromCharCode(CharCode.a + result);
}

return String.fromCharCode(CharCode.A + result - LETTERS_CNT);
}

const LIMIT_FIND_COUNT = 999;
export const LONG_LINE_BOUNDARY = 10000;

Expand Down Expand Up @@ -343,7 +328,7 @@ export class TextModel extends Disposable implements model.ITextModel {
}
});

this._instanceId = singleLetter(MODEL_ID);
this._instanceId = strings.singleLetterHash(MODEL_ID);
this._lastDecorationId = 0;
this._decorations = Object.create(null);
this._decorationsTree = new DecorationsTrees();
Expand Down
6 changes: 3 additions & 3 deletions src/vs/editor/common/viewLayout/linesLayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ export class LinesLayout {
* @param heightInPx The height of the whitespace, in pixels.
* @return An id that can be used later to mutate or delete the whitespace
*/
public insertWhitespace(afterLineNumber: number, ordinal: number, heightInPx: number, minWidth: number): number {
public insertWhitespace(afterLineNumber: number, ordinal: number, heightInPx: number, minWidth: number): string {
return this._whitespaces.insertWhitespace(afterLineNumber, ordinal, heightInPx, minWidth);
}

/**
* Change properties associated with a certain whitespace.
*/
public changeWhitespace(id: number, newAfterLineNumber: number, newHeight: number): boolean {
public changeWhitespace(id: string, newAfterLineNumber: number, newHeight: number): boolean {
return this._whitespaces.changeWhitespace(id, newAfterLineNumber, newHeight);
}

Expand All @@ -80,7 +80,7 @@ export class LinesLayout {
* @param id The whitespace to remove
* @return Returns true if the whitespace is found and it is removed.
*/
public removeWhitespace(id: number): boolean {
public removeWhitespace(id: string): boolean {
return this._whitespaces.removeWhitespace(id);
}

Expand Down
6 changes: 3 additions & 3 deletions src/vs/editor/common/viewLayout/viewLayout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,13 @@ export class ViewLayout extends Disposable implements IViewLayout {

// ---- IVerticalLayoutProvider

public addWhitespace(afterLineNumber: number, ordinal: number, height: number, minWidth: number): number {
public addWhitespace(afterLineNumber: number, ordinal: number, height: number, minWidth: number): string {
return this._linesLayout.insertWhitespace(afterLineNumber, ordinal, height, minWidth);
}
public changeWhitespace(id: number, newAfterLineNumber: number, newHeight: number): boolean {
public changeWhitespace(id: string, newAfterLineNumber: number, newHeight: number): boolean {
return this._linesLayout.changeWhitespace(id, newAfterLineNumber, newHeight);
}
public removeWhitespace(id: number): boolean {
public removeWhitespace(id: string): boolean {
return this._linesLayout.removeWhitespace(id);
}
public getVerticalOffsetForLineNumber(lineNumber: number): number {
Expand Down
Loading

0 comments on commit 16051c7

Please sign in to comment.