2016-06-05 10:49:14 +03:00
|
|
|
/**
|
|
|
|
* Tae Won Ha - http://taewon.de - @hataewon
|
|
|
|
* See LICENSE
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Cocoa
|
2016-06-18 12:43:37 +03:00
|
|
|
|
2016-06-19 11:07:03 +03:00
|
|
|
/// Contiguous piece of cells of a row that has the same attributes.
|
|
|
|
private struct RowRun: CustomStringConvertible {
|
2016-06-18 12:43:37 +03:00
|
|
|
|
|
|
|
let row: Int
|
|
|
|
let range: Range<Int>
|
|
|
|
let attrs: CellAttributes
|
|
|
|
|
|
|
|
var description: String {
|
2016-06-19 11:07:03 +03:00
|
|
|
return "RowRun<\(row): \(range)\n\(attrs)>"
|
2016-06-18 12:43:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-05 10:49:14 +03:00
|
|
|
public class NeoVimView: NSView {
|
2016-07-14 22:53:20 +03:00
|
|
|
|
2016-07-10 15:14:02 +03:00
|
|
|
public let uuid = NSUUID().UUIDString
|
2016-06-05 10:49:14 +03:00
|
|
|
public var delegate: NeoVimViewDelegate?
|
|
|
|
|
2016-07-10 15:14:02 +03:00
|
|
|
let agent: NeoVimAgent
|
|
|
|
|
|
|
|
let grid = Grid()
|
2016-06-24 22:08:34 +03:00
|
|
|
|
|
|
|
var markedText: String?
|
2016-07-02 22:38:52 +03:00
|
|
|
/// We store the last marked text because Cocoa's text input system does the following:
|
|
|
|
/// 하 -> hanja popup -> insertText(하) -> attributedSubstring...() -> setMarkedText(下) -> ...
|
|
|
|
/// We want to return "하" in attributedSubstring...()
|
|
|
|
var lastMarkedText: String?
|
|
|
|
|
|
|
|
var markedPosition = Position.null
|
2016-06-24 22:08:34 +03:00
|
|
|
var keyDownDone = true
|
2016-07-14 00:08:45 +03:00
|
|
|
|
|
|
|
var lastClickedCellPosition = Position.null
|
2016-06-06 19:25:03 +03:00
|
|
|
|
2016-07-10 15:14:02 +03:00
|
|
|
var xOffset = CGFloat(0)
|
|
|
|
var yOffset = CGFloat(0)
|
2016-06-19 16:17:43 +03:00
|
|
|
var cellSize = CGSize.zero
|
|
|
|
var descent = CGFloat(0)
|
|
|
|
var leading = CGFloat(0)
|
2016-07-14 22:53:20 +03:00
|
|
|
|
|
|
|
var scrollGuardCounterX = 9
|
|
|
|
var scrollGuardCounterY = 9
|
|
|
|
let scrollGuardYield = 10
|
2016-06-15 00:50:25 +03:00
|
|
|
|
2016-07-10 15:14:02 +03:00
|
|
|
private let drawer: TextDrawer
|
|
|
|
private var font: NSFont {
|
|
|
|
didSet {
|
|
|
|
self.drawer.font = self.font
|
|
|
|
self.cellSize = self.drawer.cellSize
|
|
|
|
self.descent = self.drawer.descent
|
|
|
|
self.leading = self.drawer.leading
|
|
|
|
|
|
|
|
// We assume that the font is valid, eg fixed width, not too small, not too big, etc..
|
|
|
|
self.resizeNeoVimUiTo(size: self.frame.size)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-09 23:52:59 +03:00
|
|
|
override init(frame rect: NSRect = CGRect.zero) {
|
2016-06-19 14:39:20 +03:00
|
|
|
self.font = NSFont(name: "Menlo", size: 16)!
|
2016-06-18 12:43:37 +03:00
|
|
|
self.drawer = TextDrawer(font: font)
|
2016-07-10 15:14:02 +03:00
|
|
|
self.agent = NeoVimAgent(uuid: self.uuid)
|
2016-07-14 22:53:20 +03:00
|
|
|
|
2016-06-05 10:49:14 +03:00
|
|
|
super.init(frame: rect)
|
2016-06-18 12:43:37 +03:00
|
|
|
|
2016-06-15 23:11:35 +03:00
|
|
|
self.wantsLayer = true
|
2016-06-18 12:43:37 +03:00
|
|
|
self.cellSize = self.drawer.cellSize
|
2016-06-19 14:39:20 +03:00
|
|
|
self.descent = self.drawer.descent
|
|
|
|
self.leading = self.drawer.leading
|
2016-07-09 23:52:59 +03:00
|
|
|
|
|
|
|
// We cannot set bridge in init since self is not available before super.init()...
|
|
|
|
self.agent.bridge = self
|
|
|
|
self.agent.establishLocalServer()
|
|
|
|
}
|
|
|
|
|
|
|
|
// deinit would have been ideal for this, but if you quit the app, deinit does not necessarily get called...
|
|
|
|
public func cleanUp() {
|
|
|
|
self.agent.cleanUp()
|
2016-06-05 10:49:14 +03:00
|
|
|
}
|
2016-06-15 23:11:35 +03:00
|
|
|
|
2016-06-27 19:42:37 +03:00
|
|
|
public func debugInfo() {
|
|
|
|
Swift.print(self.grid)
|
|
|
|
}
|
|
|
|
|
2016-07-06 20:51:31 +03:00
|
|
|
public func setFont(font: NSFont) {
|
|
|
|
guard font.fixedPitch else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: check the size whether too small or too big!
|
|
|
|
self.font = font
|
|
|
|
}
|
|
|
|
|
2016-07-06 20:11:53 +03:00
|
|
|
override public func setFrameSize(newSize: NSSize) {
|
|
|
|
super.setFrameSize(newSize)
|
|
|
|
|
|
|
|
// initial resizing is done when grid has data
|
|
|
|
guard self.grid.hasData else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.inLiveResize {
|
|
|
|
// TODO: Turn of live resizing for now.
|
|
|
|
// self.resizeNeoVimUiTo(size: newSize)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// There can be cases where the frame is resized not by live resizing, eg when the window is resized by window
|
|
|
|
// management tools. Thus, we make sure that the resize call is made when this happens.
|
|
|
|
self.resizeNeoVimUiTo(size: newSize)
|
|
|
|
}
|
2016-07-04 23:06:39 +03:00
|
|
|
|
|
|
|
override public func viewDidEndLiveResize() {
|
|
|
|
super.viewDidEndLiveResize()
|
|
|
|
self.resizeNeoVimUiTo(size: self.bounds.size)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resizeNeoVimUiTo(size size: CGSize) {
|
2016-07-10 14:51:00 +03:00
|
|
|
// NSLog("\(#function): \(size)")
|
2016-07-04 23:06:39 +03:00
|
|
|
let discreteSize = Size(width: Int(floor(size.width / self.cellSize.width)),
|
|
|
|
height: Int(floor(size.height / self.cellSize.height)))
|
|
|
|
|
|
|
|
self.xOffset = floor((size.width - self.cellSize.width * CGFloat(discreteSize.width)) / 2)
|
|
|
|
self.yOffset = floor((size.height - self.cellSize.height * CGFloat(discreteSize.height)) / 2)
|
|
|
|
|
2016-07-09 23:52:59 +03:00
|
|
|
self.agent.resizeToWidth(Int32(discreteSize.width), height: Int32(discreteSize.height))
|
2016-07-04 23:06:39 +03:00
|
|
|
}
|
|
|
|
|
2016-06-15 23:11:35 +03:00
|
|
|
override public func drawRect(dirtyUnionRect: NSRect) {
|
|
|
|
guard self.grid.hasData else {
|
|
|
|
return
|
|
|
|
}
|
2016-06-27 19:42:37 +03:00
|
|
|
|
2016-07-04 23:06:39 +03:00
|
|
|
if self.inLiveResize {
|
|
|
|
NSColor.lightGrayColor().set()
|
|
|
|
dirtyUnionRect.fill()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-07-03 20:12:25 +03:00
|
|
|
// NSLog("\(#function): \(dirtyUnionRect)")
|
2016-06-08 19:17:12 +03:00
|
|
|
let context = NSGraphicsContext.currentContext()!.CGContext
|
|
|
|
|
|
|
|
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
|
|
|
|
CGContextSetTextDrawingMode(context, .Fill);
|
|
|
|
|
2016-06-16 19:36:26 +03:00
|
|
|
let dirtyRects = self.rectsBeingDrawn()
|
2016-06-18 12:43:37 +03:00
|
|
|
|
2016-06-19 11:07:03 +03:00
|
|
|
self.rowRunIntersecting(rects: dirtyRects).forEach { rowFrag in
|
2016-06-29 19:32:33 +03:00
|
|
|
// For background drawing we don't filter out the put(0, 0)s: in some cases only the put(0, 0)-cells should be
|
2016-07-01 22:24:42 +03:00
|
|
|
// redrawn. => FIXME: probably we have to consider this also when drawing further down, ie when the range starts
|
|
|
|
// with '0'...
|
2016-07-05 21:13:46 +03:00
|
|
|
self.drawBackground(positions: rowFrag.range.map { self.pointInViewFor(row: rowFrag.row, column: $0) },
|
2016-06-27 20:04:27 +03:00
|
|
|
background: rowFrag.attrs.background)
|
|
|
|
|
2016-06-08 19:17:12 +03:00
|
|
|
let positions = rowFrag.range
|
|
|
|
// filter out the put(0, 0)s (after a wide character)
|
|
|
|
.filter { self.grid.cells[rowFrag.row][$0].string.characters.count > 0 }
|
2016-07-05 21:13:46 +03:00
|
|
|
.map { self.pointInViewFor(row: rowFrag.row, column: $0) }
|
2016-06-15 00:50:25 +03:00
|
|
|
|
2016-06-27 20:04:27 +03:00
|
|
|
if positions.isEmpty {
|
|
|
|
return
|
|
|
|
}
|
2016-06-18 12:43:37 +03:00
|
|
|
|
2016-06-16 19:36:26 +03:00
|
|
|
let string = self.grid.cells[rowFrag.row][rowFrag.range].reduce("") { $0 + $1.string }
|
2016-06-19 14:39:20 +03:00
|
|
|
let glyphPositions = positions.map { CGPoint(x: $0.x, y: $0.y + self.descent + self.leading) }
|
2016-06-18 12:43:37 +03:00
|
|
|
self.drawer.drawString(string,
|
|
|
|
positions: UnsafeMutablePointer(glyphPositions), positionsCount: positions.count,
|
|
|
|
highlightAttrs: rowFrag.attrs,
|
|
|
|
context: context)
|
2016-06-08 19:17:12 +03:00
|
|
|
}
|
2016-06-29 20:54:06 +03:00
|
|
|
|
|
|
|
self.drawCursor(self.grid.background)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func drawCursor(background: UInt32) {
|
|
|
|
// FIXME: for now do some rudimentary cursor drawing
|
2016-07-11 20:37:06 +03:00
|
|
|
let cursorPosition = self.grid.putPosition
|
|
|
|
// NSLog("\(#function): \(cursorPosition)")
|
2016-06-29 20:54:06 +03:00
|
|
|
|
2016-07-05 21:13:46 +03:00
|
|
|
var cursorRect = self.cellRectFor(row: cursorPosition.row, column: cursorPosition.column)
|
2016-06-29 21:06:31 +03:00
|
|
|
if self.grid.isNextCellEmpty(cursorPosition) {
|
|
|
|
let nextPosition = self.grid.nextCellPosition(cursorPosition)
|
2016-07-05 21:13:46 +03:00
|
|
|
cursorRect = cursorRect.union(self.cellRectFor(row: nextPosition.row, column:nextPosition.column))
|
2016-06-29 20:54:06 +03:00
|
|
|
}
|
|
|
|
|
2016-07-03 22:59:03 +03:00
|
|
|
ColorUtils.colorIgnoringAlpha(background).set()
|
2016-06-29 20:54:06 +03:00
|
|
|
NSRectFillUsingOperation(cursorRect, .CompositeDifference)
|
2016-06-15 23:11:35 +03:00
|
|
|
}
|
2016-06-08 19:17:12 +03:00
|
|
|
|
2016-06-18 12:43:37 +03:00
|
|
|
private func drawBackground(positions positions: [CGPoint], background: UInt32) {
|
2016-07-03 22:59:03 +03:00
|
|
|
ColorUtils.colorIgnoringAlpha(background).set()
|
2016-06-16 19:36:26 +03:00
|
|
|
let backgroundRect = CGRect(
|
|
|
|
x: positions[0].x, y: positions[0].y,
|
2016-07-13 21:16:04 +03:00
|
|
|
width: CGFloat(positions.count) * self.cellSize.width, height: self.cellSize.height
|
2016-06-16 19:36:26 +03:00
|
|
|
)
|
|
|
|
backgroundRect.fill()
|
|
|
|
}
|
|
|
|
|
2016-06-19 11:07:03 +03:00
|
|
|
private func rowRunIntersecting(rects rects: [CGRect]) -> [RowRun] {
|
2016-06-15 23:11:35 +03:00
|
|
|
return rects
|
2016-07-06 20:02:30 +03:00
|
|
|
.map { rect -> (Range<Int>, Range<Int>) in
|
2016-06-19 14:39:20 +03:00
|
|
|
// Get all Regions that intersects with the given rects. There can be overlaps between the Regions, but for the
|
|
|
|
// time being we ignore them; probably not necessary to optimize them away.
|
2016-07-06 20:02:30 +03:00
|
|
|
let region = self.regionFor(rect: rect)
|
|
|
|
return (region.rowRange, region.columnRange)
|
2016-06-19 14:39:20 +03:00
|
|
|
}
|
2016-07-06 20:02:30 +03:00
|
|
|
.map { self.rowRunsFor(rowRange: $0, columnRange: $1) } // All RowRuns for all Regions grouped by their row range.
|
|
|
|
.flatMap { $0 } // Flattened RowRuns for all Regions.
|
|
|
|
}
|
|
|
|
|
|
|
|
private func rowRunsFor(rowRange rowRange: Range<Int>, columnRange: Range<Int>) -> [RowRun] {
|
|
|
|
return rowRange
|
|
|
|
.map { (row) -> [RowRun] in
|
|
|
|
let rowCells = self.grid.cells[row]
|
|
|
|
let startIdx = columnRange.startIndex
|
|
|
|
|
|
|
|
var result = [ RowRun(row: row, range: startIdx...startIdx, attrs: rowCells[startIdx].attrs) ]
|
|
|
|
columnRange.forEach { idx in
|
|
|
|
if rowCells[idx].attrs == result.last!.attrs {
|
|
|
|
let last = result.popLast()!
|
|
|
|
result.append(RowRun(row: row, range: last.range.startIndex...idx, attrs: last.attrs))
|
|
|
|
} else {
|
|
|
|
result.append(RowRun(row: row, range: idx...idx, attrs: rowCells[idx].attrs))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result // All RowRuns for a row in a Region.
|
|
|
|
} // All RowRuns for all rows in a Region grouped by row.
|
|
|
|
.flatMap { $0 } // Flattened RowRuns for a Region.
|
|
|
|
}
|
|
|
|
|
|
|
|
private func regionFor(rect rect: CGRect) -> Region {
|
|
|
|
let rowStart = max(
|
|
|
|
Int(floor((self.frame.height - (rect.origin.y + rect.size.height)) / self.cellSize.height)), 0
|
|
|
|
)
|
|
|
|
let rowEnd = min(
|
|
|
|
Int(ceil((self.frame.height - rect.origin.y) / self.cellSize.height)) - 1, self.grid.size.height - 1
|
|
|
|
)
|
|
|
|
let columnStart = max(
|
|
|
|
Int(floor(rect.origin.x / self.cellSize.width)), 0
|
|
|
|
)
|
|
|
|
let columnEnd = min(
|
|
|
|
Int(ceil((rect.origin.x + rect.size.width) / self.cellSize.width)) - 1, self.grid.size.width - 1
|
|
|
|
)
|
|
|
|
|
|
|
|
return Region(top: rowStart, bottom: rowEnd, left: columnStart, right: columnEnd)
|
2016-06-05 10:49:14 +03:00
|
|
|
}
|
2016-07-01 22:24:42 +03:00
|
|
|
|
2016-07-05 21:13:46 +03:00
|
|
|
func pointInViewFor(position position: Position) -> CGPoint {
|
|
|
|
return self.pointInViewFor(row: position.row, column: position.column)
|
2016-07-01 22:24:42 +03:00
|
|
|
}
|
2016-06-05 10:49:14 +03:00
|
|
|
|
2016-07-05 21:13:46 +03:00
|
|
|
func pointInViewFor(row row: Int, column: Int) -> CGPoint {
|
2016-06-08 19:17:12 +03:00
|
|
|
return CGPoint(
|
2016-07-04 20:04:49 +03:00
|
|
|
x: CGFloat(column) * self.cellSize.width + self.xOffset,
|
2016-07-04 23:06:39 +03:00
|
|
|
y: self.frame.size.height - CGFloat(row) * self.cellSize.height - self.cellSize.height - self.yOffset
|
2016-06-08 19:17:12 +03:00
|
|
|
)
|
|
|
|
}
|
2016-06-24 22:08:34 +03:00
|
|
|
|
2016-07-05 21:13:46 +03:00
|
|
|
func cellRectFor(row row: Int, column: Int) -> CGRect {
|
|
|
|
return CGRect(origin: self.pointInViewFor(row: row, column: column), size: self.cellSize)
|
2016-06-24 22:08:34 +03:00
|
|
|
}
|
|
|
|
|
2016-07-05 21:13:46 +03:00
|
|
|
func regionRectFor(region region: Region) -> CGRect {
|
2016-07-04 20:04:49 +03:00
|
|
|
let top = CGFloat(region.top)
|
|
|
|
let bottom = CGFloat(region.bottom)
|
|
|
|
let left = CGFloat(region.left)
|
|
|
|
let right = CGFloat(region.right)
|
|
|
|
|
|
|
|
let width = right - left + 1
|
|
|
|
let height = bottom - top + 1
|
|
|
|
|
|
|
|
return CGRect(
|
|
|
|
x: left * self.cellSize.width + self.xOffset,
|
2016-07-04 23:06:39 +03:00
|
|
|
y: (CGFloat(self.grid.size.height) - bottom) * self.cellSize.height - self.yOffset,
|
2016-07-04 20:04:49 +03:00
|
|
|
width: width * self.cellSize.width,
|
|
|
|
height: height * self.cellSize.height
|
|
|
|
)
|
2016-06-27 19:42:37 +03:00
|
|
|
}
|
2016-06-27 20:04:27 +03:00
|
|
|
|
2016-07-12 00:24:40 +03:00
|
|
|
func wrapNamedKeys(string: String) -> String {
|
2016-06-27 20:04:27 +03:00
|
|
|
return "<\(string)>"
|
|
|
|
}
|
|
|
|
|
|
|
|
func vimPlainString(string: String) -> String {
|
2016-07-12 00:24:40 +03:00
|
|
|
return string.stringByReplacingOccurrencesOfString("<", withString: self.wrapNamedKeys("lt"))
|
2016-06-27 20:04:27 +03:00
|
|
|
}
|
2016-06-27 19:42:37 +03:00
|
|
|
|
2016-06-05 10:49:14 +03:00
|
|
|
required public init?(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
}
|