Skip to content

Commit

Permalink
fix: make drawHighlightOutlineDecoration a free function
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Jan 2, 2021
1 parent 90f0e65 commit cd4af73
Showing 1 changed file with 89 additions and 81 deletions.
170 changes: 89 additions & 81 deletions lib/mixins/canvas-drawer.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ export default class CanvasDrawer extends Mixin {
this.drawDecorations(screenRow, decorations, renderData, {
gutter: this.drawGutterDecoration,
'highlight-over': this.drawHighlightDecoration,
'highlight-outline': this.drawHighlightOutlineDecoration,
'highlight-outline': drawHighlightOutlineDecoration,
'foreground-custom': this.drawCustomDecoration
})
}
Expand Down Expand Up @@ -554,86 +554,6 @@ export default class CanvasDrawer extends Mixin {
}
}

/**
* Draws a highlight outline decoration.
*
* It renders only the part of the highlight corresponding to the specified
* row.
*
* @param {Decoration} decoration the decoration to render
* @param {Object} data the data need to perform the render
* @param {string} decorationColor decoration color
* @access private
*/
drawHighlightOutlineDecoration (decoration, data, decorationColor) {
let bottomWidth, colSpan, width, xBottomStart, xEnd, xStart
const { lineHeight, charWidth, canvasWidth, screenRow } = data
const range = decoration.getMarker().getScreenRange()
const rowSpan = range.end.row - range.start.row
const yStart = data.yRow
const yEnd = yStart + lineHeight

data.context.fillStyle = decorationColor

if (rowSpan === 0) {
colSpan = range.end.column - range.start.column
width = colSpan * charWidth
xStart = range.start.column * charWidth
xEnd = xStart + width

data.context.fillRect(xStart, yStart, width, 1)
data.context.fillRect(xStart, yEnd - 1, width, 1)
data.context.fillRect(xStart, yStart, 1, lineHeight)
data.context.fillRect(xEnd, yStart, 1, lineHeight)
} else if (rowSpan === 1) {
xStart = range.start.column * data.charWidth
xEnd = range.end.column * data.charWidth

if (screenRow === range.start.row) {
width = data.canvasWidth - xStart
xBottomStart = Math.max(xStart, xEnd)
bottomWidth = data.canvasWidth - xBottomStart

data.context.fillRect(xStart, yStart, width, 1)
data.context.fillRect(xBottomStart, yEnd - 1, bottomWidth, 1)
data.context.fillRect(xStart, yStart, 1, lineHeight)
data.context.fillRect(canvasWidth - 1, yStart, 1, lineHeight)
} else {
width = canvasWidth - xStart
bottomWidth = canvasWidth - xEnd

data.context.fillRect(0, yStart, xStart, 1)
data.context.fillRect(0, yEnd - 1, xEnd, 1)
data.context.fillRect(0, yStart, 1, lineHeight)
data.context.fillRect(xEnd, yStart, 1, lineHeight)
}
} else {
xStart = range.start.column * charWidth
xEnd = range.end.column * charWidth
if (screenRow === range.start.row) {
width = canvasWidth - xStart

data.context.fillRect(xStart, yStart, width, 1)
data.context.fillRect(xStart, yStart, 1, lineHeight)
data.context.fillRect(canvasWidth - 1, yStart, 1, lineHeight)
} else if (screenRow === range.end.row) {
width = canvasWidth - xStart

data.context.fillRect(0, yEnd - 1, xEnd, 1)
data.context.fillRect(0, yStart, 1, lineHeight)
data.context.fillRect(xEnd, yStart, 1, lineHeight)
} else {
data.context.fillRect(0, yStart, 1, lineHeight)
data.context.fillRect(canvasWidth - 1, yStart, 1, lineHeight)
if (screenRow === range.start.row + 1) {
data.context.fillRect(0, yStart, xStart, 1)
}
if (screenRow === range.end.row - 1) {
data.context.fillRect(xEnd, yEnd - 1, canvasWidth - xEnd, 1)
}
}
}
}

