Skip to content
This repository was archived by the owner on Oct 17, 2018. It is now read-only.

Update package in preparation for native text buffer #45

Merged
merged 2 commits into from
May 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 36 additions & 21 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import SelectListView from 'atom-select-list'
import StatusBarItem from './status-bar-item'
import helpers from './helpers'

const LineEndingRegExp = /\r\n|\n|\r/g
const LineEndingRegExp = /\r\n|\n/g
const LFRegExp = /(^|[^\r])\n/g
const CRLFRegExp = /\r\n/g

let disposables = null
let modalPanel = null
Expand Down Expand Up @@ -68,17 +70,16 @@ export function consumeStatusBar (statusBar) {
let currentBufferDisposable = null
let tooltipDisposable = null

function updateTile (buffer) {
let lineEndings = getLineEndings(buffer)
if (lineEndings.size === 0) {
let defaultLineEnding = getDefaultLineEnding()
buffer.setPreferredLineEnding(defaultLineEnding)
lineEndings = new Set().add(defaultLineEnding)
}
statusBarItem.setLineEndings(lineEndings)
}

let debouncedUpdateTile = _.debounce(updateTile, 0)
const updateTile = _.debounce((buffer) => {
getLineEndings(buffer).then((lineEndings) => {
if (lineEndings.size === 0) {
let defaultLineEnding = getDefaultLineEnding()
buffer.setPreferredLineEnding(defaultLineEnding)
lineEndings = new Set().add(defaultLineEnding)
}
statusBarItem.setLineEndings(lineEndings)
})
}, 0)

disposables.add(atom.workspace.observeActivePaneItem((item) => {
if (currentBufferDisposable) currentBufferDisposable.dispose()
Expand All @@ -89,14 +90,14 @@ export function consumeStatusBar (statusBar) {
currentBufferDisposable = buffer.onDidChange(({oldText, newText}) => {
if (!statusBarItem.hasLineEnding('\n')) {
if (newText.indexOf('\n') >= 0) {
debouncedUpdateTile(buffer)
updateTile(buffer)
}
} else if (!statusBarItem.hasLineEnding('\r\n')) {
if (newText.indexOf('\r\n') >= 0) {
debouncedUpdateTile(buffer)
updateTile(buffer)
}
} else if (LineEndingRegExp.test(oldText)) {
debouncedUpdateTile(buffer)
} else if (oldText.indexOf('\n')) {
updateTile(buffer)
}
})
} else {
Expand Down Expand Up @@ -124,7 +125,7 @@ export function consumeStatusBar (statusBar) {
)
})

let tile = statusBar.addRightTile({item: statusBarItem.element, priority: 200})
let tile = statusBar.addRightTile({item: statusBarItem, priority: 200})
disposables.add(new Disposable(() => tile.destroy()))
}

Expand All @@ -141,11 +142,25 @@ function getDefaultLineEnding () {
}

function getLineEndings (buffer) {
let result = new Set()
for (let i = 0; i < buffer.getLineCount() - 1; i++) {
result.add(buffer.lineEndingForRow(i))
if (typeof buffer.search === 'function') {
return Promise.all([
buffer.search(LFRegExp),
buffer.search(CRLFRegExp)
]).then(([hasLF, hasCRLF]) => {
const result = new Set()
if (hasLF) result.add('\n')
if (hasCRLF) result.add('\r\n')
return result
})
} else {
return new Promise((resolve) => {
const result = new Set()
for (let i = 0; i < buffer.getLineCount() - 1; i++) {
result.add(buffer.lineEndingForRow(i))
}
resolve(result)
})
}
return result
}

function setLineEnding (item, lineEnding) {
Expand Down
13 changes: 9 additions & 4 deletions lib/status-bar-item.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
'use babel'
const {Emitter} = require('atom')

export default class StatusBarItem {
module.exports =
class StatusBarItem {
constructor () {
this.element = document.createElement('a')
this.element.className = 'line-ending-tile inline-block'
this.emitter = new Emitter()
this.setLineEndings(new Set())
}

setLineEndings (lineEndings) {
this.lineEndings = lineEndings
this.element.textContent = lineEndingName(lineEndings)
this.emitter.emit('did-change')
}

onDidChange (callback) {
return this.emitter.on('did-change', callback)
}

hasLineEnding (lineEnding) {
Expand All @@ -32,8 +39,6 @@ function lineEndingName (lineEndings) {
return 'LF'
} else if (lineEndings.has('\r\n')) {
return 'CRLF'
} else if (lineEndings.has('\r')) {
return 'CR'
} else {
return ''
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"runs",
"spyOn",
"waits",
"waitsFor",
"waitsForPromise"
]
}
Expand Down
1 change: 0 additions & 1 deletion spec/fixtures/old-endings.md

This file was deleted.

Loading