Reduce duplicate with getDecorationsAtCell method

This commit is contained in:
Daniel Imms
2022-05-11 08:37:42 -07:00
parent abdce874db
commit c0b4edaf31
7 changed files with 50 additions and 58 deletions
+2 -1
View File
@@ -167,6 +167,7 @@ export class MockDecorationService implements IDecorationService {
public onDecorationRemoved = new EventEmitter<IInternalDecoration>().event;
public registerDecoration(decorationOptions: IDecorationOptions): IDecoration | undefined { return undefined; }
public reset(): void { }
public *getDecorationsOnLine(line: number): IterableIterator<IInternalDecoration> { }
public *getDecorationsAtLine(line: number): IterableIterator<IInternalDecoration> { }
public *getDecorationsAtCell(x: number, line: number): IterableIterator<IInternalDecoration> { }
public dispose(): void { }
}
+15 -1
View File
@@ -56,7 +56,7 @@ export class DecorationService extends Disposable implements IDecorationService
this._decorations.length = 0;
}
public *getDecorationsOnLine(line: number): IterableIterator<IInternalDecoration> {
public *getDecorationsAtLine(line: number): IterableIterator<IInternalDecoration> {
// TODO: This could be made much faster if _decorations was sorted by line (and col?)
for (const d of this.decorations) {
if (d.marker.line === line) {
@@ -65,6 +65,20 @@ export class DecorationService extends Disposable implements IDecorationService
}
}
public *getDecorationsAtCell(x: number, line: number): IterableIterator<IInternalDecoration> {
let xmin = 0;
let xmax = 0;
for (const d of this.decorations) {
if (d.marker.line === line) {
xmin = d.options.x ?? 0;
xmax = xmin + (d.options.width ?? 1);
if (x >= xmin && x < xmax) {
yield d;
}
}
}
}
public dispose(): void {
for (const decoration of this._decorations) {
this._onDecorationRemoved.fire(decoration);
+4 -2
View File
@@ -309,8 +309,10 @@ export interface IDecorationService extends IDisposable {
readonly onDecorationRemoved: IEvent<IInternalDecoration>;
registerDecoration(decorationOptions: IDecorationOptions): IDecoration | undefined;
reset(): void;
/** Iterates over the decorations on a line (in no particular order). */
getDecorationsOnLine(line: number): IterableIterator<IInternalDecoration>;
/** Iterates over the decorations at a line (in no particular order). */
getDecorationsAtLine(line: number): IterableIterator<IInternalDecoration>;
/** Iterates over the decorations at a cell (in no particular order). */
getDecorationsAtCell(x: number, line: number): IterableIterator<IInternalDecoration>;
}
export interface IInternalDecoration extends IDecoration {
readonly options: IDecorationOptions;