/**
* Draws a custom decoration.
Expand All @@ -656,6 +576,12 @@ export default class CanvasDrawer extends Mixin {
}
}

const SPEC_MODE = atom.inSpecMode()
if (SPEC_MODE) {
// class methods only used for spying the calls
CanvasDrawer.drawHighlightOutlineDecoration = drawHighlightOutlineDecoration
}

// ######## ######## ### ## ##
// ## ## ## ## ## ## ## ## ##
// ## ## ## ## ## ## ## ## ##
Expand Down Expand Up @@ -759,6 +685,88 @@ function getInvisibleRegExp (editor) {
}
}


/**
* Draws a highlight outline decoration.
*
* It renders only the part of the highlight corresponding to the specified
* row.
*
* @param {Decoration} decoration the decoration to render
* @param {Object} data the data need to perform the render
* @param {string} decorationColor decoration color
* @access private
*/
function drawHighlightOutlineDecoration (decoration, data, decorationColor) {
let bottomWidth, colSpan, width, xBottomStart, xEnd, xStart
const { lineHeight, charWidth, canvasWidth, screenRow } = data
const range = decoration.getMarker().getScreenRange()
const rowSpan = range.end.row - range.start.row
const yStart = data.yRow
const yEnd = yStart + lineHeight

data.context.fillStyle = decorationColor

if (rowSpan === 0) {
colSpan = range.end.column - range.start.column
width = colSpan * charWidth
xStart = range.start.column * charWidth
xEnd = xStart + width

data.context.fillRect(xStart, yStart, width, 1)
data.context.fillRect(xStart, yEnd - 1, width, 1)
data.context.fillRect(xStart, yStart, 1, lineHeight)
data.context.fillRect(xEnd, yStart, 1, lineHeight)
} else if (rowSpan === 1) {
xStart = range.start.column * data.charWidth
xEnd = range.end.column * data.charWidth

if (screenRow === range.start.row) {
width = data.canvasWidth - xStart
xBottomStart = Math.max(xStart, xEnd)
bottomWidth = data.canvasWidth - xBottomStart

data.context.fillRect(xStart, yStart, width, 1)
data.context.fillRect(xBottomStart, yEnd - 1, bottomWidth, 1)
data.context.fillRect(xStart, yStart, 1, lineHeight)
data.context.fillRect(canvasWidth - 1, yStart, 1, lineHeight)
} else {
width = canvasWidth - xStart
bottomWidth = canvasWidth - xEnd

data.context.fillRect(0, yStart, xStart, 1)
data.context.fillRect(0, yEnd - 1, xEnd, 1)
data.context.fillRect(0, yStart, 1, lineHeight)
data.context.fillRect(xEnd, yStart, 1, lineHeight)
}
} else {
xStart = range.start.column * charWidth
xEnd = range.end.column * charWidth
if (screenRow === range.start.row) {
width = canvasWidth - xStart

data.context.fillRect(xStart, yStart, width, 1)
data.context.fillRect(xStart, yStart, 1, lineHeight)
data.context.fillRect(canvasWidth - 1, yStart, 1, lineHeight)
} else if (screenRow === range.end.row) {
width = canvasWidth - xStart

data.context.fillRect(0, yEnd - 1, xEnd, 1)
data.context.fillRect(0, yStart, 1, lineHeight)
data.context.fillRect(xEnd, yStart, 1, lineHeight)
} else {
data.context.fillRect(0, yStart, 1, lineHeight)
data.context.fillRect(canvasWidth - 1, yStart, 1, lineHeight)
if (screenRow === range.start.row + 1) {
data.context.fillRect(0, yStart, xStart, 1)
}
if (screenRow === range.end.row - 1) {
data.context.fillRect(xEnd, yEnd - 1, canvasWidth - xEnd, 1)
}
}
}
}

// ###### ####### ## ####### ######## ######
// ## ## ## ## ## ## ## ## ## ## ##
// ## ## ## ## ## ## ## ## ##
Expand Down

0 comments on commit cd4af73

Please sign in to comment.