Skip to content

Commit

Permalink
more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pokey committed Jun 23, 2023
1 parent 51f7bfc commit bed8c8e
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import { VscodeFancyRangeHighlighterRenderer } from "./VscodeFancyRangeHighlight
import { generateDecorationsForCharacterRange } from "./generateDecorationsForCharacterRange";
import { generateDecorationsForLineRange } from "./generateDecorationsForLineRange";
import { generateDifferentiatedRanges } from "./generateDifferentiatedRanges";
import {
DecorationStyle,
DifferentiatedStyledRange,
} from "./getDecorationRanges.types";
import { DifferentiatedStyledRange } from "./getDecorationRanges.types";
import { groupDifferentiatedRanges } from "./groupDifferentiatedRanges";

/**
Expand All @@ -23,9 +20,7 @@ export class VscodeFancyRangeHighlighter {
}

setRanges(editor: VscodeTextEditorImpl, ranges: GeneralizedRange[]) {
const decoratedRanges: Iterable<
DifferentiatedStyledRange<DecorationStyle>
> = flatmap(
const decoratedRanges: Iterable<DifferentiatedStyledRange> = flatmap(
generateDifferentiatedRanges(ranges),

function* ({ range, differentiationIndex }) {
Expand All @@ -46,23 +41,10 @@ export class VscodeFancyRangeHighlighter {
},
);

this.renderer.setRanges(
editor,
groupDifferentiatedRanges(decoratedRanges, getStyleKey),
);
this.renderer.setRanges(editor, groupDifferentiatedRanges(decoratedRanges));
}

dispose() {
this.renderer.dispose();
}
}

function getStyleKey({
top,
right,
left,
bottom,
isWholeLine,
}: DecorationStyle) {
return [top, right, left, bottom, isWholeLine ?? false];
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {

export class VscodeFancyRangeHighlighterRenderer {
private decorationTypes: CompositeKeyDefaultMap<
DifferentiatedStyle<DecorationStyle>,
DifferentiatedStyle,
TextEditorDecorationType
>;

Expand All @@ -40,7 +40,7 @@ export class VscodeFancyRangeHighlighterRenderer {

setRanges(
editor: VscodeTextEditorImpl,
decoratedRanges: DifferentiatedStyledRangeList<DecorationStyle>[],
decoratedRanges: DifferentiatedStyledRangeList[],
) {
const untouchedDecorationTypes = new Set(this.decorationTypes.values());

Expand All @@ -50,16 +50,18 @@ export class VscodeFancyRangeHighlighterRenderer {
b.differentiatedStyles.differentiationIndex,
);

decoratedRanges.forEach(({ differentiatedStyles: styleParameters, ranges }) => {
const decorationType = this.decorationTypes.get(styleParameters);
decoratedRanges.forEach(
({ differentiatedStyles: styleParameters, ranges }) => {
const decorationType = this.decorationTypes.get(styleParameters);

editor.vscodeEditor.setDecorations(
decorationType,
ranges.map(toVscodeRange),
);
editor.vscodeEditor.setDecorations(
decorationType,
ranges.map(toVscodeRange),
);

untouchedDecorationTypes.delete(decorationType);
});
untouchedDecorationTypes.delete(decorationType);
},
);

untouchedDecorationTypes.forEach((decorationType) => {
editor.vscodeEditor.setDecorations(decorationType, []);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Range, TextEditor } from "@cursorless/common";
import { range } from "lodash";
import { BorderStyle, DecoratedRange } from "./getDecorationRanges.types";
import { BorderStyle, StyledRange } from "./getDecorationRanges.types";
import { handleMultipleLines } from "./handleMultipleLines";

export function* generateDecorationsForCharacterRange(
editor: TextEditor,
characterRange: Range,
): Iterable<DecoratedRange> {
): Iterable<StyledRange> {
if (characterRange.isSingleLine) {
yield {
range: characterRange,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Range } from "@cursorless/common";
import { BorderStyle, DecoratedRange } from "./getDecorationRanges.types";
import { BorderStyle, StyledRange } from "./getDecorationRanges.types";

export function* generateDecorationsForLineRange(
startLine: number,
endLine: number,
): Iterable<DecoratedRange> {
): Iterable<StyledRange> {
const lineCount = endLine - startLine + 1;

if (lineCount === 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import {
} from "@cursorless/common";

import { max } from "lodash";
import { DifferentiatedRange } from "./getDecorationRanges.types";
import { DifferentiatedGeneralizedRange } from "./getDecorationRanges.types";

export function* generateDifferentiatedRanges(
ranges: GeneralizedRange[],
): Iterable<DifferentiatedRange> {
): Iterable<DifferentiatedGeneralizedRange> {
ranges.sort(compareGeneralizedRangesByStart);

let currentRanges: DifferentiatedRange[] = [];
let currentRanges: DifferentiatedGeneralizedRange[] = [];

for (const range of ranges) {
currentRanges = [
Expand All @@ -25,7 +25,7 @@ export function* generateDifferentiatedRanges(
const differentiatedRange = {
range,
differentiationIndex: getDifferentiationIndex(currentRanges, range),
} as DifferentiatedRange;
} as DifferentiatedGeneralizedRange;

yield differentiatedRange;

Expand All @@ -34,7 +34,7 @@ export function* generateDifferentiatedRanges(
}

function getDifferentiationIndex(
currentRanges: DifferentiatedRange[],
currentRanges: DifferentiatedGeneralizedRange[],
range: GeneralizedRange,
): number {
const maxContainingDifferentiationIndex = max(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import assert = require("assert");
import { getDecorationRanges } from "./getDecorationRanges";
import { DecorationStyle, DifferentiatedStyle } from "./getDecorationRanges.types";
import {
DecorationStyle,
DifferentiatedStyle,
} from "./getDecorationRanges.types";
import {
Range,
RangePlainObject,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,37 @@ export interface DecorationStyle {
isWholeLine?: boolean;
}

export interface StyledRange<T> {
style: T;
range: Range;
export interface DifferentiatedStyle {
style: DecorationStyle;
differentiationIndex: number;
}

export type DecoratedRange = StyledRange<DecorationStyle>;

export interface DifferentiatedStyle<T> {
style: T;
differentiationIndex: number;
export interface StyledRange {
style: DecorationStyle;
range: Range;
}

export interface DifferentiatedStyledRange<T> {
differentiatedStyle: DifferentiatedStyle<T>;
export interface DifferentiatedStyledRange {
differentiatedStyle: DifferentiatedStyle;
range: Range;
}

export interface DifferentiatedStyledRangeList<T> {
differentiatedStyles: DifferentiatedStyle<T>;
export interface DifferentiatedStyledRangeList {
differentiatedStyles: DifferentiatedStyle;
ranges: Range[];
}

export interface DifferentiatedRange {
export interface DifferentiatedGeneralizedRange {
range: GeneralizedRange;
differentiationIndex: number;
}

export interface DifferentiatedCharacterRange extends DifferentiatedRange {
export interface DifferentiatedCharacterRange
extends DifferentiatedGeneralizedRange {
range: CharacterRange;
}

export interface DifferentiatedLineRange extends DifferentiatedRange {
export interface DifferentiatedLineRange
extends DifferentiatedGeneralizedRange {
range: LineRange;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,15 @@ import {
DifferentiatedStyledRange,
} from "./getDecorationRanges.types";

export function groupDifferentiatedRanges<T>(
decoratedRanges: Iterable<DifferentiatedStyledRange<T>>,
getKeyList: (style: T) => unknown[],
): DifferentiatedStyledRangeList<T>[] {
export function groupDifferentiatedRanges(
decoratedRanges: Iterable<DifferentiatedStyledRange>,
): DifferentiatedStyledRangeList[] {
const decorations: CompositeKeyDefaultMap<
DifferentiatedStyle<T>,
DifferentiatedStyledRangeList<T>
DifferentiatedStyle,
DifferentiatedStyledRangeList
> = new CompositeKeyDefaultMap(
(differentiatedStyles) => ({ differentiatedStyles, ranges: [] }),
({ style, differentiationIndex }) => [
...getKeyList(style),
differentiationIndex,
],
getStyleKey,
);

for (const { range, differentiatedStyle } of decoratedRanges) {
Expand All @@ -26,3 +22,10 @@ export function groupDifferentiatedRanges<T>(

return Array.from(decorations.values());
}

function getStyleKey({
style: { top, right, left, bottom, isWholeLine },
differentiationIndex,
}: DifferentiatedStyle) {
return [top, right, left, bottom, isWholeLine ?? false, differentiationIndex];
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { Range } from "@cursorless/common";
import {
BorderStyle,
DecorationStyle,
DecoratedRange,
StyledRange,
} from "./getDecorationRanges.types";
import { flatmap } from "itertools";

export function* handleMultipleLines(
lineRanges: Range[],
): Iterable<DecoratedRange> {
): Iterable<StyledRange> {
yield* flatmap(generateLineGroupings(lineRanges), handleLine);
}

Expand Down Expand Up @@ -71,7 +71,7 @@ function* handleLine({
previousLine,
currentLine,
nextLine,
}: LineGrouping): Iterable<DecoratedRange> {
}: LineGrouping): Iterable<StyledRange> {
const events: Event[] = [
...(previousLine == null
? []
Expand Down

0 comments on commit bed8c8e

Please sign in to comment.