1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-12-25 23:02:35 +03:00

GH-666 Use reversed attrs for marked text

This commit is contained in:
Tae Won Ha 2018-09-30 23:17:43 +02:00
parent 3acfd95745
commit 6dfa3d8add
4 changed files with 80 additions and 2 deletions

View File

@ -8,6 +8,7 @@ import Foundation
final class CellAttributesCollection { final class CellAttributesCollection {
static let defaultAttributesId = 0 static let defaultAttributesId = 0
static let reversedDefaultAttributesId = Int.max
private(set) var defaultAttributes = CellAttributes( private(set) var defaultAttributes = CellAttributes(
fontTrait: [], fontTrait: [],
@ -23,10 +24,20 @@ final class CellAttributesCollection {
} }
func attributes(of id: Int) -> CellAttributes? { func attributes(of id: Int) -> CellAttributes? {
guard let attrs = self.attributes[id] else { if id == Int.max {
return self.defaultAttributes.reversed
}
let absId = abs(id)
guard let attrs = self.attributes[absId] else {
return nil return nil
} }
if id < 0 {
return attrs.replacingDefaults(with: self.defaultAttributes).reversed
}
return attrs.replacingDefaults(with: self.defaultAttributes) return attrs.replacingDefaults(with: self.defaultAttributes)
} }

View File

@ -234,6 +234,13 @@ extension NvimView {
top: row, bottom: row, left: endCol, right: max(endCol, clearCol - 1) top: row, bottom: row, left: endCol, right: max(endCol, clearCol - 1)
)) ))
} }
if row == self.markedPosition.row
&& startCol <= self.markedPosition.column
&& self.markedPosition.column <= endCol
{
self.ugrid.markCell(at: self.markedPosition)
}
} }
private func doGoto(position: Position) { private func doGoto(position: Position) {

View File

@ -23,6 +23,21 @@ final class UGrid {
return !self.cells.isEmpty return !self.cells.isEmpty
} }
func markCell(at position: Position) {
let attrId = self.cells[position.row][position.column].attrId
let markedAttrId: Int
if attrId == CellAttributesCollection.defaultAttributesId {
markedAttrId = CellAttributesCollection.reversedDefaultAttributesId
} else {
markedAttrId = (-1) * attrId
}
self.cells[position.row][position.column].attrId = markedAttrId
if self.isNextCellEmpty(position) {
self.cells[position.row][position.column + 1].attrId = markedAttrId
}
}
func position(from flattenedIndex: Int) -> Position { func position(from flattenedIndex: Int) -> Position {
let row = min( let row = min(
self.size.height - 1, self.size.height - 1,

View File

@ -13,6 +13,51 @@ class UGridTest: XCTestCase {
private let ugrid = UGrid() private let ugrid = UGrid()
func testMarkPosition() {
self.ugrid.resize(Size(width: 20, height: 10))
self.ugrid.update(
row: 9,
startCol: 0,
endCol: 9,
clearCol: 0,
clearAttr: 0,
chunk: Array("0123456789".compactMap { String($0) }),
attrIds: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
)
self.ugrid.markCell(at: Position(row: 9, column: 4))
expect(self.ugrid.cells[9][4].attrId).to(equal(-4))
self.ugrid.update(
row: 7,
startCol: 0,
endCol: 9,
clearCol: 0,
clearAttr: 0,
chunk: Array("23456789".compactMap { String($0) }) + ["", ""],
attrIds: [0, 1, 2, 3, 4, 5, 6, 7, 8, 8]
)
self.ugrid.markCell(at: Position(row: 7, column: 8))
expect(self.ugrid.cells[7][8].attrId)
.to(equal(-8))
expect(self.ugrid.cells[7][9].attrId)
.to(equal(-8))
self.ugrid.update(
row: 8,
startCol: 0,
endCol: 9,
clearCol: 0,
clearAttr: 0,
chunk: ["", ""] + Array("23456789".compactMap { String($0) }),
attrIds: [0, 0, 2, 3, 4, 5, 6, 7, 8, 9]
)
self.ugrid.markCell(at: Position(row: 8, column: 0))
expect(self.ugrid.cells[8][0].attrId)
.to(equal(CellAttributesCollection.reversedDefaultAttributesId))
expect(self.ugrid.cells[8][1].attrId)
.to(equal(CellAttributesCollection.reversedDefaultAttributesId))
}
func testFlattenedIndex() { func testFlattenedIndex() {
self.ugrid.resize(Size(width: 20, height: 10)) self.ugrid.resize(Size(width: 20, height: 10))
expect( expect(