Skip to content

Commit

Permalink
add test for compensateFoldRows; small refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mkslanc committed Sep 16, 2022
1 parent 8010f2a commit f7b1b5c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 36 deletions.
56 changes: 21 additions & 35 deletions src/layer/decorators.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var Decorator = function (parent, renderer) {

this.canvas.width = this.canvasWidth;
this.canvas.height = this.canvasHeight;
this.canvas.style.top = 0 + "px";
this.canvas.style.top = 0 + "px";
this.canvas.style.right = 0 + "px";
this.canvas.style.zIndex = 7 + "px";
this.canvas.style.position = "absolute";
Expand Down Expand Up @@ -50,43 +50,39 @@ var Decorator = function (parent, renderer) {
var allLineHeight = (config.lastRow + 1) * this.lineHeight;
if (allLineHeight < this.canvasHeight) {
this.heightRatio = 1;
} else {
}
else {
this.heightRatio = this.canvasHeight / this.maxHeight;
}
}
var ctx = this.canvas.getContext("2d");

function compare(a, b) {
if (a.typeNumber < b.typeNumber) return -1;
if (a.typeNumber > b.typeNumber) return 1;
if (a.priority < b.priority) return -1;
if (a.priority > b.priority) return 1;
return 0;
}

var annotations = this.renderer.session.$annotations;
ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
if (annotations) {
var priorities = {
"info": 1,
"warning": 2,
"error": 3
};
annotations.forEach(function (item) {
switch (item.type) {
case "info":
item.typeNumber = 1;
break;
case "warning":
item.typeNumber = 2;
break;
case "error":
item.typeNumber = 3;
break;
}
item.priority = priorities[item.type] || null;
});
annotations = annotations.sort(compare);
var foldData = this.renderer.session.$foldData;

for (let i = 0; i < annotations.length; i++) {
let currentRow = annotations[i].row;
let compensateFold = this.compensateFoldRows(currentRow, foldData);
let currentY = Math.round((currentRow - compensateFold) * this.lineHeight * this.heightRatio);
let y1 = Math.round(((currentRow - compensateFold) * this.lineHeight * this.heightRatio));
let y2 = Math.round((((currentRow - compensateFold) * this.lineHeight + this.lineHeight) * this.heightRatio));
let row = annotations[i].row;
let compensateFold = this.compensateFoldRows(row, foldData);
let currentY = Math.round((row - compensateFold) * this.lineHeight * this.heightRatio);
let y1 = Math.round(((row - compensateFold) * this.lineHeight * this.heightRatio));
let y2 = Math.round((((row - compensateFold) * this.lineHeight + this.lineHeight) * this.heightRatio));
const height = y2 - y1;
if (height < this.minDecorationHeight) {
let yCenter = ((y1 + y2) / 2) | 0;
Expand All @@ -100,17 +96,7 @@ var Decorator = function (parent, renderer) {
y2 = Math.round(yCenter + this.halfMinDecorationHeight);
}

switch (annotations[i].type) {
case "info":
ctx.fillStyle = colors.info;
break;
case "warning":
ctx.fillStyle = colors.warning;
break;
case "error":
ctx.fillStyle = colors.error;
break;
}
ctx.fillStyle = colors[annotations[i].type] || null;
ctx.fillRect(0, currentY, this.canvasWidth, y2 - y1);
}
}
Expand All @@ -124,14 +110,14 @@ var Decorator = function (parent, renderer) {

};

this.compensateFoldRows = function (currentRow, foldData) {
this.compensateFoldRows = function (row, foldData) {
let compensateFold = 0;
if (foldData && foldData.length > 0) {
for (let j = 0; j < foldData.length; j++) {
if (currentRow > foldData[j].start.row && currentRow < foldData[j].end.row) {
compensateFold += currentRow - foldData[j].start.row;
if (row > foldData[j].start.row && row < foldData[j].end.row) {
compensateFold += row - foldData[j].start.row;
}
else if (currentRow >= foldData[j].end.row) {
else if (row >= foldData[j].end.row) {
compensateFold += foldData[j].end.row - foldData[j].start.row;
}
}
Expand Down
15 changes: 14 additions & 1 deletion src/virtual_renderer_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ module.exports = {
"test annotation marks": function() {
function findPointFillStyle(points, x, y) {
var point = points.find(el => el.x === x && el.y === y);
if (point == undefined) return undefined;
if (point === undefined) return;

return point.fillStyle;
}
Expand Down Expand Up @@ -285,6 +285,19 @@ module.exports = {
{x: 6, y: 6, color: "rgba(0, 0, 0, 0.5)"}
];
assertCoordsColor(values, imageData.data);
renderer.session.addFold("...", new Range(0, 0, 3, 2));
editor.moveCursorTo(10, 0);
renderer.$loop._flush();
values = [
{x: 0, y: 0, color: scrollDecoratorColors.error},
{x: 1, y: 1, color: scrollDecoratorColors.error},
{x: 2, y: 2, color: scrollDecoratorColors.warning},
{x: 3, y: 3, color: "rgba(0, 0, 0, 0)"},
{x: 4, y: 4, color: "rgba(0, 0, 0, 0)"},
{x: 5, y: 5, color: "rgba(0, 0, 0, 0)"},
{x: 6, y: 6, color: "rgba(0, 0, 0, 0)"}
];
assertCoordsColor(values, imageData.data);
}

// change tab size after setDocument (for text layer)
Expand Down

0 comments on commit f7b1b5c

Please sign in to comment.